| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- //
- // ContentView.swift
- // Todos
- //
- // Created by Sam Jaffe on 2/28/26.
- //
- import SwiftUI
- import SwiftData
- struct ContentView: View {
- @Environment(\.modelContext) private var modelContext
- @AppStorage(UserDefaultsKeys.WeekStart) private var weekStart = Date()
- let inPreview = ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
- @Query(sort: \Project.sortOrder) private var items: [Project]
- @State private var selection: Project?
- var body: some View {
- NavigationSplitView {
- List(selection: $selection) {
- ForEach(items, id: \.self) { item in
- NavigationLink(value: item) {
- Text(item.name)
- }.swipeActions(content: {
- Button("Delete", systemImage: "trash", role: .destructive) {
- deleteItem(item: item)
- }
- })
- }
- .onMove(perform: reOrder)
- }
- .navigationSplitViewColumnWidth(min: 180, ideal: 200)
- .toolbar {
- ToolbarItem {
- Button(action: addItem) {
- Label("New Project", systemImage: "plus")
- }
- }
- }
- } detail: {
- if let selection = selection {
- ProjectPanelView(item: selection)
- } else {
- let header = items.isEmpty ? "Create a New Project" : "Select a project from the sidebar"
- ContentUnavailableView(header, systemImage: "doc.text.image.fill")
- }
- }
- .onAppear(perform: autosave)
- }
- private func addItem() {
- withAnimation {
- let newItem = Project(timestamp: Date(), sortOrder: items.count)
- modelContext.insert(newItem)
- }
- }
- @MainActor
- private func reOrder(fromOffsets: IndexSet, toOffset: Int) {
- var tmp = items.sorted(by: Project.less)
- tmp.move(fromOffsets: fromOffsets, toOffset: toOffset)
- for (index, item) in tmp.enumerated() {
- item.sortOrder = index
- }
- try? self.modelContext.save()
- }
- private func autosave() {
- if inPreview {
- // This isn't great, but we shouldn't be running this in a preview
- // environment in the first place, so w/e.
- return
- }
- let now = Date()
- let sunday = Calendar.current.nextDate(after: weekStart,
- matching: DateComponents(weekday: 1),
- matchingPolicy: .nextTime)
- if now <= sunday! {
- return
- }
- let ymd = weekStart.formatted(.iso8601.year().month().day())
- SaveController.save(items, toUrl: SaveController.filename(date: ymd))
- weekStart = now
- cleanup()
- }
- private func cleanup() {
- for item in items {
- item.tasks.removeAll(where: { $0.status == .complete })
- for task in item.tasks {
- if task.status == .inProgress {
- task.status = .todo
- }
- task.subtasks.removeAll(where: { $0.status == .complete })
- for subtask in task.subtasks where subtask.status == .inProgress {
- subtask.status = .todo
- }
- }
- }
- }
- private func deleteItem(item: Project) {
- if let selection = selection, selection == item {
- self.selection = nil
- }
- withAnimation {
- modelContext.delete(item)
- }
- }
- }
- #Preview {
- ContentView()
- .modelContainer(for: Project.self, inMemory: true)
- }
|