GlucoseNotificationSettingsRootView.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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: AnyView?
  12. @State var hintLabel: String?
  13. @State private var decimalPlaceholder: Decimal = 0.0
  14. @State private var booleanPlaceholder: Bool = false
  15. private var glucoseFormatter: NumberFormatter {
  16. let formatter = NumberFormatter()
  17. formatter.numberStyle = .decimal
  18. formatter.maximumFractionDigits = 0
  19. if state.units == .mmolL {
  20. formatter.maximumFractionDigits = 1
  21. }
  22. formatter.roundingMode = .halfUp
  23. return formatter
  24. }
  25. private var carbsFormatter: NumberFormatter {
  26. let formatter = NumberFormatter()
  27. formatter.numberStyle = .decimal
  28. formatter.maximumFractionDigits = 0
  29. return formatter
  30. }
  31. @Environment(\.colorScheme) var colorScheme
  32. var color: LinearGradient {
  33. colorScheme == .dark ? LinearGradient(
  34. gradient: Gradient(colors: [
  35. Color.bgDarkBlue,
  36. Color.bgDarkerDarkBlue
  37. ]),
  38. startPoint: .top,
  39. endPoint: .bottom
  40. )
  41. :
  42. LinearGradient(
  43. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  44. startPoint: .top,
  45. endPoint: .bottom
  46. )
  47. }
  48. var body: some View {
  49. Form {
  50. SettingInputSection(
  51. decimalValue: $decimalPlaceholder,
  52. booleanValue: $state.glucoseBadge,
  53. shouldDisplayHint: $shouldDisplayHint,
  54. selectedVerboseHint: Binding(
  55. get: { selectedVerboseHint },
  56. set: {
  57. selectedVerboseHint = $0.map { AnyView($0) }
  58. hintLabel = "Show Glucose App Badge"
  59. }
  60. ),
  61. units: state.units,
  62. type: .boolean,
  63. label: "Show Glucose App Badge",
  64. miniHint: "Show your current glucose reading at the top of the Trio app icon",
  65. verboseHint: Text(
  66. "This will add your current glucose on the top right of your Trio icon as a red notification badge."
  67. ),
  68. headerText: "Various Glucose Notifications"
  69. )
  70. SettingInputSection(
  71. decimalValue: $decimalPlaceholder,
  72. booleanValue: $state.glucoseNotificationsAlways,
  73. shouldDisplayHint: $shouldDisplayHint,
  74. selectedVerboseHint: Binding(
  75. get: { selectedVerboseHint },
  76. set: {
  77. selectedVerboseHint = $0.map { AnyView($0) }
  78. hintLabel = "Always Notify Glucose"
  79. }
  80. ),
  81. units: state.units,
  82. type: .boolean,
  83. label: "Always Notify Glucose",
  84. miniHint: "A notification will be triggered every time your glucose is updated in Trio",
  85. verboseHint: Text("A notification will be triggered every time your glucose is updated in Trio.")
  86. )
  87. SettingInputSection(
  88. decimalValue: $decimalPlaceholder,
  89. booleanValue: $state.useAlarmSound,
  90. shouldDisplayHint: $shouldDisplayHint,
  91. selectedVerboseHint: Binding(
  92. get: { selectedVerboseHint },
  93. set: {
  94. selectedVerboseHint = $0.map { AnyView($0) }
  95. hintLabel = "Play Alarm Sound"
  96. }
  97. ),
  98. units: state.units,
  99. type: .boolean,
  100. label: "Play Alarm Sound",
  101. miniHint: "This will cause a sound to be triggered by every Trio notification",
  102. verboseHint: Text("This will cause a sound to be triggered by every Trio notification.")
  103. )
  104. SettingInputSection(
  105. decimalValue: $decimalPlaceholder,
  106. booleanValue: $state.addSourceInfoToGlucoseNotifications,
  107. shouldDisplayHint: $shouldDisplayHint,
  108. selectedVerboseHint: Binding(
  109. get: { selectedVerboseHint },
  110. set: {
  111. selectedVerboseHint = $0.map { AnyView($0) }
  112. hintLabel = "Add Glucose Source to Alarm"
  113. }
  114. ),
  115. units: state.units,
  116. type: .boolean,
  117. label: "Add Glucose Source to Alarm",
  118. miniHint: "The source of the glucose reading will be added to the notification",
  119. verboseHint: Text("The source of the glucose reading will be added to the notification.")
  120. )
  121. Section {
  122. HStack {
  123. Text("Low Glucose Alarm Limit")
  124. Spacer()
  125. TextFieldWithToolBar(text: $state.lowGlucose, placeholder: "0", numberFormatter: glucoseFormatter)
  126. Text(state.units.rawValue).foregroundColor(.secondary)
  127. }.padding(.top)
  128. HStack {
  129. Text("High Glucose Alarm Limit")
  130. Spacer()
  131. TextFieldWithToolBar(text: $state.highGlucose, placeholder: "0", numberFormatter: glucoseFormatter)
  132. Text(state.units.rawValue).foregroundColor(.secondary)
  133. }
  134. HStack(alignment: .top) {
  135. Text(
  136. "Set the upper and lower limits for glucose alarms. See hint for more details."
  137. )
  138. .font(.footnote)
  139. .foregroundColor(.secondary)
  140. .lineLimit(nil)
  141. Spacer()
  142. Button(
  143. action: {
  144. hintLabel = "Low and High Glucose Alarm Limits"
  145. selectedVerboseHint =
  146. AnyView(Text(
  147. "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."
  148. ))
  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. .sheet(isPresented: $shouldDisplayHint) {
  162. SettingInputHintView(
  163. hintDetent: $hintDetent,
  164. shouldDisplayHint: $shouldDisplayHint,
  165. hintLabel: hintLabel ?? "",
  166. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  167. sheetTitle: "Help"
  168. )
  169. }
  170. .scrollContentBackground(.hidden).background(color)
  171. .onAppear(perform: configureView)
  172. .navigationBarTitle("Glucose Notifications")
  173. .navigationBarTitleDisplayMode(.automatic)
  174. }
  175. }
  176. }