DataTableRootView.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. @State private var isLayered = false
  14. @FocusState private var isFocused: Bool
  15. @Environment(\.colorScheme) var colorScheme
  16. private var glucoseFormatter: NumberFormatter {
  17. let formatter = NumberFormatter()
  18. formatter.numberStyle = .decimal
  19. formatter.maximumFractionDigits = 0
  20. if state.units == .mmolL {
  21. formatter.maximumFractionDigits = 1
  22. formatter.roundingMode = .ceiling
  23. }
  24. return formatter
  25. }
  26. private var dateFormatter: DateFormatter {
  27. let formatter = DateFormatter()
  28. formatter.timeStyle = .short
  29. return formatter
  30. }
  31. var body: some View {
  32. VStack {
  33. Picker("Mode", selection: $state.mode) {
  34. ForEach(Mode.allCases.indexed(), id: \.1) { index, item in
  35. Text(item.name).tag(index)
  36. }
  37. }
  38. .pickerStyle(SegmentedPickerStyle())
  39. .padding(.horizontal)
  40. Form {
  41. switch state.mode {
  42. case .treatments: treatmentsList
  43. case .glucose: glucoseList
  44. }
  45. }
  46. }
  47. .onAppear(perform: configureView)
  48. .navigationTitle(isLayered ? "" : "History")
  49. .blur(radius: isLayered ? 3.0 : 0)
  50. .navigationBarTitleDisplayMode(.automatic)
  51. .navigationBarItems(leading: Button(isLayered ? "" : "Close", action: state.hideModal))
  52. .popup(isPresented: newGlucose, alignment: .center, direction: .top) {
  53. addGlucose
  54. }
  55. }
  56. private var treatmentsList: some View {
  57. List {
  58. ForEach(state.treatments) { item in
  59. treatmentView(item)
  60. }
  61. }
  62. }
  63. private var glucoseList: some View {
  64. List {
  65. Button {
  66. newGlucose = true
  67. isFocused = true
  68. isLayered.toggle()
  69. }
  70. label: { Text("Add") }.frame(maxWidth: .infinity, alignment: .trailing)
  71. .padding(.trailing, 20)
  72. ForEach(state.glucose) { item in
  73. glucoseView(item, isManual: item.glucose)
  74. }.onDelete(perform: deleteGlucose)
  75. }
  76. }
  77. private var addGlucose: some View {
  78. VStack {
  79. Form {
  80. Section {
  81. HStack {
  82. Text("Glucose").font(.custom("popup", fixedSize: 18))
  83. DecimalTextField(" ... ", value: $state.manualGlcuose, formatter: glucoseFormatter)
  84. .focused($isFocused).font(.custom("glucose", fixedSize: 22))
  85. Text(state.units.rawValue).foregroundStyle(.secondary)
  86. }
  87. }
  88. header: {
  89. Text("Blood Glucose Test").foregroundColor(.secondary).font(.custom("popupHeader", fixedSize: 12))
  90. .padding(.top)
  91. }
  92. HStack {
  93. Button {
  94. newGlucose = false
  95. isLayered = false
  96. }
  97. label: { Text("Cancel").foregroundColor(.red) }
  98. .frame(maxWidth: .infinity, alignment: .leading)
  99. Spacer()
  100. Button {
  101. state.addManualGlucose()
  102. newGlucose = false
  103. isLayered = false
  104. }
  105. label: { Text("Save") }
  106. .frame(maxWidth: .infinity, alignment: .trailing)
  107. .disabled(state.manualGlcuose <= 0)
  108. }
  109. .buttonStyle(BorderlessButtonStyle())
  110. .font(.custom("popupButtons", fixedSize: 16))
  111. }
  112. }
  113. .frame(maxHeight: 220)
  114. .background(
  115. RoundedRectangle(cornerRadius: 8, style: .continuous)
  116. .fill(Color(.tertiarySystemBackground))
  117. ).border(.gray).shadow(radius: 40)
  118. }
  119. @ViewBuilder private func treatmentView(_ item: Treatment) -> some View {
  120. HStack {
  121. Image(systemName: "circle.fill").foregroundColor(item.color)
  122. Text(dateFormatter.string(from: item.date))
  123. .moveDisabled(true)
  124. Text((item.isSMB ?? false) ? "SMB" : item.type.name)
  125. Text(item.amountText).foregroundColor(.secondary)
  126. if let duration = item.durationText {
  127. Text(duration).foregroundColor(.secondary)
  128. }
  129. if item.type == .carbs {
  130. if item.note != "" {
  131. Spacer()
  132. Text(item.note ?? "").foregroundColor(.brown)
  133. }
  134. Spacer()
  135. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  136. .contentShape(Rectangle())
  137. .padding(.vertical)
  138. .onTapGesture {
  139. removeCarbsAlert = Alert(
  140. title: Text("Delete carbs?"),
  141. message: Text(item.amountText),
  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 == .fpus {
  155. Spacer()
  156. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  157. .contentShape(Rectangle())
  158. .padding(.vertical)
  159. .onTapGesture {
  160. removeCarbsAlert = Alert(
  161. title: Text("Delete carb equivalents?"),
  162. message: Text(""), // Temporary fix. New to fix real amount of carb equivalents later
  163. primaryButton: .destructive(
  164. Text("Delete"),
  165. action: { state.deleteCarbs(item) }
  166. ),
  167. secondaryButton: .cancel()
  168. )
  169. isRemoveCarbsAlertPresented = true
  170. }
  171. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  172. removeCarbsAlert!
  173. }
  174. }
  175. if item.type == .bolus {
  176. Spacer()
  177. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  178. .contentShape(Rectangle())
  179. .padding(.vertical)
  180. .onTapGesture {
  181. removeInsulinAlert = Alert(
  182. title: Text("Delete insulin?"),
  183. message: Text(item.amountText),
  184. primaryButton: .destructive(
  185. Text("Delete"),
  186. action: { state.deleteInsulin(item) }
  187. ),
  188. secondaryButton: .cancel()
  189. )
  190. isRemoveInsulinAlertPresented = true
  191. }
  192. .alert(isPresented: $isRemoveInsulinAlertPresented) {
  193. removeInsulinAlert!
  194. }
  195. }
  196. }
  197. }
  198. @ViewBuilder private func glucoseView(_ item: Glucose, isManual: BloodGlucose) -> some View {
  199. VStack(alignment: .leading, spacing: 4) {
  200. HStack {
  201. Text(dateFormatter.string(from: item.glucose.dateString))
  202. Spacer()
  203. Text(item.glucose.glucose.map {
  204. glucoseFormatter.string(from: Double(
  205. state.units == .mmolL ? $0.asMmolL : Decimal($0)
  206. ) as NSNumber)!
  207. } ?? "--")
  208. Text(state.units.rawValue)
  209. if isManual.type == GlucoseType.manual.rawValue {
  210. Image(systemName: "drop.fill").symbolRenderingMode(.monochrome).foregroundStyle(.red)
  211. } else {
  212. Text(item.glucose.direction?.symbol ?? "--")
  213. }
  214. }
  215. }
  216. }
  217. private func deleteGlucose(at offsets: IndexSet) {
  218. state.deleteGlucose(at: offsets[offsets.startIndex])
  219. }
  220. }
  221. }