CalibrationsRootView.swift 2.4 KB

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