TargetsEditorRootView.swift 6.6 KB

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