CarbRatioEditorRootView.swift 6.1 KB

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