CalibrationsRootView.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import SwiftUI
  2. import Swinject
  3. extension Calibrations {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. private var formatter: NumberFormatter {
  8. let formatter = NumberFormatter()
  9. formatter.numberStyle = .decimal
  10. formatter.maximumFractionDigits = 2
  11. return formatter
  12. }
  13. var body: some View {
  14. GeometryReader { geo in
  15. Form {
  16. Section(header: Text("Add calibration")) {
  17. HStack {
  18. Text("Meter glucose")
  19. Spacer()
  20. DecimalTextField(
  21. "0",
  22. value: $state.newCalibration,
  23. formatter: formatter,
  24. autofocus: false,
  25. cleanInput: true
  26. )
  27. Text(state.units.rawValue).foregroundColor(.secondary)
  28. }
  29. Button {
  30. state.addCalibration()
  31. }
  32. label: { Text("Add") }
  33. .disabled(state.newCalibration <= 0)
  34. }
  35. Section(header: Text("Info")) {
  36. HStack {
  37. Text("Slope")
  38. Spacer()
  39. Text(formatter.string(from: state.slope as NSNumber)!)
  40. }
  41. HStack {
  42. Text("Intercept")
  43. Spacer()
  44. Text(formatter.string(from: state.intercept as NSNumber)!)
  45. }
  46. }
  47. Section(header: Text("Remove")) {
  48. Button {
  49. state.removeLast()
  50. }
  51. label: { Text("Remove Last") }
  52. .disabled(state.calibrations.isEmpty)
  53. Button {
  54. state.removeAll()
  55. }
  56. label: { Text("Remove All") }
  57. .disabled(state.calibrations.isEmpty)
  58. }
  59. Section(header: Text("Chart")) {
  60. CalibrationsChart().environmentObject(state)
  61. .frame(minHeight: geo.size.width)
  62. }
  63. }
  64. }
  65. .onAppear(perform: configureView)
  66. .navigationTitle("Calibrations")
  67. .navigationBarTitleDisplayMode(.automatic)
  68. }
  69. }
  70. }