AddTempTargetRootView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import CoreData
  2. import SwiftUI
  3. import Swinject
  4. extension AddTempTarget {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. @StateObject var state = StateModel()
  8. @State private var isPromtPresented = false
  9. @State private var isEditing = false
  10. @State private var selectedPreset: TempTarget?
  11. @State private var isEditSheetPresented = false
  12. @FetchRequest(
  13. entity: TempTargetsSlider.entity(),
  14. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
  15. ) var isEnabledArray: FetchedResults<TempTargetsSlider>
  16. private var formatter: NumberFormatter {
  17. let formatter = NumberFormatter()
  18. formatter.numberStyle = .decimal
  19. formatter.maximumFractionDigits = 1
  20. return formatter
  21. }
  22. private var displayString: String {
  23. guard let preset = selectedPreset else { return "" }
  24. var low = preset.targetBottom
  25. var high = preset.targetBottom // change to only use targetBottom instead of targetTop
  26. if state.units == .mmolL {
  27. low = low?.asMmolL
  28. high = high?.asMmolL
  29. }
  30. let formattedLow = low.flatMap { formatter.string(from: $0 as NSNumber) } ?? ""
  31. let formattedDuration = formatter.string(from: preset.duration as NSNumber) ?? ""
  32. return "\(formattedLow) \(state.units.rawValue) for \(formattedDuration) min"
  33. }
  34. var body: some View {
  35. Form {
  36. if !state.presets.isEmpty {
  37. Section(header: Text("Presets")) {
  38. ForEach(state.presets) { preset in
  39. presetView(for: preset)
  40. .swipeActions {
  41. Button(role: .destructive) {
  42. state.removePreset(id: preset.id)
  43. } label: {
  44. Label("Delete", systemImage: "trash")
  45. }
  46. Button {
  47. selectedPreset = preset
  48. state.newPresetName = preset.displayName
  49. state.low = state.units == .mmolL ? preset.targetBottom?.asMmolL ?? 0 : preset
  50. .targetBottom ?? 0
  51. state.duration = preset.duration
  52. state.date = preset.date as? Date ?? Date()
  53. isEditSheetPresented = true
  54. } label: {
  55. Label("Edit", systemImage: "square.and.pencil")
  56. }
  57. .tint(.blue)
  58. }
  59. }
  60. }
  61. }
  62. HStack {
  63. Text("Experimental")
  64. Toggle(isOn: $state.viewPercantage) {}.controlSize(.mini)
  65. Image(systemName: "figure.highintensity.intervaltraining")
  66. Image(systemName: "fork.knife")
  67. }
  68. if state.viewPercantage {
  69. Section(
  70. header: Text("")
  71. ) {
  72. VStack {
  73. Slider(
  74. value: $state.percentage,
  75. in: 15 ...
  76. min(Double(state.maxValue * 100), 200),
  77. step: 1,
  78. onEditingChanged: { editing in
  79. isEditing = editing
  80. }
  81. )
  82. HStack {
  83. Text("\(state.percentage.formatted(.number)) % Insulin")
  84. .foregroundColor(isEditing ? .orange : .blue)
  85. .font(.largeTitle)
  86. }
  87. // Only display target slider when not 100 %
  88. if state.percentage != 100 {
  89. Divider()
  90. Slider(
  91. value: $state.hbt,
  92. in: 101 ... 295,
  93. step: 1
  94. ).accentColor(.green)
  95. HStack {
  96. Text(
  97. (
  98. state
  99. .units == .mmolL ?
  100. "\(state.computeTarget().asMmolL.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))) mmol/L" :
  101. "\(state.computeTarget().formatted(.number.grouping(.never).rounded().precision(.fractionLength(0)))) mg/dl"
  102. )
  103. + NSLocalizedString(" Target Glucose", comment: "")
  104. )
  105. .foregroundColor(.green)
  106. }
  107. }
  108. }
  109. }
  110. } else {
  111. Section(header: Text("Custom")) {
  112. HStack {
  113. Text("Target")
  114. Spacer()
  115. DecimalTextField("0", value: $state.low, formatter: formatter, cleanInput: true)
  116. Text(state.units.rawValue).foregroundColor(.secondary)
  117. }
  118. HStack {
  119. Text("Duration")
  120. Spacer()
  121. DecimalTextField("0", value: $state.duration, formatter: formatter, cleanInput: true)
  122. Text("minutes").foregroundColor(.secondary)
  123. }
  124. DatePicker("Date", selection: $state.date)
  125. Button { isPromtPresented = true }
  126. label: { Text("Save as preset") }
  127. }
  128. }
  129. if state.viewPercantage {
  130. Section {
  131. HStack {
  132. Text("Duration")
  133. Spacer()
  134. DecimalTextField("0", value: $state.duration, formatter: formatter, cleanInput: true)
  135. Text("minutes").foregroundColor(.secondary)
  136. }
  137. DatePicker("Date", selection: $state.date)
  138. Button { isPromtPresented = true }
  139. label: { Text("Save as preset") }
  140. .disabled(state.duration == 0)
  141. }
  142. }
  143. Section {
  144. Button { state.enact() }
  145. label: { Text("Enact") }
  146. Button { state.cancel() }
  147. label: { Text("Cancel Temp Target") }
  148. }
  149. }
  150. .popover(isPresented: $isPromtPresented) {
  151. Form {
  152. Section(header: Text("Enter preset name")) {
  153. TextField("Name", text: $state.newPresetName)
  154. }
  155. Section {
  156. Button {
  157. state.save()
  158. isPromtPresented = false
  159. }
  160. label: { Text("Save") }
  161. Button { isPromtPresented = false }
  162. label: { Text("Cancel") }
  163. }
  164. }
  165. }
  166. .sheet(isPresented: $isEditSheetPresented) {
  167. editPresetPopover()
  168. .padding()
  169. }
  170. .onAppear {
  171. configureView()
  172. state.hbt = isEnabledArray.first?.hbt ?? 160
  173. }
  174. .navigationTitle("Enact Temp Target")
  175. .navigationBarTitleDisplayMode(.automatic)
  176. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  177. }
  178. @ViewBuilder private func editPresetPopover() -> some View {
  179. Form {
  180. Section(header: Text("Edit Preset")) {
  181. TextField("Name", text: $state.newPresetName)
  182. Text(displayString)
  183. .foregroundColor(.secondary)
  184. .font(.caption)
  185. HStack {
  186. Text("New Target")
  187. Spacer()
  188. DecimalTextField("0", value: $state.low, formatter: formatter, cleanInput: true)
  189. Text(state.units.rawValue)
  190. }
  191. HStack {
  192. Text("New Duration")
  193. Spacer()
  194. DecimalTextField("0", value: $state.duration, formatter: formatter, cleanInput: true)
  195. Text("minutes")
  196. }
  197. }
  198. Section {
  199. Button("Save") {
  200. guard let selectedPreset = selectedPreset else { return }
  201. state.updatePreset(
  202. selectedPreset,
  203. low: state.units == .mmolL ? state.low.asMgdL : state.low
  204. )
  205. isEditSheetPresented = false
  206. }
  207. .disabled(state.newPresetName.isEmpty)
  208. Button("Cancel") {
  209. isEditSheetPresented = false
  210. }
  211. }
  212. }
  213. }
  214. private func presetView(for preset: TempTarget) -> some View {
  215. var low = preset.targetBottom
  216. var high = preset.targetBottom // change to only use targetBottom instead of targetTop
  217. if state.units == .mmolL {
  218. low = low?.asMmolL
  219. high = high?.asMmolL
  220. }
  221. return HStack {
  222. VStack {
  223. HStack {
  224. Text(preset.displayName)
  225. Spacer()
  226. Button {
  227. selectedPreset = preset
  228. state.newPresetName = preset.displayName
  229. state.low = state.units == .mmolL ? preset.targetBottom?
  230. .asMmolL ?? 0 : preset.targetBottom ?? 0
  231. state.duration = preset.duration
  232. state.date = preset.date as? Date ?? Date()
  233. isEditSheetPresented = true
  234. } label: {}
  235. }
  236. HStack(spacing: 2) {
  237. if let lowValue = low,
  238. let formattedLow = formatter.string(from: lowValue as NSNumber)
  239. {
  240. Text(formattedLow)
  241. .foregroundColor(.secondary)
  242. .font(.caption)
  243. }
  244. Text(state.units.rawValue)
  245. .foregroundColor(.secondary)
  246. .font(.caption)
  247. Text("for")
  248. .foregroundColor(.secondary)
  249. .font(.caption)
  250. Text("\(formatter.string(from: preset.duration as NSNumber)!)")
  251. .foregroundColor(.secondary)
  252. .font(.caption)
  253. Text("min")
  254. .foregroundColor(.secondary)
  255. .font(.caption)
  256. Spacer()
  257. }.padding(.bottom, 2)
  258. }
  259. .contentShape(Rectangle())
  260. .onTapGesture {
  261. state.enactPreset(id: preset.id)
  262. }
  263. }
  264. }
  265. }
  266. }
  267. extension AddTempTarget.StateModel {
  268. func updatePreset(_ preset: TempTarget, low: Decimal) {
  269. if let index = presets.firstIndex(where: { $0.id == preset.id }) {
  270. presets[index] = TempTarget(
  271. id: preset.id,
  272. name: newPresetName,
  273. createdAt: preset.createdAt,
  274. targetTop: low,
  275. targetBottom: low,
  276. duration: duration,
  277. enteredBy: preset.enteredBy,
  278. reason: newPresetName
  279. )
  280. storage.storePresets(presets)
  281. }
  282. }
  283. }