TargetsEditorRootView.swift 6.0 KB

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