DataTableRootView.swift 9.7 KB

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