mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
- Include the latest assistant preview in the silent-push fingerprint so result-only changes wake the phone; the status-only fingerprint missed results that landed while a session stayed working. - Coalesce WidgetKit reloads (60s) and reload only installed widget kinds, with a fallback when configuration lookup is empty; the previous every-update/all-kind reloads exhausted the system redraw budget. - Wait for AppModel/hub restoration on a cold silent-push launch instead of clearing the last good snapshot. - Use a 15-minute widget timeline fallback and 60-second hub push throttle. - Add the medium Session Detail layout with in-card paging, plain-text previews, status labels, and the hidden-row indicator. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> Co-Authored-By: Claude Code <noreply@anthropic.com>
86 lines
2.8 KiB
Swift
86 lines
2.8 KiB
Swift
import BackgroundTasks
|
|
import Foundation
|
|
import OSLog
|
|
|
|
/// Registers the periodic widget refresh and the silent-push wake-up path.
|
|
/// iOS suspends the app after backgrounding; these are the only entry points
|
|
/// that let it rewrite the App Group snapshot without the user opening it.
|
|
@MainActor
|
|
enum WidgetRefreshScheduler {
|
|
static let taskIdentifier = "run.hapi.companion.widget-refresh"
|
|
private static let log = Logger(subsystem: "run.hapi.companion", category: "widget-refresh")
|
|
private static var registered = false
|
|
|
|
static func register() {
|
|
guard !registered else { return }
|
|
registered = true
|
|
BGTaskScheduler.shared.register(forTaskWithIdentifier: taskIdentifier, using: nil) { task in
|
|
Task { @MainActor in
|
|
handle(task)
|
|
}
|
|
}
|
|
}
|
|
|
|
static func schedule() {
|
|
let request = BGAppRefreshTaskRequest(identifier: taskIdentifier)
|
|
request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
|
|
do {
|
|
try BGTaskScheduler.shared.submit(request)
|
|
} catch {
|
|
log.debug("BGAppRefreshTask submit failed: \(error.localizedDescription, privacy: .public)")
|
|
}
|
|
}
|
|
|
|
private static func handle(_ task: BGTask) {
|
|
// Always keep a next request queued; iOS decides when it runs.
|
|
schedule()
|
|
guard let refresh = task as? BGAppRefreshTask else {
|
|
task.setTaskCompleted(success: false)
|
|
return
|
|
}
|
|
let work = Task { @MainActor in
|
|
let ok = await WidgetRefreshBridge.shared.refresh()
|
|
refresh.setTaskCompleted(success: ok)
|
|
}
|
|
refresh.expirationHandler = {
|
|
work.cancel()
|
|
}
|
|
}
|
|
|
|
/// A background APNs payload has no alert and `content-available: 1`.
|
|
nonisolated static func isSilentPush(_ userInfo: [AnyHashable: Any]) -> Bool {
|
|
guard let aps = userInfo["aps"] as? [String: Any] else { return false }
|
|
if let value = aps["content-available"] as? Int {
|
|
return value == 1
|
|
}
|
|
if let value = aps["content-available"] as? Bool {
|
|
return value
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
/// The app delegate and the background task need the active model. AppModel
|
|
/// installs this closure; the delegate never holds the model directly.
|
|
@MainActor
|
|
final class WidgetRefreshBridge {
|
|
static let shared = WidgetRefreshBridge()
|
|
|
|
private var handler: (@MainActor () async -> Bool)?
|
|
|
|
func install(_ handler: @escaping @MainActor () async -> Bool) {
|
|
self.handler = handler
|
|
}
|
|
|
|
func refresh() async -> Bool {
|
|
// Cold launch from a silent push can race AppModel initialization.
|
|
for _ in 0..<10 {
|
|
if let handler {
|
|
return await handler()
|
|
}
|
|
try? await Task.sleep(for: .milliseconds(100))
|
|
}
|
|
return false
|
|
}
|
|
}
|