hex/ios/HEXMobile/HEXMobileApp.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

62 lines
2.5 KiB
Swift

import SwiftUI
@main
struct HEXMobileApp: App {
@Environment(\.scenePhase) private var scenePhase
@State private var dictation = DictationController.shared
@State private var openedFromKeyboard = Self.initialOpenedFromKeyboard
@State private var showSetup = false
init() {
FullKeyboardSettings().applyLaunchArguments()
TypingLogSettings().applyLaunchArguments()
}
private static var initialOpenedFromKeyboard: Bool {
let args = ProcessInfo.processInfo.arguments
guard let idx = args.firstIndex(of: "--snap-state"), idx + 1 < args.count else {
return false
}
return args[idx + 1].hasPrefix("handoff-")
}
var body: some Scene {
WindowGroup {
if ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil {
Color.clear
} else if let keyboardScene = KeyboardSnapshotStage.requestedScene {
KeyboardSnapshotStage(scene: keyboardScene)
} else if let fullKeyboardScene = FullKeyboardSnapshotStage.requestedScene {
FullKeyboardSnapshotStage(scene: fullKeyboardScene)
} else if let experimentsScene = ExperimentsSnapshotStage.requestedScene {
ExperimentsSnapshotStage(scene: experimentsScene)
} else if let hostedScene = HostedKeyboardConversationStage.requestedScene {
HostedKeyboardConversationStage(scene: hostedScene, dictation: dictation)
} else {
ContentView(
dictation: dictation,
openedFromKeyboard: $openedFromKeyboard,
showSetup: $showSetup
)
.onOpenURL { url in
guard let request = KeyboardLaunchRequest(url: url) else { return }
if case .setup = request {
openedFromKeyboard = false
showSetup = true
} else {
openedFromKeyboard = true
}
dictation.handleKeyboardLaunch(request)
}
.onChange(of: scenePhase) { _, phase in
if phase == .background {
dictation.noteSceneDidEnterBackground()
openedFromKeyboard = false
} else if phase == .active {
dictation.endStaleLiveActivities()
}
}
}
}
}
}