AutotuneConfigRootView.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import SwiftUI
  2. import Swinject
  3. extension AutotuneConfig {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. @State private var shouldDisplayHint: Bool = false
  8. @State var hintDetent = PresentationDetent.large
  9. @State var selectedVerboseHint: AnyView?
  10. @State var hintLabel: String?
  11. @State private var decimalPlaceholder: Decimal = 0.0
  12. @State private var booleanPlaceholder: Bool = false
  13. @State var replaceAlert = false
  14. @Environment(\.colorScheme) var colorScheme
  15. var color: LinearGradient {
  16. colorScheme == .dark ? LinearGradient(
  17. gradient: Gradient(colors: [
  18. Color.bgDarkBlue,
  19. Color.bgDarkerDarkBlue
  20. ]),
  21. startPoint: .top,
  22. endPoint: .bottom
  23. )
  24. :
  25. LinearGradient(
  26. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  27. startPoint: .top,
  28. endPoint: .bottom
  29. )
  30. }
  31. private var isfFormatter: NumberFormatter {
  32. let formatter = NumberFormatter()
  33. formatter.numberStyle = .decimal
  34. formatter.maximumFractionDigits = 2
  35. return formatter
  36. }
  37. private var rateFormatter: NumberFormatter {
  38. let formatter = NumberFormatter()
  39. formatter.numberStyle = .decimal
  40. formatter.maximumFractionDigits = 2
  41. return formatter
  42. }
  43. private var dateFormatter: DateFormatter {
  44. let formatter = DateFormatter()
  45. formatter.dateStyle = .medium
  46. formatter.timeStyle = .short
  47. return formatter
  48. }
  49. var body: some View {
  50. Form {
  51. SettingInputSection(
  52. decimalValue: $decimalPlaceholder,
  53. booleanValue: $state.useAutotune,
  54. shouldDisplayHint: $shouldDisplayHint,
  55. selectedVerboseHint: Binding(
  56. get: { selectedVerboseHint },
  57. set: {
  58. selectedVerboseHint = $0.map { AnyView($0) }
  59. hintLabel = "Use Autotune"
  60. }
  61. ),
  62. units: state.units,
  63. type: .boolean,
  64. label: "Use Autotune",
  65. miniHint: "It is not advised to use Autotune with Trio.",
  66. verboseHint:
  67. VStack(alignment: .leading, spacing: 10) {
  68. Text("It is not advised to use Autotune with Trio").bold()
  69. Text("Autotune is not designed to work with Trio. It is best to keep Autotune off and do not use it.")
  70. },
  71. headerText: "Data-driven Adjustments"
  72. )
  73. if state.useAutotune {
  74. SettingInputSection(
  75. decimalValue: $decimalPlaceholder,
  76. booleanValue: $state.onlyAutotuneBasals,
  77. shouldDisplayHint: $shouldDisplayHint,
  78. selectedVerboseHint: Binding(
  79. get: { selectedVerboseHint },
  80. set: {
  81. selectedVerboseHint = $0.map { AnyView($0) }
  82. hintLabel = "Only Autotune Basal Insulin"
  83. }
  84. ),
  85. units: state.units,
  86. type: .boolean,
  87. label: "Only Autotune Basal Insulin",
  88. miniHint: "Restricts Autotune adjustments to only basal settings.",
  89. verboseHint: Text("Restricts Autotune adjustments to only basal settings.")
  90. )
  91. }
  92. Section(
  93. header: HStack {
  94. Text("Last run")
  95. Spacer()
  96. Text(dateFormatter.string(from: state.publishedDate))
  97. },
  98. content: {
  99. Button {
  100. state.run()
  101. } label: {
  102. Text("Run now")
  103. }
  104. .frame(maxWidth: .infinity, alignment: .center)
  105. .listRowBackground(Color(.systemBlue))
  106. .tint(.white)
  107. }
  108. )
  109. if let autotune = state.autotune {
  110. if !state.onlyAutotuneBasals {
  111. Section {
  112. HStack {
  113. Text("Carb ratio")
  114. Spacer()
  115. Text(isfFormatter.string(from: autotune.carbRatio as NSNumber) ?? "0")
  116. Text("g/U").foregroundColor(.secondary)
  117. }
  118. HStack {
  119. Text("Sensitivity")
  120. Spacer()
  121. if state.units == .mmolL {
  122. Text(isfFormatter.string(from: autotune.sensitivity.asMmolL as NSNumber) ?? "0")
  123. } else {
  124. Text(isfFormatter.string(from: autotune.sensitivity as NSNumber) ?? "0")
  125. }
  126. Text(state.units.rawValue + "/U").foregroundColor(.secondary)
  127. }
  128. }
  129. .listRowBackground(Color.chart)
  130. }
  131. Section(header: Text("Basal profile")) {
  132. ForEach(0 ..< autotune.basalProfile.count, id: \.self) { index in
  133. HStack {
  134. Text(autotune.basalProfile[index].start).foregroundColor(.secondary)
  135. Spacer()
  136. Text(rateFormatter.string(from: autotune.basalProfile[index].rate as NSNumber) ?? "0")
  137. Text("U/hr").foregroundColor(.secondary)
  138. }
  139. }
  140. HStack {
  141. Text("Total")
  142. .bold()
  143. .foregroundColor(.primary)
  144. Spacer()
  145. Text(rateFormatter.string(from: autotune.basalProfile.reduce(0) { $0 + $1.rate } as NSNumber) ?? "0")
  146. .foregroundColor(.primary) +
  147. Text(" U/day")
  148. .foregroundColor(.secondary)
  149. }
  150. }
  151. .listRowBackground(Color.chart)
  152. Section {
  153. Button {
  154. Task {
  155. await state.delete()
  156. }
  157. } label: {
  158. Text("Delete Autotune Data")
  159. }
  160. .frame(maxWidth: .infinity, alignment: .center)
  161. .listRowBackground(Color(.loopRed))
  162. .tint(.white)
  163. }
  164. Section {
  165. Button {
  166. replaceAlert = true
  167. } label: {
  168. Text("Save as Normal Basal Rates")
  169. }
  170. .frame(maxWidth: .infinity, alignment: .center)
  171. .listRowBackground(Color(.systemGray4))
  172. .tint(.white)
  173. }
  174. }
  175. }
  176. .sheet(isPresented: $shouldDisplayHint) {
  177. SettingInputHintView(
  178. hintDetent: $hintDetent,
  179. shouldDisplayHint: $shouldDisplayHint,
  180. hintLabel: hintLabel ?? "",
  181. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  182. sheetTitle: "Help"
  183. )
  184. }
  185. .scrollContentBackground(.hidden).background(color)
  186. .onAppear(perform: configureView)
  187. .navigationTitle("Autotune")
  188. .navigationBarTitleDisplayMode(.automatic)
  189. .alert(Text("Are you sure?"), isPresented: $replaceAlert) {
  190. Button("Yes", action: {
  191. state.replace()
  192. replaceAlert.toggle()
  193. })
  194. Button("No", action: { replaceAlert.toggle() })
  195. }
  196. }
  197. }
  198. }