Files
cindy/apps/mobile/plugins/usage-widgets/ComputerIntent.swift
Dash 26c7169308 feat(mobile): 支持 iOS 与 Android 锁屏余量和每日用量组件 (#9130)
* feat(mobile): 支持 iOS 锁屏余量和每日用量组件

Add circular, rectangular and inline widgets with horizontal quota bars, provider icons, lowest-quota selection and actual daily usage trends.

Assisted-by: Cindy (https://cindy.app)
Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>

* fix(mobile): 保留锁屏余量填充和完整辅助朗读

Constrain the emphasized bar gap at 2x and share reset timing between single and paired inline readings.

Assisted-by: Cindy (https://cindy.app)
Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>

* feat(mobile): 安卓用量组件支持放到原生锁屏

两个组件声明 home_screen|keyguard,在 Android 16 QPR2 起的原生锁屏组件页
可直接添加,沿用桌面版式、账号排序与数据来源设置;锁屏点击由系统先要求解锁。
文档记录三星 One UI 8.5 原生锁屏不放行第三方组件的实测结论与模拟器验证方法。

Assisted-by: Cindy (https://cindy.app)
Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>

---------

Signed-off-by: Dash <125997726+dashhuang@users.noreply.github.com>
2026-11-07 10:37:57 +08:00

126 lines
4.6 KiB
Swift

import AppIntents
import WidgetKit
/// A computer a widget can show, or "automatic": the one that answered the app last.
struct ComputerEntity: AppEntity {
static let autoID = "auto"
static let automatic = ComputerEntity(id: autoID, name: "")
let id: String
let name: String
static var typeDisplayRepresentation: TypeDisplayRepresentation = "Computer"
static var defaultQuery = ComputerQuery()
var displayRepresentation: DisplayRepresentation {
id == Self.autoID
? DisplayRepresentation(title: "Automatic", subtitle: "The computer that answered last")
: DisplayRepresentation(title: "\(name)")
}
}
/// The computers the app last listed (other computers that allow remote control).
struct ComputerQuery: EntityQuery {
func entities(for identifiers: [String]) async throws -> [ComputerEntity] {
all().filter { identifiers.contains($0.id) }
}
func suggestedEntities() async throws -> [ComputerEntity] {
all()
}
func defaultResult() async -> ComputerEntity? {
.automatic
}
private func all() -> [ComputerEntity] {
[.automatic] + (WidgetStore.load()?.computers.map { ComputerEntity(id: $0.id, name: $0.name) } ?? [])
}
}
/// An account the quota widget can list: one of the sources a computer sent. The id carries the
/// logo and name too (`AccountKey`), so a choice still finds the account on another computer.
struct AccountEntity: AppEntity {
let key: AccountKey
var id: String { key.encoded }
static var typeDisplayRepresentation: TypeDisplayRepresentation = "Account"
static var defaultQuery = AccountQuery()
var displayRepresentation: DisplayRepresentation {
let image = key.logo.map { DisplayRepresentation.Image(named: "logo-\($0)") }
return DisplayRepresentation(title: "\(key.name)", image: image)
}
}
/// The accounts of the widget's chosen computer, in that computer's order. Hidden ones on the
/// computer are not sent, so they are not offered either.
struct AccountQuery: EntityQuery {
@IntentParameterDependency<QuotaWidgetIntent>(\.$computer)
var config
func entities(for identifiers: [String]) async throws -> [AccountEntity] {
identifiers.map { AccountEntity(key: AccountKey(encoded: $0)) }
}
func suggestedEntities() async throws -> [AccountEntity] {
let sources = WidgetState.load(computer: config?.computer.id).snapshot?.sources ?? []
return sources.map { AccountEntity(key: AccountKey(source: $0)) }
}
}
/// What a widget's edit sheet chose; nil or empty means the defaults.
protocol UsageWidgetConfiguration: WidgetConfigurationIntent {
var computerID: String? { get }
/// Accounts placed first, in this order (`AccountKey` strings).
var pinnedAccounts: [String]? { get }
/// Accounts left out (`AccountKey` strings).
var hiddenAccounts: [String]? { get }
}
/// "Data source" in the daily usage widget's edit sheet.
struct SelectComputerIntent: UsageWidgetConfiguration {
static var title: LocalizedStringResource = "Data source"
static var description = IntentDescription("Choose which computer this widget shows.")
@Parameter(title: "Computer")
var computer: ComputerEntity?
var computerID: String? { computer?.id }
var pinnedAccounts: [String]? { nil }
var hiddenAccounts: [String]? { nil }
}
/// The quota widget's edit sheet, like the Mac menu bar's settings: accounts placed first and
/// accounts hidden; every other account, including ones added later, follows in the computer's
/// order.
struct QuotaWidgetIntent: UsageWidgetConfiguration {
static var title: LocalizedStringResource = "Data source"
static var description = IntentDescription(
"Choose which computer this widget shows, which accounts come first and which to hide. Other accounts, including ones added later, follow in that computer's order."
)
@Parameter(title: "Computer")
var computer: ComputerEntity?
/// Up to what each size shows; a plain number would mean exactly that many, with no way to
/// remove one or clear the choice.
@Parameter(title: "Accounts first", size: [
.systemSmall: IntentCollectionSize(min: 0, max: 4),
.systemMedium: IntentCollectionSize(min: 0, max: 6),
.systemLarge: IntentCollectionSize(min: 0, max: 7),
.accessoryCircular: IntentCollectionSize(min: 0, max: 2),
.accessoryRectangular: IntentCollectionSize(min: 0, max: 4),
.accessoryInline: IntentCollectionSize(min: 0, max: 1),
])
var pinned: [AccountEntity]?
@Parameter(title: "Hidden accounts")
var hidden: [AccountEntity]?
var computerID: String? { computer?.id }
var pinnedAccounts: [String]? { pinned?.map(\.id) }
var hiddenAccounts: [String]? { hidden?.map(\.id) }
}