AutotuneConfigRootView.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 = 2
  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. private var dateFormatter: DateFormatter {
  18. let formatter = DateFormatter()
  19. formatter.dateStyle = .medium
  20. formatter.timeStyle = .short
  21. return formatter
  22. }
  23. var body: some View {
  24. Form {
  25. Section {
  26. Toggle("Use Autotune", isOn: $viewModel.useAutotune)
  27. }
  28. Section {
  29. HStack {
  30. Text("Last run")
  31. Spacer()
  32. Text(dateFormatter.string(from: viewModel.publishedDate))
  33. }
  34. Button { viewModel.run() }
  35. label: { Text("Run now") }
  36. }
  37. if let autotune = viewModel.autotune {
  38. Section {
  39. HStack {
  40. Text("Carb ratio")
  41. Spacer()
  42. Text(isfFormatter.string(from: autotune.carbRatio as NSNumber) ?? "0")
  43. Text("g/U").foregroundColor(.secondary)
  44. }
  45. HStack {
  46. Text("Sensitivity")
  47. Spacer()
  48. if viewModel.units == .mmolL {
  49. Text(isfFormatter.string(from: autotune.sensitivity.asMmolL as NSNumber) ?? "0")
  50. } else {
  51. Text(isfFormatter.string(from: autotune.sensitivity as NSNumber) ?? "0")
  52. }
  53. Text(viewModel.units.rawValue + "/U").foregroundColor(.secondary)
  54. }
  55. }
  56. Section(header: Text("Basal profile")) {
  57. ForEach(0 ..< autotune.basalProfile.count, id: \.self) { index in
  58. HStack {
  59. Text(autotune.basalProfile[index].start).foregroundColor(.secondary)
  60. Spacer()
  61. Text(rateFormatter.string(from: autotune.basalProfile[index].rate as NSNumber) ?? "0")
  62. Text("U/hr").foregroundColor(.secondary)
  63. }
  64. }
  65. }
  66. Section {
  67. Button { viewModel.delete() }
  68. label: { Text("Delete autotune data") }
  69. .foregroundColor(.red)
  70. }
  71. }
  72. }
  73. .navigationTitle("Autotune")
  74. .navigationBarTitleDisplayMode(.automatic)
  75. }
  76. }
  77. }