hex/ios/HEXMobile/LiveActivityCoordinator.swift
Ryan Vogel da6289f872 iOS: start Live Activities only for system dictations and end them reliably
Testers saw Hex's activity (dots + ✓, expanded "Copied") stay in the
Dynamic Island, even with Keep mic warm Off. The ✓/Copied look is the
widget's stale rendering of an activity nobody ended: every recording
(keyboard warm dictations and in-app ones included) requested an activity,
and the end ran in a detached task that a suspending app (microphone just
released) could skip, so it went stale and stayed for hours.

- DictationController requests an activity only for dictations that copy
  (Action Button, Control Center, Siri/Shortcuts); the keyboard and the app
  show their own recording UI. Removed: the activity for keyboard and in-app
  dictations.
- LiveActivityCoordinator: ending (cancel, failure, short capture, warm
  session cancel) ends every Hex activity there is at that moment with
  .immediate; Copied still lingers 2 s via .after. Ends run under a UIKit
  background task, and a dictation that is transcribing keeps one until its
  activity ends. A current activity the system already ended is replaced
  rather than updated.
- Stale activities end at launch (DictationController.shared init), when Hex
  becomes active, and when the warm session ends or Keep mic warm is set to
  Off, unless a dictation is recording or transcribing.
- README: when Live Activities show and end.

Verified: xcodebuild test on the iPhone 18 Pro simulator, 210 tests pass
(1 skipped as before). A temporary, uncommitted test on the simulator showed
no activity for a warm keyboard dictation, one for a copy dictation that
ended after Copied, one ended at once on cancel, and an orphan activity
ended by Keep mic warm › Off. The Dynamic Island itself, and background
suspension timing, need a physical iPhone.
2026-11-20 08:04:42 -05:00

170 lines
5.9 KiB
Swift

import ActivityKit
import Foundation
import UIKit
/// The Dynamic Island and Lock Screen activity of a dictation started by the Action Button,
/// Control Center, Siri, or Shortcuts. Ending one also ends every other Hex activity, so none
/// outlives its dictation; each end runs under a background task, so a suspending app can't
/// leave one behind.
@MainActor
final class LiveActivityCoordinator {
private var currentActivityID: String?
/// Holds the app awake from transcribing until the activity ends: the microphone, which
/// kept it running in the background, is already released.
private var transcribingAssertion: BackgroundAssertion?
func startRecording(startedAt: Date) {
guard ActivityAuthorizationInfo().areActivitiesEnabled else { return }
let state = HexDictationActivityAttributes.ContentState(
stage: .recording,
startedAt: startedAt,
subtitle: "Recording on this iPhone"
)
if let id = currentActivityID, Self.isActive(id) {
Task.detached {
await Self.updateActivity(id: id, state: state, staleDate: startedAt.addingTimeInterval(90))
}
return
}
let attributes = HexDictationActivityAttributes(sessionID: UUID().uuidString)
if let created = try? Activity.request(
attributes: attributes,
content: ActivityContent(state: state, staleDate: startedAt.addingTimeInterval(90)),
pushType: nil
) {
currentActivityID = created.id
}
}
func markTranscribing(startedAt: Date) {
guard let id = currentActivityID else { return }
if transcribingAssertion == nil {
transcribingAssertion = BackgroundAssertion(name: "Hex transcription")
}
let state = HexDictationActivityAttributes.ContentState(
stage: .transcribing,
startedAt: startedAt,
subtitle: "Transcribing locally…"
)
Task.detached {
await Self.updateActivity(id: id, state: state, staleDate: Date().addingTimeInterval(30))
}
}
/// Shows Copied for 2 s, then the activity goes; any other Hex activity ends now.
func finishCopied() {
guard let id = currentActivityID else {
endImmediately()
return
}
currentActivityID = nil
let others = Self.activityIDs().filter { $0 != id }
let state = HexDictationActivityAttributes.ContentState(
stage: .copied,
startedAt: Date(),
subtitle: "Copied to clipboard"
)
runEnding {
await Self.endActivities(ids: others)
await Self.endActivity(
id: id,
state: state,
staleDate: Date().addingTimeInterval(4),
dismissAfterSeconds: 2
)
}
}
/// Ends every Hex activity there is now, the current one and any a suspended or
/// killed process left behind. One a later dictation starts is not touched.
func endImmediately() {
currentActivityID = nil
let ids = Self.activityIDs()
guard !ids.isEmpty else {
transcribingAssertion?.end()
transcribingAssertion = nil
return
}
runEnding {
await Self.endActivities(ids: ids)
}
}
private func runEnding(_ work: @escaping @Sendable () async -> Void) {
let assertion = BackgroundAssertion(name: "Hex Live Activity end")
let transcribing = transcribingAssertion
transcribingAssertion = nil
Task { @MainActor in
await work()
assertion.end()
transcribing?.end()
}
}
private static func activityIDs() -> [String] {
Activity<HexDictationActivityAttributes>.activities.map(\.id)
}
private static func isActive(_ id: String) -> Bool {
Activity<HexDictationActivityAttributes>.activities.contains { $0.id == id && $0.activityState == .active }
}
nonisolated private static func updateActivity(
id: String,
state: HexDictationActivityAttributes.ContentState,
staleDate: Date?
) async {
guard let activity = Activity<HexDictationActivityAttributes>.activities.first(where: { $0.id == id }) else {
return
}
await activity.update(ActivityContent(state: state, staleDate: staleDate))
}
nonisolated private static func endActivity(
id: String,
state: HexDictationActivityAttributes.ContentState,
staleDate: Date?,
dismissAfterSeconds: TimeInterval
) async {
guard let activity = Activity<HexDictationActivityAttributes>.activities.first(where: { $0.id == id }) else {
return
}
await activity.end(
ActivityContent(state: state, staleDate: staleDate),
dismissalPolicy: .after(Date().addingTimeInterval(dismissAfterSeconds))
)
}
nonisolated private static func endActivities(ids: [String]) async {
let state = HexDictationActivityAttributes.ContentState(
stage: .copied,
startedAt: Date(),
subtitle: "Done"
)
for activity in Activity<HexDictationActivityAttributes>.activities where ids.contains(activity.id) {
await activity.end(
ActivityContent(state: state, staleDate: nil),
dismissalPolicy: .immediate
)
}
}
}
/// A UIKit background task, ended once (or by the system's expiry).
@MainActor
private final class BackgroundAssertion {
private var id = UIBackgroundTaskIdentifier.invalid
init(name: String) {
id = UIApplication.shared.beginBackgroundTask(withName: name) { [weak self] in
MainActor.assumeIsolated { self?.end() }
}
}
func end() {
guard id != .invalid else { return }
UIApplication.shared.endBackgroundTask(id)
id = .invalid
}
}