AddTempTargetForm.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import Foundation
  2. import SwiftUI
  3. struct AddTempTargetForm: View {
  4. @StateObject var state: OverrideConfig.StateModel
  5. @Environment(\.presentationMode) var presentationMode
  6. @Environment(\.colorScheme) var colorScheme
  7. @Environment(\.dismiss) var dismiss
  8. @State private var showAlert = false
  9. @State private var showPresetAlert = false
  10. @State private var alertString = ""
  11. @State private var isUsingSlider = false
  12. @State private var advancedConfiguration = false
  13. @State private var didPressSave =
  14. false // only used for fixing the Disclaimer showing up after pressing save (after the state was resetted), maybe refactor this...
  15. @State private var shouldDisplayHint: Bool = false
  16. @State var hintDetent = PresentationDetent.large
  17. @State var selectedVerboseHint: String?
  18. @State var hintLabel: String?
  19. var color: LinearGradient {
  20. colorScheme == .dark ? LinearGradient(
  21. gradient: Gradient(colors: [
  22. Color.bgDarkBlue,
  23. Color.bgDarkerDarkBlue
  24. ]),
  25. startPoint: .top,
  26. endPoint: .bottom
  27. )
  28. :
  29. LinearGradient(
  30. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  31. startPoint: .top,
  32. endPoint: .bottom
  33. )
  34. }
  35. private var formatter: NumberFormatter {
  36. let formatter = NumberFormatter()
  37. formatter.numberStyle = .decimal
  38. formatter.maximumFractionDigits = 0
  39. return formatter
  40. }
  41. private var glucoseFormatter: NumberFormatter {
  42. let formatter = NumberFormatter()
  43. formatter.numberStyle = .decimal
  44. formatter.maximumFractionDigits = 0
  45. if state.units == .mmolL {
  46. formatter.maximumFractionDigits = 1
  47. }
  48. formatter.roundingMode = .halfUp
  49. return formatter
  50. }
  51. var body: some View {
  52. NavigationView {
  53. Form {
  54. addTempTarget()
  55. }.scrollContentBackground(.hidden).background(color)
  56. .navigationTitle("Add Temp Target")
  57. .navigationBarTitleDisplayMode(.inline)
  58. .navigationBarItems(leading: Button("Close") {
  59. presentationMode.wrappedValue.dismiss()
  60. })
  61. .alert(
  62. "Start Temp Target",
  63. isPresented: $showAlert,
  64. actions: {
  65. Button("Cancel", role: .cancel) { state.isTempTargetEnabled = false }
  66. Button("Start Temp Target", role: .destructive) {
  67. Task {
  68. didPressSave.toggle()
  69. await setupAlertString()
  70. state.isTempTargetEnabled.toggle()
  71. await state.saveCustomTempTarget()
  72. await state.resetTempTargetState()
  73. dismiss()
  74. }
  75. }
  76. },
  77. message: {
  78. Text(alertString)
  79. }
  80. )
  81. .sheet(isPresented: $shouldDisplayHint) {
  82. SettingInputHintView(
  83. hintDetent: $hintDetent,
  84. shouldDisplayHint: $shouldDisplayHint,
  85. hintLabel: hintLabel ?? "",
  86. hintText: selectedVerboseHint ?? "",
  87. sheetTitle: "Help"
  88. )
  89. }
  90. }
  91. }
  92. @ViewBuilder private func addTempTarget() -> some View {
  93. Section(
  94. header: Text("Configure Temp Target"),
  95. content: {
  96. HStack {
  97. Text("Name")
  98. Spacer()
  99. TextField("Enter Name (optional)", text: $state.tempTargetName)
  100. .multilineTextAlignment(.trailing)
  101. }
  102. HStack {
  103. Text("Target")
  104. Spacer()
  105. TextFieldWithToolBar(text: $state.tempTargetTarget, placeholder: "0", numberFormatter: glucoseFormatter)
  106. Text(state.units.rawValue).foregroundColor(.secondary)
  107. }
  108. HStack {
  109. Text("Duration")
  110. Spacer()
  111. TextFieldWithToolBar(text: $state.tempTargetDuration, placeholder: "0", numberFormatter: formatter)
  112. Text("minutes").foregroundColor(.secondary)
  113. }
  114. DatePicker("Date", selection: $state.date)
  115. }
  116. ).listRowBackground(Color.chart)
  117. // TODO: with iOS 17 we can change the body content wrapper from FORM to LIST and apply the .listSpacing modifier to make this all nice and small.
  118. Section {
  119. Button(action: {
  120. showAlert.toggle()
  121. }, label: {
  122. Text("Enact Temp Target")
  123. })
  124. .disabled(state.tempTargetDuration == 0)
  125. .frame(maxWidth: .infinity, alignment: .center)
  126. .tint(.white)
  127. }.listRowBackground(state.tempTargetDuration == 0 ? Color(.systemGray4) : Color(.systemBlue))
  128. Section {
  129. Button(action: {
  130. Task {
  131. didPressSave.toggle()
  132. await state.saveTempTargetPreset()
  133. dismiss()
  134. }
  135. }, label: {
  136. Text("Save as Preset")
  137. })
  138. .disabled(state.tempTargetDuration == 0)
  139. .frame(maxWidth: .infinity, alignment: .center)
  140. .tint(.white)
  141. }.listRowBackground(state.tempTargetDuration == 0 ? Color(.systemGray4) : Color(.orange))
  142. Section {
  143. VStack {
  144. Toggle("Enable Advanced Configuration", isOn: $advancedConfiguration).padding(.top)
  145. HStack(alignment: .top) {
  146. Text(
  147. "Add an explanation of the advanced configuration options here."
  148. )
  149. .font(.footnote)
  150. .foregroundColor(.secondary)
  151. .lineLimit(nil)
  152. Spacer()
  153. Button(
  154. action: {
  155. hintLabel = "Advanced Temp Target Configuration"
  156. selectedVerboseHint = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
  157. shouldDisplayHint.toggle()
  158. },
  159. label: {
  160. HStack {
  161. Image(systemName: "questionmark.circle")
  162. }
  163. }
  164. ).buttonStyle(BorderlessButtonStyle())
  165. }.padding(.top)
  166. }.padding(.bottom)
  167. }.listRowBackground(Color.chart)
  168. if advancedConfiguration && state.tempTargetTarget != 0 {
  169. if sliderEnabled {
  170. Section {
  171. VStack {
  172. Text("\(state.percentage.formatted(.number)) % Insulin")
  173. .foregroundColor(isUsingSlider ? .orange : Color.tabBar)
  174. .font(.largeTitle)
  175. Slider(value: $state.percentage, in: state.computeSliderLow() ... state.computeSliderHigh(), step: 5) {}
  176. minimumValueLabel: {
  177. Text("\(state.computeSliderLow(), specifier: "%.0f")%")
  178. }
  179. maximumValueLabel: {
  180. Text("\(state.computeSliderHigh(), specifier: "%.0f")%")
  181. }
  182. onEditingChanged: { editing in
  183. isUsingSlider = editing
  184. state.halfBasalTarget = Decimal(state.computeHalfBasalTarget())
  185. }
  186. .disabled(!sliderEnabled)
  187. Divider()
  188. Text(
  189. state
  190. .units == .mgdL ?
  191. "Half Basal Exercise Target at: \(state.computeHalfBasalTarget().formatted(.number.precision(.fractionLength(0)))) mg/dl" :
  192. "Half Basal Exercise Target at: \(state.computeHalfBasalTarget().asMmolL.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))) mmol/L"
  193. )
  194. .foregroundColor(.secondary)
  195. .font(.caption).italic()
  196. }
  197. }.listRowBackground(Color.chart)
  198. } else {
  199. Section {
  200. VStack(alignment: .leading) {
  201. Text(
  202. "You have not enabled the proper Preferences to change sensitivity with chosen TempTarget. Verify Autosens Max > 1 & lowTT lowers Sens is on for lowTT's. For high TTs check highTT raises Sens is on (or Exercise Mode)!"
  203. ).bold()
  204. }
  205. }.listRowBackground(Color.tabBar)
  206. }
  207. } else if advancedConfiguration && state.tempTargetTarget == 0 && !didPressSave {
  208. Section {
  209. VStack(alignment: .leading) {
  210. Text(
  211. "You need to input a Target for your Temp Target at first to use the advanced configuration!"
  212. ).bold()
  213. }
  214. }.listRowBackground(Color.tabBar)
  215. }
  216. }
  217. var sliderEnabled: Bool {
  218. state.computeSliderHigh() > state.computeSliderLow()
  219. }
  220. private func setupAlertString() async {
  221. alertString =
  222. (
  223. state.tempTargetDuration > 0 ?
  224. (
  225. state
  226. .tempTargetDuration
  227. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(0))) +
  228. " min."
  229. ) :
  230. NSLocalizedString(" infinite duration.", comment: "")
  231. ) +
  232. (
  233. state.tempTargetTarget == 0 ? "" :
  234. (" Target: " + state.tempTargetTarget.formatted() + " " + state.units.rawValue + ".")
  235. )
  236. +
  237. "\n\n"
  238. +
  239. NSLocalizedString(
  240. "Starting this Temp Target will change your profiles and/or your Target Glucose used for looping during the entire selected duration. Tapping ”Start Temp Target” will start your new Temp Target or edit your current active Temp Target.",
  241. comment: ""
  242. )
  243. }
  244. }