]> git.r.bdr.sh - rbdr/captura/blame - Captura/Intents/GetRemoteCaptures.swift
Format the code
[rbdr/captura] / Captura / Intents / GetRemoteCaptures.swift
CommitLineData
5802c153
RBR
1/*
2 Copyright (C) 2024 Rubén Beltrán del Río
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://captura.tranquil.systems.
16 */
377442f2
RBR
17import AppIntents
18import CoreData
19
20struct GetRemoteCaptures: AppIntent {
505c1e62
RBR
21 static var title: LocalizedStringResource = "Get remote captures"
22
23 static var description =
24 IntentDescription("Return a list of remote captures")
25
377442f2 26 @Parameter(title: "Count") var count: Int?
505c1e62 27
377442f2 28 static var parameterSummary: some ParameterSummary {
505c1e62 29 Summary("Get \(\.$count) latest captures.")
377442f2 30 }
505c1e62 31
377442f2
RBR
32 func perform() async throws -> some IntentResult & ReturnsValue {
33 let viewContext = PersistenceController.shared.container.viewContext
34 let fetchRequest = NSFetchRequest<CapturaRemoteFile>(entityName: "CapturaRemoteFile")
35 fetchRequest.fetchLimit = min(10, max(1, count ?? 5))
36 fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: false)]
505c1e62 37
377442f2
RBR
38 let results = await viewContext.perform {
39 return try? viewContext.fetch(fetchRequest)
40 }
505c1e62
RBR
41
42 let finalResults = results?.compactMap { URL(string: $0.url ?? "") } ?? []
377442f2
RBR
43 return .result(value: finalResults)
44 }
45}