DataTableRootView.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 showNonPumpInsulin: Bool = false
  13. @State private var showFutureEntries: Bool = false // default to hide future entries
  14. @State private var showManualGlucose: Bool = false
  15. @State private var isAmountUnconfirmed: Bool = true
  16. @Environment(\.colorScheme) var colorScheme
  17. private var insulinFormatter: NumberFormatter {
  18. let formatter = NumberFormatter()
  19. formatter.numberStyle = .decimal
  20. formatter.maximumFractionDigits = 2
  21. return formatter
  22. }
  23. private var glucoseFormatter: NumberFormatter {
  24. let formatter = NumberFormatter()
  25. formatter.numberStyle = .decimal
  26. formatter.maximumFractionDigits = 0
  27. if state.units == .mmolL {
  28. formatter.maximumFractionDigits = 1
  29. formatter.roundingMode = .ceiling
  30. }
  31. return formatter
  32. }
  33. private var dateFormatter: DateFormatter {
  34. let formatter = DateFormatter()
  35. formatter.timeStyle = .short
  36. return formatter
  37. }
  38. var body: some View {
  39. VStack {
  40. Picker("Mode", selection: $state.mode) {
  41. ForEach(Mode.allCases.indexed(), id: \.1) { index, item in
  42. Text(item.name).tag(index)
  43. }
  44. }
  45. .pickerStyle(SegmentedPickerStyle())
  46. .padding(.horizontal)
  47. Form {
  48. switch state.mode {
  49. case .treatments: treatmentsList
  50. case .glucose: glucoseList
  51. }
  52. }
  53. }
  54. .onAppear(perform: configureView)
  55. .navigationTitle("History")
  56. .navigationBarTitleDisplayMode(.inline)
  57. .navigationBarItems(leading: Button("Close", action: state.hideModal))
  58. .sheet(isPresented: $showManualGlucose) {
  59. addGlucoseView
  60. }
  61. .sheet(isPresented: $showNonPumpInsulin, onDismiss: { if isAmountUnconfirmed { state.nonPumpInsulinAmount = 0
  62. state.nonPumpInsulinDate = Date() } }) {
  63. addNonPumpInsulinView
  64. }
  65. }
  66. private var treatmentsList: some View {
  67. List {
  68. HStack {
  69. Button(action: { showNonPumpInsulin = true
  70. state.nonPumpInsulinDate = Date() }, label: {
  71. HStack {
  72. Image(systemName: "syringe")
  73. Text("Add")
  74. .foregroundColor(Color.secondary)
  75. .font(.caption)
  76. }.frame(maxWidth: .infinity, alignment: .leading)
  77. }).buttonStyle(.borderless)
  78. Spacer()
  79. Button(action: { showFutureEntries.toggle() }, label: {
  80. HStack {
  81. Text(showFutureEntries ? "Hide Future" : "Show Future")
  82. .foregroundColor(Color.secondary)
  83. .font(.caption)
  84. Image(systemName: showFutureEntries ? "calendar.badge.minus" : "calendar.badge.plus")
  85. }.frame(maxWidth: .infinity, alignment: .trailing)
  86. }).buttonStyle(.borderless)
  87. }.listRowBackground(
  88. Rectangle()
  89. .cornerRadius(20)
  90. .background(Color.clear)
  91. .foregroundColor(
  92. colorScheme == .dark ? Color(.systemBackground) :
  93. Color(.secondarySystemBackground)
  94. )
  95. ).listRowSeparator(.hidden, edges: .bottom)
  96. if !state.treatments.isEmpty {
  97. if !showFutureEntries {
  98. ForEach(state.treatments.filter { item in
  99. item.date <= Date()
  100. }) { item in
  101. treatmentView(item)
  102. }
  103. } else {
  104. ForEach(state.treatments) { item in
  105. treatmentView(item)
  106. }
  107. }
  108. } else {
  109. HStack {
  110. Text("No data.")
  111. }
  112. }
  113. }
  114. }
  115. private var glucoseList: some View {
  116. List {
  117. HStack {
  118. Button(
  119. action: { showManualGlucose = true
  120. state.manualGlucose = 0 },
  121. label: { Image(systemName: "plus.circle.fill").foregroundStyle(.secondary)
  122. }
  123. ).buttonStyle(.borderless)
  124. Text(state.units.rawValue).foregroundStyle(.secondary)
  125. Spacer()
  126. Text("Time").foregroundStyle(.secondary)
  127. }.listRowBackground(
  128. Rectangle()
  129. .background(Color.clear)
  130. .foregroundColor(
  131. colorScheme == .dark ? Color(.systemBackground) :
  132. Color(.secondarySystemBackground)
  133. )
  134. ).listRowSeparator(.hidden, edges: .bottom)
  135. if !state.glucose.isEmpty {
  136. ForEach(state.glucose) { item in
  137. glucoseView(item, isManual: item.glucose)
  138. }
  139. .onDelete(perform: deleteGlucose)
  140. } else {
  141. HStack {
  142. Text("No data.")
  143. }
  144. }
  145. }
  146. }
  147. var addGlucoseView: some View {
  148. NavigationView {
  149. VStack {
  150. Form {
  151. Section {
  152. HStack {
  153. Text("New Glucose")
  154. DecimalTextField(
  155. " ... ",
  156. value: $state.manualGlucose,
  157. formatter: glucoseFormatter,
  158. autofocus: true,
  159. cleanInput: true
  160. )
  161. Text(state.units.rawValue).foregroundStyle(.secondary)
  162. }
  163. }
  164. Section {
  165. HStack {
  166. let limitLow: Decimal = state.units == .mmolL ? 0.8 : 40
  167. let limitHigh: Decimal = state.units == .mgdL ? 14 : 720
  168. Button {
  169. state.addManualGlucose()
  170. isAmountUnconfirmed = false
  171. showManualGlucose = false
  172. }
  173. label: { Text("Save") }
  174. .frame(maxWidth: .infinity, alignment: .center)
  175. .disabled(state.manualGlucose < limitLow || state.manualGlucose > limitHigh)
  176. }
  177. }
  178. }
  179. }
  180. .onAppear(perform: configureView)
  181. .navigationTitle("Add Glucose")
  182. .navigationBarTitleDisplayMode(.automatic)
  183. .navigationBarItems(leading: Button("Close", action: { showManualGlucose = false }))
  184. }
  185. }
  186. @ViewBuilder private func treatmentView(_ item: Treatment) -> some View {
  187. HStack {
  188. Image(systemName: "circle.fill").foregroundColor(item.color)
  189. Text((item.isSMB ?? false) ? "SMB" : item.type.name)
  190. Text(item.amountText).foregroundColor(.secondary)
  191. if let duration = item.durationText {
  192. Text(duration).foregroundColor(.secondary)
  193. }
  194. if item.type == .carbs {
  195. if item.note != "" {
  196. Spacer()
  197. Text(item.note ?? "").foregroundColor(.brown)
  198. }
  199. Spacer()
  200. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  201. .contentShape(Rectangle())
  202. .padding(.vertical)
  203. .onTapGesture {
  204. removeCarbsAlert = Alert(
  205. title: Text("Delete carbs?"),
  206. message: Text(item.amountText),
  207. primaryButton: .destructive(
  208. Text("Delete"),
  209. action: {
  210. state.deleteCarbs(item) }
  211. ),
  212. secondaryButton: .cancel()
  213. )
  214. isRemoveCarbsAlertPresented = true
  215. }
  216. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  217. removeCarbsAlert!
  218. }
  219. }
  220. if item.type == .fpus {
  221. Spacer()
  222. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  223. .contentShape(Rectangle())
  224. .padding(.vertical)
  225. .onTapGesture {
  226. removeCarbsAlert = Alert(
  227. title: Text("Delete carb equivalents?"),
  228. message: Text(""), // Temporary fix. New to fix real amount of carb equivalents later
  229. primaryButton: .destructive(
  230. Text("Delete"),
  231. action: { state.deleteCarbs(item) }
  232. ),
  233. secondaryButton: .cancel()
  234. )
  235. isRemoveCarbsAlertPresented = true
  236. }
  237. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  238. removeCarbsAlert!
  239. }
  240. }
  241. if item.type == .bolus {
  242. Spacer()
  243. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  244. .contentShape(Rectangle())
  245. .padding(.vertical)
  246. .onTapGesture {
  247. removeInsulinAlert = Alert(
  248. title: Text("Delete insulin?"),
  249. message: Text(item.amountText),
  250. primaryButton: .destructive(
  251. Text("Delete"),
  252. action: { state.deleteInsulin(item) }
  253. ),
  254. secondaryButton: .cancel()
  255. )
  256. isRemoveInsulinAlertPresented = true
  257. }
  258. .alert(isPresented: $isRemoveInsulinAlertPresented) {
  259. removeInsulinAlert!
  260. }
  261. }
  262. Spacer()
  263. Text(dateFormatter.string(from: item.date))
  264. .moveDisabled(true)
  265. }
  266. }
  267. var addNonPumpInsulinView: some View {
  268. NavigationView {
  269. VStack {
  270. Form {
  271. Section {
  272. HStack {
  273. Text("Amount")
  274. Spacer()
  275. DecimalTextField(
  276. "0",
  277. value: $state.nonPumpInsulinAmount,
  278. formatter: insulinFormatter,
  279. autofocus: true,
  280. cleanInput: true
  281. )
  282. Text("U").foregroundColor(.secondary)
  283. }
  284. }
  285. Section {
  286. DatePicker("Date", selection: $state.nonPumpInsulinDate, in: ...Date())
  287. }
  288. let amountWarningCondition = (state.nonPumpInsulinAmount > state.maxBolus)
  289. Section {
  290. HStack {
  291. Button {
  292. state.addNonPumpInsulin()
  293. isAmountUnconfirmed = false
  294. showNonPumpInsulin = false
  295. }
  296. label: {
  297. Text("Log non-pump insulin")
  298. }
  299. .foregroundColor(amountWarningCondition ? Color.white : Color.accentColor)
  300. .frame(maxWidth: .infinity, alignment: .center)
  301. .disabled(
  302. state.nonPumpInsulinAmount <= 0 || state.nonPumpInsulinAmount > state.maxBolus * 3
  303. )
  304. }
  305. }
  306. header: {
  307. if amountWarningCondition
  308. {
  309. Text("⚠️ Warning! The entered insulin amount is greater than your Max Bolus setting!")
  310. }
  311. }
  312. .listRowBackground(
  313. amountWarningCondition ? Color
  314. .red : colorScheme == .dark ? Color(UIColor.secondarySystemBackground) : Color.white
  315. )
  316. }
  317. }
  318. .onAppear(perform: configureView)
  319. .navigationTitle("Non-Pump Insulin")
  320. .navigationBarTitleDisplayMode(.inline)
  321. .navigationBarItems(leading: Button("Close", action: { showNonPumpInsulin = false
  322. state.nonPumpInsulinAmount = 0 }))
  323. }
  324. }
  325. @ViewBuilder private func glucoseView(_ item: Glucose, isManual: BloodGlucose) -> some View {
  326. VStack(alignment: .leading, spacing: 4) {
  327. HStack {
  328. Text(item.glucose.glucose.map {
  329. glucoseFormatter.string(from: Double(
  330. state.units == .mmolL ? $0.asMmolL : Decimal($0)
  331. ) as NSNumber)!
  332. } ?? "--")
  333. if isManual.type == GlucoseType.manual.rawValue {
  334. Image(systemName: "drop.fill").symbolRenderingMode(.monochrome).foregroundStyle(.red)
  335. } else {
  336. Text(item.glucose.direction?.symbol ?? "--")
  337. }
  338. Spacer()
  339. Text(dateFormatter.string(from: item.glucose.dateString))
  340. }
  341. }
  342. }
  343. private func deleteGlucose(at offsets: IndexSet) {
  344. state.deleteGlucose(at: offsets[offsets.startIndex])
  345. }
  346. }
  347. }