ContentView.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // ContentView.swift
  3. // Todos
  4. //
  5. // Created by Sam Jaffe on 2/28/26.
  6. //
  7. import SwiftUI
  8. import SwiftData
  9. struct ContentView: View {
  10. @Environment(\.modelContext) private var modelContext
  11. @AppStorage(UserDefaultsKeys.WeekStart) private var weekStart = Date()
  12. @Query(sort: \Project.sortOrder) private var items: [Project]
  13. @State private var selection: Project?
  14. @Binding var rotate: RotateTracking
  15. var body: some View {
  16. NavigationSplitView {
  17. List(selection: $selection) {
  18. ForEach(items, id: \.id) { item in
  19. NavigationLink(value: item) {
  20. Text(item.name)
  21. }.swipeActions(content: {
  22. Button("Delete", systemImage: "trash", role: .destructive) {
  23. deleteItem(item: item)
  24. }
  25. })
  26. }
  27. .onMove(perform: reOrder)
  28. }
  29. .navigationSplitViewColumnWidth(min: 180, ideal: 200)
  30. .toolbar {
  31. ToolbarItem {
  32. Button(action: addItem) {
  33. Label("New Project", systemImage: "plus")
  34. }
  35. }
  36. }
  37. } detail: {
  38. if let selection = selection {
  39. ProjectPanelView(item: selection)
  40. } else {
  41. let header = items.isEmpty ? "Create a New Project" : "Select a project from the sidebar"
  42. ContentUnavailableView(header, systemImage: "doc.text.image.fill")
  43. }
  44. }
  45. .confirmationDialog("Manually run \"Save and Cleanup\"?",
  46. isPresented: $rotate.manuallyInvoked) {
  47. Button("Yes") {
  48. RotateController().saveAndRotate(items, &weekStart, &rotate)
  49. rotate.summary = nil
  50. }
  51. Button("Cancel", role: .cancel) {}
  52. } message: {
  53. if let summary = rotate.summary {
  54. Text(summary)
  55. }
  56. }
  57. .alert("Autosave", isPresented: $rotate.hasTriggeredAutosave) {
  58. Text("Cleaned up the following Tasks:\n" + (rotate.summary ?? ""))
  59. Button("OK") {
  60. rotate.hasTriggeredAutosave = false
  61. rotate.summary = nil
  62. }
  63. } message: {
  64. Text("All completed tasks/subtasks have been deleted")
  65. if let summary = rotate.summary {
  66. Text(summary)
  67. }
  68. }
  69. }
  70. private func addItem() {
  71. withAnimation {
  72. let newItem = Project(sortOrder: items.count)
  73. modelContext.insert(newItem)
  74. }
  75. }
  76. @MainActor
  77. private func reOrder(fromOffsets: IndexSet, toOffset: Int) {
  78. var tmp = items.sorted(by: Project.less)
  79. tmp.move(fromOffsets: fromOffsets, toOffset: toOffset)
  80. for (index, item) in tmp.enumerated() {
  81. item.sortOrder = index
  82. }
  83. try? self.modelContext.save()
  84. }
  85. private func deleteItem(item: Project) {
  86. if let selection = selection, selection == item {
  87. self.selection = nil
  88. }
  89. withAnimation {
  90. modelContext.delete(item)
  91. }
  92. }
  93. }
  94. #Preview {
  95. @Previewable @State var rotate = RotateTracking()
  96. ContentView(rotate: $rotate)
  97. .modelContainer(for: Project.self, inMemory: true)
  98. }