diff --git a/mac/Sources/Byre/Drafts.swift b/mac/Sources/Byre/Drafts.swift index 580c35d..02033cc 100644 --- a/mac/Sources/Byre/Drafts.swift +++ b/mac/Sources/Byre/Drafts.swift @@ -12,6 +12,7 @@ private struct Saved: Codable { var checked: [String: [String]] = [:] var texts: [String: String] = [:] var comments: [String: [LineComment]]? = nil + var visits: [String: Double]? = nil } struct LineAnchor: Codable, Hashable { @@ -41,8 +42,8 @@ final class DraftSend { @MainActor func migrateRecordIDs() { var changed = false - for v in fetchRecords(FetchDescriptor()) where migratedID("tsk", v.taskId) != v.taskId { - v.taskId = migratedID("tsk", v.taskId) + for v in fetchRecords(FetchDescriptor()) { + persistence.delete(v) changed = true } let composerKey = { (key: String) in @@ -93,6 +94,7 @@ final class Drafts { private var checked: [String: [String]] = [:] private var texts: [String: String] = [:] private var comments: [String: [LineComment]] = [:] + @ObservationIgnored private(set) var visits: [String: Double] = [:] @ObservationIgnored private let scope: String @ObservationIgnored private var timer: Task? @ObservationIgnored private var dirty = false @@ -113,6 +115,12 @@ final class Drafts { checked = saved.checked texts = saved.texts comments = saved.comments ?? [:] + visits = saved.visits ?? [:] + } + + func visit(_ id: String, _ at: Double) { + visits[id] = at + save() } func reload() { @@ -148,6 +156,10 @@ final class Drafts { texts[id] = nil changed = true } + for id in visits.keys where store.findTask(id)?.status != .active && !store.projects.contains(where: { $0.id == id }) { + visits[id] = nil + changed = true + } if changed { save() } @@ -179,7 +191,7 @@ final class Drafts { return } dirty = false - var out = Saved(checked: checked, texts: texts, comments: comments) + var out = Saved(checked: checked, texts: texts, comments: comments, visits: visits) let keys = Set(composerTexts.keys).union(composerImages.keys).union(sending.map(\.key)).union(lostImages.keys) for key in keys { let inFlight = sending.filter { $0.key == key } diff --git a/mac/Sources/Byre/Store.swift b/mac/Sources/Byre/Store.swift index 20dcb2b..cc80184 100644 --- a/mac/Sources/Byre/Store.swift +++ b/mac/Sources/Byre/Store.swift @@ -680,26 +680,14 @@ final class Store { } func lastVisits() -> [String: Double] { - fetchRecords(FetchDescriptor()).reduce(into: [:]) { visits, v in - visits[v.taskId] = max(visits[v.taskId] ?? 0, v.at) - } + drafts?.visits ?? [:] } private func markVisited(_ id: String?) { guard let id else { return } - let at = Date().timeIntervalSince1970 * 1000 - let records = fetchRecords(FetchDescriptor(predicate: #Predicate { $0.taskId == id })) - if records.isEmpty { - persistence.insert(VisitRecord(taskId: id, at: at)) - } else { - records[0].at = at - for record in records.dropFirst() { - persistence.delete(record) - } - } - saveRecords() + drafts?.visit(id, Date().timeIntervalSince1970 * 1000) } func loadSummaryReads() async throws {