AutotuneConfigRootView.swift 8.3 KB

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