BasalProfileEditorRootView.swift 6.4 KB

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