Skip to content

Commit

Permalink
fix #64 crash when user gives invalid value in calibration textfield
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanDegraeve committed Jan 25, 2020
1 parent 3f91781 commit ecd44a5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion xdrip/Storyboards/en.lproj/Common.strings
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
"yellow" = "yellow";
"name" = "Name";
"WiFi" = "WiFi";

"invalidValue" = "Invalid Value";
4 changes: 4 additions & 0 deletions xdrip/Texts/TextsCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,8 @@ class Texts_Common {
return NSLocalizedString("Delete", tableName: filename, bundle: Bundle.main, value: "Delete", comment: "Delete")
}()

static let invalidValue = {
return NSLocalizedString("invalidValue", tableName: filename, bundle: Bundle.main, value: "Invalid Value", comment: "whenever invalid value is given by user somewhere in a field")
}()

}
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,12 @@ final class RootViewController: UIViewController {
if let coreDataManager = self.coreDataManager, let bgReadingsAccessor = self.bgReadingsAccessor {
if let textField = alert.textFields, let first = textField.first, let value = first.text {

let valueAsDouble = value.toDouble()!.mmolToMgdl(mgdl: UserDefaults.standard.bloodGlucoseUnitIsMgDl)
guard let valueAsDouble = value.toDouble() else {
self.present(UIAlertController(title: Texts_Common.warning, message: Texts_Common.invalidValue, actionHandler: nil), animated: true, completion: nil)
return
}

let valueAsDoubleConvertedToMgDl = valueAsDouble.mmolToMgdl(mgdl: UserDefaults.standard.bloodGlucoseUnitIsMgDl)

var latestReadings = bgReadingsAccessor.getLatestBgReadings(limit: 36, howOld: nil, forSensor: activeSensor, ignoreRawData: false, ignoreCalculatedValue: true)

Expand All @@ -734,12 +739,12 @@ final class RootViewController: UIViewController {
if let calibrator = self.calibrator {
if latestCalibrations.count == 0 {
// calling initialCalibration will create two calibrations, they are returned also but we don't need them
_ = calibrator.initialCalibration(firstCalibrationBgValue: valueAsDouble, firstCalibrationTimeStamp: Date(timeInterval: -(5*60), since: Date()), secondCalibrationBgValue: valueAsDouble, sensor: activeSensor, lastBgReadingsWithCalculatedValue0AndForSensor: &latestReadings, deviceName: UserDefaults.standard.cgmTransmitterDeviceName, nsManagedObjectContext: coreDataManager.mainManagedObjectContext)
_ = calibrator.initialCalibration(firstCalibrationBgValue: valueAsDoubleConvertedToMgDl, firstCalibrationTimeStamp: Date(timeInterval: -(5*60), since: Date()), secondCalibrationBgValue: valueAsDoubleConvertedToMgDl, sensor: activeSensor, lastBgReadingsWithCalculatedValue0AndForSensor: &latestReadings, deviceName: UserDefaults.standard.cgmTransmitterDeviceName, nsManagedObjectContext: coreDataManager.mainManagedObjectContext)
} else {
// it's not the first calibration
if let firstCalibrationForActiveSensor = calibrationsAccessor.firstCalibrationForActiveSensor(withActivesensor: activeSensor) {
// calling createNewCalibration will create a new calibrations, it is returned but we don't need it
_ = calibrator.createNewCalibration(bgValue: valueAsDouble, lastBgReading: latestReadings[0], sensor: activeSensor, lastCalibrationsForActiveSensorInLastXDays: &latestCalibrations, firstCalibration: firstCalibrationForActiveSensor, deviceName: UserDefaults.standard.cgmTransmitterDeviceName, nsManagedObjectContext: coreDataManager.mainManagedObjectContext)
_ = calibrator.createNewCalibration(bgValue: valueAsDoubleConvertedToMgDl, lastBgReading: latestReadings[0], sensor: activeSensor, lastCalibrationsForActiveSensorInLastXDays: &latestCalibrations, firstCalibration: firstCalibrationForActiveSensor, deviceName: UserDefaults.standard.cgmTransmitterDeviceName, nsManagedObjectContext: coreDataManager.mainManagedObjectContext)
}
}

Expand Down

0 comments on commit ecd44a5

Please sign in to comment.