BasalProfileEditorRootView.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import SwiftUI
  2. import Swinject
  3. extension BasalProfileEditor {
  4. struct RootView: BaseView {
  5. let resolver: Resolver
  6. @State 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. let shouldDisableButton = state.syncInProgress || state.items.isEmpty || !state.hasChanges
  39. Section(header: Text("Schedule")) {
  40. list
  41. }.listRowBackground(Color.chart)
  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. }.listRowBackground(Color.chart)
  54. Section {
  55. HStack {
  56. if state.syncInProgress {
  57. ProgressView().padding(.trailing, 10)
  58. }
  59. Button {
  60. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  61. impactHeavy.impactOccurred()
  62. state.save()
  63. } label: {
  64. Text(state.syncInProgress ? "Saving..." : "Save")
  65. }
  66. .disabled(shouldDisableButton)
  67. .frame(maxWidth: .infinity, alignment: .center)
  68. .tint(.white)
  69. }
  70. }.listRowBackground(shouldDisableButton ? Color(.systemGray4) : Color(.systemBlue))
  71. }
  72. .alert(isPresented: $state.showAlert) {
  73. Alert(
  74. title: Text("Unable to Save"),
  75. message: Text("Trio could not communicate with your pump. Changes to your basal profile were not saved."),
  76. dismissButton: .default(Text("Close"))
  77. )
  78. }
  79. .onChange(of: state.items) { state.calcTotal() }
  80. .scrollContentBackground(.hidden).background(color)
  81. .onAppear(perform: configureView)
  82. .navigationTitle("Basal Profile")
  83. .navigationBarTitleDisplayMode(.automatic)
  84. .toolbar(content: {
  85. ToolbarItem(placement: .topBarTrailing) {
  86. EditButton()
  87. }
  88. ToolbarItem(placement: .topBarTrailing) {
  89. addButton
  90. }
  91. })
  92. .environment(\.editMode, $editMode)
  93. .onAppear {
  94. state.validate()
  95. }
  96. }
  97. private func pickers(for index: Int) -> some View {
  98. Form {
  99. Section {
  100. Picker(selection: $state.items[index].rateIndex, label: Text("Rate")) {
  101. ForEach(0 ..< state.rateValues.count, id: \.self) { i in
  102. Text(
  103. (
  104. self.rateFormatter
  105. .string(from: state.rateValues[i] as NSNumber) ?? ""
  106. ) + " U/hr"
  107. ).tag(i)
  108. }
  109. }
  110. .onChange(of: state.items[index].rateIndex, { state.calcTotal() })
  111. }.listRowBackground(Color.chart)
  112. Section {
  113. Picker(selection: $state.items[index].timeIndex, label: Text("Time")) {
  114. ForEach(state.availableTimeIndices(index), id: \.self) { i in
  115. Text(
  116. self.dateFormatter
  117. .string(from: Date(
  118. timeIntervalSince1970: state
  119. .timeValues[i]
  120. ))
  121. ).tag(i)
  122. }
  123. }
  124. .onChange(of: state.items[index].timeIndex, { state.calcTotal() })
  125. }.listRowBackground(Color.chart)
  126. }
  127. .padding(.top)
  128. .scrollContentBackground(.hidden).background(color)
  129. .navigationTitle("Set Rate")
  130. .navigationBarTitleDisplayMode(.automatic)
  131. }
  132. private var list: some View {
  133. List {
  134. ForEach(state.items.indexed(), id: \.1.id) { index, item in
  135. NavigationLink(destination: pickers(for: index)) {
  136. HStack {
  137. Text("Rate").foregroundColor(.secondary)
  138. Text(
  139. "\(rateFormatter.string(from: state.rateValues[item.rateIndex] as NSNumber) ?? "0") U/hr"
  140. )
  141. Spacer()
  142. Text("starts at").foregroundColor(.secondary)
  143. Text(
  144. "\(dateFormatter.string(from: Date(timeIntervalSince1970: state.timeValues[item.timeIndex])))"
  145. )
  146. }
  147. }
  148. .moveDisabled(true)
  149. }
  150. .onDelete(perform: onDelete)
  151. }
  152. }
  153. private var addButton: some View {
  154. guard state.canAdd else {
  155. return AnyView(EmptyView())
  156. }
  157. switch editMode {
  158. case .inactive:
  159. return AnyView(Button(action: onAdd) { Image(systemName: "plus") })
  160. default:
  161. return AnyView(EmptyView())
  162. }
  163. }
  164. func onAdd() {
  165. state.add()
  166. }
  167. private func onDelete(offsets: IndexSet) {
  168. state.items.remove(atOffsets: offsets)
  169. state.validate()
  170. state.calcTotal()
  171. }
  172. }
  173. }