ContentView.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. let inPreview = ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
  13. @Query(sort: \Project.sortOrder) private var items: [Project]
  14. @State private var selection: Project?
  15. @State private var hasAutosave: Bool = false
  16. var body: some View {
  17. NavigationSplitView {
  18. List(selection: $selection) {
  19. ForEach(items, id: \.id) { item in
  20. NavigationLink(value: item) {
  21. Text(item.name)
  22. }.swipeActions(content: {
  23. Button("Delete", systemImage: "trash", role: .destructive) {
  24. deleteItem(item: item)
  25. }
  26. })
  27. }
  28. .onMove(perform: reOrder)
  29. }
  30. .navigationSplitViewColumnWidth(min: 180, ideal: 200)
  31. .toolbar {
  32. ToolbarItem {
  33. Button(action: addItem) {
  34. Label("New Project", systemImage: "plus")
  35. }
  36. }
  37. }
  38. } detail: {
  39. if let selection = selection {
  40. ProjectPanelView(item: selection)
  41. } else {
  42. let header = items.isEmpty ? "Create a New Project" : "Select a project from the sidebar"
  43. ContentUnavailableView(header, systemImage: "doc.text.image.fill")
  44. }
  45. }
  46. .onAppear(perform: autosave)
  47. .alert("Autosave", isPresented: $hasAutosave) {
  48. Button("OK") {
  49. hasAutosave = false
  50. }
  51. } message: {
  52. Text("All completed tasks/subtasks have been deleted")
  53. }
  54. }
  55. private func addItem() {
  56. withAnimation {
  57. let newItem = Project(sortOrder: items.count)
  58. modelContext.insert(newItem)
  59. }
  60. }
  61. @MainActor
  62. private func reOrder(fromOffsets: IndexSet, toOffset: Int) {
  63. var tmp = items.sorted(by: Project.less)
  64. tmp.move(fromOffsets: fromOffsets, toOffset: toOffset)
  65. for (index, item) in tmp.enumerated() {
  66. item.sortOrder = index
  67. }
  68. try? self.modelContext.save()
  69. }
  70. private func autosave() {
  71. if inPreview {
  72. // This isn't great, but we shouldn't be running this in a preview
  73. // environment in the first place, so w/e.
  74. return
  75. }
  76. let now = Date()
  77. let sunday = Calendar.current.nextDate(after: weekStart,
  78. matching: DateComponents(weekday: 1),
  79. matchingPolicy: .nextTime)
  80. if now <= sunday! {
  81. return
  82. }
  83. let ymd = weekStart.formatted(.iso8601.year().month().day())
  84. SaveController.save(items, toUrl: SaveController.filename(date: ymd))
  85. weekStart = now
  86. cleanup()
  87. hasAutosave = true
  88. }
  89. private func cleanup() {
  90. for item in items {
  91. item.tasks.removeAll(where: { $0.status == .complete })
  92. for task in item.tasks {
  93. if task.status == .inProgress {
  94. task.status = .todo
  95. }
  96. task.subtasks.removeAll(where: { $0.status == .complete })
  97. for subtask in task.subtasks where subtask.status == .inProgress {
  98. subtask.status = .todo
  99. }
  100. }
  101. }
  102. }
  103. private func deleteItem(item: Project) {
  104. if let selection = selection, selection == item {
  105. self.selection = nil
  106. }
  107. withAnimation {
  108. modelContext.delete(item)
  109. }
  110. }
  111. }
  112. #Preview {
  113. ContentView()
  114. .modelContainer(for: Project.self, inMemory: true)
  115. }