TargetsEditorRootView.swift 6.8 KB

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