ContentView.swift 3.0 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 private var items: [Category]
  14. @State private var showingPopup = false
  15. @State private var currentHint: URLHint = URLHint()
  16. var body: some View {
  17. NavigationSplitView {
  18. List {
  19. ForEach(items) { item in
  20. NavigationLink {
  21. CategoryPanelView(item: item)
  22. } label: {
  23. CategorySidebarView(name: Bindable(item).name)
  24. } .contextMenu {
  25. Button(action: { deleteItem(item: item) }) {
  26. Label("Delete", systemImage: "trash")
  27. }
  28. }
  29. }
  30. .onDelete(perform: deleteItems)
  31. }
  32. .navigationSplitViewColumnWidth(min: 180, ideal: 200)
  33. .toolbar {
  34. ToolbarItem {
  35. Button(action: addItem) {
  36. Label("New Category", systemImage: "plus")
  37. }
  38. }
  39. }
  40. } detail: {
  41. Text("Select an item")
  42. }
  43. .onAppear(perform: autosave)
  44. }
  45. private func addItem() {
  46. withAnimation {
  47. let newItem = Category(timestamp: Date())
  48. let count = items.count(where: { $0.name.starts(with: "New Category") })
  49. if (count > 0) {
  50. newItem.name += " (\(count))"
  51. }
  52. modelContext.insert(newItem)
  53. }
  54. }
  55. private func autosave() {
  56. if inPreview {
  57. // This isn't great, but we shouldn't be running this in a preview
  58. // environment in the first place, so w/e.
  59. return
  60. }
  61. let now = Date()
  62. let sunday = Calendar.current.nextDate(after: weekStart,
  63. matching: DateComponents(weekday: 1),
  64. matchingPolicy: .nextTime)
  65. if now <= sunday! {
  66. return
  67. }
  68. let ymd = weekStart.formatted(.iso8601.year().month().day())
  69. SaveController.save(items, to: SaveController.filename(date: ymd))
  70. weekStart = now
  71. cleanup()
  72. }
  73. private func cleanup() {
  74. for item in items {
  75. item.tasks.removeAll(where: { $0.status == .Complete })
  76. for task in item.tasks {
  77. if task.status == .InProgress {
  78. task.status = .Todo
  79. }
  80. task.subtasks.removeAll(where: { $0.status == .Complete })
  81. for subtask in task.subtasks {
  82. if subtask.status == .InProgress {
  83. subtask.status = .Todo
  84. }
  85. }
  86. }
  87. }
  88. }
  89. private func deleteItem(item: Category) {
  90. withAnimation {
  91. modelContext.delete(item)
  92. }
  93. }
  94. private func deleteItems(offsets: IndexSet) {
  95. withAnimation {
  96. for index in offsets {
  97. modelContext.delete(items[index])
  98. }
  99. }
  100. }
  101. }
  102. #Preview {
  103. ContentView()
  104. .modelContainer(for: Category.self, inMemory: true)
  105. }