ISFEditorRootView.swift 9.6 KB

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