|
@@ -10,12 +10,10 @@ import SwiftData
|
|
|
|
|
|
|
|
struct ContentView: View {
|
|
struct ContentView: View {
|
|
|
@Environment(\.modelContext) private var modelContext
|
|
@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]
|
|
@Query(sort: \Project.sortOrder) private var items: [Project]
|
|
|
@State private var selection: Project?
|
|
@State private var selection: Project?
|
|
|
- @State private var hasAutosave: Bool = false
|
|
|
|
|
|
|
+ @Binding var hasAutosave: Bool
|
|
|
|
|
|
|
|
var body: some View {
|
|
var body: some View {
|
|
|
NavigationSplitView {
|
|
NavigationSplitView {
|
|
@@ -47,7 +45,6 @@ struct ContentView: View {
|
|
|
ContentUnavailableView(header, systemImage: "doc.text.image.fill")
|
|
ContentUnavailableView(header, systemImage: "doc.text.image.fill")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- .onAppear(perform: autosave)
|
|
|
|
|
.alert("Autosave", isPresented: $hasAutosave) {
|
|
.alert("Autosave", isPresented: $hasAutosave) {
|
|
|
Button("OK") {
|
|
Button("OK") {
|
|
|
hasAutosave = false
|
|
hasAutosave = false
|
|
@@ -74,45 +71,6 @@ struct ContentView: View {
|
|
|
try? self.modelContext.save()
|
|
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()
|
|
|
|
|
- hasAutosave = true
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- 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) {
|
|
private func deleteItem(item: Project) {
|
|
|
if let selection = selection, selection == item {
|
|
if let selection = selection, selection == item {
|
|
|
self.selection = nil
|
|
self.selection = nil
|
|
@@ -124,6 +82,7 @@ struct ContentView: View {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#Preview {
|
|
#Preview {
|
|
|
- ContentView()
|
|
|
|
|
|
|
+ @Previewable @State var hasAutosave = false
|
|
|
|
|
+ ContentView(hasAutosave: $hasAutosave)
|
|
|
.modelContainer(for: Project.self, inMemory: true)
|
|
.modelContainer(for: Project.self, inMemory: true)
|
|
|
}
|
|
}
|