BasalProfileEditorRootView.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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) { _ in
  80. state.calcTotal()
  81. }
  82. .scrollContentBackground(.hidden).background(color)
  83. .onAppear(perform: configureView)
  84. .navigationTitle("Basal Profile")
  85. .navigationBarTitleDisplayMode(.automatic)
  86. .toolbar(content: {
  87. ToolbarItem(placement: .topBarTrailing) {
  88. EditButton()
  89. }
  90. ToolbarItem(placement: .topBarTrailing) {
  91. addButton
  92. }
  93. })
  94. .environment(\.editMode, $editMode)
  95. .onAppear {
  96. state.validate()
  97. }
  98. }
  99. private func pickers(for index: Int) -> some View {
  100. Form {
  101. Section {
  102. Picker(selection: $state.items[index].rateIndex, label: Text("Rate")) {
  103. ForEach(0 ..< state.rateValues.count, id: \.self) { i in
  104. Text(
  105. (
  106. self.rateFormatter
  107. .string(from: state.rateValues[i] as NSNumber) ?? ""
  108. ) + " U/hr"
  109. ).tag(i)
  110. }
  111. }
  112. .onChange(of: state.items[index].rateIndex, perform: { _ in state.calcTotal() })
  113. }.listRowBackground(Color.chart)
  114. Section {
  115. Picker(selection: $state.items[index].timeIndex, label: Text("Time")) {
  116. ForEach(0 ..< state.timeValues.count, id: \.self) { i in
  117. Text(
  118. self.dateFormatter
  119. .string(from: Date(
  120. timeIntervalSince1970: state
  121. .timeValues[i]
  122. ))
  123. ).tag(i)
  124. }
  125. }
  126. .onChange(of: state.items[index].timeIndex, perform: { _ in state.calcTotal() })
  127. }.listRowBackground(Color.chart)
  128. }
  129. .padding(.top)
  130. .scrollContentBackground(.hidden).background(color)
  131. .navigationTitle("Set Rate")
  132. .navigationBarTitleDisplayMode(.automatic)
  133. }
  134. private var list: some View {
  135. List {
  136. ForEach(state.items.indexed(), id: \.1.id) { index, item in
  137. NavigationLink(destination: pickers(for: index)) {
  138. HStack {
  139. Text("Rate").foregroundColor(.secondary)
  140. Text(
  141. "\(rateFormatter.string(from: state.rateValues[item.rateIndex] as NSNumber) ?? "0") U/hr"
  142. )
  143. Spacer()
  144. Text("starts at").foregroundColor(.secondary)
  145. Text(
  146. "\(dateFormatter.string(from: Date(timeIntervalSince1970: state.timeValues[item.timeIndex])))"
  147. )
  148. }
  149. }
  150. .moveDisabled(true)
  151. }
  152. .onDelete(perform: onDelete)
  153. }
  154. }
  155. private var addButton: some View {
  156. guard state.canAdd else {
  157. return AnyView(EmptyView())
  158. }
  159. switch editMode {
  160. case .inactive:
  161. return AnyView(Button(action: onAdd) { Image(systemName: "plus") })
  162. default:
  163. return AnyView(EmptyView())
  164. }
  165. }
  166. func onAdd() {
  167. state.add()
  168. }
  169. private func onDelete(offsets: IndexSet) {
  170. state.items.remove(atOffsets: offsets)
  171. state.validate()
  172. state.calcTotal()
  173. }
  174. }
  175. }