AddOverrideForm.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import Foundation
  2. import SwiftUI
  3. struct AddOverrideForm: View {
  4. @Environment(\.presentationMode) var presentationMode
  5. @StateObject var state: OverrideConfig.StateModel
  6. @State private var isEditing = false
  7. @State private var overrideTarget = false
  8. @Environment(\.colorScheme) var colorScheme
  9. @State private var showAlert = false
  10. @State private var alertString = ""
  11. @Environment(\.dismiss) var dismiss
  12. var color: LinearGradient {
  13. colorScheme == .dark ? LinearGradient(
  14. gradient: Gradient(colors: [
  15. Color.bgDarkBlue,
  16. Color.bgDarkerDarkBlue
  17. ]),
  18. startPoint: .top,
  19. endPoint: .bottom
  20. )
  21. :
  22. LinearGradient(
  23. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  24. startPoint: .top,
  25. endPoint: .bottom
  26. )
  27. }
  28. private var formatter: NumberFormatter {
  29. let formatter = NumberFormatter()
  30. formatter.numberStyle = .decimal
  31. formatter.maximumFractionDigits = 0
  32. return formatter
  33. }
  34. private var glucoseFormatter: NumberFormatter {
  35. let formatter = NumberFormatter()
  36. formatter.numberStyle = .decimal
  37. formatter.maximumFractionDigits = 0
  38. if state.units == .mmolL {
  39. formatter.maximumFractionDigits = 1
  40. }
  41. formatter.roundingMode = .halfUp
  42. return formatter
  43. }
  44. private var alertMessage: String {
  45. let target: String = state.units == .mgdL ? "70-270 mg/dl" : "4-15 mmol/l"
  46. return "Please enter a valid target between" + " \(target)."
  47. }
  48. var body: some View {
  49. NavigationView {
  50. Form {
  51. addOverride()
  52. }.scrollContentBackground(.hidden).background(color)
  53. .navigationTitle("Add Override")
  54. .navigationBarItems(trailing: Button("Cancel") {
  55. presentationMode.wrappedValue.dismiss()
  56. })
  57. }
  58. }
  59. @ViewBuilder private func addOverride() -> some View {
  60. Section {
  61. VStack {
  62. TextField("Name", text: $state.overrideName)
  63. }
  64. } header: {
  65. Text("Name")
  66. }.listRowBackground(Color.chart)
  67. Section {
  68. VStack {
  69. Spacer()
  70. Text("\(state.overrideSliderPercentage.formatted(.number)) %")
  71. .foregroundColor(
  72. state
  73. .overrideSliderPercentage >= 130 ? .red :
  74. (isEditing ? .orange : Color.tabBar)
  75. )
  76. .font(.largeTitle)
  77. Slider(
  78. value: $state.overrideSliderPercentage,
  79. in: 10 ... 200,
  80. step: 1,
  81. onEditingChanged: { editing in
  82. isEditing = editing
  83. }
  84. )
  85. Spacer()
  86. Toggle(isOn: $state.indefinite) {
  87. Text("Enable indefinitely")
  88. }
  89. }
  90. if !state.indefinite {
  91. HStack {
  92. Text("Duration")
  93. TextFieldWithToolBar(text: $state.overrideDuration, placeholder: "0", numberFormatter: formatter)
  94. Text("minutes").foregroundColor(.secondary)
  95. }
  96. }
  97. HStack {
  98. Toggle(isOn: $state.shouldOverrideTarget) {
  99. Text("Override Profile Target")
  100. }
  101. }
  102. if state.shouldOverrideTarget {
  103. HStack {
  104. Text("Target Glucose")
  105. TextFieldWithToolBar(text: $state.target, placeholder: "0", numberFormatter: glucoseFormatter)
  106. Text(state.units.rawValue).foregroundColor(.secondary)
  107. }
  108. }
  109. HStack {
  110. Toggle(isOn: $state.advancedSettings) {
  111. Text("More options")
  112. }
  113. }
  114. if state.advancedSettings {
  115. HStack {
  116. Toggle(isOn: $state.smbIsOff) {
  117. Text("Disable SMBs")
  118. }
  119. }
  120. HStack {
  121. Toggle(isOn: $state.smbIsAlwaysOff) {
  122. Text("Schedule when SMBs are Off")
  123. }.disabled(!state.smbIsOff)
  124. }
  125. if state.smbIsAlwaysOff {
  126. HStack {
  127. Text("First Hour SMBs are Off (24 hours)")
  128. TextFieldWithToolBar(text: $state.start, placeholder: "0", numberFormatter: formatter)
  129. Text("hour").foregroundColor(.secondary)
  130. }
  131. HStack {
  132. Text("Last Hour SMBs are Off (24 hours)")
  133. TextFieldWithToolBar(text: $state.end, placeholder: "0", numberFormatter: formatter)
  134. Text("hour").foregroundColor(.secondary)
  135. }
  136. }
  137. HStack {
  138. Toggle(isOn: $state.isfAndCr) {
  139. Text("Change ISF and CR")
  140. }
  141. }
  142. if !state.isfAndCr {
  143. HStack {
  144. Toggle(isOn: $state.isf) {
  145. Text("Change ISF")
  146. }
  147. }
  148. HStack {
  149. Toggle(isOn: $state.cr) {
  150. Text("Change CR")
  151. }
  152. }
  153. }
  154. HStack {
  155. Text("SMB Minutes")
  156. TextFieldWithToolBar(text: $state.smbMinutes, placeholder: "0", numberFormatter: formatter)
  157. Text("minutes").foregroundColor(.secondary)
  158. }
  159. HStack {
  160. Text("UAM SMB Minutes")
  161. TextFieldWithToolBar(text: $state.uamMinutes, placeholder: "0", numberFormatter: formatter)
  162. Text("minutes").foregroundColor(.secondary)
  163. }
  164. }
  165. startAndSaveProfiles
  166. }
  167. header: { Text("Add custom Override") }
  168. footer: {
  169. Text(
  170. "Your profile basal insulin will be adjusted with the override percentage and your profile ISF and CR will be inversly adjusted with the percentage."
  171. )
  172. }.listRowBackground(Color.chart)
  173. }
  174. private var startAndSaveProfiles: some View {
  175. HStack {
  176. Button("Start new Override") {
  177. if !state.isInputInvalid(target: state.target) {
  178. showAlert.toggle()
  179. alertString = "\(state.overrideSliderPercentage.formatted(.number)) %, " +
  180. (
  181. state.overrideDuration > 0 || !state
  182. .indefinite ?
  183. (
  184. state
  185. .overrideDuration
  186. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(0))) +
  187. " min."
  188. ) :
  189. NSLocalizedString(" infinite duration.", comment: "")
  190. ) +
  191. (
  192. (state.target == 0 || !state.shouldOverrideTarget) ? "" :
  193. (" Target: " + state.target.formatted() + " " + state.units.rawValue + ".")
  194. )
  195. +
  196. (
  197. state
  198. .smbIsOff ?
  199. NSLocalizedString(
  200. " SMBs are disabled either by schedule or during the entire duration.",
  201. comment: ""
  202. ) : ""
  203. )
  204. +
  205. "\n\n"
  206. +
  207. NSLocalizedString(
  208. "Starting this override will change your profiles and/or your Target Glucose used for looping during the entire selected duration. Tapping ”Start Override” will start your new Override or edit your current active Override.",
  209. comment: ""
  210. )
  211. }
  212. }
  213. .disabled(unChanged())
  214. .buttonStyle(BorderlessButtonStyle())
  215. .font(.callout)
  216. .controlSize(.mini)
  217. .alert(
  218. "Start Override",
  219. isPresented: $showAlert,
  220. actions: {
  221. Button("Cancel", role: .cancel) { state.isEnabled = false }
  222. Button("Start Override", role: .destructive) {
  223. Task {
  224. if state.indefinite { state.overrideDuration = 0 }
  225. state.isEnabled.toggle()
  226. await state.saveCustomOverride()
  227. await state.resetStateVariables()
  228. dismiss()
  229. }
  230. }
  231. },
  232. message: {
  233. Text(alertString)
  234. }
  235. )
  236. .alert(isPresented: $state.showInvalidTargetAlert) {
  237. Alert(
  238. title: Text("Invalid Input"),
  239. message: Text("\(state.alertMessage)"),
  240. dismissButton: .default(Text("OK")) { state.showInvalidTargetAlert = false }
  241. )
  242. }
  243. Button {
  244. Task {
  245. if !state.isInputInvalid(target: state.target) {
  246. await state.saveOverridePreset()
  247. dismiss()
  248. }
  249. }
  250. }
  251. label: { Text("Save as Preset") }
  252. .tint(.orange)
  253. .frame(maxWidth: .infinity, alignment: .trailing)
  254. .buttonStyle(BorderlessButtonStyle())
  255. .controlSize(.mini)
  256. .disabled(unChanged())
  257. }
  258. }
  259. private func unChanged() -> Bool {
  260. let isChanged = (
  261. state.overrideSliderPercentage == 100 && !state.shouldOverrideTarget && !state.smbIsOff && !state
  262. .advancedSettings
  263. ) ||
  264. (!state.indefinite && state.overrideDuration == 0) || (state.shouldOverrideTarget && state.target == 0) ||
  265. (
  266. state.overrideSliderPercentage == 100 && !state.shouldOverrideTarget && !state.smbIsOff && state.isf && state
  267. .cr && state
  268. .smbMinutes == state.defaultSmbMinutes && state.uamMinutes == state.defaultUamMinutes
  269. )
  270. return isChanged
  271. }
  272. }