AutotuneConfigRootView.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import SwiftUI
  2. import Swinject
  3. extension AutotuneConfig {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. @State var replaceAlert = false
  8. <<<<<<< HEAD
  9. @Environment(\.colorScheme) var colorScheme
  10. var color: LinearGradient {
  11. colorScheme == .dark ? LinearGradient(
  12. gradient: Gradient(colors: [
  13. Color.bgDarkBlue,
  14. Color.bgDarkerDarkBlue
  15. ]),
  16. startPoint: .top,
  17. endPoint: .bottom
  18. )
  19. :
  20. LinearGradient(
  21. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  22. startPoint: .top,
  23. endPoint: .bottom
  24. )
  25. }
  26. =======
  27. >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133
  28. private var isfFormatter: NumberFormatter {
  29. let formatter = NumberFormatter()
  30. formatter.numberStyle = .decimal
  31. formatter.maximumFractionDigits = 2
  32. return formatter
  33. }
  34. private var rateFormatter: NumberFormatter {
  35. let formatter = NumberFormatter()
  36. formatter.numberStyle = .decimal
  37. formatter.maximumFractionDigits = 2
  38. return formatter
  39. }
  40. private var dateFormatter: DateFormatter {
  41. let formatter = DateFormatter()
  42. formatter.dateStyle = .medium
  43. formatter.timeStyle = .short
  44. return formatter
  45. }
  46. var body: some View {
  47. Form {
  48. Section {
  49. Toggle("Use Autotune", isOn: $state.useAutotune)
  50. if state.useAutotune {
  51. Toggle("Only Autotune Basal Insulin", isOn: $state.onlyAutotuneBasals)
  52. }
  53. }
  54. Section {
  55. HStack {
  56. Text("Last run")
  57. Spacer()
  58. Text(dateFormatter.string(from: state.publishedDate))
  59. }
  60. Button { state.run() }
  61. label: { Text("Run now") }
  62. }
  63. if let autotune = state.autotune {
  64. if !state.onlyAutotuneBasals {
  65. Section {
  66. HStack {
  67. Text("Carb ratio")
  68. Spacer()
  69. Text(isfFormatter.string(from: autotune.carbRatio as NSNumber) ?? "0")
  70. Text("g/U").foregroundColor(.secondary)
  71. }
  72. HStack {
  73. Text("Sensitivity")
  74. Spacer()
  75. if state.units == .mmolL {
  76. Text(isfFormatter.string(from: autotune.sensitivity.asMmolL as NSNumber) ?? "0")
  77. } else {
  78. Text(isfFormatter.string(from: autotune.sensitivity as NSNumber) ?? "0")
  79. }
  80. Text(state.units.rawValue + "/U").foregroundColor(.secondary)
  81. }
  82. }
  83. }
  84. Section(header: Text("Basal profile")) {
  85. ForEach(0 ..< autotune.basalProfile.count, id: \.self) { index in
  86. HStack {
  87. Text(autotune.basalProfile[index].start).foregroundColor(.secondary)
  88. Spacer()
  89. Text(rateFormatter.string(from: autotune.basalProfile[index].rate as NSNumber) ?? "0")
  90. Text("U/hr").foregroundColor(.secondary)
  91. }
  92. }
  93. HStack {
  94. Text("Total")
  95. .bold()
  96. .foregroundColor(.primary)
  97. Spacer()
  98. Text(rateFormatter.string(from: autotune.basalProfile.reduce(0) { $0 + $1.rate } as NSNumber) ?? "0")
  99. .foregroundColor(.primary) +
  100. Text(" U/day")
  101. .foregroundColor(.secondary)
  102. }
  103. }
  104. Section {
  105. Button {
  106. Task {
  107. await state.delete()
  108. }
  109. }
  110. label: { Text("Delete autotune data") }
  111. .foregroundColor(.red)
  112. }
  113. Section {
  114. Button {
  115. replaceAlert = true
  116. }
  117. label: { Text("Save as your Normal Basal Rates") }
  118. } header: {
  119. <<<<<<< HEAD
  120. Text("Save on Pump")
  121. =======
  122. Text("Replace Normal Basal")
  123. >>>>>>> 9672da256c317a314acc76d6e4f6e82cc174d133
  124. }
  125. }
  126. }
  127. .scrollContentBackground(.hidden).background(color)
  128. .onAppear(perform: configureView)
  129. .navigationTitle("Autotune")
  130. .navigationBarTitleDisplayMode(.automatic)
  131. .alert(Text("Are you sure?"), isPresented: $replaceAlert) {
  132. Button("Yes", action: {
  133. state.replace()
  134. replaceAlert.toggle()
  135. })
  136. Button("No", action: { replaceAlert.toggle() })
  137. }
  138. }
  139. }
  140. }