TargetsEditorRootView.swift 6.0 KB

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