IceCubesApp/IceCubesAppIntents/ListEntity.swift

56 lines
1.3 KiB
Swift
Raw Normal View History

2024-05-15 07:27:35 +00:00
import AppAccount
import AppIntents
import Env
import Foundation
import Models
import Network
import Timeline
public struct ListEntity: Identifiable, AppEntity {
public var id: String { list.id }
public let list: Models.List
public static let defaultQuery = DefaultListEntityQuery()
public static let typeDisplayRepresentation: TypeDisplayRepresentation = "List"
public var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(list.title)")
}
}
public struct DefaultListEntityQuery: EntityQuery {
public init() {}
2024-08-01 06:58:54 +00:00
2024-05-15 07:27:35 +00:00
@IntentParameterDependency<ListsWidgetConfiguration>(
\.$account
)
var account
2024-08-01 06:58:54 +00:00
2024-05-15 07:27:35 +00:00
public func entities(for _: [ListEntity.ID]) async throws -> [ListEntity] {
2024-08-01 06:58:54 +00:00
await fetchLists().map { .init(list: $0) }
2024-05-15 07:27:35 +00:00
}
public func suggestedEntities() async throws -> [ListEntity] {
2024-08-01 06:58:54 +00:00
await fetchLists().map { .init(list: $0) }
2024-05-15 07:27:35 +00:00
}
public func defaultResult() async -> ListEntity? {
nil
}
2024-08-01 06:58:54 +00:00
2024-05-15 07:27:35 +00:00
private func fetchLists() async -> [Models.List] {
guard let account = account?.account.account else {
return []
}
let client = Client(server: account.server, oauthToken: account.oauthToken)
do {
let lists: [Models.List] = try await client.get(endpoint: Lists.lists)
return lists
} catch {
return []
}
}
}