ISFEditorRootView.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. Text(
  78. rateFormatter
  79. .string(from: (
  80. (
  81. !state.settingsManager.preferences
  82. .useNewFormula ? newISF as NSDecimalNumber : dynamicISF
  83. ) ?? 0
  84. ) as NSNumber) ?? "0"
  85. )
  86. Text(state.units.rawValue + "/U").foregroundColor(.secondary)
  87. }
  88. }.listRowBackground(Color.chart)
  89. }
  90. Section(header: Text("Schedule")) {
  91. list
  92. }.listRowBackground(Color.chart)
  93. Section {
  94. HStack {
  95. if state.shouldDisplaySaving {
  96. ProgressView().padding(.trailing, 10)
  97. }
  98. Button {
  99. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  100. impactHeavy.impactOccurred()
  101. state.save()
  102. // deactivate saving display after 1.25 seconds
  103. DispatchQueue.main.asyncAfter(deadline: .now() + 1.25) {
  104. state.shouldDisplaySaving = false
  105. }
  106. } label: {
  107. Text(state.shouldDisplaySaving ? "Saving..." : "Save")
  108. }
  109. .disabled(shouldDisableButton)
  110. .frame(maxWidth: .infinity, alignment: .center)
  111. .tint(.white)
  112. }
  113. }.listRowBackground(shouldDisableButton ? Color(.systemGray4) : Color(.systemBlue))
  114. }
  115. .scrollContentBackground(.hidden).background(color)
  116. .onAppear(perform: configureView)
  117. .navigationTitle("Insulin Sensitivities")
  118. .navigationBarTitleDisplayMode(.automatic)
  119. .toolbar(content: {
  120. ToolbarItem(placement: .topBarTrailing) {
  121. EditButton()
  122. }
  123. ToolbarItem(placement: .topBarTrailing) {
  124. addButton
  125. }
  126. })
  127. .environment(\.editMode, $editMode)
  128. .onAppear {
  129. state.validate()
  130. }
  131. }
  132. private func pickers(for index: Int) -> some View {
  133. Form {
  134. Section {
  135. Picker(selection: $state.items[index].rateIndex, label: Text("Rate")) {
  136. ForEach(0 ..< state.rateValues.count, id: \.self) { i in
  137. Text(
  138. (
  139. self.rateFormatter
  140. .string(
  141. from: state.units == .mgdL ? state.rateValues[i] as NSNumber : state.rateValues[i]
  142. .asMmolL as NSNumber
  143. ) ?? ""
  144. ) + " \(state.units.rawValue)/U"
  145. ).tag(i)
  146. }
  147. }
  148. }.listRowBackground(Color.chart)
  149. Section {
  150. Picker(selection: $state.items[index].timeIndex, label: Text("Time")) {
  151. ForEach(0 ..< state.timeValues.count, id: \.self) { i in
  152. Text(
  153. self.dateFormatter
  154. .string(from: Date(
  155. timeIntervalSince1970: state
  156. .timeValues[i]
  157. ))
  158. ).tag(i)
  159. }
  160. }
  161. }.listRowBackground(Color.chart)
  162. }
  163. .padding(.top)
  164. .scrollContentBackground(.hidden).background(color)
  165. .navigationTitle("Set Rate")
  166. .navigationBarTitleDisplayMode(.automatic)
  167. }
  168. private var list: some View {
  169. List {
  170. ForEach(state.items.indexed(), id: \.1.id) { index, item in
  171. let displayValue = rateFormatter
  172. .string(
  173. from: state.units == .mgdL ? state.rateValues[item.rateIndex] as NSNumber : state
  174. .rateValues[item.rateIndex].asMmolL as NSNumber
  175. )
  176. NavigationLink(destination: pickers(for: index)) {
  177. HStack {
  178. Text("Rate").foregroundColor(.secondary)
  179. Text(
  180. "\(displayValue ?? "0") \(state.units.rawValue)/U"
  181. )
  182. Spacer()
  183. Text("starts at").foregroundColor(.secondary)
  184. Text(
  185. "\(dateFormatter.string(from: Date(timeIntervalSince1970: state.timeValues[item.timeIndex])))"
  186. )
  187. }
  188. }
  189. .moveDisabled(true)
  190. }
  191. .onDelete(perform: onDelete)
  192. }
  193. }
  194. private var addButton: some View {
  195. guard state.canAdd else {
  196. return AnyView(EmptyView())
  197. }
  198. switch editMode {
  199. case .inactive:
  200. return AnyView(Button(action: onAdd) { Image(systemName: "plus") })
  201. default:
  202. return AnyView(EmptyView())
  203. }
  204. }
  205. func onAdd() {
  206. state.add()
  207. }
  208. private func onDelete(offsets: IndexSet) {
  209. state.items.remove(atOffsets: offsets)
  210. state.validate()
  211. }
  212. }
  213. }