AddTempTargetRootView.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 isPromptPresented = 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. private var formatter: NumberFormatter {
  17. let formatter = NumberFormatter()
  18. formatter.numberStyle = .decimal
  19. formatter.maximumFractionDigits = 1
  20. return formatter
  21. }
  22. var body: some View {
  23. Form {
  24. if !state.presets.isEmpty {
  25. Section(header: Text("Presets")) {
  26. ForEach(state.presets) { preset in
  27. presetView(for: preset)
  28. }
  29. }
  30. }
  31. HStack {
  32. Text("Experimental")
  33. Toggle(isOn: $state.viewPercantage) {}.controlSize(.mini)
  34. Image(systemName: "figure.highintensity.intervaltraining")
  35. Image(systemName: "fork.knife")
  36. }
  37. if state.viewPercantage {
  38. Section(
  39. header: Text("")
  40. ) {
  41. VStack {
  42. Slider(
  43. value: $state.percentage,
  44. in: 15 ...
  45. min(Double(state.maxValue * 100), 200),
  46. step: 1,
  47. onEditingChanged: { editing in
  48. isEditing = editing
  49. }
  50. )
  51. HStack {
  52. Text("\(state.percentage.formatted(.number)) % Insulin")
  53. .foregroundColor(isEditing ? .orange : .blue)
  54. .font(.largeTitle)
  55. }
  56. // Only display target slider when not 100 %
  57. if state.percentage != 100 {
  58. Divider()
  59. Slider(
  60. value: $state.hbt,
  61. in: 101 ... 295,
  62. step: 1
  63. ).accentColor(.green)
  64. HStack {
  65. Text(
  66. (
  67. state
  68. .units == .mmolL ?
  69. "\(state.computeTarget().asMmolL.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))) mmol/L" :
  70. "\(state.computeTarget().formatted(.number.grouping(.never).rounded().precision(.fractionLength(0)))) mg/dl"
  71. )
  72. + NSLocalizedString(" Target Glucose", comment: "")
  73. )
  74. .foregroundColor(.green)
  75. }
  76. }
  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 { isPromptPresented = 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 { isPromptPresented = true }
  108. label: { Text("Save as preset") }
  109. .disabled(state.duration == 0)
  110. }
  111. }
  112. Section {
  113. Button { state.enact() }
  114. label: { Text("Enact") }
  115. Button { state.cancel() }
  116. label: { Text("Cancel Temp Target") }
  117. }
  118. }
  119. .popover(isPresented: $isPromptPresented) {
  120. Form {
  121. Section(header: Text("Enter preset name")) {
  122. TextField("Name", text: $state.newPresetName)
  123. Button {
  124. state.save()
  125. isPromptPresented = false
  126. }
  127. label: { Text("Save") }
  128. Button { isPromptPresented = false }
  129. label: { Text("Cancel") }
  130. }
  131. }
  132. }
  133. .onAppear {
  134. configureView()
  135. state.hbt = isEnabledArray.first?.hbt ?? 160
  136. }
  137. .navigationTitle("Enact Temp Target")
  138. .navigationBarTitleDisplayMode(.inline)
  139. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  140. }
  141. private func presetView(for preset: TempTarget) -> some View {
  142. var low = preset.targetBottom
  143. var high = preset.targetTop
  144. if state.units == .mmolL {
  145. low = low?.asMmolL
  146. high = high?.asMmolL
  147. }
  148. return HStack {
  149. VStack {
  150. HStack {
  151. Text(preset.displayName)
  152. Spacer()
  153. }
  154. HStack(spacing: 2) {
  155. Text(
  156. "\(formatter.string(from: (low ?? 0) as NSNumber)!) - \(formatter.string(from: (high ?? 0) as NSNumber)!)"
  157. )
  158. .foregroundColor(.secondary)
  159. .font(.caption)
  160. Text(state.units.rawValue)
  161. .foregroundColor(.secondary)
  162. .font(.caption)
  163. Text("for")
  164. .foregroundColor(.secondary)
  165. .font(.caption)
  166. Text("\(formatter.string(from: preset.duration as NSNumber)!)")
  167. .foregroundColor(.secondary)
  168. .font(.caption)
  169. Text("min")
  170. .foregroundColor(.secondary)
  171. .font(.caption)
  172. Spacer()
  173. }.padding(.top, 2)
  174. }
  175. .contentShape(Rectangle())
  176. .onTapGesture {
  177. state.enactPreset(id: preset.id)
  178. }
  179. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  180. .contentShape(Rectangle())
  181. .padding(.vertical)
  182. .onTapGesture {
  183. removeAlert = Alert(
  184. title: Text("Are you sure?"),
  185. message: Text("Delete preset \"\(preset.displayName)\""),
  186. primaryButton: .destructive(Text("Delete"), action: { state.removePreset(id: preset.id) }),
  187. secondaryButton: .cancel()
  188. )
  189. isRemoveAlertPresented = true
  190. }
  191. .alert(isPresented: $isRemoveAlertPresented) {
  192. removeAlert!
  193. }
  194. }
  195. }
  196. }
  197. }