BasalProfileEditorRootView.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import SwiftUI
  2. import Swinject
  3. extension BasalProfileEditor {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @StateObject var state = StateModel()
  7. @State private var editMode = EditMode.inactive
  8. @Environment(\.colorScheme) var colorScheme
  9. var color: LinearGradient {
  10. colorScheme == .dark ? LinearGradient(
  11. gradient: Gradient(colors: [
  12. Color.bgDarkBlue,
  13. Color.bgDarkerDarkBlue
  14. ]),
  15. startPoint: .top,
  16. endPoint: .bottom
  17. )
  18. :
  19. LinearGradient(
  20. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  21. startPoint: .top,
  22. endPoint: .bottom
  23. )
  24. }
  25. private var dateFormatter: DateFormatter {
  26. let formatter = DateFormatter()
  27. formatter.timeZone = TimeZone(secondsFromGMT: 0)
  28. formatter.timeStyle = .short
  29. return formatter
  30. }
  31. private var rateFormatter: NumberFormatter {
  32. let formatter = NumberFormatter()
  33. formatter.numberStyle = .decimal
  34. return formatter
  35. }
  36. var body: some View {
  37. Form {
  38. Section(header: Text("Schedule")) {
  39. list
  40. addButton
  41. }
  42. Section {
  43. HStack {
  44. Text("Total")
  45. .bold()
  46. .foregroundColor(.primary)
  47. Spacer()
  48. Text(rateFormatter.string(from: state.total as NSNumber) ?? "0")
  49. .foregroundColor(.primary) +
  50. Text(" U/day")
  51. .foregroundColor(.secondary)
  52. }
  53. }
  54. Section {
  55. HStack {
  56. if state.syncInProgress {
  57. ProgressView().padding(.trailing, 10)
  58. }
  59. Button { state.save() }
  60. label: {
  61. Text(state.syncInProgress ? "Saving..." : "Save on Pump")
  62. }
  63. .disabled(state.syncInProgress || state.items.isEmpty)
  64. }
  65. }
  66. }
  67. .scrollContentBackground(.hidden).background(color)
  68. .onAppear(perform: configureView)
  69. .navigationTitle("Basal Profile")
  70. .navigationBarTitleDisplayMode(.automatic)
  71. .navigationBarItems(
  72. trailing: EditButton()
  73. )
  74. .environment(\.editMode, $editMode)
  75. .onAppear {
  76. state.validate()
  77. }
  78. }
  79. private func pickers(for index: Int) -> some View {
  80. GeometryReader { geometry in
  81. VStack {
  82. HStack {
  83. Text("Rate").frame(width: geometry.size.width / 2)
  84. Text("Time").frame(width: geometry.size.width / 2)
  85. }
  86. HStack(spacing: 0) {
  87. Picker(selection: $state.items[index].rateIndex, label: EmptyView()) {
  88. ForEach(0 ..< state.rateValues.count, id: \.self) { i in
  89. Text(
  90. (
  91. self.rateFormatter
  92. .string(from: state.rateValues[i] as NSNumber) ?? ""
  93. ) + " U/hr"
  94. ).tag(i)
  95. }
  96. }
  97. .onChange(of: state.items[index].rateIndex, perform: { _ in state.calcTotal() })
  98. .frame(maxWidth: geometry.size.width / 2)
  99. .clipped()
  100. Picker(selection: $state.items[index].timeIndex, label: EmptyView()) {
  101. ForEach(0 ..< state.timeValues.count, id: \.self) { i in
  102. Text(
  103. self.dateFormatter
  104. .string(from: Date(
  105. timeIntervalSince1970: state
  106. .timeValues[i]
  107. ))
  108. ).tag(i)
  109. }
  110. }
  111. .onChange(of: state.items[index].timeIndex, perform: { _ in state.calcTotal() })
  112. .frame(maxWidth: geometry.size.width / 2)
  113. .clipped()
  114. }
  115. }
  116. }
  117. }
  118. private var list: some View {
  119. List {
  120. ForEach(state.items.indexed(), id: \.1.id) { index, item in
  121. NavigationLink(destination: pickers(for: index)) {
  122. HStack {
  123. Text("Rate").foregroundColor(.secondary)
  124. Text(
  125. "\(rateFormatter.string(from: state.rateValues[item.rateIndex] as NSNumber) ?? "0") U/hr"
  126. )
  127. Spacer()
  128. Text("starts at").foregroundColor(.secondary)
  129. Text(
  130. "\(dateFormatter.string(from: Date(timeIntervalSince1970: state.timeValues[item.timeIndex])))"
  131. )
  132. }
  133. }
  134. .moveDisabled(true)
  135. }
  136. .onDelete(perform: onDelete)
  137. }
  138. }
  139. private var addButton: some View {
  140. guard state.canAdd else {
  141. return AnyView(EmptyView())
  142. }
  143. switch editMode {
  144. case .inactive:
  145. return AnyView(Button(action: onAdd) { Text("Add") })
  146. default:
  147. return AnyView(EmptyView())
  148. }
  149. }
  150. func onAdd() {
  151. state.add()
  152. }
  153. private func onDelete(offsets: IndexSet) {
  154. state.items.remove(atOffsets: offsets)
  155. state.validate()
  156. state.calcTotal()
  157. }
  158. }
  159. }