GlucoseNotificationSettingsRootView.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import ActivityKit
  2. import Combine
  3. import SwiftUI
  4. import Swinject
  5. extension GlucoseNotificationSettings {
  6. struct RootView: BaseView {
  7. let resolver: Resolver
  8. @StateObject var state = StateModel()
  9. @State private var shouldDisplayHint: Bool = false
  10. @State var hintDetent = PresentationDetent.large
  11. @State var selectedVerboseHint: String?
  12. @State var hintLabel: String?
  13. @State private var decimalPlaceholder: Decimal = 0.0
  14. @State private var booleanPlaceholder: Bool = false
  15. @State private var displayPickerLowGlucose: Bool = false
  16. @State private var displayPickerHighGlucose: Bool = false
  17. private var glucoseFormatter: NumberFormatter {
  18. let formatter = NumberFormatter()
  19. formatter.numberStyle = .decimal
  20. formatter.maximumFractionDigits = 0
  21. if state.units == .mmolL {
  22. formatter.maximumFractionDigits = 1
  23. }
  24. formatter.roundingMode = .halfUp
  25. return formatter
  26. }
  27. private var carbsFormatter: NumberFormatter {
  28. let formatter = NumberFormatter()
  29. formatter.numberStyle = .decimal
  30. formatter.maximumFractionDigits = 0
  31. return formatter
  32. }
  33. @Environment(\.colorScheme) var colorScheme
  34. @Environment(AppState.self) var appState
  35. var body: some View {
  36. Form {
  37. SettingInputSection(
  38. decimalValue: $decimalPlaceholder,
  39. booleanValue: $state.notificationsPump,
  40. shouldDisplayHint: $shouldDisplayHint,
  41. selectedVerboseHint: Binding(
  42. get: { selectedVerboseHint },
  43. set: {
  44. selectedVerboseHint = $0
  45. hintLabel = "Always Notify Pump"
  46. }
  47. ),
  48. units: state.units,
  49. type: .boolean,
  50. label: "Always Notify Pump",
  51. miniHint: "Always Notify Pump Warnings",
  52. verboseHint: "With iOS Trio Notifications enabled, you can let Trio display most Pump Notifications in iOS Notification Center as a Banner, List and on the Lock Screen. It allows you to refer to Trio Information at a glance and troubleshoot any informational issue. Set iOS Notifications Banner Style to Persistent to display banners in the app until dismissed.\n\nIf iOS Trio Notifications is disabled, Trio will display these messages in-app as a banner only.\n\nAn example of a Pump Warning is 'Pod Expiration Reminder'",
  53. headerText: "Trio Information Notifications"
  54. )
  55. SettingInputSection(
  56. decimalValue: $decimalPlaceholder,
  57. booleanValue: $state.notificationsCgm,
  58. shouldDisplayHint: $shouldDisplayHint,
  59. selectedVerboseHint: Binding(
  60. get: { selectedVerboseHint },
  61. set: {
  62. selectedVerboseHint = $0
  63. hintLabel = "Always Notify CGM"
  64. }
  65. ),
  66. units: state.units,
  67. type: .boolean,
  68. label: "Always Notify CGM",
  69. miniHint: "Always Notify CGM Warnings",
  70. verboseHint: "With iOS Trio Notifications enabled, you can let Trio display most CGM Notifications in iOS Notification Center as a Banner, List and on the Lock Screen. It allows you to refer to Trio Information at a glance and troubleshoot any informational issue. Set iOS Notifications Banner Style to Persistent to display banners in the app until dismissed.\n\nIf iOS Trio Notifications is disabled, Trio will display these messages in-app as a banner only.\n\nAn example of a CGM Warning is 'Unable to open the app'"
  71. )
  72. SettingInputSection(
  73. decimalValue: $decimalPlaceholder,
  74. booleanValue: $state.notificationsCarb,
  75. shouldDisplayHint: $shouldDisplayHint,
  76. selectedVerboseHint: Binding(
  77. get: { selectedVerboseHint },
  78. set: {
  79. selectedVerboseHint = $0
  80. hintLabel = "Always Notify Carb"
  81. }
  82. ),
  83. units: state.units,
  84. type: .boolean,
  85. label: "Always Notify Carb",
  86. miniHint: "Always Notify Carb Warnings",
  87. verboseHint: "With iOS Trio Notifications enabled, you can let Trio display most Carb Notifications in iOS Notification Center as a Banner, List and on the Lock Screen. It allows you to refer to Trio Information at a glance and troubleshoot any informational issue. Set iOS Notifications Banner Style to Persistent to display banners in the app until dismissed.\n\nIf iOS Trio Notifications is disabled, Trio will display these messages in-app as a banner only.\n\nAn example of a Carb Warning is 'Carbs required: 30 g'"
  88. )
  89. SettingInputSection(
  90. decimalValue: $decimalPlaceholder,
  91. booleanValue: $state.notificationsAlgorithm,
  92. shouldDisplayHint: $shouldDisplayHint,
  93. selectedVerboseHint: Binding(
  94. get: { selectedVerboseHint },
  95. set: {
  96. selectedVerboseHint = $0
  97. hintLabel = "Always Notify Algorithm"
  98. }
  99. ),
  100. units: state.units,
  101. type: .boolean,
  102. label: "Always Notify Algorithm",
  103. miniHint: "Always Notify Algorithm Warnings",
  104. verboseHint: "With iOS Trio Notifications enabled, you can let Trio display most Algorithm Notifications in iOS Notification Center as a Banner, List and on the Lock Screen. It allows you to refer to Trio Information at a glance and troubleshoot any informational issue. Set iOS Notifications Banner Style to Persistent to display banners in the app until dismissed.\n\nIf iOS Trio Notifications is disabled, Trio will display these messages in-app as a banner only.\n\nAn example of an Algorithm Warning is 'Error: Invalid glucose: Not enough glucose data'"
  105. )
  106. SettingInputSection(
  107. decimalValue: $decimalPlaceholder,
  108. booleanValue: $state.glucoseBadge,
  109. shouldDisplayHint: $shouldDisplayHint,
  110. selectedVerboseHint: Binding(
  111. get: { selectedVerboseHint },
  112. set: {
  113. selectedVerboseHint = $0
  114. hintLabel = "Show Glucose App Badge"
  115. }
  116. ),
  117. units: state.units,
  118. type: .boolean,
  119. label: "Show Glucose App Badge",
  120. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  121. verboseHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  122. headerText: "Various Glucose Notifications"
  123. )
  124. SettingInputSection(
  125. decimalValue: $decimalPlaceholder,
  126. booleanValue: $state.glucoseNotificationsAlways,
  127. shouldDisplayHint: $shouldDisplayHint,
  128. selectedVerboseHint: Binding(
  129. get: { selectedVerboseHint },
  130. set: {
  131. selectedVerboseHint = $0
  132. hintLabel = "e"
  133. }
  134. ),
  135. units: state.units,
  136. type: .boolean,
  137. label: "Always Notify Glucose",
  138. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  139. verboseHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
  140. )
  141. SettingInputSection(
  142. decimalValue: $decimalPlaceholder,
  143. booleanValue: $state.useAlarmSound,
  144. shouldDisplayHint: $shouldDisplayHint,
  145. selectedVerboseHint: Binding(
  146. get: { selectedVerboseHint },
  147. set: {
  148. selectedVerboseHint = $0
  149. hintLabel = "Play Alarm Sound"
  150. }
  151. ),
  152. units: state.units,
  153. type: .boolean,
  154. label: "Play Alarm Sound",
  155. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  156. verboseHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
  157. )
  158. SettingInputSection(
  159. decimalValue: $decimalPlaceholder,
  160. booleanValue: $state.addSourceInfoToGlucoseNotifications,
  161. shouldDisplayHint: $shouldDisplayHint,
  162. selectedVerboseHint: Binding(
  163. get: { selectedVerboseHint },
  164. set: {
  165. selectedVerboseHint = $0
  166. hintLabel = "Add Glucose Source to Alarm"
  167. }
  168. ),
  169. units: state.units,
  170. type: .boolean,
  171. label: "Add Glucose Source to Alarm",
  172. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  173. verboseHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
  174. )
  175. self.lowAndHighGlucoseAlertSection
  176. }
  177. .sheet(isPresented: $shouldDisplayHint) {
  178. SettingInputHintView(
  179. hintDetent: $hintDetent,
  180. shouldDisplayHint: $shouldDisplayHint,
  181. hintLabel: hintLabel ?? "",
  182. hintText: selectedVerboseHint ?? "",
  183. sheetTitle: "Help"
  184. )
  185. }
  186. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  187. .onAppear(perform: configureView)
  188. .navigationBarTitle("Trio Notifications")
  189. .navigationBarTitleDisplayMode(.automatic)
  190. }
  191. var lowAndHighGlucoseAlertSection: some View {
  192. Section {
  193. VStack {
  194. VStack {
  195. HStack {
  196. Text("Low Glucose Alarm Limit")
  197. Spacer()
  198. Group {
  199. Text(
  200. state.units == .mgdL ? state.lowGlucose.description : state.lowGlucose.formattedAsMmolL
  201. )
  202. .foregroundColor(!displayPickerLowGlucose ? .primary : .accentColor)
  203. Text(state.units == .mgdL ? " mg/dL" : " mmol/L").foregroundColor(.secondary)
  204. }
  205. }
  206. .onTapGesture {
  207. displayPickerLowGlucose.toggle()
  208. }
  209. }
  210. .padding(.top)
  211. if displayPickerLowGlucose {
  212. let setting = PickerSettingsProvider.shared.settings.lowGlucose
  213. Picker(selection: $state.lowGlucose, label: Text("")) {
  214. ForEach(
  215. PickerSettingsProvider.shared.generatePickerValues(from: setting, units: state.units),
  216. id: \.self
  217. ) { value in
  218. let displayValue = state.units == .mgdL ? value.description : value.formattedAsMmolL
  219. Text(displayValue).tag(value)
  220. }
  221. }
  222. .pickerStyle(WheelPickerStyle())
  223. .frame(maxWidth: .infinity)
  224. }
  225. VStack {
  226. HStack {
  227. Text("High Glucose Alarm Limit")
  228. Spacer()
  229. Group {
  230. Text(
  231. state.units == .mgdL ? state.highGlucose.description : state.highGlucose.formattedAsMmolL
  232. )
  233. .foregroundColor(!displayPickerHighGlucose ? .primary : .accentColor)
  234. Text(state.units == .mgdL ? " mg/dL" : " mmol/L").foregroundColor(.secondary)
  235. }
  236. }
  237. .onTapGesture {
  238. displayPickerHighGlucose.toggle()
  239. }
  240. }
  241. .padding(.top)
  242. if displayPickerHighGlucose {
  243. let setting = PickerSettingsProvider.shared.settings.highGlucose
  244. Picker(selection: $state.highGlucose, label: Text("")) {
  245. ForEach(
  246. PickerSettingsProvider.shared.generatePickerValues(from: setting, units: state.units),
  247. id: \.self
  248. ) { value in
  249. let displayValue = state.units == .mgdL ? value.description : value.formattedAsMmolL
  250. Text(displayValue).tag(value)
  251. }
  252. }
  253. .pickerStyle(WheelPickerStyle())
  254. .frame(maxWidth: .infinity)
  255. }
  256. HStack(alignment: .top) {
  257. Text(
  258. "Set the lower and upper limit for glucose alarms. See hint for more details."
  259. )
  260. .lineLimit(nil)
  261. .font(.footnote)
  262. .foregroundColor(.secondary)
  263. Spacer()
  264. Button(
  265. action: {
  266. hintLabel = "Low and High Glucose Alarm Limits"
  267. selectedVerboseHint =
  268. "These two settings limit the range outside of which you will be notified via push notifications. If your CGM readings are below 'Low' or above 'High', you will receive an alarm via push notification."
  269. shouldDisplayHint.toggle()
  270. },
  271. label: {
  272. HStack {
  273. Image(systemName: "questionmark.circle")
  274. }
  275. }
  276. ).buttonStyle(BorderlessButtonStyle())
  277. }.padding(.top)
  278. }.padding(.bottom)
  279. }.listRowBackground(Color.chart)
  280. }
  281. }
  282. }