IceCubesApp/IceCubesAppIntents/AppAccountEntity.swift

41 lines
1.2 KiB
Swift
Raw Normal View History

2024-05-05 11:12:19 +00:00
import AppAccount
2024-05-06 06:38:37 +00:00
import AppIntents
2024-05-05 11:12:19 +00:00
import Env
import Foundation
import Models
import Network
public struct AppAccountEntity: Identifiable, AppEntity {
public var id: String { account.id }
public let account: AppAccount
public static let defaultQuery = DefaultAppAccountEntityQuery()
public static let typeDisplayRepresentation: TypeDisplayRepresentation = "AppAccount"
public var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(account.accountName ?? account.server)")
}
}
public struct DefaultAppAccountEntityQuery: EntityQuery {
2024-05-06 06:38:37 +00:00
public init() {}
2024-05-05 11:12:19 +00:00
public func entities(for identifiers: [AppAccountEntity.ID]) async throws -> [AppAccountEntity] {
return await AppAccountsManager.shared.availableAccounts.filter { account in
identifiers.contains { id in
id == account.id
}
}.map { AppAccountEntity(account: $0) }
}
public func suggestedEntities() async throws -> [AppAccountEntity] {
await AppAccountsManager.shared.availableAccounts.map { .init(account: $0) }
}
public func defaultResult() async -> AppAccountEntity? {
await .init(account: AppAccountsManager.shared.currentAccount)
}
}