AutotuneConfigRootView.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import SwiftUI
  2. extension AutotuneConfig {
  3. struct RootView: BaseView {
  4. @EnvironmentObject var viewModel: ViewModel<Provider>
  5. private var isfFormatter: NumberFormatter {
  6. let formatter = NumberFormatter()
  7. formatter.numberStyle = .decimal
  8. formatter.maximumFractionDigits = 1
  9. return formatter
  10. }
  11. private var rateFormatter: NumberFormatter {
  12. let formatter = NumberFormatter()
  13. formatter.numberStyle = .decimal
  14. formatter.maximumFractionDigits = 3
  15. return formatter
  16. }
  17. var body: some View {
  18. Form {
  19. Section {
  20. Toggle("Use Autotune", isOn: $viewModel.useAutotune)
  21. }
  22. Section {
  23. Button { viewModel.run() }
  24. label: { Text("Run now") }
  25. }
  26. if let autotune = viewModel.autotune {
  27. Section {
  28. HStack {
  29. Text("Carb ratio")
  30. Spacer()
  31. Text(isfFormatter.string(from: autotune.carbRatio as NSNumber) ?? "0")
  32. Text("g/U").foregroundColor(.secondary)
  33. }
  34. HStack {
  35. Text("Sensitivity")
  36. Spacer()
  37. if viewModel.units == .mmolL {
  38. Text(isfFormatter.string(from: autotune.sensitivity.asMmolL as NSNumber) ?? "0")
  39. } else {
  40. Text(isfFormatter.string(from: autotune.sensitivity as NSNumber) ?? "0")
  41. }
  42. Text(viewModel.units.rawValue + "/U").foregroundColor(.secondary)
  43. }
  44. }
  45. Section(header: Text("Basal profile")) {
  46. ForEach(0 ..< autotune.basalProfile.count, id: \.self) { index in
  47. HStack {
  48. Text(autotune.basalProfile[index].start).foregroundColor(.secondary)
  49. Spacer()
  50. Text(rateFormatter.string(from: autotune.basalProfile[index].rate as NSNumber) ?? "0")
  51. Text("U/h").foregroundColor(.secondary)
  52. }
  53. }
  54. }
  55. Section {
  56. Button { viewModel.delete() }
  57. label: { Text("Delete autotune data") }
  58. .foregroundColor(.red)
  59. }
  60. }
  61. }
  62. .navigationTitle("Autotune")
  63. .navigationBarTitleDisplayMode(.automatic)
  64. }
  65. }
  66. }