CarbRatioEditorRootView.swift 7.4 KB

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