UserInterfaceSettingsRootView.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. import SwiftUI
  2. import Swinject
  3. extension UserInterfaceSettings {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. @State private var shouldDisplayHint: Bool = false
  8. @State var hintDetent = PresentationDetent.large
  9. @State var selectedVerboseHint: AnyView?
  10. @State var hintLabel: String?
  11. @State private var decimalPlaceholder: Decimal = 0.0
  12. @State private var booleanPlaceholder: Bool = false
  13. @State private var displayPickerLowThreshold: Bool = false
  14. @State private var displayPickerHighThreshold: Bool = false
  15. @AppStorage("colorSchemePreference") private var colorSchemePreference: ColorSchemeOption = .systemDefault
  16. @Environment(\.colorScheme) var colorScheme
  17. var color: LinearGradient {
  18. colorScheme == .dark ? LinearGradient(
  19. gradient: Gradient(colors: [
  20. Color.bgDarkBlue,
  21. Color.bgDarkerDarkBlue
  22. ]),
  23. startPoint: .top,
  24. endPoint: .bottom
  25. )
  26. :
  27. LinearGradient(
  28. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  29. startPoint: .top,
  30. endPoint: .bottom
  31. )
  32. }
  33. private var glucoseFormatter: NumberFormatter {
  34. let formatter = NumberFormatter()
  35. formatter.numberStyle = .decimal
  36. formatter.maximumFractionDigits = 0
  37. if state.units == .mmolL {
  38. formatter.maximumFractionDigits = 1
  39. }
  40. formatter.roundingMode = .halfUp
  41. return formatter
  42. }
  43. private var carbsFormatter: NumberFormatter {
  44. let formatter = NumberFormatter()
  45. formatter.numberStyle = .decimal
  46. formatter.maximumFractionDigits = 0
  47. return formatter
  48. }
  49. var body: some View {
  50. Form {
  51. Section(
  52. header: Text("General Appearance"),
  53. content: {
  54. VStack {
  55. Picker(
  56. selection: $colorSchemePreference,
  57. label: Text("Trio Color Scheme")
  58. ) {
  59. ForEach(ColorSchemeOption.allCases) { selection in
  60. Text(selection.displayName).tag(selection)
  61. }
  62. }.padding(.top)
  63. HStack(alignment: .top) {
  64. Text(
  65. "Choose between Light, Dark, or System Default for the app color scheme"
  66. )
  67. .font(.footnote)
  68. .foregroundColor(.secondary)
  69. .lineLimit(nil)
  70. Spacer()
  71. Button(
  72. action: {
  73. hintLabel = "Color Scheme Preference"
  74. selectedVerboseHint =
  75. AnyView(
  76. Text(
  77. "Set the app color scheme using the following options \n\nSystem Default: Follows the phone's current color scheme setting at that time\nLight: Always in Light mode \nDark: Always in Dark mode"
  78. )
  79. )
  80. shouldDisplayHint.toggle()
  81. },
  82. label: {
  83. HStack {
  84. Image(systemName: "questionmark.circle")
  85. }
  86. }
  87. ).buttonStyle(BorderlessButtonStyle())
  88. }.padding(.top)
  89. }.padding(.bottom)
  90. }
  91. ).listRowBackground(Color.chart)
  92. Section {
  93. VStack {
  94. Picker(
  95. selection: $state.glucoseColorScheme,
  96. label: Text("Glucose Color Scheme")
  97. ) {
  98. ForEach(GlucoseColorScheme.allCases) { selection in
  99. Text(selection.displayName).tag(selection)
  100. }
  101. }.padding(.top)
  102. HStack(alignment: .top) {
  103. Text(
  104. "Choose between Static or Dynamic coloring for glucose readings"
  105. )
  106. .font(.footnote)
  107. .foregroundColor(.secondary)
  108. .lineLimit(nil)
  109. Spacer()
  110. Button(
  111. action: {
  112. hintLabel = "Glucose Color Scheme"
  113. selectedVerboseHint =
  114. AnyView(
  115. Text(
  116. "Set the color scheme for glucose readings on the main glucose graph, live activities, and bolus calculator using the following options: \n\nStatic: Below-Range Target readings will be in RED, In-Range will be GREEN, Above-Range will be YELLOW \n\nDynamic: Readings on Target will be GREEN. As readings approach and exceed below target, they become more RED. As readings approach and exceed above targer, they become more PURPLE."
  117. )
  118. )
  119. shouldDisplayHint.toggle()
  120. },
  121. label: {
  122. HStack {
  123. Image(systemName: "questionmark.circle")
  124. }
  125. }
  126. ).buttonStyle(BorderlessButtonStyle())
  127. }.padding(.top)
  128. }.padding(.bottom)
  129. }.listRowBackground(Color.chart)
  130. Section(
  131. header: Text("Home View Settings"),
  132. content: {
  133. VStack {
  134. Toggle("Show X-Axis Grid Lines", isOn: $state.xGridLines)
  135. Toggle("Show Y-Axis Grid Lines", isOn: $state.yGridLines)
  136. HStack(alignment: .top) {
  137. Text(
  138. "Display the grid lines behind the glucose graph"
  139. )
  140. .font(.footnote)
  141. .foregroundColor(.secondary)
  142. .lineLimit(nil)
  143. Spacer()
  144. Button(
  145. action: {
  146. hintLabel = "Show Main Chart X- and Y-Axis Grid Lines"
  147. selectedVerboseHint =
  148. AnyView(
  149. Text("Choose whether or not to display one or both X- and Y-Axis grid lines.")
  150. )
  151. shouldDisplayHint.toggle()
  152. },
  153. label: {
  154. HStack {
  155. Image(systemName: "questionmark.circle")
  156. }
  157. }
  158. ).buttonStyle(BorderlessButtonStyle())
  159. }.padding(.top)
  160. }.padding(.vertical)
  161. }
  162. ).listRowBackground(Color.chart)
  163. SettingInputSection(
  164. decimalValue: $decimalPlaceholder,
  165. booleanValue: $state.rulerMarks,
  166. shouldDisplayHint: $shouldDisplayHint,
  167. selectedVerboseHint: Binding(
  168. get: { selectedVerboseHint },
  169. set: {
  170. selectedVerboseHint = $0.map { AnyView($0) }
  171. hintLabel = "Show Low and High Thresholds"
  172. }
  173. ),
  174. units: state.units,
  175. type: .boolean,
  176. label: "Show Low and High Thresholds",
  177. miniHint: "Display the Low and High glucose thresholds set below",
  178. verboseHint: Text(
  179. "This setting displays the upper and lower values for your glucose target range. \n\nThis range is for display and statistical purposes only and does not influence insulin dosing."
  180. )
  181. )
  182. if state.rulerMarks {
  183. Section {
  184. VStack {
  185. VStack {
  186. HStack {
  187. Text("Low Threshold")
  188. Spacer()
  189. Group {
  190. Text(state.units == .mgdL ? state.low.description : state.low.asMmolL.description)
  191. .foregroundColor(!displayPickerLowThreshold ? .primary : .accentColor)
  192. Text(state.units == .mgdL ? " mg/dL" : " mmol/L").foregroundColor(.secondary)
  193. }
  194. }
  195. .onTapGesture {
  196. displayPickerLowThreshold.toggle()
  197. }
  198. }
  199. .padding(.top)
  200. if displayPickerLowThreshold {
  201. let setting = PickerSettingsProvider.shared.settings.low
  202. Picker(selection: $state.low, label: Text("")) {
  203. ForEach(
  204. PickerSettingsProvider.shared.generatePickerValues(from: setting, units: state.units),
  205. id: \.self
  206. ) { value in
  207. let displayValue = state.units == .mgdL ? value : value.asMmolL
  208. Text("\(displayValue.description)").tag(value)
  209. }
  210. }
  211. .pickerStyle(WheelPickerStyle())
  212. .frame(maxWidth: .infinity)
  213. }
  214. VStack {
  215. HStack {
  216. Text("High Threshold")
  217. Spacer()
  218. Group {
  219. Text(state.units == .mgdL ? state.high.description : state.high.asMmolL.description)
  220. .foregroundColor(!displayPickerHighThreshold ? .primary : .accentColor)
  221. Text(state.units == .mgdL ? " mg/dL" : " mmol/L").foregroundColor(.secondary)
  222. }
  223. }
  224. .onTapGesture {
  225. displayPickerHighThreshold.toggle()
  226. }
  227. }
  228. .padding(.top)
  229. if displayPickerHighThreshold {
  230. let setting = PickerSettingsProvider.shared.settings.high
  231. Picker(selection: $state.high, label: Text("")) {
  232. ForEach(
  233. PickerSettingsProvider.shared.generatePickerValues(from: setting, units: state.units),
  234. id: \.self
  235. ) { value in
  236. let displayValue = state.units == .mgdL ? value : value.asMmolL
  237. Text("\(displayValue.description)").tag(value)
  238. }
  239. }
  240. .pickerStyle(WheelPickerStyle())
  241. .frame(maxWidth: .infinity)
  242. }
  243. HStack(alignment: .top) {
  244. Text(
  245. "Set low and high glucose values for the main screen glucose graph and statistics \nLow Default: 70 \nHigh Default: 180"
  246. )
  247. .lineLimit(nil)
  248. .font(.footnote)
  249. .foregroundColor(.secondary)
  250. Spacer()
  251. Button(
  252. action: {
  253. hintLabel = "Low and High Thresholds"
  254. selectedVerboseHint =
  255. AnyView(
  256. Text(
  257. "Default values are based on internationally accepted Time in Range values of 70-180 mg/dL (5.5-10 mmol/L) \nSet the values used in the main screen glucose graph and to determine Time in Range for Statistics. \nNote: These values are not used to calculate insulin dosing."
  258. )
  259. )
  260. shouldDisplayHint.toggle()
  261. },
  262. label: {
  263. HStack {
  264. Image(systemName: "questionmark.circle")
  265. }
  266. }
  267. ).buttonStyle(BorderlessButtonStyle())
  268. }.padding(.top)
  269. }.padding(.bottom)
  270. }.listRowBackground(Color.chart)
  271. }
  272. Section {
  273. VStack {
  274. Picker(
  275. selection: $state.forecastDisplayType,
  276. label: Text("Forecast Display Type")
  277. ) {
  278. ForEach(ForecastDisplayType.allCases) { selection in
  279. Text(selection.displayName).tag(selection)
  280. }
  281. }.padding(.top)
  282. HStack(alignment: .top) {
  283. Text(
  284. "Choose between the OpenAPS colored Lines or the Cone of Uncertainty for the Forecast Lines"
  285. )
  286. .font(.footnote)
  287. .foregroundColor(.secondary)
  288. .lineLimit(nil)
  289. Spacer()
  290. Button(
  291. action: {
  292. hintLabel = "Forecast Display Type"
  293. selectedVerboseHint =
  294. AnyView(
  295. Text(
  296. "This setting allows you to choose between the following two options for the Forecast lines (previously: Prediction Lines). \n\nLines: Uses the IOB, COB, UAM, and ZT forecast lines from OpenAPS \n\nCone: Uses a combined range of all possible forecasts from the OpenAPS lines and provides you with a range of possible forecasts. This option has shown to reduce confusion and stress around algorithm forecasts by providing a less concerning visual representation."
  297. )
  298. )
  299. shouldDisplayHint.toggle()
  300. },
  301. label: {
  302. HStack {
  303. Image(systemName: "questionmark.circle")
  304. }
  305. }
  306. ).buttonStyle(BorderlessButtonStyle())
  307. }.padding(.top)
  308. }.padding(.bottom)
  309. }.listRowBackground(Color.chart)
  310. SettingInputSection(
  311. decimalValue: $state.hours,
  312. booleanValue: $booleanPlaceholder,
  313. shouldDisplayHint: $shouldDisplayHint,
  314. selectedVerboseHint: Binding(
  315. get: { selectedVerboseHint },
  316. set: {
  317. selectedVerboseHint = $0.map { AnyView($0) }
  318. hintLabel = "X-Axis Interval Step"
  319. }
  320. ),
  321. units: state.units,
  322. type: .decimal("hours"),
  323. label: "X-Axis Interval Step",
  324. miniHint: "Determines how many hours are shown in the main graph",
  325. verboseHint: Text(
  326. "Default: 6 hours \n\nThis setting determines how many hours are shown in the primary view of the main graph."
  327. )
  328. )
  329. Section {
  330. VStack {
  331. Picker(
  332. selection: $state.totalInsulinDisplayType,
  333. label: Text("Total Insulin Display Type")
  334. ) {
  335. ForEach(TotalInsulinDisplayType.allCases) { selection in
  336. Text(selection.displayName).tag(selection)
  337. }
  338. }.padding(.top)
  339. HStack(alignment: .top) {
  340. Text(
  341. "Choose between Total Daily Dose (TDD) or Total Insulin in Scope (TINS) to be displayed above the main glucose graph"
  342. )
  343. .font(.footnote)
  344. .foregroundColor(.secondary)
  345. .lineLimit(nil)
  346. Spacer()
  347. Button(
  348. action: {
  349. hintLabel = "Total Insulin Display Type"
  350. selectedVerboseHint =
  351. AnyView(
  352. Text(
  353. "Choose between Total Daily Dose (TDD) or Total Insulin in Scope (TINS) to be displayed above the main glucose graph.\n\nTotal Daily Dose: Displays the last 24 hours of total insulin administered, both basal and bolus. \n\nTotal Insulin in Scope: Displays the total insulin administered since midnight, both basal and bolus."
  354. )
  355. )
  356. shouldDisplayHint.toggle()
  357. },
  358. label: {
  359. HStack {
  360. Image(systemName: "questionmark.circle")
  361. }
  362. }
  363. ).buttonStyle(BorderlessButtonStyle())
  364. }.padding(.top)
  365. }.padding(.bottom)
  366. }.listRowBackground(Color.chart)
  367. // TODO: this needs to be a picker: mmol/L or %
  368. SettingInputSection(
  369. decimalValue: $decimalPlaceholder,
  370. booleanValue: $state.overrideHbA1cUnit,
  371. shouldDisplayHint: $shouldDisplayHint,
  372. selectedVerboseHint: Binding(
  373. get: { selectedVerboseHint },
  374. set: {
  375. selectedVerboseHint = $0.map { AnyView($0) }
  376. hintLabel = "Override HbA1c Unit"
  377. }
  378. ),
  379. units: state.units,
  380. type: .boolean,
  381. label: "Override HbA1c Unit",
  382. miniHint: "Display HbA1c in mmol/mol or %",
  383. verboseHint: Text(
  384. "Choose which format you'd prefer the HbA1c value in the statistics view as a percentage (Example: 6.5%) or mmol/mol (Example: 48 mmol/mol)"
  385. ),
  386. headerText: "Trio Statistics"
  387. )
  388. // TODO: this needs to be a picker: choose bar chart or progress bar
  389. SettingInputSection(
  390. decimalValue: $decimalPlaceholder,
  391. booleanValue: $state.oneDimensionalGraph,
  392. shouldDisplayHint: $shouldDisplayHint,
  393. selectedVerboseHint: Binding(
  394. get: { selectedVerboseHint },
  395. set: {
  396. selectedVerboseHint = $0.map { AnyView($0) }
  397. hintLabel = "Standing / Laying TIR Chart"
  398. }
  399. ),
  400. units: state.units,
  401. type: .boolean,
  402. label: "Standing / Laying TIR Chart",
  403. miniHint: "Select a vertical chart or horizontal chart to display your Time in Range Statistics",
  404. verboseHint: Text(
  405. "Select a vertical / standing chart by turning this feature OFF \n\nSelect a horizontal / laying chart by turning this feature ON"
  406. )
  407. )
  408. SettingInputSection(
  409. decimalValue: $state.carbsRequiredThreshold,
  410. booleanValue: $state.showCarbsRequiredBadge,
  411. shouldDisplayHint: $shouldDisplayHint,
  412. selectedVerboseHint: Binding(
  413. get: { selectedVerboseHint },
  414. set: {
  415. selectedVerboseHint = $0.map { AnyView($0) }
  416. hintLabel = "Show Carbs Required Badge"
  417. }
  418. ),
  419. units: state.units,
  420. type: .conditionalDecimal("carbsRequiredThreshold"),
  421. label: "Show Carbs Required Badge",
  422. conditionalLabel: "Carbs Required Threshold",
  423. miniHint: "Show carbs required as a notification badge on the home screen",
  424. verboseHint: Text(
  425. "Turning this on will show the grams of carbs needed to prevent a low as a notification badge on the Trio home screen located above the main icon"
  426. ),
  427. headerText: "Carbs Required Badge"
  428. )
  429. }
  430. .sheet(isPresented: $shouldDisplayHint) {
  431. SettingInputHintView(
  432. hintDetent: $hintDetent,
  433. shouldDisplayHint: $shouldDisplayHint,
  434. hintLabel: hintLabel ?? "",
  435. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  436. sheetTitle: "Help"
  437. )
  438. }
  439. .scrollContentBackground(.hidden).background(color)
  440. .onAppear(perform: configureView)
  441. .navigationBarTitle("User Interface")
  442. .navigationBarTitleDisplayMode(.automatic)
  443. }
  444. }
  445. }