ContentView.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. ToolbarItem {
  40. Button(action: autosave) {
  41. Label("Save Historical", systemImage: "document")
  42. }
  43. }
  44. }
  45. } detail: {
  46. Text("Select an item")
  47. }
  48. .onAppear(perform: autosave)
  49. }
  50. private func addItem() {
  51. withAnimation {
  52. let newItem = Category(timestamp: Date())
  53. let count = items.count(where: { $0.name.starts(with: "New Category") })
  54. if (count > 0) {
  55. newItem.name += " (\(count))"
  56. }
  57. modelContext.insert(newItem)
  58. }
  59. }
  60. private func autosave() {
  61. if inPreview {
  62. // This isn't great, but we shouldn't be running this in a preview
  63. // environment in the first place, so w/e.
  64. return
  65. }
  66. let now = Date()
  67. let sunday = Calendar.current.nextDate(after: weekStart,
  68. matching: DateComponents(weekday: 1),
  69. matchingPolicy: .nextTime)
  70. if now <= sunday! {
  71. return
  72. }
  73. let ymd = weekStart.formatted(.iso8601.year().month().day())
  74. let data = Data(items.map({ $0.yaml() }).joined().utf8)
  75. let url = URL.documentsDirectory.appending(path: "Todo \(ymd).yaml")
  76. do {
  77. try data.write(to: url, options: [.atomic, .completeFileProtection])
  78. let input = try String(contentsOf: url, encoding: .utf8)
  79. print(input)
  80. } catch {
  81. print(error.localizedDescription)
  82. }
  83. weekStart = now
  84. }
  85. private func deleteItem(item: Category) {
  86. withAnimation {
  87. modelContext.delete(item)
  88. }
  89. }
  90. private func deleteItems(offsets: IndexSet) {
  91. withAnimation {
  92. for index in offsets {
  93. modelContext.delete(items[index])
  94. }
  95. }
  96. }
  97. }
  98. #Preview {
  99. ContentView()
  100. .modelContainer(for: Category.self, inMemory: true)
  101. }