TargetsEditorRootView.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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("Background_1"),
  13. Color("Background_1"),
  14. Color("Background_2")
  15. // Color("Background_1")
  16. ]),
  17. startPoint: .top,
  18. endPoint: .bottom
  19. )
  20. :
  21. LinearGradient(
  22. gradient: Gradient(colors: [Color.gray.opacity(0.1)]),
  23. startPoint: .top,
  24. endPoint: .bottom
  25. )
  26. }
  27. private var dateFormatter: DateFormatter {
  28. let formatter = DateFormatter()
  29. formatter.timeZone = TimeZone(secondsFromGMT: 0)
  30. formatter.timeStyle = .short
  31. return formatter
  32. }
  33. private var rateFormatter: NumberFormatter {
  34. let formatter = NumberFormatter()
  35. formatter.numberStyle = .decimal
  36. return formatter
  37. }
  38. var body: some View {
  39. Form {
  40. Section(header: Text("Schedule")) {
  41. list
  42. addButton
  43. }
  44. Section {
  45. Button {
  46. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  47. impactHeavy.impactOccurred()
  48. state.save()
  49. }
  50. label: {
  51. Text("Save")
  52. }
  53. .disabled(state.items.isEmpty)
  54. }
  55. }
  56. .scrollContentBackground(.hidden).background(color)
  57. .onAppear(perform: configureView)
  58. .navigationTitle("Target Glucose")
  59. .navigationBarTitleDisplayMode(.automatic)
  60. .navigationBarItems(
  61. trailing: EditButton()
  62. )
  63. .environment(\.editMode, $editMode)
  64. .onAppear {
  65. state.validate()
  66. }
  67. }
  68. private func pickers(for index: Int) -> some View {
  69. GeometryReader { geometry in
  70. VStack {
  71. HStack {
  72. Text("Target").frame(width: geometry.size.width / 3)
  73. Text("Time").frame(width: geometry.size.width / 3)
  74. }
  75. HStack(spacing: 0) {
  76. Picker(selection: $state.items[index].lowIndex, label: EmptyView()) {
  77. ForEach(0 ..< state.rateValues.count, id: \.self) { i in
  78. Text(
  79. self.rateFormatter
  80. .string(from: state.rateValues[i] as NSNumber) ?? ""
  81. ).tag(i)
  82. }
  83. }
  84. .frame(maxWidth: geometry.size.width / 3)
  85. .clipped()
  86. Picker(selection: $state.items[index].timeIndex, label: EmptyView()) {
  87. ForEach(0 ..< state.timeValues.count, id: \.self) { i in
  88. Text(
  89. self.dateFormatter
  90. .string(from: Date(
  91. timeIntervalSince1970: state
  92. .timeValues[i]
  93. ))
  94. ).tag(i)
  95. }
  96. }
  97. .frame(maxWidth: geometry.size.width / 3)
  98. .clipped()
  99. }
  100. }
  101. }
  102. }
  103. private var list: some View {
  104. List {
  105. ForEach(state.items.indexed(), id: \.1.id) { index, item in
  106. NavigationLink(destination: pickers(for: index)) {
  107. HStack {
  108. Text(
  109. "\(rateFormatter.string(from: state.rateValues[item.lowIndex] as NSNumber) ?? "0")"
  110. )
  111. Text("\(state.units.rawValue)").foregroundColor(.secondary)
  112. Spacer()
  113. Text("starts at").foregroundColor(.secondary)
  114. Text(
  115. "\(dateFormatter.string(from: Date(timeIntervalSince1970: state.timeValues[item.timeIndex])))"
  116. )
  117. }
  118. }
  119. .moveDisabled(true)
  120. }
  121. .onDelete(perform: onDelete)
  122. }
  123. }
  124. private var addButton: some View {
  125. guard state.canAdd else {
  126. return AnyView(EmptyView())
  127. }
  128. switch editMode {
  129. case .inactive:
  130. return AnyView(Button(action: onAdd) { Text("Add") })
  131. default:
  132. return AnyView(EmptyView())
  133. }
  134. }
  135. func onAdd() {
  136. state.add()
  137. }
  138. private func onDelete(offsets: IndexSet) {
  139. state.items.remove(atOffsets: offsets)
  140. state.validate()
  141. }
  142. }
  143. }