From a70818096fc87f55971496ea4d0e4cf2ac62a987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20All=C3=A8ne?= Date: Sun, 3 Dec 2023 15:45:28 +0100 Subject: [PATCH] Add toggle to hide slopes in alarms --- xdrip/Extensions/UserDefaults.swift | 11 ++++++++++ xdrip/Managers/Alerts/AlertKind.swift | 20 +++++++++++------- xdrip/Texts/TextsSettingsView.swift | 4 ++++ .../SettingsViewAlertSettingsViewModel.swift | 21 +++++++++++++++++-- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/xdrip/Extensions/UserDefaults.swift b/xdrip/Extensions/UserDefaults.swift index 111cfe68d..9016fe6bd 100644 --- a/xdrip/Extensions/UserDefaults.swift +++ b/xdrip/Extensions/UserDefaults.swift @@ -141,6 +141,8 @@ extension UserDefaults { case snoozeAllAlertsFromDate = "snoozeAllAlertsFromDate" /// for how long did the user snooze all alarms case snoozeAllAlertsUntilDate = "snoozeAllAlertsUntilDate" + /// hide slope in alarms + case hideSlopeInAlarms = "hideSlopeInAlarms" // Housekeeper settings @@ -1185,6 +1187,15 @@ extension UserDefaults { } } + /// - show slopes in alarms + var showSlopeInAlarms: Bool { + get { + return !bool(forKey: Key.hideSlopeInAlarms.rawValue) + } + set { + set(!newValue, forKey: Key.hideSlopeInAlarms.rawValue) + } + } // MARK: Alert Settings diff --git a/xdrip/Managers/Alerts/AlertKind.swift b/xdrip/Managers/Alerts/AlertKind.swift index 4ff4f443c..109c21acd 100644 --- a/xdrip/Managers/Alerts/AlertKind.swift +++ b/xdrip/Managers/Alerts/AlertKind.swift @@ -180,6 +180,7 @@ public enum AlertKind:Int, CaseIterable { //Not all input parameters in the closure are needed for every type of alert. - this is to make it generic let isMgDl = UserDefaults.standard.bloodGlucoseUnitIsMgDl + let showSlope = UserDefaults.standard.showSlopeInAlarms switch self { @@ -193,7 +194,7 @@ public enum AlertKind:Int, CaseIterable { // now do the actual check if alert is applicable or not if lastBgReading.calculatedValue.bgValueRounded(mgDl: isMgDl) < Double(currentAlertEntry.value).bgValueRounded(mgDl: isMgDl) { - return (true, createAlertBodyForBgReadingAlerts(bgReading: lastBgReading, alertKind: self), createAlertTitleForBgReadingAlerts(alertKind: self), nil) + return (true, createAlertBodyForBgReadingAlerts(bgReading: lastBgReading, alertKind: self, showSlope: showSlope), createAlertTitleForBgReadingAlerts(alertKind: self, showSlope: showSlope), nil) } else {return (false, nil, nil, nil)} } else {return (false, nil, nil, nil)} @@ -206,7 +207,7 @@ public enum AlertKind:Int, CaseIterable { if lastBgReading.calculatedValue == 0.0 {return (false, nil, nil, nil)} // now do the actual check if alert is applicable or not if lastBgReading.calculatedValue.bgValueRounded(mgDl: isMgDl) > Double(currentAlertEntry.value).bgValueRounded(mgDl: isMgDl){ - return (true, createAlertBodyForBgReadingAlerts(bgReading: lastBgReading, alertKind: self), createAlertTitleForBgReadingAlerts(alertKind: self), nil) + return (true, createAlertBodyForBgReadingAlerts(bgReading: lastBgReading, alertKind: self, showSlope: showSlope), createAlertTitleForBgReadingAlerts(alertKind: self, showSlope: showSlope), nil) } else {return (false, nil, nil, nil)} } else {return (false, nil, nil, nil)} @@ -223,7 +224,7 @@ public enum AlertKind:Int, CaseIterable { if lastBgReading.calculatedValue == 0.0 || lastButOneBgReading.calculatedValue == 0.0 {return (false, nil, nil, nil)} // now do the actual check if alert is applicable or not if lastButOneBgReading.calculatedValue.bgValueRounded(mgDl: isMgDl) - lastBgReading.calculatedValue.bgValueRounded(mgDl: isMgDl) > Double(currentAlertEntry.value).bgValueRounded(mgDl: isMgDl) { - return (true, createAlertBodyForBgReadingAlerts(bgReading: lastBgReading, alertKind: self), createAlertTitleForBgReadingAlerts(alertKind: self), nil) + return (true, createAlertBodyForBgReadingAlerts(bgReading: lastBgReading, alertKind: self, showSlope: showSlope), createAlertTitleForBgReadingAlerts(alertKind: self, showSlope: showSlope), nil) } else {return (false, nil, nil, nil)} } else {return (false, nil, nil, nil)} @@ -243,7 +244,7 @@ public enum AlertKind:Int, CaseIterable { if lastBgReading.calculatedValue == 0.0 || lastButOneBgReading.calculatedValue == 0.0 {return (false, nil, nil, nil)} // now do the actual check if alert is applicable or not if lastBgReading.calculatedValue.bgValueRounded(mgDl: isMgDl) - lastButOneBgReading.calculatedValue.bgValueRounded(mgDl: isMgDl) > Double(currentAlertEntry.value).bgValueRounded(mgDl: isMgDl) { - return (true, createAlertBodyForBgReadingAlerts(bgReading: lastBgReading, alertKind: self), createAlertTitleForBgReadingAlerts(alertKind: self), nil) + return (true, createAlertBodyForBgReadingAlerts(bgReading: lastBgReading, alertKind: self, showSlope: showSlope), createAlertTitleForBgReadingAlerts(alertKind: self, showSlope: showSlope), nil) } else {return (false, nil, nil, nil)} } else {return (false, nil, nil, nil)} @@ -454,7 +455,12 @@ public enum AlertKind:Int, CaseIterable { } // specifically for high, low, very high, very low because these need the same kind of alertTitle -fileprivate func createAlertTitleForBgReadingAlerts(alertKind: AlertKind) -> String { +fileprivate func createAlertTitleForBgReadingAlerts(alertKind: AlertKind, showSlope:Bool) -> String { + guard showSlope else { + return "" + } + + var returnValue:String = "" // the start of the body, which says like "High Alert" switch alertKind { case .low: @@ -475,14 +481,14 @@ fileprivate func createAlertTitleForBgReadingAlerts(alertKind: AlertKind) -> Str } // specifically for high, low, very high, very low because these need to show an alert body with the BG value etc -fileprivate func createAlertBodyForBgReadingAlerts(bgReading:BgReading, alertKind:AlertKind) -> String { +fileprivate func createAlertBodyForBgReadingAlerts(bgReading:BgReading, alertKind:AlertKind, showSlope:Bool) -> String { var returnValue:String = "" // add unit returnValue = returnValue + " " + bgReading.calculatedValue.mgDlToMmolAndToString(mgDl: UserDefaults.standard.bloodGlucoseUnitIsMgDl) // add slopeArrow - if !bgReading.hideSlope { + if showSlope && !bgReading.hideSlope { returnValue = returnValue + " " + bgReading.slopeArrow() } diff --git a/xdrip/Texts/TextsSettingsView.swift b/xdrip/Texts/TextsSettingsView.swift index bf4a361f6..823553bbf 100644 --- a/xdrip/Texts/TextsSettingsView.swift +++ b/xdrip/Texts/TextsSettingsView.swift @@ -766,6 +766,10 @@ class Texts_SettingsView { return NSLocalizedString("volumeTestiOSSound", tableName: filename, bundle: Bundle.main, value: "Volume Test (Current iPhone Volume)", comment: "In Settings, Alerts section, there's an option to test the volume of ios sound, this is the title of the row") }() + static let showSlopeInAlarms: String = { + return NSLocalizedString("showSlopeInAlarms", tableName: filename, bundle: Bundle.main, value: "Show Slope in Alarms", comment: "In Settings, Alerts section, there's an option to show the slopes in the alarms") + }() + static let volumeTestiOSSoundExplanation: String = { return NSLocalizedString("volumeTestiOSSoundExplanation", tableName: filename, bundle: Bundle.main, value: "An alarm sound is now being played with the same volume that will be used for an Alarm Type with 'Override Mute' = Off\n\n(Also used always for Missed Reading alarms which use the iOS volume.)\n\nPress one of the volume buttons to stop the sound, then change the volume with the volume buttons to the desired volume and test again.", comment: "In Settings, Alerts section, there's an option to test the volume settings, this is text explaining the test when clicking the row - this is for ios sound volume test") }() diff --git a/xdrip/View Controllers/SettingsNavigationController/SettingsViewController/SettingsViewModels/SettingsViewAlertSettingsViewModel.swift b/xdrip/View Controllers/SettingsNavigationController/SettingsViewController/SettingsViewModels/SettingsViewAlertSettingsViewModel.swift index 6c5320f7a..9363b6ec2 100644 --- a/xdrip/View Controllers/SettingsNavigationController/SettingsViewController/SettingsViewModels/SettingsViewAlertSettingsViewModel.swift +++ b/xdrip/View Controllers/SettingsNavigationController/SettingsViewController/SettingsViewModels/SettingsViewAlertSettingsViewModel.swift @@ -15,6 +15,9 @@ fileprivate enum Setting:Int, CaseIterable { /// volume test for sound play in iOS notification case volumeTestiOSSound = 3 + /// show slope in alarms + case showSlopeInAlarms = 4 + } /// conforms to SettingsViewModelProtocol for all alert settings in the first sections screen @@ -83,7 +86,11 @@ struct SettingsViewAlertSettingsViewModel:SettingsViewModelProtocol { return SettingsSelectedRowAction.showInfoText(title: Texts_Common.warning, message: Texts_SettingsView.volumeTestiOSSoundExplanation, actionHandler: nil) + case .showSlopeInAlarms: + return .nothing + } + } func storeRowReloadClosure(rowReloadClosure: ((Int) -> Void)) {} @@ -97,7 +104,14 @@ struct SettingsViewAlertSettingsViewModel:SettingsViewModelProtocol { } func uiView(index: Int) -> UIView? { - return nil + guard let setting = Setting(rawValue: index) else { fatalError("Unexpected Section") } + + switch setting { + case .showSlopeInAlarms: + return UISwitch(isOn: UserDefaults.standard.showSlopeInAlarms, action: {(isOn:Bool) in UserDefaults.standard.showSlopeInAlarms = isOn}) + default: + return nil + } } func settingsRowText(index: Int) -> String { @@ -115,6 +129,9 @@ struct SettingsViewAlertSettingsViewModel:SettingsViewModelProtocol { case .volumeTestiOSSound: return Texts_SettingsView.volumeTestiOSSound + + case .showSlopeInAlarms: + return Texts_SettingsView.showSlopeInAlarms } } @@ -129,7 +146,7 @@ struct SettingsViewAlertSettingsViewModel:SettingsViewModelProtocol { return .disclosureIndicator - case .volumeTestSoundPlayer, .volumeTestiOSSound: + case .volumeTestSoundPlayer, .volumeTestiOSSound, .showSlopeInAlarms: return .none