ISFEditorRootView.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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(from: state.rateValues[i] as NSNumber) ?? ""
  131. ) + " \(state.units.rawValue)/U"
  132. ).tag(i)
  133. }
  134. }
  135. }.listRowBackground(Color.chart)
  136. Section {
  137. Picker(selection: $state.items[index].timeIndex, label: Text("Time")) {
  138. ForEach(0 ..< state.timeValues.count, id: \.self) { i in
  139. Text(
  140. self.dateFormatter
  141. .string(from: Date(
  142. timeIntervalSince1970: state
  143. .timeValues[i]
  144. ))
  145. ).tag(i)
  146. }
  147. }
  148. }.listRowBackground(Color.chart)
  149. }
  150. .padding(.top)
  151. .scrollContentBackground(.hidden).background(color)
  152. .navigationTitle("Set Rate")
  153. .navigationBarTitleDisplayMode(.automatic)
  154. }
  155. private var list: some View {
  156. List {
  157. ForEach(state.items.indexed(), id: \.1.id) { index, item in
  158. NavigationLink(destination: pickers(for: index)) {
  159. HStack {
  160. Text("Rate").foregroundColor(.secondary)
  161. Text(
  162. "\(rateFormatter.string(from: state.rateValues[item.rateIndex] as NSNumber) ?? "0") \(state.units.rawValue)/U"
  163. )
  164. Spacer()
  165. Text("starts at").foregroundColor(.secondary)
  166. Text(
  167. "\(dateFormatter.string(from: Date(timeIntervalSince1970: state.timeValues[item.timeIndex])))"
  168. )
  169. }
  170. }
  171. .moveDisabled(true)
  172. }
  173. .onDelete(perform: onDelete)
  174. }
  175. }
  176. private var addButton: some View {
  177. guard state.canAdd else {
  178. return AnyView(EmptyView())
  179. }
  180. switch editMode {
  181. case .inactive:
  182. return AnyView(Button(action: onAdd) { Image(systemName: "plus") })
  183. default:
  184. return AnyView(EmptyView())
  185. }
  186. }
  187. func onAdd() {
  188. state.add()
  189. }
  190. private func onDelete(offsets: IndexSet) {
  191. state.items.remove(atOffsets: offsets)
  192. state.validate()
  193. }
  194. }
  195. }