BasalProfileEditorRootView.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import SwiftUI
  2. extension BasalProfileEditor {
  3. struct RootView: BaseView {
  4. @EnvironmentObject var viewModel: ViewModel<Provider>
  5. @State private var editMode = EditMode.inactive
  6. private var dateFormatter: DateFormatter {
  7. let formatter = DateFormatter()
  8. formatter.timeZone = TimeZone(secondsFromGMT: 0)
  9. formatter.timeStyle = .short
  10. return formatter
  11. }
  12. private var rateFormatter: NumberFormatter {
  13. let formatter = NumberFormatter()
  14. formatter.numberStyle = .decimal
  15. return formatter
  16. }
  17. var body: some View {
  18. Form {
  19. Section(header: Text("Schedule")) {
  20. list
  21. addButton
  22. }
  23. Section {
  24. HStack {
  25. if viewModel.syncInProgress {
  26. ProgressView().padding(.trailing, 10)
  27. }
  28. Button { viewModel.save() }
  29. label: {
  30. Text(viewModel.syncInProgress ? "Saving..." : "Save on Pump")
  31. }
  32. .disabled(viewModel.syncInProgress || viewModel.items.isEmpty)
  33. }
  34. }
  35. }
  36. .navigationTitle("Basal Profile")
  37. .navigationBarTitleDisplayMode(.automatic)
  38. .navigationBarItems(
  39. leading: Button("Close", action: viewModel.hideModal),
  40. trailing: EditButton()
  41. )
  42. .environment(\.editMode, $editMode)
  43. .onAppear {
  44. viewModel.validate()
  45. }
  46. }
  47. private func pickers(for index: Int) -> some View {
  48. GeometryReader { geometry in
  49. VStack {
  50. HStack {
  51. Text("Rate").frame(width: geometry.size.width / 2)
  52. Text("Time").frame(width: geometry.size.width / 2)
  53. }
  54. HStack(spacing: 0) {
  55. Picker(selection: $viewModel.items[index].rateIndex, label: EmptyView()) {
  56. ForEach(0 ..< viewModel.rateValues.count, id: \.self) { i in
  57. Text(
  58. (
  59. self.rateFormatter
  60. .string(from: viewModel.rateValues[i] as NSNumber) ?? ""
  61. ) + " U/h"
  62. ).tag(i)
  63. }
  64. }
  65. .frame(maxWidth: geometry.size.width / 2)
  66. .clipped()
  67. Picker(selection: $viewModel.items[index].timeIndex, label: EmptyView()) {
  68. ForEach(0 ..< viewModel.timeValues.count, id: \.self) { i in
  69. Text(
  70. self.dateFormatter
  71. .string(from: Date(
  72. timeIntervalSince1970: viewModel
  73. .timeValues[i]
  74. ))
  75. ).tag(i)
  76. }
  77. }
  78. .frame(maxWidth: geometry.size.width / 2)
  79. .clipped()
  80. }
  81. }
  82. }
  83. }
  84. private var list: some View {
  85. List {
  86. ForEach(viewModel.items.indexed(), id: \.1.id) { index, item in
  87. NavigationLink(destination: pickers(for: index)) {
  88. HStack {
  89. Text("Rate").foregroundColor(.secondary)
  90. Text(
  91. "\(rateFormatter.string(from: viewModel.rateValues[item.rateIndex] as NSNumber) ?? "0") U/h"
  92. )
  93. Spacer()
  94. Text("starts at").foregroundColor(.secondary)
  95. Text(
  96. "\(dateFormatter.string(from: Date(timeIntervalSince1970: viewModel.timeValues[item.timeIndex])))"
  97. )
  98. }
  99. }
  100. .moveDisabled(true)
  101. }
  102. .onDelete(perform: onDelete)
  103. }
  104. }
  105. private var addButton: some View {
  106. guard viewModel.canAdd else {
  107. return AnyView(EmptyView())
  108. }
  109. switch editMode {
  110. case .inactive:
  111. return AnyView(Button(action: onAdd) { Text("Add") })
  112. default:
  113. return AnyView(EmptyView())
  114. }
  115. }
  116. func onAdd() {
  117. viewModel.add()
  118. }
  119. private func onDelete(offsets: IndexSet) {
  120. viewModel.items.remove(atOffsets: offsets)
  121. viewModel.validate()
  122. }
  123. }
  124. }