AddTempTargetRootView.swift 9.6 KB

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