DataTableRootView.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 ? 4.0 : 0)
  50. .navigationBarTitleDisplayMode(.inline)
  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. HStack {
  66. Text("Time").foregroundStyle(.secondary)
  67. Spacer()
  68. Text(state.units.rawValue).foregroundStyle(.secondary)
  69. Button {
  70. newGlucose = true
  71. isFocused = true
  72. isLayered.toggle()
  73. }
  74. label: { Image(systemName: "plus.circle.fill").foregroundStyle(.secondary) }
  75. .buttonStyle(.borderless)
  76. }
  77. ForEach(state.glucose) { item in
  78. glucoseView(item, isManual: item.glucose)
  79. }.onDelete(perform: deleteGlucose)
  80. }
  81. }
  82. private var addGlucose: some View {
  83. VStack {
  84. Form {
  85. Section {
  86. HStack {
  87. Text("Glucose").font(.custom("popup", fixedSize: 18))
  88. DecimalTextField(" ... ", value: $state.manualGlcuose, formatter: glucoseFormatter)
  89. .focused($isFocused).font(.custom("glucose", fixedSize: 22))
  90. Text(state.units.rawValue).foregroundStyle(.secondary)
  91. }
  92. }
  93. header: {
  94. Text("Blood Glucose Test").foregroundColor(.secondary).font(.custom("popupHeader", fixedSize: 12))
  95. .padding(.top)
  96. }
  97. HStack {
  98. Button {
  99. newGlucose = false
  100. isLayered = false
  101. }
  102. label: { Text("Cancel").foregroundColor(.red) }
  103. .frame(maxWidth: .infinity, alignment: .leading)
  104. Spacer()
  105. Button {
  106. state.addManualGlucose()
  107. newGlucose = false
  108. isLayered = false
  109. }
  110. label: { Text("Save") }
  111. .frame(maxWidth: .infinity, alignment: .trailing)
  112. .disabled(state.manualGlcuose <= 0)
  113. }
  114. .buttonStyle(BorderlessButtonStyle())
  115. .font(.custom("popupButtons", fixedSize: 16))
  116. }
  117. }
  118. .frame(minHeight: 220, maxHeight: 260).cornerRadius(20)
  119. .background(
  120. RoundedRectangle(cornerRadius: 20, style: .continuous)
  121. .fill(Color(.tertiarySystemBackground))
  122. ).shadow(radius: 40)
  123. }
  124. @ViewBuilder private func treatmentView(_ item: Treatment) -> some View {
  125. HStack {
  126. Image(systemName: "circle.fill").foregroundColor(item.color)
  127. Text(dateFormatter.string(from: item.date))
  128. .moveDisabled(true)
  129. Text((item.isSMB ?? false) ? "SMB" : item.type.name)
  130. Text(item.amountText).foregroundColor(.secondary)
  131. if let duration = item.durationText {
  132. Text(duration).foregroundColor(.secondary)
  133. }
  134. if item.type == .carbs {
  135. if item.note != "" {
  136. Spacer()
  137. Text(item.note ?? "").foregroundColor(.brown)
  138. }
  139. Spacer()
  140. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  141. .contentShape(Rectangle())
  142. .padding(.vertical)
  143. .onTapGesture {
  144. removeCarbsAlert = Alert(
  145. title: Text("Delete carbs?"),
  146. message: Text(item.amountText),
  147. primaryButton: .destructive(
  148. Text("Delete"),
  149. action: { state.deleteCarbs(item) }
  150. ),
  151. secondaryButton: .cancel()
  152. )
  153. isRemoveCarbsAlertPresented = true
  154. }
  155. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  156. removeCarbsAlert!
  157. }
  158. }
  159. if item.type == .fpus {
  160. Spacer()
  161. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  162. .contentShape(Rectangle())
  163. .padding(.vertical)
  164. .onTapGesture {
  165. removeCarbsAlert = Alert(
  166. title: Text("Delete carb equivalents?"),
  167. message: Text(""), // Temporary fix. New to fix real amount of carb equivalents later
  168. primaryButton: .destructive(
  169. Text("Delete"),
  170. action: { state.deleteCarbs(item) }
  171. ),
  172. secondaryButton: .cancel()
  173. )
  174. isRemoveCarbsAlertPresented = true
  175. }
  176. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  177. removeCarbsAlert!
  178. }
  179. }
  180. if item.type == .bolus {
  181. Spacer()
  182. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  183. .contentShape(Rectangle())
  184. .padding(.vertical)
  185. .onTapGesture {
  186. removeInsulinAlert = Alert(
  187. title: Text("Delete insulin?"),
  188. message: Text(item.amountText),
  189. primaryButton: .destructive(
  190. Text("Delete"),
  191. action: { state.deleteInsulin(item) }
  192. ),
  193. secondaryButton: .cancel()
  194. )
  195. isRemoveInsulinAlertPresented = true
  196. }
  197. .alert(isPresented: $isRemoveInsulinAlertPresented) {
  198. removeInsulinAlert!
  199. }
  200. }
  201. }
  202. }
  203. @ViewBuilder private func glucoseView(_ item: Glucose, isManual: BloodGlucose) -> some View {
  204. VStack(alignment: .leading, spacing: 4) {
  205. HStack {
  206. Text(dateFormatter.string(from: item.glucose.dateString))
  207. Spacer()
  208. Text(item.glucose.glucose.map {
  209. glucoseFormatter.string(from: Double(
  210. state.units == .mmolL ? $0.asMmolL : Decimal($0)
  211. ) as NSNumber)!
  212. } ?? "--")
  213. if isManual.type == GlucoseType.manual.rawValue {
  214. Image(systemName: "drop.fill").symbolRenderingMode(.monochrome).foregroundStyle(.red)
  215. } else {
  216. Text(item.glucose.direction?.symbol ?? "--")
  217. }
  218. }
  219. }
  220. }
  221. private func deleteGlucose(at offsets: IndexSet) {
  222. state.deleteGlucose(at: offsets[offsets.startIndex])
  223. }
  224. }
  225. }