AddTempTargetRootView.swift 9.7 KB

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