BasalProfileEditorRootView.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. }.listRowBackground(Color.chart)
  41. Section {
  42. HStack {
  43. Text("Total")
  44. .bold()
  45. .foregroundColor(.primary)
  46. Spacer()
  47. Text(rateFormatter.string(from: state.total as NSNumber) ?? "0")
  48. .foregroundColor(.primary) +
  49. Text(" U/day")
  50. .foregroundColor(.secondary)
  51. }
  52. }.listRowBackground(Color.chart)
  53. Section {
  54. HStack {
  55. if state.syncInProgress {
  56. ProgressView().padding(.trailing, 10)
  57. }
  58. Button {
  59. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  60. impactHeavy.impactOccurred()
  61. state.save()
  62. } label: {
  63. Text(state.syncInProgress ? "Saving..." : "Save")
  64. }
  65. .disabled(state.syncInProgress || state.items.isEmpty)
  66. .frame(maxWidth: .infinity, alignment: .center)
  67. .tint(.white)
  68. }
  69. }.listRowBackground(state.syncInProgress || state.items.isEmpty ? Color(.systemGray4) : Color(.systemBlue))
  70. }
  71. .scrollContentBackground(.hidden).background(color)
  72. .onAppear(perform: configureView)
  73. .navigationTitle("Basal Profile")
  74. .navigationBarTitleDisplayMode(.automatic)
  75. .toolbar(content: {
  76. ToolbarItem(placement: .topBarTrailing) {
  77. EditButton()
  78. }
  79. ToolbarItem(placement: .topBarTrailing) {
  80. addButton
  81. }
  82. })
  83. .environment(\.editMode, $editMode)
  84. .onAppear {
  85. state.validate()
  86. }
  87. }
  88. private func pickers(for index: Int) -> some View {
  89. Form {
  90. Section {
  91. Picker(selection: $state.items[index].rateIndex, label: Text("Rate")) {
  92. ForEach(0 ..< state.rateValues.count, id: \.self) { i in
  93. Text(
  94. (
  95. self.rateFormatter
  96. .string(from: state.rateValues[i] as NSNumber) ?? ""
  97. ) + " U/hr"
  98. ).tag(i)
  99. }
  100. }
  101. .onChange(of: state.items[index].rateIndex, perform: { _ in state.calcTotal() })
  102. }.listRowBackground(Color.chart)
  103. Section {
  104. Picker(selection: $state.items[index].timeIndex, label: Text("Text")) {
  105. ForEach(0 ..< state.timeValues.count, id: \.self) { i in
  106. Text(
  107. self.dateFormatter
  108. .string(from: Date(
  109. timeIntervalSince1970: state
  110. .timeValues[i]
  111. ))
  112. ).tag(i)
  113. }
  114. }
  115. .onChange(of: state.items[index].timeIndex, perform: { _ in state.calcTotal() })
  116. }.listRowBackground(Color.chart)
  117. }
  118. .padding(.top)
  119. .scrollContentBackground(.hidden).background(color)
  120. .navigationTitle("Set Rate")
  121. .navigationBarTitleDisplayMode(.automatic)
  122. }
  123. private var list: some View {
  124. List {
  125. ForEach(state.items.indexed(), id: \.1.id) { index, item in
  126. NavigationLink(destination: pickers(for: index)) {
  127. HStack {
  128. Text("Rate").foregroundColor(.secondary)
  129. Text(
  130. "\(rateFormatter.string(from: state.rateValues[item.rateIndex] as NSNumber) ?? "0") U/hr"
  131. )
  132. Spacer()
  133. Text("starts at").foregroundColor(.secondary)
  134. Text(
  135. "\(dateFormatter.string(from: Date(timeIntervalSince1970: state.timeValues[item.timeIndex])))"
  136. )
  137. }
  138. }
  139. .moveDisabled(true)
  140. }
  141. .onDelete(perform: onDelete)
  142. }
  143. }
  144. private var addButton: some View {
  145. guard state.canAdd else {
  146. return AnyView(EmptyView())
  147. }
  148. switch editMode {
  149. case .inactive:
  150. return AnyView(Button(action: onAdd) { Image(systemName: "plus") })
  151. default:
  152. return AnyView(EmptyView())
  153. }
  154. }
  155. func onAdd() {
  156. state.add()
  157. }
  158. private func onDelete(offsets: IndexSet) {
  159. state.items.remove(atOffsets: offsets)
  160. state.validate()
  161. state.calcTotal()
  162. }
  163. }
  164. }