DataTableRootView.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. Button { newGlucose = false }
  62. label: { Text("Cancel") }.frame(maxWidth: .infinity, alignment: .leading)
  63. Button {
  64. state.addManualGlucose()
  65. newGlucose = false
  66. }
  67. label: { Text("Save") }
  68. .frame(maxWidth: .infinity, alignment: .trailing)
  69. .disabled(state.manualGlcuose < 2.2 || state.manualGlcuose > 22)
  70. }.padding(20)
  71. }
  72. .frame(maxHeight: 140)
  73. .background(
  74. RoundedRectangle(cornerRadius: 8, style: .continuous)
  75. .fill(Color(colorScheme == .dark ? UIColor.systemGray2 : UIColor.systemGray6))
  76. )
  77. }
  78. }
  79. private var treatmentsList: some View {
  80. List {
  81. ForEach(state.treatments) { item in
  82. treatmentView(item)
  83. }
  84. }
  85. }
  86. private var glucoseList: some View {
  87. List {
  88. Button { newGlucose = true }
  89. label: { Text("Add") }.frame(maxWidth: .infinity, alignment: .trailing)
  90. .padding(.trailing, 20)
  91. ForEach(state.glucose) { item in
  92. glucoseView(item)
  93. }.onDelete(perform: deleteGlucose)
  94. }
  95. }
  96. @ViewBuilder private func treatmentView(_ item: Treatment) -> some View {
  97. HStack {
  98. Image(systemName: "circle.fill").foregroundColor(item.color)
  99. Text(dateFormatter.string(from: item.date))
  100. .moveDisabled(true)
  101. Text(item.type.name)
  102. Text(item.amountText).foregroundColor(.secondary)
  103. if let duration = item.durationText {
  104. Text(duration).foregroundColor(.secondary)
  105. }
  106. if item.type == .carbs {
  107. Spacer()
  108. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  109. .contentShape(Rectangle())
  110. .padding(.vertical)
  111. .onTapGesture {
  112. removeCarbsAlert = Alert(
  113. title: Text("Delete carbs?"),
  114. message: Text(item.amountText),
  115. primaryButton: .destructive(
  116. Text("Delete"),
  117. action: { state.deleteCarbs(item) }
  118. ),
  119. secondaryButton: .cancel()
  120. )
  121. isRemoveCarbsAlertPresented = true
  122. }
  123. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  124. removeCarbsAlert!
  125. }
  126. }
  127. if item.type == .fpus {
  128. Spacer()
  129. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  130. .contentShape(Rectangle())
  131. .padding(.vertical)
  132. .onTapGesture {
  133. removeCarbsAlert = Alert(
  134. title: Text("Delete carb equivalents?"),
  135. message: Text(""), // Temporary fix. New to fix real amount of carb equivalents later
  136. primaryButton: .destructive(
  137. Text("Delete"),
  138. action: { state.deleteCarbs(item) }
  139. ),
  140. secondaryButton: .cancel()
  141. )
  142. isRemoveCarbsAlertPresented = true
  143. }
  144. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  145. removeCarbsAlert!
  146. }
  147. }
  148. if item.type == .bolus {
  149. Spacer()
  150. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  151. .contentShape(Rectangle())
  152. .padding(.vertical)
  153. .onTapGesture {
  154. removeInsulinAlert = Alert(
  155. title: Text("Delete insulin?"),
  156. message: Text(item.amountText),
  157. primaryButton: .destructive(
  158. Text("Delete"),
  159. action: { state.deleteInsulin(item) }
  160. ),
  161. secondaryButton: .cancel()
  162. )
  163. isRemoveInsulinAlertPresented = true
  164. }
  165. .alert(isPresented: $isRemoveInsulinAlertPresented) {
  166. removeInsulinAlert!
  167. }
  168. }
  169. }
  170. }
  171. @ViewBuilder private func glucoseView(_ item: Glucose) -> some View {
  172. VStack(alignment: .leading, spacing: 4) {
  173. HStack {
  174. Text(dateFormatter.string(from: item.glucose.dateString))
  175. Spacer()
  176. Text(item.glucose.glucose.map {
  177. glucoseFormatter.string(from: Double(
  178. state.units == .mmolL ? $0.asMmolL : Decimal($0)
  179. ) as NSNumber)!
  180. } ?? "--")
  181. Text(state.units.rawValue)
  182. Text(item.glucose.direction?.symbol ?? "--")
  183. }
  184. Text("ID: " + item.glucose.id).font(.caption2).foregroundColor(.secondary)
  185. }
  186. }
  187. private func deleteGlucose(at offsets: IndexSet) {
  188. state.deleteGlucose(at: offsets[offsets.startIndex])
  189. }
  190. }
  191. }