CalibrationsRootView.swift 4.7 KB

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