ISFEditorRootView.swift 9.1 KB

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