CarbRatioEditorRootView.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import SwiftUI
  2. import Swinject
  3. extension CarbRatioEditor {
  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 saveButton: some View {
  37. ZStack {
  38. let shouldDisableButton = state.shouldDisplaySaving || state.items.isEmpty || !state.hasChanges
  39. Rectangle()
  40. .frame(width: UIScreen.main.bounds.width, height: 65)
  41. .foregroundStyle(colorScheme == .dark ? Color.bgDarkerDarkBlue : Color.white)
  42. .background(.thinMaterial)
  43. .opacity(0.8)
  44. .clipShape(Rectangle())
  45. Group {
  46. HStack {
  47. HStack {
  48. Button {
  49. let impactHeavy = UIImpactFeedbackGenerator(style: .heavy)
  50. impactHeavy.impactOccurred()
  51. state.save()
  52. // deactivate saving display after 1.25 seconds
  53. DispatchQueue.main.asyncAfter(deadline: .now() + 1.25) {
  54. state.shouldDisplaySaving = false
  55. }
  56. } label: {
  57. HStack {
  58. if state.shouldDisplaySaving {
  59. ProgressView().padding(.trailing, 10)
  60. }
  61. Text(state.shouldDisplaySaving ? "Saving..." : "Save")
  62. }.padding(10)
  63. }
  64. }
  65. .frame(width: UIScreen.main.bounds.width * 0.9, alignment: .center)
  66. .disabled(shouldDisableButton)
  67. .background(shouldDisableButton ? Color(.systemGray4) : Color(.systemBlue))
  68. .tint(.white)
  69. .clipShape(RoundedRectangle(cornerRadius: 8))
  70. }
  71. }.padding(5)
  72. }
  73. }
  74. var body: some View {
  75. Form {
  76. if let autotune = state.autotune, !state.settingsManager.settings.onlyAutotuneBasals {
  77. Section(header: Text("Autotune")) {
  78. HStack {
  79. Text("Calculated Ratio")
  80. Spacer()
  81. Text(rateFormatter.string(from: autotune.carbRatio as NSNumber) ?? "0")
  82. Text("g/U").foregroundColor(.secondary)
  83. }
  84. }.listRowBackground(Color.chart)
  85. }
  86. Section(header: Text("Schedule")) {
  87. list
  88. }.listRowBackground(Color.chart)
  89. }
  90. .safeAreaInset(edge: .bottom, spacing: 30) { saveButton }
  91. .scrollContentBackground(.hidden).background(color)
  92. .onAppear(perform: configureView)
  93. .navigationTitle("Carb Ratios")
  94. .navigationBarTitleDisplayMode(.automatic)
  95. .toolbar(content: {
  96. ToolbarItem(placement: .topBarTrailing) {
  97. EditButton()
  98. }
  99. ToolbarItem(placement: .topBarTrailing) {
  100. addButton
  101. }
  102. })
  103. .environment(\.editMode, $editMode)
  104. .onAppear {
  105. state.validate()
  106. }
  107. }
  108. private func pickers(for index: Int) -> some View {
  109. Form {
  110. Section {
  111. Picker(selection: $state.items[index].rateIndex, label: Text("Ratio")) {
  112. ForEach(0 ..< state.rateValues.count, id: \.self) { i in
  113. Text(
  114. (
  115. self.rateFormatter
  116. .string(from: state.rateValues[i] as NSNumber) ?? ""
  117. ) + " g/U"
  118. ).tag(i)
  119. }
  120. }
  121. }.listRowBackground(Color.chart)
  122. Section {
  123. Picker(selection: $state.items[index].timeIndex, label: Text("Time")) {
  124. ForEach(0 ..< state.timeValues.count, id: \.self) { i in
  125. Text(
  126. self.dateFormatter
  127. .string(from: Date(
  128. timeIntervalSince1970: state
  129. .timeValues[i]
  130. ))
  131. ).tag(i)
  132. }
  133. }
  134. }.listRowBackground(Color.chart)
  135. }
  136. .padding(.top)
  137. .scrollContentBackground(.hidden).background(color)
  138. .navigationTitle("Set Ratio")
  139. .navigationBarTitleDisplayMode(.automatic)
  140. }
  141. private var list: some View {
  142. List {
  143. ForEach(state.items.indexed(), id: \.1.id) { index, item in
  144. NavigationLink(destination: pickers(for: index)) {
  145. HStack {
  146. Text("Ratio").foregroundColor(.secondary)
  147. Text(
  148. "\(rateFormatter.string(from: state.rateValues[item.rateIndex] as NSNumber) ?? "0") g/U"
  149. )
  150. Spacer()
  151. Text("starts at").foregroundColor(.secondary)
  152. Text(
  153. "\(dateFormatter.string(from: Date(timeIntervalSince1970: state.timeValues[item.timeIndex])))"
  154. )
  155. }
  156. }
  157. .moveDisabled(true)
  158. }
  159. .onDelete(perform: onDelete)
  160. }
  161. }
  162. private var addButton: some View {
  163. guard state.canAdd else {
  164. return AnyView(EmptyView())
  165. }
  166. switch editMode {
  167. case .inactive:
  168. return AnyView(Button(action: onAdd) { Image(systemName: "plus") })
  169. default:
  170. return AnyView(EmptyView())
  171. }
  172. }
  173. func onAdd() {
  174. state.add()
  175. }
  176. private func onDelete(offsets: IndexSet) {
  177. state.items.remove(atOffsets: offsets)
  178. state.validate()
  179. }
  180. }
  181. }