DataTableRootView.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. if item.note != "" {
  110. Spacer()
  111. Text(item.note ?? "").foregroundColor(.brown)
  112. }
  113. Spacer()
  114. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  115. .contentShape(Rectangle())
  116. .padding(.vertical)
  117. .onTapGesture {
  118. removeCarbsAlert = Alert(
  119. title: Text("Delete carbs?"),
  120. message: Text(item.amountText),
  121. primaryButton: .destructive(
  122. Text("Delete"),
  123. action: { state.deleteCarbs(item) }
  124. ),
  125. secondaryButton: .cancel()
  126. )
  127. isRemoveCarbsAlertPresented = true
  128. }
  129. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  130. removeCarbsAlert!
  131. }
  132. }
  133. if item.type == .fpus {
  134. Spacer()
  135. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  136. .contentShape(Rectangle())
  137. .padding(.vertical)
  138. .onTapGesture {
  139. removeCarbsAlert = Alert(
  140. title: Text("Delete carb equivalents?"),
  141. message: Text(""), // Temporary fix. New to fix real amount of carb equivalents later
  142. primaryButton: .destructive(
  143. Text("Delete"),
  144. action: { state.deleteCarbs(item) }
  145. ),
  146. secondaryButton: .cancel()
  147. )
  148. isRemoveCarbsAlertPresented = true
  149. }
  150. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  151. removeCarbsAlert!
  152. }
  153. }
  154. if item.type == .bolus {
  155. Spacer()
  156. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  157. .contentShape(Rectangle())
  158. .padding(.vertical)
  159. .onTapGesture {
  160. removeInsulinAlert = Alert(
  161. title: Text("Delete insulin?"),
  162. message: Text(item.amountText),
  163. primaryButton: .destructive(
  164. Text("Delete"),
  165. action: { state.deleteInsulin(item) }
  166. ),
  167. secondaryButton: .cancel()
  168. )
  169. isRemoveInsulinAlertPresented = true
  170. }
  171. .alert(isPresented: $isRemoveInsulinAlertPresented) {
  172. removeInsulinAlert!
  173. }
  174. }
  175. }
  176. }
  177. @ViewBuilder private func glucoseView(_ item: Glucose) -> some View {
  178. VStack(alignment: .leading, spacing: 4) {
  179. HStack {
  180. Text(dateFormatter.string(from: item.glucose.dateString))
  181. Spacer()
  182. Text(item.glucose.glucose.map {
  183. glucoseFormatter.string(from: Double(
  184. state.units == .mmolL ? $0.asMmolL : Decimal($0)
  185. ) as NSNumber)!
  186. } ?? "--")
  187. Text(state.units.rawValue)
  188. Text(item.glucose.direction?.symbol ?? "--")
  189. }
  190. Text("ID: " + item.glucose.id).font(.caption2).foregroundColor(.secondary)
  191. }
  192. }
  193. private func deleteGlucose(at offsets: IndexSet) {
  194. state.deleteGlucose(at: offsets[offsets.startIndex])
  195. }
  196. }
  197. }