DataTableRootView.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import SwiftUI
  2. extension DataTable {
  3. struct RootView: BaseView {
  4. @EnvironmentObject var viewModel: ViewModel<Provider>
  5. @State private var isRemoveCarbsAlertPresented = false
  6. @State private var removeCarbsAlert: Alert?
  7. private var dateFormatter: DateFormatter {
  8. let formatter = DateFormatter()
  9. formatter.timeStyle = .short
  10. return formatter
  11. }
  12. var body: some View {
  13. Form {
  14. list
  15. }
  16. .navigationTitle("History")
  17. .navigationBarTitleDisplayMode(.automatic)
  18. .navigationBarItems(
  19. leading: Button("Close", action: viewModel.hideModal)
  20. )
  21. }
  22. private var list: some View {
  23. List {
  24. ForEach(viewModel.items.indexed(), id: \.1.id) { _, item in
  25. HStack {
  26. Image(systemName: "circle.fill").foregroundColor(item.color)
  27. Text(dateFormatter.string(from: item.date))
  28. .moveDisabled(true)
  29. Text(item.type.name)
  30. Text(item.amountText).foregroundColor(.secondary)
  31. if let duration = item.durationText {
  32. Text(duration).foregroundColor(.secondary)
  33. }
  34. if item.type == .carbs {
  35. Spacer()
  36. Image(systemName: "xmark.circle").foregroundColor(.secondary)
  37. .contentShape(Rectangle())
  38. .padding(.vertical)
  39. .onTapGesture {
  40. removeCarbsAlert = Alert(
  41. title: Text("Delete carbs?"),
  42. message: Text(item.amountText),
  43. primaryButton: .destructive(
  44. Text("Delete"),
  45. action: { viewModel.deleteCarbs(at: item.date) }
  46. ),
  47. secondaryButton: .cancel()
  48. )
  49. isRemoveCarbsAlertPresented = true
  50. }
  51. .alert(isPresented: $isRemoveCarbsAlertPresented) {
  52. removeCarbsAlert!
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }