28 lines
1.0 KiB
Swift
28 lines
1.0 KiB
Swift
import Foundation
|
|
import SwiftData
|
|
|
|
/// Reads project photos through the lead association in a fresh context after AR closes.
|
|
@MainActor
|
|
final class LeadARPhotoStore {
|
|
private let context: ModelContext
|
|
|
|
init(container: ModelContainer) {
|
|
context = ModelContext(container)
|
|
context.autosaveEnabled = false
|
|
}
|
|
|
|
func photos(for leadID: String) throws -> [ProjectPhoto] {
|
|
let links = try context.fetch(FetchDescriptor<LeadARProjectLink>(
|
|
predicate: #Predicate { $0.leadID == leadID }))
|
|
let legacy = try context.fetch(FetchDescriptor<Project>(
|
|
predicate: #Predicate { $0.leadID == leadID }))
|
|
var projects = links.compactMap(\.project)
|
|
let linkedIDs = Set(projects.map(\.projectID))
|
|
projects.append(contentsOf: legacy.filter { !linkedIDs.contains($0.projectID) })
|
|
var seen = Set<PersistentIdentifier>()
|
|
return projects.flatMap(\.photos)
|
|
.filter { seen.insert($0.persistentModelID).inserted }
|
|
.sorted { $0.creationDate > $1.creationDate }
|
|
}
|
|
}
|