ContentView.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. var body: some View {
  16. NavigationSplitView {
  17. List(selection: $selection) {
  18. ForEach(items, id: \.self) { 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. .onAppear(perform: autosave)
  46. }
  47. private func addItem() {
  48. withAnimation {
  49. let newItem = Project(timestamp: Date(), sortOrder: items.count)
  50. modelContext.insert(newItem)
  51. }
  52. }
  53. @MainActor
  54. private func reOrder(fromOffsets: IndexSet, toOffset: Int) {
  55. var tmp = items.sorted(by: Project.less)
  56. tmp.move(fromOffsets: fromOffsets, toOffset: toOffset)
  57. for (index, item) in tmp.enumerated() {
  58. item.sortOrder = index
  59. }
  60. try? self.modelContext.save()
  61. }
  62. private func autosave() {
  63. if inPreview {
  64. // This isn't great, but we shouldn't be running this in a preview
  65. // environment in the first place, so w/e.
  66. return
  67. }
  68. let now = Date()
  69. let sunday = Calendar.current.nextDate(after: weekStart,
  70. matching: DateComponents(weekday: 1),
  71. matchingPolicy: .nextTime)
  72. if now <= sunday! {
  73. return
  74. }
  75. let ymd = weekStart.formatted(.iso8601.year().month().day())
  76. SaveController.save(items, toUrl: SaveController.filename(date: ymd))
  77. weekStart = now
  78. cleanup()
  79. }
  80. private func cleanup() {
  81. for item in items {
  82. item.tasks.removeAll(where: { $0.status == .complete })
  83. for task in item.tasks {
  84. if task.status == .inProgress {
  85. task.status = .todo
  86. }
  87. task.subtasks.removeAll(where: { $0.status == .complete })
  88. for subtask in task.subtasks where subtask.status == .inProgress {
  89. subtask.status = .todo
  90. }
  91. }
  92. }
  93. }
  94. private func deleteItem(item: Project) {
  95. if let selection = selection, selection == item {
  96. self.selection = nil
  97. }
  98. withAnimation {
  99. modelContext.delete(item)
  100. }
  101. }
  102. }
  103. #Preview {
  104. ContentView()
  105. .modelContainer(for: Project.self, inMemory: true)
  106. }