AutotuneConfigRootView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. private var isfFormatter: NumberFormatter {
  9. let formatter = NumberFormatter()
  10. formatter.numberStyle = .decimal
  11. formatter.maximumFractionDigits = 2
  12. return formatter
  13. }
  14. private var rateFormatter: NumberFormatter {
  15. let formatter = NumberFormatter()
  16. formatter.numberStyle = .decimal
  17. formatter.maximumFractionDigits = 2
  18. return formatter
  19. }
  20. private var dateFormatter: DateFormatter {
  21. let formatter = DateFormatter()
  22. formatter.dateStyle = .medium
  23. formatter.timeStyle = .short
  24. return formatter
  25. }
  26. var body: some View {
  27. Form {
  28. Section {
  29. Toggle("Use Autotune", isOn: $state.useAutotune)
  30. if state.useAutotune {
  31. Toggle("Only Autotune Basal Insulin", isOn: $state.onlyAutotuneBasals)
  32. }
  33. }
  34. Section {
  35. HStack {
  36. Text("Last run")
  37. Spacer()
  38. Text(dateFormatter.string(from: state.publishedDate))
  39. }
  40. Button { state.run() }
  41. label: { Text("Run now") }
  42. }
  43. if let autotune = state.autotune {
  44. if !state.onlyAutotuneBasals {
  45. Section {
  46. HStack {
  47. Text("Carb ratio")
  48. Spacer()
  49. Text(isfFormatter.string(from: autotune.carbRatio as NSNumber) ?? "0")
  50. Text("g/U").foregroundColor(.secondary)
  51. }
  52. HStack {
  53. Text("Sensitivity")
  54. Spacer()
  55. if state.units == .mmolL {
  56. Text(isfFormatter.string(from: autotune.sensitivity.asMmolL as NSNumber) ?? "0")
  57. } else {
  58. Text(isfFormatter.string(from: autotune.sensitivity as NSNumber) ?? "0")
  59. }
  60. Text(state.units.rawValue + "/U").foregroundColor(.secondary)
  61. }
  62. }
  63. }
  64. Section(header: Text("Basal profile")) {
  65. ForEach(0 ..< autotune.basalProfile.count, id: \.self) { index in
  66. HStack {
  67. Text(autotune.basalProfile[index].start).foregroundColor(.secondary)
  68. Spacer()
  69. Text(rateFormatter.string(from: autotune.basalProfile[index].rate as NSNumber) ?? "0")
  70. Text("U/hr").foregroundColor(.secondary)
  71. }
  72. }
  73. HStack {
  74. Text("Total")
  75. .bold()
  76. .foregroundColor(.primary)
  77. Spacer()
  78. Text(rateFormatter.string(from: autotune.basalProfile.reduce(0) { $0 + $1.rate } as NSNumber) ?? "0")
  79. .foregroundColor(.primary) +
  80. Text(" U/day")
  81. .foregroundColor(.secondary)
  82. }
  83. }
  84. Section {
  85. Button { state.delete() }
  86. label: { Text("Delete autotune data") }
  87. .foregroundColor(.red)
  88. }
  89. Section {
  90. Button {
  91. replaceAlert = true
  92. }
  93. label: { Text("Save as your Normal Basal Rates") }
  94. } header: {
  95. Text("Save on Pump")
  96. }
  97. }
  98. }
  99. .onAppear(perform: configureView)
  100. .navigationTitle("Autotune")
  101. .navigationBarTitleDisplayMode(.automatic)
  102. .alert(Text("Are you sure?"), isPresented: $replaceAlert) {
  103. Button("Yes", action: {
  104. state.replace()
  105. replaceAlert.toggle()
  106. })
  107. Button("No", action: { replaceAlert.toggle() })
  108. }
  109. }
  110. }
  111. }