CalibrationsRootView.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import SwiftUI
  2. import Swinject
  3. extension Calibrations {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. @Environment(\.colorScheme) var colorScheme
  8. var color: LinearGradient {
  9. colorScheme == .dark ? LinearGradient(
  10. gradient: Gradient(colors: [
  11. Color("Background_1"),
  12. Color("Background_1"),
  13. Color("Background_2")
  14. // Color("Background_1")
  15. ]),
  16. startPoint: .top,
  17. endPoint: .bottom
  18. )
  19. :
  20. LinearGradient(
  21. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  22. startPoint: .top,
  23. endPoint: .bottom
  24. )
  25. }
  26. private var formatter: NumberFormatter {
  27. let formatter = NumberFormatter()
  28. formatter.numberStyle = .decimal
  29. formatter.maximumFractionDigits = 2
  30. return formatter
  31. }
  32. private var dateFormatter: DateFormatter {
  33. let formatter = DateFormatter()
  34. formatter.timeStyle = .short
  35. formatter.dateStyle = .short
  36. return formatter
  37. }
  38. var body: some View {
  39. GeometryReader { geo in
  40. Form {
  41. Section(header: Text("Add calibration")) {
  42. HStack {
  43. Text("Meter glucose")
  44. Spacer()
  45. DecimalTextField(
  46. "0",
  47. value: $state.newCalibration,
  48. formatter: formatter,
  49. autofocus: false,
  50. cleanInput: true
  51. )
  52. Text(state.units.rawValue).foregroundColor(.secondary)
  53. }
  54. Button {
  55. state.addCalibration()
  56. }
  57. label: { Text("Add") }
  58. .disabled(state.newCalibration <= 0)
  59. }
  60. Section(header: Text("Info")) {
  61. HStack {
  62. Text("Slope")
  63. Spacer()
  64. Text(formatter.string(from: state.slope as NSNumber)!)
  65. }
  66. HStack {
  67. Text("Intercept")
  68. Spacer()
  69. Text(formatter.string(from: state.intercept as NSNumber)!)
  70. }
  71. }
  72. Section(header: Text("Remove")) {
  73. Button {
  74. state.removeLast()
  75. }
  76. label: { Text("Remove Last") }
  77. .disabled(state.calibrations.isEmpty)
  78. Button {
  79. state.removeAll()
  80. }
  81. label: { Text("Remove All") }
  82. .disabled(state.calibrations.isEmpty)
  83. List {
  84. ForEach(state.items) { item in
  85. HStack {
  86. Text(dateFormatter.string(from: item.calibration.date))
  87. Spacer()
  88. VStack(alignment: .leading) {
  89. Text("raw: \(item.calibration.x)")
  90. .font(.caption2)
  91. .foregroundColor(.secondary)
  92. Text("value: \(item.calibration.y)")
  93. .font(.caption2)
  94. .foregroundColor(.secondary)
  95. }
  96. }
  97. }.onDelete(perform: delete)
  98. }
  99. }
  100. if state.calibrations.isNotEmpty {
  101. Section(header: Text("Chart")) {
  102. CalibrationsChart().environmentObject(state)
  103. .frame(minHeight: geo.size.width)
  104. }
  105. }
  106. }
  107. }
  108. .scrollContentBackground(.hidden).background(color)
  109. .onAppear(perform: configureView)
  110. .navigationTitle("Calibrations")
  111. .navigationBarItems(trailing: EditButton().disabled(state.calibrations.isEmpty))
  112. .navigationBarTitleDisplayMode(.automatic)
  113. }
  114. private func delete(at offsets: IndexSet) {
  115. state.removeAtIndex(offsets[offsets.startIndex])
  116. }
  117. }
  118. }