]> git.r.bdr.sh - rbdr/captura/blob - Captura/Intents/GetRemoteCaptures.swift
58d9583e5a473214d10efd45c9bb46d4fa85461f
[rbdr/captura] / Captura / Intents / GetRemoteCaptures.swift
1 import AppIntents
2 import CoreData
3
4 struct GetRemoteCaptures: AppIntent {
5 static var title: LocalizedStringResource = "Get remote captures"
6
7 static var description =
8 IntentDescription("Return a list of remote captures")
9
10 @Parameter(title: "Count") var count: Int?
11
12 static var parameterSummary: some ParameterSummary {
13 Summary("Get \(\.$count) latest captures.")
14 }
15
16 func perform() async throws -> some IntentResult & ReturnsValue {
17 let viewContext = PersistenceController.shared.container.viewContext
18 let fetchRequest = NSFetchRequest<CapturaRemoteFile>(entityName: "CapturaRemoteFile")
19 fetchRequest.fetchLimit = min(10, max(1, count ?? 5))
20 fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: false)]
21
22 let results = await viewContext.perform {
23 return try? viewContext.fetch(fetchRequest)
24 }
25
26 let finalResults = results?.compactMap { URL(string: $0.url ?? "")} ?? []
27 return .result(value: finalResults)
28 }
29 }