GlucoseNotificationSettingsRootView.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 var notificationsDisabled = false
  16. private var glucoseFormatter: NumberFormatter {
  17. let formatter = NumberFormatter()
  18. formatter.numberStyle = .decimal
  19. formatter.maximumFractionDigits = 0
  20. if state.units == .mmolL {
  21. formatter.maximumFractionDigits = 1
  22. }
  23. formatter.roundingMode = .halfUp
  24. return formatter
  25. }
  26. private var carbsFormatter: NumberFormatter {
  27. let formatter = NumberFormatter()
  28. formatter.numberStyle = .decimal
  29. formatter.maximumFractionDigits = 0
  30. return formatter
  31. }
  32. @Environment(\.colorScheme) var colorScheme
  33. var color: LinearGradient {
  34. colorScheme == .dark ? LinearGradient(
  35. gradient: Gradient(colors: [
  36. Color.bgDarkBlue,
  37. Color.bgDarkerDarkBlue
  38. ]),
  39. startPoint: .top,
  40. endPoint: .bottom
  41. )
  42. :
  43. LinearGradient(
  44. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  45. startPoint: .top,
  46. endPoint: .bottom
  47. )
  48. }
  49. var body: some View {
  50. Form {
  51. if !notificationsDisabled {
  52. SettingInputSection(
  53. decimalValue: $decimalPlaceholder,
  54. booleanValue: $state.glucoseBadge,
  55. shouldDisplayHint: $shouldDisplayHint,
  56. selectedVerboseHint: Binding(
  57. get: { selectedVerboseHint },
  58. set: {
  59. selectedVerboseHint = $0
  60. hintLabel = "Show Glucose App Badge"
  61. }
  62. ),
  63. units: state.units,
  64. type: .boolean,
  65. label: "Show Glucose App Badge",
  66. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  67. verboseHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  68. headerText: "Various Glucose Notifications"
  69. )
  70. }
  71. SettingInputSection(
  72. decimalValue: $decimalPlaceholder,
  73. booleanValue: $state.glucoseNotificationsAlways,
  74. shouldDisplayHint: $shouldDisplayHint,
  75. selectedVerboseHint: Binding(
  76. get: { selectedVerboseHint },
  77. set: {
  78. selectedVerboseHint = $0
  79. hintLabel = "Always Notify Glucose"
  80. }
  81. ),
  82. units: state.units,
  83. type: .boolean,
  84. label: "Always Notify Glucose",
  85. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  86. verboseHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
  87. )
  88. if !notificationsDisabled {
  89. SettingInputSection(
  90. decimalValue: $decimalPlaceholder,
  91. booleanValue: $state.useAlarmSound,
  92. shouldDisplayHint: $shouldDisplayHint,
  93. selectedVerboseHint: Binding(
  94. get: { selectedVerboseHint },
  95. set: {
  96. selectedVerboseHint = $0
  97. hintLabel = "Play Alarm Sound"
  98. }
  99. ),
  100. units: state.units,
  101. type: .boolean,
  102. label: "Play Alarm Sound",
  103. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  104. verboseHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
  105. )
  106. SettingInputSection(
  107. decimalValue: $decimalPlaceholder,
  108. booleanValue: $state.addSourceInfoToGlucoseNotifications,
  109. shouldDisplayHint: $shouldDisplayHint,
  110. selectedVerboseHint: Binding(
  111. get: { selectedVerboseHint },
  112. set: {
  113. selectedVerboseHint = $0
  114. hintLabel = "Add Glucose Source to Alarm"
  115. }
  116. ),
  117. units: state.units,
  118. type: .boolean,
  119. label: "Add Glucose Source to Alarm",
  120. miniHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr.",
  121. verboseHint: "Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
  122. )
  123. Section {
  124. HStack {
  125. Text("Low Glucose Alarm Limit")
  126. Spacer()
  127. TextFieldWithToolBar(text: $state.lowGlucose, placeholder: "0", numberFormatter: glucoseFormatter)
  128. Text(state.units.rawValue).foregroundColor(.secondary)
  129. }.padding(.top)
  130. HStack {
  131. Text("High Glucose Alarm Limit")
  132. Spacer()
  133. TextFieldWithToolBar(text: $state.highGlucose, placeholder: "0", numberFormatter: glucoseFormatter)
  134. Text(state.units.rawValue).foregroundColor(.secondary)
  135. }
  136. HStack(alignment: .top) {
  137. Text(
  138. "Set the lower and upper limit for glucose alarms. See hint for more details."
  139. )
  140. .font(.footnote)
  141. .foregroundColor(.secondary)
  142. .lineLimit(nil)
  143. Spacer()
  144. Button(
  145. action: {
  146. hintLabel = "Low and High Glucose Alarm Limits"
  147. selectedVerboseHint =
  148. "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 a glucose alarm."
  149. shouldDisplayHint.toggle()
  150. },
  151. label: {
  152. HStack {
  153. Image(systemName: "questionmark.circle")
  154. }
  155. }
  156. ).buttonStyle(BorderlessButtonStyle())
  157. }.padding(.vertical)
  158. }
  159. .listRowBackground(Color.chart)
  160. }
  161. }
  162. .sheet(isPresented: $shouldDisplayHint) {
  163. SettingInputHintView(
  164. hintDetent: $hintDetent,
  165. shouldDisplayHint: $shouldDisplayHint,
  166. hintLabel: hintLabel ?? "",
  167. hintText: selectedVerboseHint ?? "",
  168. sheetTitle: "Help"
  169. )
  170. }
  171. .onReceive(resolver.resolve(AlertPermissionsChecker.self)!.$notificationsDisabled, perform: {
  172. notificationsDisabled = $0
  173. })
  174. .scrollContentBackground(.hidden).background(color)
  175. .onAppear(perform: configureView)
  176. .navigationBarTitle("Glucose Notifications")
  177. .navigationBarTitleDisplayMode(.automatic)
  178. }
  179. }
  180. }