AddTempTargetRootView.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. VStack {
  40. Text("\(state.percentage.formatted(.number)) % Insulin")
  41. .foregroundColor(isEditing ? .orange : .blue)
  42. .font(.largeTitle)
  43. .padding(.vertical)
  44. Slider(
  45. value: $state.percentage,
  46. in: 15 ...
  47. min(Double(state.maxValue * 100), 200),
  48. step: 1,
  49. onEditingChanged: { editing in
  50. isEditing = editing
  51. }
  52. )
  53. // Only display target slider when not 100 %
  54. if state.percentage != 100 {
  55. Spacer()
  56. Divider()
  57. Text(
  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. + NSLocalizedString(" Target Glucose", comment: "")
  65. )
  66. .foregroundColor(.green)
  67. .padding(.vertical)
  68. Slider(
  69. value: $state.hbt,
  70. in: 101 ... 295,
  71. step: 1
  72. ).accentColor(.green)
  73. }
  74. }
  75. }
  76. } else {
  77. Section(header: Text("Custom")) {
  78. HStack {
  79. Text("Target")
  80. Spacer()
  81. DecimalTextField("0", value: $state.low, formatter: formatter, cleanInput: true)
  82. Text(state.units.rawValue).foregroundColor(.secondary)
  83. }
  84. HStack {
  85. Text("Duration")
  86. Spacer()
  87. DecimalTextField("0", value: $state.duration, formatter: formatter, cleanInput: true)
  88. Text("minutes").foregroundColor(.secondary)
  89. }
  90. DatePicker("Date", selection: $state.date)
  91. Button { isPromptPresented = true }
  92. label: { Text("Save as preset") }
  93. }
  94. }
  95. if state.viewPercantage {
  96. Section {
  97. HStack {
  98. Text("Duration")
  99. Spacer()
  100. DecimalTextField("0", value: $state.duration, formatter: formatter, cleanInput: true)
  101. Text("minutes").foregroundColor(.secondary)
  102. }
  103. DatePicker("Date", selection: $state.date)
  104. Button { isPromptPresented = true }
  105. label: { Text("Save as preset") }
  106. .disabled(state.duration == 0)
  107. }
  108. }
  109. Section {
  110. Button { state.enact() }
  111. label: { Text("Enact") }
  112. Button { state.cancel() }
  113. label: { Text("Cancel Temp Target") }
  114. }
  115. }
  116. .popover(isPresented: $isPromptPresented) {
  117. Form {
  118. Section(header: Text("Enter preset name")) {
  119. TextField("Name", text: $state.newPresetName)
  120. Button {
  121. state.save()
  122. isPromptPresented = false
  123. }
  124. label: { Text("Save") }
  125. Button { isPromptPresented = false }
  126. label: { Text("Cancel") }
  127. }
  128. }
  129. }
  130. .onAppear {
  131. configureView()
  132. state.hbt = isEnabledArray.first?.hbt ?? 160
  133. }
  134. .navigationTitle("Enact Temp Target")
  135. .navigationBarTitleDisplayMode(.inline)
  136. .navigationBarItems(trailing: Button("Close", action: state.hideModal))
  137. }
  138. private func presetView(for preset: TempTarget) -> some View {
  139. var low = preset.targetBottom
  140. var high = preset.targetTop
  141. if state.units == .mmolL {
  142. low = low?.asMmolL
  143. high = high?.asMmolL
  144. }
  145. return HStack {
  146. VStack {
  147. HStack {
  148. Text(preset.displayName)
  149. Spacer()
  150. }
  151. HStack(spacing: 2) {
  152. Text(
  153. "\(formatter.string(from: (low ?? 0) as NSNumber)!) - \(formatter.string(from: (high ?? 0) as NSNumber)!)"
  154. )
  155. .foregroundColor(.secondary)
  156. .font(.caption)
  157. Text(state.units.rawValue)
  158. .foregroundColor(.secondary)
  159. .font(.caption)
  160. Text("for")
  161. .foregroundColor(.secondary)
  162. .font(.caption)
  163. Text("\(formatter.string(from: preset.duration as NSNumber)!)")
  164. .foregroundColor(.secondary)
  165. .font(.caption)
  166. Text("min")
  167. .foregroundColor(.secondary)
  168. .font(.caption)
  169. Spacer()
  170. }.padding(.top, 2)
  171. }
  172. .contentShape(Rectangle())
  173. .onTapGesture {
  174. state.enactPreset(id: preset.id)
  175. }
  176. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  177. .contentShape(Rectangle())
  178. .padding(.vertical)
  179. .onTapGesture {
  180. removeAlert = Alert(
  181. title: Text("Are you sure?"),
  182. message: Text("Delete preset \"\(preset.displayName)\""),
  183. primaryButton: .destructive(Text("Delete"), action: { state.removePreset(id: preset.id) }),
  184. secondaryButton: .cancel()
  185. )
  186. isRemoveAlertPresented = true
  187. }
  188. .alert(isPresented: $isRemoveAlertPresented) {
  189. removeAlert!
  190. }
  191. }
  192. }
  193. }
  194. }