TargetsEditorRootView.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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(from: state.rateValues[i] as NSNumber) ?? ""
  83. )
  84. + " \(state.units.rawValue)"
  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. "\(rateFormatter.string(from: state.rateValues[item.lowIndex] as NSNumber) ?? "0")"
  115. )
  116. Text("\(state.units.rawValue)").foregroundColor(.secondary)
  117. Spacer()
  118. Text("starts at").foregroundColor(.secondary)
  119. Text(
  120. "\(dateFormatter.string(from: Date(timeIntervalSince1970: state.timeValues[item.timeIndex])))"
  121. )
  122. }
  123. }
  124. .moveDisabled(true)
  125. }
  126. .onDelete(perform: onDelete)
  127. }
  128. }
  129. private var addButton: some View {
  130. guard state.canAdd else {
  131. return AnyView(EmptyView())
  132. }
  133. switch editMode {
  134. case .inactive:
  135. return AnyView(Button(action: onAdd) { Image(systemName: "plus") })
  136. default:
  137. return AnyView(EmptyView())
  138. }
  139. }
  140. func onAdd() {
  141. state.add()
  142. }
  143. private func onDelete(offsets: IndexSet) {
  144. state.items.remove(atOffsets: offsets)
  145. state.validate()
  146. }
  147. }
  148. }