TargetsEditorRootView.swift 5.4 KB

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