CalibrationsRootView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. state.addCalibration()
  33. }
  34. label: { Text("Add") }
  35. .disabled(state.newCalibration <= 0)
  36. }.listRowBackground(Color.chart)
  37. Section(header: Text("Info")) {
  38. HStack {
  39. Text("Slope")
  40. Spacer()
  41. Text(formatter.string(from: state.slope as NSNumber)!)
  42. }
  43. HStack {
  44. Text("Intercept")
  45. Spacer()
  46. Text(formatter.string(from: state.intercept as NSNumber)!)
  47. }
  48. }.listRowBackground(Color.chart)
  49. Section(header: Text("Remove")) {
  50. Button {
  51. state.removeLast()
  52. }
  53. label: { Text("Remove Last") }
  54. .disabled(state.calibrations.isEmpty)
  55. Button {
  56. state.removeAll()
  57. }
  58. label: { Text("Remove All") }
  59. .disabled(state.calibrations.isEmpty)
  60. List {
  61. ForEach(state.items) { item in
  62. HStack {
  63. Text(dateFormatter.string(from: item.calibration.date))
  64. Spacer()
  65. VStack(alignment: .leading) {
  66. Text("raw: \(item.calibration.x)")
  67. .font(.caption2)
  68. .foregroundColor(.secondary)
  69. Text("value: \(item.calibration.y)")
  70. .font(.caption2)
  71. .foregroundColor(.secondary)
  72. }
  73. }
  74. }.onDelete(perform: delete)
  75. }
  76. }.listRowBackground(Color.chart)
  77. if state.calibrations.isNotEmpty {
  78. Section(header: Text("Chart")) {
  79. CalibrationsChart(state: state)
  80. .frame(minHeight: geo.size.width)
  81. }.listRowBackground(Color.chart)
  82. }
  83. }
  84. }
  85. .scrollContentBackground(.hidden).background(appState.trioBackgroundColor(for: colorScheme))
  86. .dynamicTypeSize(...DynamicTypeSize.xxLarge)
  87. .onAppear(perform: configureView)
  88. .navigationTitle("Calibrations")
  89. .navigationBarItems(trailing: EditButton().disabled(state.calibrations.isEmpty))
  90. .navigationBarTitleDisplayMode(.automatic)
  91. }
  92. private func delete(at offsets: IndexSet) {
  93. state.removeAtIndex(offsets[offsets.startIndex])
  94. }
  95. }
  96. }