ISFEditorRootView.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. if let autotune = state.autotune, !state.settingsManager.settings.onlyAutotuneBasals {
  40. Section(header: Text("Autotune")) {
  41. HStack {
  42. Text("Calculated Sensitivity")
  43. Spacer()
  44. if state.units == .mmolL {
  45. Text(rateFormatter.string(from: autotune.sensitivity.asMmolL as NSNumber) ?? "0")
  46. } else {
  47. Text(rateFormatter.string(from: autotune.sensitivity as NSNumber) ?? "0")
  48. }
  49. Text(state.units.rawValue + "/U").foregroundColor(.secondary)
  50. }
  51. }.listRowBackground(Color.chart)
  52. }
  53. if let newISF = state.autosensISF {
  54. Section(
  55. header: !state.settingsManager.preferences
  56. .useNewFormula ? Text("Autosens") : Text("Dynamic Sensitivity")
  57. ) {
  58. let dynamicRatio = state.determinationsFromPersistence.first?.sensitivityRatio
  59. let dynamicISF = state.determinationsFromPersistence.first?.insulinSensitivity
  60. HStack {
  61. Text("Sensitivity Ratio")
  62. Spacer()
  63. Text(
  64. rateFormatter
  65. .string(from: (
  66. (
  67. !state.settingsManager.preferences.useNewFormula ? state
  68. .autosensRatio as NSDecimalNumber : dynamicRatio
  69. ) ?? 1
  70. ) as NSNumber) ?? "1"
  71. )
  72. }
  73. HStack {
  74. Text("Calculated Sensitivity")
  75. Spacer()
  76. Text(
  77. rateFormatter
  78. .string(from: (
  79. (
  80. !state.settingsManager.preferences
  81. .useNewFormula ? newISF as NSDecimalNumber : dynamicISF
  82. ) ?? 0
  83. ) as NSNumber) ?? "0"
  84. )
  85. Text(state.units.rawValue + "/U").foregroundColor(.secondary)
  86. }
  87. }.listRowBackground(Color.chart)
  88. }
  89. Section(header: Text("Schedule")) {
  90. list
  91. }.listRowBackground(Color.chart)
  92. Section {
  93. Button {
  94. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  95. impactHeavy.impactOccurred()
  96. state.save()
  97. } label: {
  98. Text("Save")
  99. }
  100. .disabled(state.items.isEmpty)
  101. .frame(maxWidth: .infinity, alignment: .center)
  102. .tint(.white)
  103. }.listRowBackground(state.items.isEmpty ? Color(.systemGray4) : Color(.systemBlue))
  104. }
  105. .scrollContentBackground(.hidden).background(color)
  106. .onAppear(perform: configureView)
  107. .navigationTitle("Insulin Sensitivities")
  108. .navigationBarTitleDisplayMode(.automatic)
  109. .toolbar(content: {
  110. ToolbarItem(placement: .topBarTrailing) {
  111. EditButton()
  112. }
  113. ToolbarItem(placement: .topBarTrailing) {
  114. addButton
  115. }
  116. })
  117. .environment(\.editMode, $editMode)
  118. .onAppear {
  119. state.validate()
  120. }
  121. }
  122. private func pickers(for index: Int) -> some View {
  123. Form {
  124. Section {
  125. Picker(selection: $state.items[index].rateIndex, label: Text("Rate")) {
  126. ForEach(0 ..< state.rateValues.count, id: \.self) { i in
  127. Text(
  128. (
  129. self.rateFormatter
  130. .string(
  131. from: state.units == .mgdL ? state.rateValues[i] as NSNumber : state.rateValues[i]
  132. .asMmolL as NSNumber
  133. ) ?? ""
  134. ) + " \(state.units.rawValue)/U"
  135. ).tag(i)
  136. }
  137. }
  138. }.listRowBackground(Color.chart)
  139. Section {
  140. Picker(selection: $state.items[index].timeIndex, label: Text("Time")) {
  141. ForEach(0 ..< state.timeValues.count, id: \.self) { i in
  142. Text(
  143. self.dateFormatter
  144. .string(from: Date(
  145. timeIntervalSince1970: state
  146. .timeValues[i]
  147. ))
  148. ).tag(i)
  149. }
  150. }
  151. }.listRowBackground(Color.chart)
  152. }
  153. .padding(.top)
  154. .scrollContentBackground(.hidden).background(color)
  155. .navigationTitle("Set Rate")
  156. .navigationBarTitleDisplayMode(.automatic)
  157. }
  158. private var list: some View {
  159. List {
  160. ForEach(state.items.indexed(), id: \.1.id) { index, item in
  161. let displayValue = rateFormatter
  162. .string(
  163. from: state.units == .mgdL ? state.rateValues[item.rateIndex] as NSNumber : state
  164. .rateValues[item.rateIndex].asMmolL as NSNumber
  165. )
  166. NavigationLink(destination: pickers(for: index)) {
  167. HStack {
  168. Text("Rate").foregroundColor(.secondary)
  169. Text(
  170. "\(displayValue ?? "0") \(state.units.rawValue)/U"
  171. )
  172. Spacer()
  173. Text("starts at").foregroundColor(.secondary)
  174. Text(
  175. "\(dateFormatter.string(from: Date(timeIntervalSince1970: state.timeValues[item.timeIndex])))"
  176. )
  177. }
  178. }
  179. .moveDisabled(true)
  180. }
  181. .onDelete(perform: onDelete)
  182. }
  183. }
  184. private var addButton: some View {
  185. guard state.canAdd else {
  186. return AnyView(EmptyView())
  187. }
  188. switch editMode {
  189. case .inactive:
  190. return AnyView(Button(action: onAdd) { Image(systemName: "plus") })
  191. default:
  192. return AnyView(EmptyView())
  193. }
  194. }
  195. func onAdd() {
  196. state.add()
  197. }
  198. private func onDelete(offsets: IndexSet) {
  199. state.items.remove(atOffsets: offsets)
  200. state.validate()
  201. }
  202. }
  203. }