CalibrationsRootView.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import SwiftUI
  2. import Swinject
  3. extension Calibrations {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @State var state = StateModel()
  7. @Environment(\.colorScheme) var colorScheme
  8. @Environment(AppState.self) var appState
  9. private var formatter: NumberFormatter {
  10. let formatter = NumberFormatter()
  11. formatter.numberStyle = .decimal
  12. formatter.maximumFractionDigits = 2
  13. return formatter
  14. }
  15. private var dateFormatter: DateFormatter {
  16. let formatter = DateFormatter()
  17. formatter.timeStyle = .short
  18. formatter.dateStyle = .short
  19. return formatter
  20. }
  21. var body: some View {
  22. GeometryReader { geo in
  23. Form {
  24. Section(header: Text("Add calibration")) {
  25. HStack {
  26. Text("Meter glucose")
  27. Spacer()
  28. TextFieldWithToolBar(text: $state.newCalibration, placeholder: "0", numberFormatter: formatter)
  29. Text(state.units.rawValue).foregroundColor(.secondary)
  30. }
  31. Button {
  32. Task {
  33. await state.addCalibration()
  34. }
  35. }
  36. label: { Text("Add") }
  37. .disabled(state.newCalibration <= 0)
  38. }.listRowBackground(Color.chart)
  39. Section(header: Text("Info")) {
  40. HStack {
  41. Text("Slope")
  42. Spacer()
  43. Text(formatter.string(from: state.slope as NSNumber)!)
  44. }
  45. HStack {
  46. Text("Intercept")
  47. Spacer()
  48. Text(formatter.string(from: state.intercept as NSNumber)!)
  49. }
  50. }.listRowBackground(Color.chart)
  51. Section(header: Text("Remove")) {
  52. Button {
  53. state.removeLast()
  54. }
  55. label: { Text("Remove Last") }
  56. .disabled(state.calibrations.isEmpty)
  57. Button {
  58. state.removeAll()
  59. }
  60. label: { Text("Remove All") }
  61. .disabled(state.calibrations.isEmpty)
  62. List {
  63. ForEach(state.items) { item in
  64. HStack {
  65. Text(dateFormatter.string(from: item.calibration.date))
  66. Spacer()
  67. VStack(alignment: .leading) {
  68. Text("raw: \(item.calibration.x)")
  69. .font(.caption2)
  70. .foregroundColor(.secondary)
  71. Text("value: \(item.calibration.y)")
  72. .font(.caption2)
  73. .foregroundColor(.secondary)
  74. }
  75. }
  76. }.onDelete(perform: delete)
  77. }
  78. }.listRowBackground(Color.chart)
  79. if state.calibrations.isNotEmpty {
  80. Section(header: Text("Chart")) {
  81. CalibrationsChart(state: state)
  82. .frame(minHeight: geo.size.width)
  83. }.listRowBackground(Color.chart)
  84. }
  85. }
  86. }
  87. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  88. .dynamicTypeSize(...DynamicTypeSize.xxLarge)
  89. .onAppear(perform: configureView)
  90. .navigationTitle("Calibrations")
  91. .navigationBarItems(trailing: EditButton().disabled(state.calibrations.isEmpty))
  92. .navigationBarTitleDisplayMode(.automatic)
  93. }
  94. private func delete(at offsets: IndexSet) {
  95. state.removeAtIndex(offsets[offsets.startIndex])
  96. }
  97. }
  98. }