CarbRatioEditorRootView.swift 6.7 KB

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