AutotuneConfigRootView.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import SwiftUI
  2. import Swinject
  3. extension AutotuneConfig {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. private var isfFormatter: NumberFormatter {
  8. let formatter = NumberFormatter()
  9. formatter.numberStyle = .decimal
  10. formatter.maximumFractionDigits = 2
  11. return formatter
  12. }
  13. private var rateFormatter: NumberFormatter {
  14. let formatter = NumberFormatter()
  15. formatter.numberStyle = .decimal
  16. formatter.maximumFractionDigits = 2
  17. return formatter
  18. }
  19. private var dateFormatter: DateFormatter {
  20. let formatter = DateFormatter()
  21. formatter.dateStyle = .medium
  22. formatter.timeStyle = .short
  23. return formatter
  24. }
  25. var body: some View {
  26. Form {
  27. Section {
  28. Toggle("Use Autotune", isOn: $state.useAutotune)
  29. if state.useAutotune {
  30. Toggle("Only Autotune Basal Insulin", isOn: $state.onlyAutotuneBasals)
  31. }
  32. }
  33. Section {
  34. HStack {
  35. Text("Last run")
  36. Spacer()
  37. Text(dateFormatter.string(from: state.publishedDate))
  38. }
  39. Button { state.run() }
  40. label: { Text("Run now") }
  41. }
  42. if let autotune = state.autotune {
  43. if !state.onlyAutotuneBasals {
  44. Section {
  45. HStack {
  46. Text("Carb ratio")
  47. Spacer()
  48. Text(isfFormatter.string(from: autotune.carbRatio as NSNumber) ?? "0")
  49. Text("g/U").foregroundColor(.secondary)
  50. }
  51. HStack {
  52. Text("Sensitivity")
  53. Spacer()
  54. if state.units == .mmolL {
  55. Text(isfFormatter.string(from: autotune.sensitivity.asMmolL as NSNumber) ?? "0")
  56. } else {
  57. Text(isfFormatter.string(from: autotune.sensitivity as NSNumber) ?? "0")
  58. }
  59. Text(state.units.rawValue + "/U").foregroundColor(.secondary)
  60. }
  61. }
  62. }
  63. Section(header: Text("Basal profile")) {
  64. ForEach(0 ..< autotune.basalProfile.count, id: \.self) { index in
  65. HStack {
  66. Text(autotune.basalProfile[index].start).foregroundColor(.secondary)
  67. Spacer()
  68. Text(rateFormatter.string(from: autotune.basalProfile[index].rate as NSNumber) ?? "0")
  69. Text("U/hr").foregroundColor(.secondary)
  70. }
  71. }
  72. HStack {
  73. Text("Total")
  74. .bold()
  75. .foregroundColor(.primary)
  76. Spacer()
  77. Text(rateFormatter.string(from: autotune.basalProfile.reduce(0) { $0 + $1.rate } as NSNumber) ?? "0")
  78. .foregroundColor(.primary) +
  79. Text(" U/day")
  80. .foregroundColor(.secondary)
  81. }
  82. }
  83. Section {
  84. Button { state.delete() }
  85. label: { Text("Delete autotune data") }
  86. .foregroundColor(.red)
  87. }
  88. }
  89. }
  90. .onAppear(perform: configureView)
  91. .navigationTitle("Autotune")
  92. .navigationBarTitleDisplayMode(.automatic)
  93. }
  94. }
  95. }