AddOverrideForm.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. import Foundation
  2. import SwiftUI
  3. struct AddOverrideForm: View {
  4. @Environment(\.presentationMode) var presentationMode
  5. @Bindable var state: OverrideConfig.StateModel
  6. @State private var selectedIsfCrOption: IsfAndOrCrOptions = .isfAndCr
  7. @State private var selectedDisableSmbOption: DisableSmbOptions = .dontDisable
  8. @State private var percentageStep: Int = 5
  9. @State private var displayPickerPercentage: Bool = false
  10. @State private var displayPickerDuration: Bool = false
  11. @State private var targetStep: Decimal = 5
  12. @State private var displayPickerTarget: Bool = false
  13. @State private var displayPickerDisableSmbSchedule: Bool = false
  14. @State private var displayPickerSmbMinutes: Bool = false
  15. @State private var durationHours = 0
  16. @State private var durationMinutes = 0
  17. @State private var overrideTarget = false
  18. @State private var didPressSave = false
  19. @Environment(\.colorScheme) var colorScheme
  20. @Environment(\.dismiss) var dismiss
  21. var color: LinearGradient {
  22. colorScheme == .dark ? LinearGradient(
  23. gradient: Gradient(colors: [
  24. Color.bgDarkBlue,
  25. Color.bgDarkerDarkBlue
  26. ]),
  27. startPoint: .top,
  28. endPoint: .bottom
  29. ) :
  30. LinearGradient(
  31. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  32. startPoint: .top,
  33. endPoint: .bottom
  34. )
  35. }
  36. var body: some View {
  37. NavigationView {
  38. List {
  39. addOverride()
  40. saveButton
  41. }
  42. .listSectionSpacing(10)
  43. .padding(.top, 30)
  44. .ignoresSafeArea(edges: .top)
  45. .scrollContentBackground(.hidden).background(color)
  46. .navigationTitle("Add Override")
  47. .navigationBarTitleDisplayMode(.inline)
  48. .toolbar {
  49. ToolbarItem(placement: .topBarLeading) {
  50. Button(action: {
  51. presentationMode.wrappedValue.dismiss()
  52. }, label: {
  53. Text("Cancel")
  54. })
  55. }
  56. ToolbarItem(placement: .topBarTrailing) {
  57. Button(
  58. action: {
  59. state.isHelpSheetPresented.toggle()
  60. },
  61. label: {
  62. Image(systemName: "questionmark.circle")
  63. }
  64. )
  65. }
  66. }
  67. .onAppear { targetStep = state.units == .mgdL ? 5 : 9 }
  68. .sheet(isPresented: $state.isHelpSheetPresented) {
  69. NavigationStack {
  70. List {
  71. Text("Lorem Ipsum Dolor Sit Amet")
  72. }
  73. .padding(.trailing, 10)
  74. .navigationBarTitle("Help", displayMode: .inline)
  75. Button { state.isHelpSheetPresented.toggle() }
  76. label: { Text("Got it!").frame(maxWidth: .infinity, alignment: .center) }
  77. .buttonStyle(.bordered)
  78. .padding(.top)
  79. }
  80. .padding()
  81. .presentationDetents(
  82. [.fraction(0.9), .large],
  83. selection: $state.helpSheetDetent
  84. )
  85. }
  86. }
  87. }
  88. @ViewBuilder private func addOverride() -> some View {
  89. Group {
  90. Section {
  91. HStack {
  92. Text("Name")
  93. Spacer()
  94. TextField("(Optional)", text: $state.overrideName).multilineTextAlignment(.trailing)
  95. }
  96. }
  97. .listRowBackground(Color.chart)
  98. Section {
  99. Toggle(isOn: $state.indefinite) {
  100. Text("Enable Indefinitely")
  101. }
  102. if !state.indefinite {
  103. HStack {
  104. Text("Duration")
  105. Spacer()
  106. Text(formatHrMin(Int(state.overrideDuration)))
  107. .foregroundColor(!displayPickerDuration ? .primary : .accentColor)
  108. }
  109. .onTapGesture {
  110. displayPickerDuration = toggleScrollWheel(displayPickerDuration)
  111. }
  112. if displayPickerDuration {
  113. HStack {
  114. Picker("Hours", selection: $durationHours) {
  115. ForEach(0 ..< 24) { hour in
  116. Text("\(hour) hr").tag(hour)
  117. }
  118. }
  119. .pickerStyle(WheelPickerStyle())
  120. .frame(maxWidth: .infinity)
  121. .onChange(of: durationHours) {
  122. state.overrideDuration = convertToMinutes(durationHours, durationMinutes)
  123. }
  124. Picker("Minutes", selection: $durationMinutes) {
  125. ForEach(Array(stride(from: 0, through: 55, by: 5)), id: \.self) { minute in
  126. Text("\(minute) min").tag(minute)
  127. }
  128. }
  129. .pickerStyle(WheelPickerStyle())
  130. .frame(maxWidth: .infinity)
  131. .onChange(of: durationMinutes) {
  132. state.overrideDuration = convertToMinutes(durationHours, durationMinutes)
  133. }
  134. }
  135. .listRowSeparator(.hidden, edges: .top)
  136. }
  137. }
  138. }
  139. .listRowBackground(Color.chart)
  140. Section(footer: percentageDescription(state.overridePercentage)) {
  141. // Percentage Picker
  142. HStack {
  143. Text("Change Basal Rate by")
  144. Spacer()
  145. Text("\(state.overridePercentage.formatted(.number)) %")
  146. .foregroundColor(!displayPickerPercentage ? .primary : .accentColor)
  147. }
  148. .onTapGesture {
  149. displayPickerPercentage = toggleScrollWheel(displayPickerPercentage)
  150. }
  151. if displayPickerPercentage {
  152. HStack {
  153. // Radio buttons and text on the left side
  154. VStack(alignment: .leading) {
  155. // Radio buttons for step iteration
  156. ForEach([1, 5], id: \.self) { step in
  157. RadioButton(isSelected: percentageStep == step, label: "\(step) %") {
  158. percentageStep = step
  159. state.overridePercentage = OverrideConfig.StateModel.roundOverridePercentageToStep(
  160. state.overridePercentage,
  161. step
  162. )
  163. }
  164. .padding(.top, 10)
  165. }
  166. }
  167. .frame(maxWidth: .infinity)
  168. Spacer()
  169. // Picker on the right side
  170. Picker(
  171. selection: Binding(
  172. get: { Int(truncating: state.overridePercentage as NSNumber) },
  173. set: { state.overridePercentage = Double($0) }
  174. ), label: Text("")
  175. ) {
  176. ForEach(Array(stride(from: 40, through: 150, by: percentageStep)), id: \.self) { percent in
  177. Text("\(percent) %").tag(percent)
  178. }
  179. }
  180. .pickerStyle(WheelPickerStyle())
  181. .frame(maxWidth: .infinity)
  182. }
  183. .frame(maxWidth: .infinity)
  184. .listRowSeparator(.hidden, edges: .top)
  185. }
  186. // Picker for ISF/CR settings
  187. Picker("Also Inversely Change", selection: $selectedIsfCrOption) {
  188. ForEach(IsfAndOrCrOptions.allCases, id: \.self) { option in
  189. Text(option.rawValue).tag(option)
  190. }
  191. }
  192. .pickerStyle(MenuPickerStyle())
  193. .onChange(of: selectedIsfCrOption) { _, newValue in
  194. switch newValue {
  195. case .isfAndCr:
  196. state.isfAndCr = true
  197. state.isf = true
  198. state.cr = true
  199. case .isf:
  200. state.isfAndCr = false
  201. state.isf = true
  202. state.cr = false
  203. case .cr:
  204. state.isfAndCr = false
  205. state.isf = false
  206. state.cr = true
  207. case .nothing:
  208. state.isfAndCr = false
  209. state.isf = false
  210. state.cr = false
  211. }
  212. }
  213. }
  214. .listRowBackground(Color.chart)
  215. Section {
  216. Toggle(isOn: $state.shouldOverrideTarget) {
  217. Text("Override Target")
  218. }
  219. if state.shouldOverrideTarget {
  220. let settingsProvider = PickerSettingsProvider.shared
  221. let glucoseSetting = PickerSetting(value: 0, step: targetStep, min: 72, max: 270, type: .glucose)
  222. TargetPicker(
  223. label: "Target Glucose",
  224. selection: Binding(
  225. get: { state.target },
  226. set: { state.target = $0 }
  227. ),
  228. options: settingsProvider.generatePickerValues(
  229. from: glucoseSetting,
  230. units: state.units,
  231. roundMinToStep: true
  232. ),
  233. units: state.units,
  234. targetStep: $targetStep,
  235. displayPickerTarget: $displayPickerTarget,
  236. toggleScrollWheel: toggleScrollWheel
  237. )
  238. .onAppear {
  239. if state.target == 0 {
  240. state.target = 100
  241. }
  242. }
  243. }
  244. }
  245. .listRowBackground(Color.chart)
  246. Section {
  247. // Picker for ISF/CR settings
  248. Picker("Disable SMBs", selection: $selectedDisableSmbOption) {
  249. ForEach(DisableSmbOptions.allCases, id: \.self) { option in
  250. Text(option.rawValue).tag(option)
  251. }
  252. }
  253. .pickerStyle(MenuPickerStyle())
  254. .onChange(of: selectedDisableSmbOption) { _, newValue in
  255. switch newValue {
  256. case .dontDisable:
  257. state.smbIsOff = false
  258. state.smbIsScheduledOff = false
  259. case .disable:
  260. state.smbIsOff = true
  261. state.smbIsScheduledOff = false
  262. case .disableOnSchedule:
  263. state.smbIsOff = false
  264. state.smbIsScheduledOff = true
  265. }
  266. }
  267. if state.smbIsScheduledOff {
  268. // First Hour SMBs Are Disabled
  269. HStack {
  270. Text("From")
  271. Spacer()
  272. Text(
  273. is24HourFormat() ? format24Hour(Int(truncating: state.start as NSNumber)) + ":00" :
  274. convertTo12HourFormat(Int(truncating: state.start as NSNumber))
  275. )
  276. .foregroundColor(!displayPickerDisableSmbSchedule ? .primary : .accentColor)
  277. Spacer()
  278. Divider().frame(width: 1, height: 20)
  279. Spacer()
  280. Text("To")
  281. Spacer()
  282. Text(
  283. is24HourFormat() ? format24Hour(Int(truncating: state.end as NSNumber)) + ":00" :
  284. convertTo12HourFormat(Int(truncating: state.end as NSNumber))
  285. )
  286. .foregroundColor(!displayPickerDisableSmbSchedule ? .primary : .accentColor)
  287. Spacer()
  288. }
  289. .onTapGesture {
  290. displayPickerDisableSmbSchedule = toggleScrollWheel(displayPickerDisableSmbSchedule)
  291. }
  292. if displayPickerDisableSmbSchedule {
  293. HStack {
  294. // From Picker
  295. Picker(selection: Binding(
  296. get: { Int(truncating: state.start as NSNumber) },
  297. set: { state.start = Decimal($0) }
  298. ), label: Text("")) {
  299. ForEach(0 ..< 24, id: \.self) { hour in
  300. Text(is24HourFormat() ? format24Hour(hour) + ":00" : convertTo12HourFormat(hour))
  301. .tag(hour)
  302. }
  303. }
  304. .pickerStyle(WheelPickerStyle())
  305. .frame(maxWidth: .infinity)
  306. // To Picker
  307. Picker(selection: Binding(
  308. get: { Int(truncating: state.end as NSNumber) },
  309. set: { state.end = Decimal($0) }
  310. ), label: Text("")) {
  311. ForEach(0 ..< 24, id: \.self) { hour in
  312. Text(is24HourFormat() ? format24Hour(hour) + ":00" : convertTo12HourFormat(hour))
  313. .tag(hour)
  314. }
  315. }
  316. .pickerStyle(WheelPickerStyle())
  317. .frame(maxWidth: .infinity)
  318. }
  319. .listRowSeparator(.hidden, edges: .top)
  320. }
  321. }
  322. }
  323. .listRowBackground(Color.chart)
  324. if !state.smbIsOff {
  325. Section {
  326. Toggle(isOn: $state.advancedSettings) {
  327. Text("Override Max SMB Minutes")
  328. }
  329. if state.advancedSettings {
  330. // SMB Minutes Picker
  331. HStack {
  332. Text("SMB")
  333. Spacer()
  334. Text("\(state.smbMinutes.formatted(.number)) min")
  335. .foregroundColor(!displayPickerSmbMinutes ? .primary : .accentColor)
  336. Spacer()
  337. Divider().frame(width: 1, height: 20)
  338. Spacer()
  339. Text("UAM")
  340. Spacer()
  341. Text("\(state.uamMinutes.formatted(.number)) min")
  342. .foregroundColor(!displayPickerSmbMinutes ? .primary : .accentColor)
  343. }
  344. .onTapGesture {
  345. displayPickerSmbMinutes = toggleScrollWheel(displayPickerSmbMinutes)
  346. }
  347. if displayPickerSmbMinutes {
  348. HStack {
  349. Picker(selection: Binding(
  350. get: { Int(truncating: state.smbMinutes as NSNumber) },
  351. set: { state.smbMinutes = Decimal($0) }
  352. ), label: Text("")) {
  353. ForEach(Array(stride(from: 0, through: 180, by: 5)), id: \.self) { minute in
  354. Text("\(minute) min").tag(minute)
  355. }
  356. }
  357. .pickerStyle(WheelPickerStyle())
  358. .frame(maxWidth: .infinity)
  359. Picker(selection: Binding(
  360. get: { Int(truncating: state.uamMinutes as NSNumber) },
  361. set: { state.uamMinutes = Decimal($0) }
  362. ), label: Text("")) {
  363. ForEach(Array(stride(from: 0, through: 180, by: 5)), id: \.self) { minute in
  364. Text("\(minute) min").tag(minute)
  365. }
  366. }
  367. .pickerStyle(WheelPickerStyle())
  368. .frame(maxWidth: .infinity)
  369. }
  370. .listRowSeparator(.hidden, edges: .top)
  371. }
  372. }
  373. }
  374. .listRowBackground(Color.chart)
  375. }
  376. }
  377. }
  378. private var saveButton: some View {
  379. let (isInvalid, errorMessage) = isOverrideInvalid()
  380. return Group {
  381. Section(
  382. header:
  383. HStack {
  384. Spacer()
  385. Text(errorMessage ?? "").textCase(nil)
  386. .foregroundColor(colorScheme == .dark ? .orange : .accentColor)
  387. Spacer()
  388. },
  389. content: {
  390. Button(action: {
  391. Task {
  392. if state.indefinite { state.overrideDuration = 0 }
  393. state.isEnabled.toggle()
  394. await state.saveCustomOverride()
  395. await state.resetStateVariables()
  396. dismiss()
  397. }
  398. }, label: {
  399. Text("Enact Override")
  400. })
  401. .disabled(isInvalid)
  402. .frame(maxWidth: .infinity, alignment: .center)
  403. .tint(.white)
  404. }
  405. ).listRowBackground(isInvalid ? Color(.systemGray4) : Color(.systemBlue))
  406. Section {
  407. Button(action: {
  408. Task {
  409. await state.saveOverridePreset()
  410. dismiss()
  411. }
  412. }, label: {
  413. Text("Save as Preset")
  414. })
  415. .disabled(isInvalid)
  416. .frame(maxWidth: .infinity, alignment: .center)
  417. .tint(.white)
  418. }
  419. .listRowBackground(
  420. isInvalid ? Color(.systemGray4) : Color.secondary
  421. )
  422. }
  423. }
  424. private func toggleScrollWheel(_ toggle: Bool) -> Bool {
  425. displayPickerDuration = false
  426. displayPickerPercentage = false
  427. displayPickerTarget = false
  428. displayPickerDisableSmbSchedule = false
  429. displayPickerSmbMinutes = false
  430. return !toggle
  431. }
  432. private func isOverrideInvalid() -> (Bool, String?) {
  433. let noDurationSpecified = !state.indefinite && state.overrideDuration == 0
  434. let targetZeroWithOverride = state.shouldOverrideTarget && state.target == 0
  435. let allSettingsDefault = state.overridePercentage == 100 && !state.shouldOverrideTarget &&
  436. !state.advancedSettings && !state.smbIsOff && !state.smbIsScheduledOff
  437. if noDurationSpecified {
  438. return (true, "Enable indefinitely or set a duration.")
  439. }
  440. if targetZeroWithOverride {
  441. return (true, "Target glucose is out of range (\(state.units == .mgdL ? "72-270" : "4-14")).")
  442. }
  443. if allSettingsDefault {
  444. return (true, "All settings are at default values.")
  445. }
  446. return (false, nil)
  447. }
  448. }