NotificationSettingsView.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // NotificationSettingsView.swift
  3. // LibreTransmitterUI
  4. //
  5. // Created by Bjørn Inge Berg on 27/05/2021.
  6. // Copyright © 2021 Mark Wilson. All rights reserved.
  7. //
  8. import SwiftUI
  9. import Combine
  10. import HealthKit
  11. struct NotificationSettingsView: View {
  12. @State private var presentableStatus: StatusMessage?
  13. private var glucoseUnit: HKUnit
  14. private let glucoseSegments = [HKUnit.millimolesPerLiter, HKUnit.milligramsPerDeciliter]
  15. private lazy var glucoseSegmentStrings = self.glucoseSegments.map({ $0.localizedShortUnitString })
  16. public init(glucoseUnit: HKUnit) {
  17. if let savedGlucoseUnit = UserDefaults.standard.mmGlucoseUnit {
  18. self.glucoseUnit = savedGlucoseUnit
  19. } else {
  20. self.glucoseUnit = glucoseUnit
  21. UserDefaults.standard.mmGlucoseUnit = glucoseUnit
  22. }
  23. }
  24. private enum Key: String {
  25. case mmAlertLowBatteryWarning = "no.bjorninge.mmLowBatteryWarning"
  26. case mmAlertInvalidSensorDetected = "no.bjorninge.mmInvalidSensorDetected"
  27. //case mmAlertalarmNotifications
  28. case mmAlertNewSensorDetected = "no.bjorninge.mmNewSensorDetected"
  29. case mmAlertNoSensorDetected = "no.bjorninge.mmNoSensorDetected"
  30. case mmAlertSensorSoonExpire = "no.bjorninge.mmAlertSensorSoonExpire"
  31. //handle specially:
  32. case mmGlucoseUnit = "no.bjorninge.mmGlucoseUnit"
  33. }
  34. @AppStorage(Key.mmAlertLowBatteryWarning.rawValue) var mmAlertLowBatteryWarning: Bool = true
  35. @AppStorage(Key.mmAlertInvalidSensorDetected.rawValue) var mmAlertInvalidSensorDetected: Bool = true
  36. @AppStorage(Key.mmAlertNewSensorDetected.rawValue) var mmAlertNewSensorDetected: Bool = true
  37. @AppStorage(Key.mmAlertNoSensorDetected.rawValue) var mmAlertNoSensorDetected: Bool = true
  38. @AppStorage(Key.mmAlertSensorSoonExpire.rawValue) var mmAlertSensorSoonExpire: Bool = true
  39. //especially handled mostly for backward compat
  40. @AppStorage(Key.mmGlucoseUnit.rawValue) var mmGlucoseUnit: String = ""
  41. @State var notifyErrorState = FormErrorState()
  42. @State private var favoriteGlucoseUnit = 0
  43. static let formatter = NumberFormatter()
  44. var additionalNotificationsSection : some View {
  45. Section(header: Text("Additional notification types")) {
  46. Toggle("Low battery", isOn:$mmAlertLowBatteryWarning)
  47. Toggle("Invalid sensor", isOn:$mmAlertInvalidSensorDetected)
  48. Toggle("Sensor change", isOn:$mmAlertNewSensorDetected)
  49. Toggle("Sensor not found", isOn:$mmAlertNoSensorDetected)
  50. Toggle("Sensor expires soon", isOn:$mmAlertSensorSoonExpire)
  51. }
  52. }
  53. var miscSection : some View {
  54. Section(header: Text("Misc")) {
  55. HStack {
  56. Text("Unit override")
  57. Picker(selection: $favoriteGlucoseUnit, label: Text("Unit override")) {
  58. Text(HKUnit.millimolesPerLiter.localizedShortUnitString).tag(0)
  59. Text(HKUnit.milligramsPerDeciliter.localizedShortUnitString).tag(1)
  60. }
  61. .pickerStyle(SegmentedPickerStyle())
  62. .clipped()
  63. }
  64. }
  65. }
  66. var body: some View {
  67. List {
  68. additionalNotificationsSection
  69. miscSection
  70. .onAppear {
  71. favoriteGlucoseUnit = glucoseSegments.firstIndex(of: glucoseUnit) ?? 0
  72. }
  73. .onChange(of: favoriteGlucoseUnit){ newValue in
  74. let newUnit = glucoseSegments[newValue]
  75. if newUnit == HKUnit.milligramsPerDeciliter {
  76. mmGlucoseUnit = "mgdl"
  77. } else if newUnit == HKUnit.millimolesPerLiter {
  78. mmGlucoseUnit = "mmol"
  79. }
  80. }
  81. }
  82. .listStyle(InsetGroupedListStyle())
  83. .alert(item: $presentableStatus) { status in
  84. Alert(title: Text(status.title), message: Text(status.message) , dismissButton: .default(Text("Got it!")))
  85. }
  86. .navigationBarTitle("Notification Settings")
  87. }
  88. }
  89. struct NotificationSettingsView_Previews: PreviewProvider {
  90. static var previews: some View {
  91. NotificationSettingsView(glucoseUnit: HKUnit.millimolesPerLiter)
  92. }
  93. }