DataTableRootView.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import CoreData
  2. import SwiftUI
  3. import Swinject
  4. extension DataTable {
  5. struct RootView: BaseView {
  6. let resolver: Resolver
  7. @StateObject var state = StateModel()
  8. @State private var isRemoveCarbsAlertPresented = false
  9. @State private var removeCarbsAlert: Alert?
  10. @State private var isRemoveInsulinAlertPresented = false
  11. @State private var removeInsulinAlert: Alert?
  12. @State private var newGlucose = false
  13. @Environment(\.colorScheme) var colorScheme
  14. private var glucoseFormatter: NumberFormatter {
  15. let formatter = NumberFormatter()
  16. formatter.numberStyle = .decimal
  17. formatter.maximumFractionDigits = 0
  18. if state.units == .mmolL {
  19. formatter.minimumFractionDigits = 1
  20. formatter.maximumFractionDigits = 1
  21. }
  22. formatter.roundingMode = .halfUp
  23. return formatter
  24. }
  25. private var dateFormatter: DateFormatter {
  26. let formatter = DateFormatter()
  27. formatter.timeStyle = .short
  28. return formatter
  29. }
  30. var body: some View {
  31. VStack {
  32. Picker("Mode", selection: $state.mode) {
  33. ForEach(Mode.allCases.indexed(), id: \.1) { index, item in
  34. Text(item.name).tag(index)
  35. }
  36. }
  37. .pickerStyle(SegmentedPickerStyle())
  38. .padding(.horizontal)
  39. Form {
  40. switch state.mode {
  41. case .treatments: treatmentsList
  42. case .glucose: glucoseList
  43. }
  44. }
  45. }
  46. .onAppear(perform: configureView)
  47. .navigationTitle("History")
  48. .navigationBarTitleDisplayMode(.automatic)
  49. .navigationBarItems(
  50. leading: Button("Close", action: state.hideModal),
  51. trailing: state.mode == .glucose ? EditButton().asAny() : EmptyView().asAny()
  52. )
  53. .popup(isPresented: newGlucose, alignment: .top, direction: .bottom) {
  54. VStack(spacing: 20) {
  55. HStack {
  56. Text("New Glucose")
  57. DecimalTextField(" ... ", value: $state.manualGlcuose, formatter: glucoseFormatter)
  58. Text(state.units.rawValue)
  59. }.padding(.horizontal, 20)
  60. HStack {
  61. let limitLow: Decimal = state.units == .mmolL ? 2.2 : 40
  62. let limitHigh: Decimal = state.units == .mmolL ? 21 : 380
  63. Button { newGlucose = false }
  64. label: { Text("Cancel") }.frame(maxWidth: .infinity, alignment: .leading)
  65. Button {
  66. state.addManualGlucose()
  67. newGlucose = false
  68. }
  69. label: { Text("Save") }
  70. .frame(maxWidth: .infinity, alignment: .trailing)
  71. .disabled(state.manualGlcuose < limitLow || state.manualGlcuose > limitHigh)
  72. }.padding(20)
  73. }
  74. .frame(maxHeight: 140)
  75. .background(
  76. RoundedRectangle(cornerRadius: 8, style: .continuous)
  77. .fill(Color(colorScheme == .dark ? UIColor.systemGray2 : UIColor.systemGray6))
  78. )
  79. }
  80. }
  81. private var treatmentsList: some View {
  82. List {
  83. ForEach(state.treatments) { item in
  84. treatmentView(item)
  85. }
  86. }
  87. }
  88. private var glucoseList: some View {
  89. List {
  90. Button { newGlucose = true }
  91. label: { Text("Add") }.frame(maxWidth: .infinity, alignment: .trailing)
  92. .padding(.trailing, 20)
  93. ForEach(state.glucose) { item in
  94. glucoseView(item)
  95. }.onDelete(perform: deleteGlucose)
  96. }
  97. }
  98. @ViewBuilder private func treatmentView(_ item: Treatment) -> some View {
  99. HStack {
  100. Image(systemName: "circle.fill").foregroundColor(item.color)
  101. Text(dateFormatter.string(from: item.date))
  102. .moveDisabled(true)
  103. Text(item.type.name)
  104. Text(item.amountText).foregroundColor(.secondary)
  105. if let duration = item.durationText {
  106. Text(duration).foregroundColor(.secondary)
  107. }
  108. if item.type == .carbs {
  109. Spacer()
  110. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  111. .contentShape(Rectangle())
  112. .padding(.vertical)
  113. .onTapGesture {
  114. removeCarbsAlert = Alert(
  115. title: Text("Delete carbs?"),
  116. message: Text(item.amountText),
  117. primaryButton: .destructive(
  118. Text("Delete"),
  119. action: { state.deleteCarbs(item) }
  120. ),
  121. secondaryButton: .cancel()
  122. )
  123. isRemoveCarbsAlertPresented = true
  124. }
  125. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  126. removeCarbsAlert!
  127. }
  128. }
  129. if item.type == .fpus {
  130. Spacer()
  131. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  132. .contentShape(Rectangle())
  133. .padding(.vertical)
  134. .onTapGesture {
  135. removeCarbsAlert = Alert(
  136. title: Text("Delete carb equivalents?"),
  137. message: Text(""), // Temporary fix. New to fix real amount of carb equivalents later
  138. primaryButton: .destructive(
  139. Text("Delete"),
  140. action: { state.deleteCarbs(item) }
  141. ),
  142. secondaryButton: .cancel()
  143. )
  144. isRemoveCarbsAlertPresented = true
  145. }
  146. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  147. removeCarbsAlert!
  148. }
  149. }
  150. if item.type == .bolus {
  151. Spacer()
  152. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  153. .contentShape(Rectangle())
  154. .padding(.vertical)
  155. .onTapGesture {
  156. removeInsulinAlert = Alert(
  157. title: Text("Delete insulin?"),
  158. message: Text(item.amountText),
  159. primaryButton: .destructive(
  160. Text("Delete"),
  161. action: { state.deleteInsulin(item) }
  162. ),
  163. secondaryButton: .cancel()
  164. )
  165. isRemoveInsulinAlertPresented = true
  166. }
  167. .alert(isPresented: $isRemoveInsulinAlertPresented) {
  168. removeInsulinAlert!
  169. }
  170. }
  171. }
  172. }
  173. @ViewBuilder private func glucoseView(_ item: Glucose) -> some View {
  174. VStack(alignment: .leading, spacing: 4) {
  175. HStack {
  176. Text(dateFormatter.string(from: item.glucose.dateString))
  177. Spacer()
  178. Text(item.glucose.glucose.map {
  179. glucoseFormatter.string(from: Double(
  180. state.units == .mmolL ? $0.asMmolL : Decimal($0)
  181. ) as NSNumber)!
  182. } ?? "--")
  183. Text(state.units.rawValue)
  184. Text(item.glucose.direction?.symbol ?? "--")
  185. }
  186. Text("ID: " + item.glucose.id).font(.caption2).foregroundColor(.secondary)
  187. }
  188. }
  189. private func deleteGlucose(at offsets: IndexSet) {
  190. state.deleteGlucose(at: offsets[offsets.startIndex])
  191. }
  192. }
  193. }