CREditorRootView.swift 6.1 KB

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