ISFEditorRootView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import SwiftUI
  2. import Swinject
  3. extension ISFEditor {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. @State private var editMode = EditMode.inactive
  8. @Environment(\.colorScheme) var colorScheme
  9. var color: LinearGradient {
  10. colorScheme == .dark ? LinearGradient(
  11. gradient: Gradient(colors: [
  12. Color.bgDarkBlue,
  13. Color.bgDarkerDarkBlue
  14. ]),
  15. startPoint: .top,
  16. endPoint: .bottom
  17. )
  18. :
  19. LinearGradient(
  20. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  21. startPoint: .top,
  22. endPoint: .bottom
  23. )
  24. }
  25. private var dateFormatter: DateFormatter {
  26. let formatter = DateFormatter()
  27. formatter.timeZone = TimeZone(secondsFromGMT: 0)
  28. formatter.timeStyle = .short
  29. return formatter
  30. }
  31. private var rateFormatter: NumberFormatter {
  32. let formatter = NumberFormatter()
  33. formatter.numberStyle = .decimal
  34. formatter.maximumFractionDigits = 2
  35. return formatter
  36. }
  37. var body: some View {
  38. Form {
  39. let shouldDisableButton = state.items.isEmpty || !state.hasChanges
  40. if let autotune = state.autotune, !state.settingsManager.settings.onlyAutotuneBasals {
  41. Section(header: Text("Autotune")) {
  42. HStack {
  43. Text("Calculated Sensitivity")
  44. Spacer()
  45. if state.units == .mmolL {
  46. Text(rateFormatter.string(from: autotune.sensitivity.asMmolL as NSNumber) ?? "0")
  47. } else {
  48. Text(rateFormatter.string(from: autotune.sensitivity as NSNumber) ?? "0")
  49. }
  50. Text(state.units.rawValue + "/U").foregroundColor(.secondary)
  51. }
  52. }.listRowBackground(Color.chart)
  53. }
  54. if let newISF = state.autosensISF {
  55. Section(
  56. header: !state.settingsManager.preferences
  57. .useNewFormula ? Text("Autosens") : Text("Dynamic Sensitivity")
  58. ) {
  59. let dynamicRatio = state.determinationsFromPersistence.first?.sensitivityRatio
  60. let dynamicISF = state.determinationsFromPersistence.first?.insulinSensitivity
  61. HStack {
  62. Text("Sensitivity Ratio")
  63. Spacer()
  64. Text(
  65. rateFormatter
  66. .string(from: (
  67. (
  68. !state.settingsManager.preferences.useNewFormula ? state
  69. .autosensRatio as NSDecimalNumber : dynamicRatio
  70. ) ?? 1
  71. ) as NSNumber) ?? "1"
  72. )
  73. }
  74. HStack {
  75. Text("Calculated Sensitivity")
  76. Spacer()
  77. if state.units == .mgdL {
  78. Text(
  79. rateFormatter
  80. .string(from: (
  81. (
  82. !state.settingsManager.preferences
  83. .useNewFormula ? newISF as NSDecimalNumber : dynamicISF
  84. ) ?? 0
  85. ) as NSNumber) ?? "0"
  86. )
  87. } else {
  88. Text(
  89. rateFormatter
  90. .string(from: (
  91. (
  92. !state.settingsManager.preferences
  93. .useNewFormula ? newISF.asMmolL as NSDecimalNumber as Decimal : dynamicISF?
  94. .decimalValue.asMmolL
  95. ) ?? 0
  96. ) as NSNumber) ?? "0"
  97. )
  98. }
  99. Text(state.units.rawValue + "/U").foregroundColor(.secondary)
  100. }
  101. }.listRowBackground(Color.chart)
  102. }
  103. Section(header: Text("Schedule")) {
  104. list
  105. }.listRowBackground(Color.chart)
  106. Section {
  107. HStack {
  108. if state.shouldDisplaySaving {
  109. ProgressView().padding(.trailing, 10)
  110. }
  111. Button {
  112. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  113. impactHeavy.impactOccurred()
  114. state.save()
  115. // deactivate saving display after 1.25 seconds
  116. DispatchQueue.main.asyncAfter(deadline: .now() + 1.25) {
  117. state.shouldDisplaySaving = false
  118. }
  119. } label: {
  120. Text(state.shouldDisplaySaving ? "Saving..." : "Save")
  121. }
  122. .disabled(shouldDisableButton)
  123. .frame(maxWidth: .infinity, alignment: .center)
  124. .tint(.white)
  125. }
  126. }.listRowBackground(shouldDisableButton ? Color(.systemGray4) : Color(.systemBlue))
  127. }
  128. .scrollContentBackground(.hidden).background(color)
  129. .onAppear(perform: configureView)
  130. .navigationTitle("Insulin Sensitivities")
  131. .navigationBarTitleDisplayMode(.automatic)
  132. .toolbar(content: {
  133. ToolbarItem(placement: .topBarTrailing) {
  134. EditButton()
  135. }
  136. ToolbarItem(placement: .topBarTrailing) {
  137. addButton
  138. }
  139. })
  140. .environment(\.editMode, $editMode)
  141. .onAppear {
  142. state.validate()
  143. }
  144. }
  145. private func pickers(for index: Int) -> some View {
  146. Form {
  147. Section {
  148. Picker(selection: $state.items[index].rateIndex, label: Text("Rate")) {
  149. ForEach(0 ..< state.rateValues.count, id: \.self) { i in
  150. Text(
  151. (
  152. self.rateFormatter
  153. .string(
  154. from: state.units == .mgdL ? state.rateValues[i] as NSNumber : state.rateValues[i]
  155. .asMmolL as NSNumber
  156. ) ?? ""
  157. ) + " \(state.units.rawValue)/U"
  158. ).tag(i)
  159. }
  160. }
  161. }.listRowBackground(Color.chart)
  162. Section {
  163. Picker(selection: $state.items[index].timeIndex, label: Text("Time")) {
  164. ForEach(0 ..< state.timeValues.count, id: \.self) { i in
  165. Text(
  166. self.dateFormatter
  167. .string(from: Date(
  168. timeIntervalSince1970: state
  169. .timeValues[i]
  170. ))
  171. ).tag(i)
  172. }
  173. }
  174. }.listRowBackground(Color.chart)
  175. }
  176. .padding(.top)
  177. .scrollContentBackground(.hidden).background(color)
  178. .navigationTitle("Set Rate")
  179. .navigationBarTitleDisplayMode(.automatic)
  180. }
  181. private var list: some View {
  182. List {
  183. ForEach(state.items.indexed(), id: \.1.id) { index, item in
  184. let displayValue = rateFormatter
  185. .string(
  186. from: state.units == .mgdL ? state.rateValues[item.rateIndex] as NSNumber : state
  187. .rateValues[item.rateIndex].asMmolL as NSNumber
  188. )
  189. NavigationLink(destination: pickers(for: index)) {
  190. HStack {
  191. Text("Rate").foregroundColor(.secondary)
  192. Text(
  193. "\(displayValue ?? "0") \(state.units.rawValue)/U"
  194. )
  195. Spacer()
  196. Text("starts at").foregroundColor(.secondary)
  197. Text(
  198. "\(dateFormatter.string(from: Date(timeIntervalSince1970: state.timeValues[item.timeIndex])))"
  199. )
  200. }
  201. }
  202. .moveDisabled(true)
  203. }
  204. .onDelete(perform: onDelete)
  205. }
  206. }
  207. private var addButton: some View {
  208. guard state.canAdd else {
  209. return AnyView(EmptyView())
  210. }
  211. switch editMode {
  212. case .inactive:
  213. return AnyView(Button(action: onAdd) { Image(systemName: "plus") })
  214. default:
  215. return AnyView(EmptyView())
  216. }
  217. }
  218. func onAdd() {
  219. state.add()
  220. }
  221. private func onDelete(offsets: IndexSet) {
  222. state.items.remove(atOffsets: offsets)
  223. state.validate()
  224. }
  225. }
  226. }