AutotuneConfigRootView.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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: VStack(spacing: 10) {
  67. Text("Default: OFF").bold()
  68. VStack(alignment: .leading, spacing: 10) {
  69. Text("It is not advised to use Autotune with Trio").bold()
  70. Text("Autotune is not designed to work with Trio. It is best to keep Autotune off and do not use it.")
  71. }
  72. },
  73. headerText: "Data-driven Adjustments"
  74. )
  75. if state.useAutotune {
  76. SettingInputSection(
  77. decimalValue: $decimalPlaceholder,
  78. booleanValue: $state.onlyAutotuneBasals,
  79. shouldDisplayHint: $shouldDisplayHint,
  80. selectedVerboseHint: Binding(
  81. get: { selectedVerboseHint },
  82. set: {
  83. selectedVerboseHint = $0.map { AnyView($0) }
  84. hintLabel = "Only Autotune Basal Insulin"
  85. }
  86. ),
  87. units: state.units,
  88. type: .boolean,
  89. label: "Only Autotune Basal Insulin",
  90. miniHint: "Restricts Autotune adjustments to only basal settings",
  91. verboseHint: Text("Restricts Autotune adjustments to only basal settings.")
  92. )
  93. }
  94. Section(
  95. header: HStack {
  96. Text("Last run")
  97. Spacer()
  98. Text(dateFormatter.string(from: state.publishedDate))
  99. },
  100. content: {
  101. Button {
  102. state.run()
  103. } label: {
  104. Text("Run now")
  105. }
  106. .frame(maxWidth: .infinity, alignment: .center)
  107. .listRowBackground(Color(.systemBlue))
  108. .tint(.white)
  109. }
  110. )
  111. if let autotune = state.autotune {
  112. if !state.onlyAutotuneBasals {
  113. Section {
  114. HStack {
  115. Text("Carb ratio")
  116. Spacer()
  117. Text(isfFormatter.string(from: autotune.carbRatio as NSNumber) ?? "0")
  118. Text("g/U").foregroundColor(.secondary)
  119. }
  120. HStack {
  121. Text("Sensitivity")
  122. Spacer()
  123. if state.units == .mmolL {
  124. Text(isfFormatter.string(from: autotune.sensitivity.asMmolL as NSNumber) ?? "0")
  125. } else {
  126. Text(isfFormatter.string(from: autotune.sensitivity as NSNumber) ?? "0")
  127. }
  128. Text(state.units.rawValue + "/U").foregroundColor(.secondary)
  129. }
  130. }
  131. .listRowBackground(Color.chart)
  132. }
  133. Section(header: Text("Basal profile")) {
  134. ForEach(0 ..< autotune.basalProfile.count, id: \.self) { index in
  135. HStack {
  136. Text(autotune.basalProfile[index].start).foregroundColor(.secondary)
  137. Spacer()
  138. Text(rateFormatter.string(from: autotune.basalProfile[index].rate as NSNumber) ?? "0")
  139. Text("U/hr").foregroundColor(.secondary)
  140. }
  141. }
  142. HStack {
  143. Text("Total")
  144. .bold()
  145. .foregroundColor(.primary)
  146. Spacer()
  147. Text(rateFormatter.string(from: autotune.basalProfile.reduce(0) { $0 + $1.rate } as NSNumber) ?? "0")
  148. .foregroundColor(.primary) +
  149. Text(" U/day")
  150. .foregroundColor(.secondary)
  151. }
  152. }
  153. .listRowBackground(Color.chart)
  154. Section {
  155. Button {
  156. Task {
  157. await state.delete()
  158. }
  159. } label: {
  160. Text("Delete Autotune Data")
  161. }
  162. .frame(maxWidth: .infinity, alignment: .center)
  163. .listRowBackground(Color(.loopRed))
  164. .tint(.white)
  165. }
  166. Section {
  167. Button {
  168. replaceAlert = true
  169. } label: {
  170. Text("Save as Normal Basal Rates")
  171. }
  172. .frame(maxWidth: .infinity, alignment: .center)
  173. .listRowBackground(Color(.systemGray4))
  174. .tint(.white)
  175. }
  176. }
  177. }
  178. .sheet(isPresented: $shouldDisplayHint) {
  179. SettingInputHintView(
  180. hintDetent: $hintDetent,
  181. shouldDisplayHint: $shouldDisplayHint,
  182. hintLabel: hintLabel ?? "",
  183. hintText: selectedVerboseHint ?? AnyView(EmptyView()),
  184. sheetTitle: "Help"
  185. )
  186. }
  187. .scrollContentBackground(.hidden).background(color)
  188. .onAppear(perform: configureView)
  189. .navigationTitle("Autotune")
  190. .navigationBarTitleDisplayMode(.automatic)
  191. .alert(Text("Are you sure?"), isPresented: $replaceAlert) {
  192. Button("Yes", action: {
  193. state.replace()
  194. replaceAlert.toggle()
  195. })
  196. Button("No", action: { replaceAlert.toggle() })
  197. }
  198. }
  199. }
  200. }