BasalProfileEditorRootView.swift 6.8 KB

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