AddTempTargetForm.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. .onChange(of: state.tempTargetTarget) { _ in
  107. // Recalculate the percentage when tempTargetTarget changes
  108. state.percentage = Double(state.computePercentage() * 100)
  109. }
  110. Text(state.units.rawValue).foregroundColor(.secondary)
  111. }
  112. HStack {
  113. Text("Duration")
  114. Spacer()
  115. TextFieldWithToolBar(text: $state.tempTargetDuration, placeholder: "0", numberFormatter: formatter)
  116. Text("minutes").foregroundColor(.secondary)
  117. }
  118. DatePicker("Date", selection: $state.date)
  119. }
  120. ).listRowBackground(Color.chart)
  121. // 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.
  122. Section {
  123. Button(action: {
  124. showAlert.toggle()
  125. }, label: {
  126. Text("Enact Temp Target")
  127. })
  128. .disabled(state.tempTargetDuration == 0)
  129. .frame(maxWidth: .infinity, alignment: .center)
  130. .tint(.white)
  131. }.listRowBackground(state.tempTargetDuration == 0 ? Color(.systemGray4) : Color(.systemBlue))
  132. Section {
  133. Button(action: {
  134. Task {
  135. didPressSave.toggle()
  136. await state.saveTempTargetPreset()
  137. dismiss()
  138. }
  139. }, label: {
  140. Text("Save as Preset")
  141. })
  142. .disabled(state.tempTargetDuration == 0)
  143. .frame(maxWidth: .infinity, alignment: .center)
  144. .tint(.white)
  145. }.listRowBackground(state.tempTargetDuration == 0 ? Color(.systemGray4) : Color(.orange))
  146. Section {
  147. VStack {
  148. Toggle("Enable Advanced Configuration", isOn: $advancedConfiguration).padding(.top)
  149. HStack(alignment: .top) {
  150. Text(
  151. "Add an explanation of the advanced configuration options here."
  152. )
  153. .font(.footnote)
  154. .foregroundColor(.secondary)
  155. .lineLimit(nil)
  156. Spacer()
  157. Button(
  158. action: {
  159. hintLabel = "Advanced Temp Target Configuration"
  160. selectedVerboseHint = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr."
  161. shouldDisplayHint.toggle()
  162. },
  163. label: {
  164. HStack {
  165. Image(systemName: "questionmark.circle")
  166. }
  167. }
  168. ).buttonStyle(BorderlessButtonStyle())
  169. }.padding(.top)
  170. }.padding(.bottom)
  171. }.listRowBackground(Color.chart)
  172. if advancedConfiguration && state.tempTargetTarget != 0 {
  173. if sliderEnabled {
  174. Section {
  175. VStack {
  176. // Display the percentage in large text
  177. Text("\(Int(state.percentage)) % Insulin")
  178. .foregroundColor(isUsingSlider ? .orange : Color.tabBar)
  179. .font(.largeTitle)
  180. // Bind the slider to the percentage
  181. Slider(
  182. value: $state.percentage,
  183. in: state.computeSliderLow() ... state.computeSliderHigh(),
  184. step: 5
  185. ) {} minimumValueLabel: {
  186. Text("\(state.computeSliderLow(), specifier: "%.0f")%")
  187. } maximumValueLabel: {
  188. Text("\(state.computeSliderHigh(), specifier: "%.0f")%")
  189. } onEditingChanged: { editing in
  190. isUsingSlider = editing
  191. state.halfBasalTarget = Decimal(state.computeHalfBasalTarget())
  192. }
  193. .disabled(!sliderEnabled)
  194. Divider()
  195. Text(
  196. state
  197. .units == .mgdL ?
  198. "Half Basal Exercise Target at: \(state.computeHalfBasalTarget().formatted(.number.precision(.fractionLength(0)))) mg/dl" :
  199. "Half Basal Exercise Target at: \(state.computeHalfBasalTarget().asMmolL.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))) mmol/L"
  200. )
  201. .foregroundColor(.secondary)
  202. .font(.caption).italic()
  203. }
  204. }.listRowBackground(Color.chart)
  205. } else {
  206. Section {
  207. VStack(alignment: .leading) {
  208. Text(
  209. "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)!"
  210. ).bold()
  211. }
  212. }.listRowBackground(Color.tabBar)
  213. }
  214. } else if advancedConfiguration && state.tempTargetTarget == 0 && !didPressSave {
  215. Section {
  216. VStack(alignment: .leading) {
  217. Text(
  218. "You need to input a Target for your Temp Target at first to use the advanced configuration!"
  219. ).bold()
  220. }
  221. }.listRowBackground(Color.tabBar)
  222. }
  223. }
  224. var sliderEnabled: Bool {
  225. state.computeSliderHigh() > state.computeSliderLow()
  226. }
  227. private func setupAlertString() async {
  228. alertString =
  229. (
  230. state.tempTargetDuration > 0 ?
  231. (
  232. state
  233. .tempTargetDuration
  234. .formatted(.number.grouping(.never).rounded().precision(.fractionLength(0))) +
  235. " min."
  236. ) :
  237. NSLocalizedString(" infinite duration.", comment: "")
  238. ) +
  239. (
  240. state.tempTargetTarget == 0 ? "" :
  241. (" Target: " + state.tempTargetTarget.formatted() + " " + state.units.rawValue + ".")
  242. )
  243. +
  244. "\n\n"
  245. +
  246. NSLocalizedString(
  247. "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.",
  248. comment: ""
  249. )
  250. }
  251. }