AddTempTargetRootView.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 isRemoveAlertPresented = false
  10. @State private var removeAlert: Alert?
  11. @State private var isEditing = false
  12. @FetchRequest(
  13. entity: TempTargetsSlider.entity(),
  14. sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)]
  15. ) var isEnabledArray: FetchedResults<TempTargetsSlider>
  16. @Environment(\.managedObjectContext) var moc
  17. private var formatter: NumberFormatter {
  18. let formatter = NumberFormatter()
  19. formatter.numberStyle = .decimal
  20. formatter.maximumFractionDigits = 1
  21. return formatter
  22. }
  23. var body: some View {
  24. Form {
  25. if !state.presets.isEmpty {
  26. Section(header: Text("Presets")) {
  27. ForEach(state.presets) { preset in
  28. presetView(for: preset)
  29. }
  30. }
  31. }
  32. HStack {
  33. Text("Advanced")
  34. Toggle(isOn: $state.viewPercantage) {}.controlSize(.mini)
  35. Image(systemName: "figure.highintensity.intervaltraining")
  36. Image(systemName: "fork.knife")
  37. }
  38. if state.viewPercantage {
  39. Section(
  40. header: Text("Percent Insulin")
  41. ) {
  42. VStack {
  43. Slider(
  44. value: $state.percentage,
  45. in: 15 ...
  46. min(Double(state.maxValue * 100), 200),
  47. step: 1,
  48. onEditingChanged: { editing in
  49. isEditing = editing
  50. }
  51. )
  52. Text("\(state.percentage.formatted(.number)) %")
  53. .foregroundColor(isEditing ? .orange : .blue)
  54. .font(.largeTitle)
  55. Divider()
  56. Text(
  57. NSLocalizedString("Target glucose", comment: "") +
  58. (
  59. state
  60. .units == .mmolL ?
  61. ": \(state.computeTarget().asMmolL.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))) mmol/L" :
  62. ": \(state.computeTarget().formatted(.number.grouping(.never).rounded().precision(.fractionLength(0)))) mg/dl"
  63. )
  64. )
  65. Slider(
  66. value: $state.hbt,
  67. in: 101 ... 295,
  68. step: 1
  69. )
  70. Text(
  71. state
  72. .units == .mgdL ? "Half normal Basal at: \(state.hbt.formatted(.number)) mg/dl" :
  73. "Half normal Basal at: \(state.hbt.asMmolL.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))) mmol/L"
  74. )
  75. .foregroundColor(.secondary)
  76. .font(.caption).italic()
  77. }
  78. }
  79. } else {
  80. Section(header: Text("Custom")) {
  81. HStack {
  82. Text("Target")
  83. Spacer()
  84. DecimalTextField("0", value: $state.low, formatter: formatter, cleanInput: true)
  85. Text(state.units.rawValue).foregroundColor(.secondary)
  86. }
  87. HStack {
  88. Text("Duration")
  89. Spacer()
  90. DecimalTextField("0", value: $state.duration, formatter: formatter, cleanInput: true)
  91. Text("minutes").foregroundColor(.secondary)
  92. }
  93. DatePicker("Date", selection: $state.date)
  94. Button { isPromtPresented = true }
  95. label: { Text("Save as preset") }
  96. }
  97. }
  98. if state.viewPercantage {
  99. Section {
  100. HStack {
  101. Text("Duration")
  102. Spacer()
  103. DecimalTextField("0", value: $state.duration, formatter: formatter, cleanInput: true)
  104. Text("minutes").foregroundColor(.secondary)
  105. }
  106. DatePicker("Date", selection: $state.date)
  107. Button { isPromtPresented = true }
  108. label: { Text("Save as preset") }
  109. }
  110. }
  111. Section {
  112. Button { state.enact() }
  113. label: { Text("Enact") }
  114. Button { state.cancel() }
  115. label: { Text("Cancel Temp Target") }
  116. }
  117. }
  118. .popover(isPresented: $isPromtPresented) {
  119. Form {
  120. Section(header: Text("Enter preset name")) {
  121. TextField("Name", text: $state.newPresetName)
  122. Button {
  123. state.save()
  124. isPromtPresented = false
  125. }
  126. label: { Text("Save") }
  127. Button { isPromtPresented = false }
  128. label: { Text("Cancel") }
  129. }
  130. }
  131. }
  132. .onAppear {
  133. configureView()
  134. state.hbt = isEnabledArray.first?.hbt ?? 160
  135. }
  136. .navigationTitle("Enact Temp Target")
  137. .navigationBarTitleDisplayMode(.automatic)
  138. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  139. .onDisappear {
  140. if state.viewPercantage, state.saveSettings {
  141. let isEnabledMoc = TempTargetsSlider(context: moc)
  142. isEnabledMoc.enabled = true
  143. isEnabledMoc.date = Date()
  144. isEnabledMoc.hbt = state.hbt
  145. isEnabledMoc.duration = state.duration as NSDecimalNumber
  146. isEnabledMoc.isPreset = false
  147. try? moc.save()
  148. } else {
  149. let isEnabledMoc = TempTargetsSlider(context: moc)
  150. isEnabledMoc.enabled = false
  151. isEnabledMoc.date = Date()
  152. // isEnabledMoc.hbt = isEnabledArray.first?.hbt ?? 160
  153. try? moc.save()
  154. }
  155. }
  156. }
  157. private func presetView(for preset: TempTarget) -> some View {
  158. var low = preset.targetBottom
  159. var high = preset.targetTop
  160. if state.units == .mmolL {
  161. low = low?.asMmolL
  162. high = high?.asMmolL
  163. }
  164. return HStack {
  165. VStack {
  166. HStack {
  167. Text(preset.displayName)
  168. Spacer()
  169. }
  170. HStack(spacing: 2) {
  171. Text(
  172. "\(formatter.string(from: (low ?? 0) as NSNumber)!) - \(formatter.string(from: (high ?? 0) as NSNumber)!)"
  173. )
  174. .foregroundColor(.secondary)
  175. .font(.caption)
  176. Text(state.units.rawValue)
  177. .foregroundColor(.secondary)
  178. .font(.caption)
  179. Text("for")
  180. .foregroundColor(.secondary)
  181. .font(.caption)
  182. Text("\(formatter.string(from: preset.duration as NSNumber)!)")
  183. .foregroundColor(.secondary)
  184. .font(.caption)
  185. Text("min")
  186. .foregroundColor(.secondary)
  187. .font(.caption)
  188. Spacer()
  189. }.padding(.top, 2)
  190. }
  191. .contentShape(Rectangle())
  192. .onTapGesture {
  193. state.enactPreset(id: preset.id)
  194. }
  195. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  196. .contentShape(Rectangle())
  197. .padding(.vertical)
  198. .onTapGesture {
  199. removeAlert = Alert(
  200. title: Text("Are you sure?"),
  201. message: Text("Delete preset \"\(preset.displayName)\""),
  202. primaryButton: .destructive(Text("Delete"), action: { state.removePreset(id: preset.id) }),
  203. secondaryButton: .cancel()
  204. )
  205. isRemoveAlertPresented = true
  206. }
  207. .alert(isPresented: $isRemoveAlertPresented) {
  208. removeAlert!
  209. }
  210. }
  211. }
  212. }
  213. }