AddTempTargetRootView.swift 9.6 KB

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