mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
What changed - Experiments › Record typing logs (off by default, App Group key experiments.typingLogs). It is separate from Learn how I type, which is unchanged. While it is on, the keyboard writes NDJSON day files to TypingLogs/yyyy-MM-dd.ndjson in the App Group. Events: session_start/session_end (field traits, keyboard size and safe area, orientation, appearance, device, build, locale); layout (every key's frame and hit frame, the strip and its suggestion slots, and a hash, with the key list written once per file); touch (began/moved/ended/cancelled with raw x/y, majorRadius and tolerance, force, UITouch.timestamp, the key the engine resolved, and the key under the point when they differ; moves are throttled to 2 pt and 8 ms); edit (insert, delete, word delete, autocorrect, autocorrect undo, suggestion, double-space period, return and send, paste, undo dictation, trackpad cursor); candidates; context (documentContextBefore/AfterInput capped at 200 characters, after each word, return and dictation and once host changes settle, marked cleared when the host empties the field); ui (accents, trackpad, the Hex key and its menu, strip chips, globe); dictation (raw, learned fixes, spelling, tagger, Apple Intelligence, translation, formatting, inserted text, durations, engine and locale, from in-keyboard and app dictation); and dictation_outcome (from a second DictationEditTracker). The schema is in ios/docs/typing-logs.md. - Password, new password, one-time code, every credit card content type, and secure fields write one skipped line and nothing else. A new field, or new traits, starts a new session. - TypingLogger buffers events on the main thread. TypingLogStore encodes and appends them on a serial utility queue every 2 s, when the keyboard goes away, and on a memory warning. Each day is capped at 64 MB with one cap_reached line, and files are kept 14 days. When the App Group can't be reached (no Full Access), events are dropped silently. Nothing is sent. - Hooks: FullKeyboardEngine.typingLog, and the KeyboardKeyPlaneView touch path, which the UITouch overrides now reach through began/moved/ended/cancelled(id:sample:). Every call is skipped while the logger is nil, so typing is unchanged with the experiment off. KeyboardSnapshot gains optional stages, sent only while the experiment is on. KeyboardSpeech.transcribe also returns its locale. - App: the toggle and a Typing logs screen (each day with its size and event count, Share per day, Share all through the share sheet, Delete all with a confirmation). Also --snap-state experiments and typing-logs scenes, and an --experiment-typing-logs launch argument. Why Ryan wants to export a day of raw typing to his Mac, to test and train prediction models and a per-user touch model (adaptive key targets). Verified - xcodebuild test on the iPhone 18 Pro simulator: 177 tests, 0 failures. This includes 9 new TypingLogTests: line format and escaping; day rotation with layouts in full once per file; Delete all while the writer holds the file open; the daily cap (tested at 4 KB) across two writers; 14-day retention; the skip rule; a skipped session writing one line; and a real FullKeyboardView typing 25 taps through the plane's touch path (autocorrect, delete, double-space period, the 123 layer, return, a long-press é). That log's edits replay to the field's text, "Don't go yet. 7 ok\nCafé". An independent Python replay of the same file matched, and every lifted key's point was inside its logged hit frame. - Overhead, simulator Debug, 3,000 taps: about 9 µs a tap on the main thread (38 → 47 µs) and about 1 KB a tap (5.2 lines), so about 12 MB an hour at 200 characters a minute. - The Release generic-iOS build with CODE_SIGNING_ALLOWED=NO succeeds. - Simulator screenshots of the toggle and the Typing logs screen. - Not verified on a physical iPhone: real touch radii and sampling rates, how often hosts call textDidChange, whether documentIdentifier stays stable, the extension's write cost, and AirDrop from the share sheet.
624 lines
26 KiB
Swift
624 lines
26 KiB
Swift
import SwiftUI
|
|
import UIKit
|
|
|
|
@MainActor
|
|
final class KeyboardViewController: UIInputViewController {
|
|
private let bridge = KeyboardBridge()
|
|
private let viewModel = KeyboardViewModel()
|
|
private let localDictation = KeyboardLocalDictation()
|
|
private var pollTimer: Timer?
|
|
private var coldStartJobID = UUID().uuidString
|
|
private var lastURLRefreshBucket: Int = 0
|
|
private var lastFullAccessNote: TimeInterval = 0
|
|
private var surface: UIView?
|
|
private var fullKeyboard: FullKeyboardView?
|
|
private var heightConstraint: NSLayoutConstraint?
|
|
private var pollInterval: TimeInterval = 0
|
|
private var didHandOffCurrentField = false
|
|
private var didWarmLexicon = false
|
|
/// Learn how I type: what becomes of each dictation Hex inserts.
|
|
private let dictationEdits = DictationEditTracker()
|
|
private lazy var documentTarget = DocumentProxyTextTarget { [weak self] in self?.textDocumentProxy }
|
|
/// Record typing logs: the process's logger while the experiment is on.
|
|
private var typingLog: TypingLogger?
|
|
/// The field the current typing log session is for.
|
|
private var typingLogTraits: TypingLogTraits?
|
|
/// Follows dictations for typing logs' outcomes, apart from Learn how I type's tracker.
|
|
private let logDictationEdits = DictationEditTracker()
|
|
private var pendingHostContext: DispatchWorkItem?
|
|
private var isShown = false
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
configureView()
|
|
syncFromBridge(animated: false)
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
coldStartJobID = UUID().uuidString
|
|
didHandOffCurrentField = false
|
|
// The keyboard may come back for another field: settle on where the dictation was last seen.
|
|
dictationEdits.finish(nil)
|
|
mountSurface()
|
|
fullKeyboard?.prepareForField()
|
|
isShown = true
|
|
beginTypingLogSession(trigger: "appear")
|
|
handOffNumericPadIfNeeded()
|
|
syncFromBridge(animated: false)
|
|
schedulePoll(force: true)
|
|
}
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
super.viewDidAppear(animated)
|
|
warmFullKeyboardLexiconIfNeeded()
|
|
}
|
|
|
|
override func viewWillDisappear(_ animated: Bool) {
|
|
super.viewWillDisappear(animated)
|
|
if dictationEdits.isTracking { dictationEdits.finish(editContext) }
|
|
TypingJournal.sharedIfEnabled()?.save()
|
|
isShown = false
|
|
endTypingLogSession(reason: "disappear", settleDictation: editContext)
|
|
pollTimer?.invalidate()
|
|
pollTimer = nil
|
|
}
|
|
|
|
override func didReceiveMemoryWarning() {
|
|
super.didReceiveMemoryWarning()
|
|
fullKeyboard?.handleMemoryWarning()
|
|
typingLog?.flush()
|
|
}
|
|
|
|
override func viewWillLayoutSubviews() {
|
|
super.viewWillLayoutSubviews()
|
|
if let fullKeyboard, heightConstraint?.constant != fullKeyboard.preferredHeight {
|
|
heightConstraint?.constant = fullKeyboard.preferredHeight
|
|
preferredContentSize.height = fullKeyboard.preferredHeight
|
|
}
|
|
}
|
|
|
|
override func textWillChange(_ textInput: UITextInput?) {
|
|
super.textWillChange(textInput)
|
|
if KeyboardFieldRouting.route(for: textDocumentProxy.keyboardType) != .handOff {
|
|
didHandOffCurrentField = false
|
|
}
|
|
}
|
|
|
|
override func textDidChange(_ textInput: UITextInput?) {
|
|
super.textDidChange(textInput)
|
|
updateKeyboardTraits()
|
|
followTypingLogField()
|
|
fullKeyboard?.textDidChange()
|
|
observeDictationEdits(fromKeys: false)
|
|
noteHostTextChange()
|
|
handOffNumericPadIfNeeded()
|
|
}
|
|
|
|
// MARK: - Record typing logs
|
|
|
|
/// Starts a session for the field the keyboard is in, while the experiment is on. A
|
|
/// field that must not be logged gets a `skipped` line and nothing else.
|
|
private func beginTypingLogSession(trigger: String) {
|
|
guard let log = TypingLogger.sharedIfEnabled() else {
|
|
typingLog = nil
|
|
return
|
|
}
|
|
typingLog = log
|
|
log.contextSource = { [weak self] in
|
|
self?.typingLogContext ?? TypingLogContext(before: nil, after: nil, hasText: false)
|
|
}
|
|
let traits = TypingLogTraits(textDocumentProxy, document: textDocumentProxy.documentIdentifier)
|
|
typingLogTraits = traits
|
|
log.beginSession(typingLogSessionInfo(trigger: trigger, traits: traits))
|
|
attachTypingLog()
|
|
}
|
|
|
|
/// `settleDictation` is the text the last dictation's outcome is judged on (nil: where
|
|
/// it was last seen, for a field the keyboard already left).
|
|
private func endTypingLogSession(reason: String, settleDictation context: DictationEditTracker.Context?) {
|
|
pendingHostContext?.cancel()
|
|
pendingHostContext = nil
|
|
guard let log = typingLog else { return }
|
|
if logDictationEdits.isTracking { logDictationEdits.finish(context) }
|
|
fullKeyboard?.typingLog = nil
|
|
log.endSession(reason: reason)
|
|
typingLogTraits = nil
|
|
}
|
|
|
|
private func attachTypingLog() {
|
|
guard let fullKeyboard else { return }
|
|
let log = typingLog?.isRecording == true ? typingLog : nil
|
|
guard fullKeyboard.typingLog !== log || log != nil else { return }
|
|
fullKeyboard.typingLog = log
|
|
fullKeyboard.typingLogSessionBegan()
|
|
}
|
|
|
|
/// Turned on or off while the keyboard is shown.
|
|
private func syncTypingLogEnabled() {
|
|
let enabled = TypingLogSettings().isEnabled
|
|
if enabled, typingLog == nil, isShown {
|
|
beginTypingLogSession(trigger: "enabled")
|
|
} else if !enabled, typingLog != nil {
|
|
endTypingLogSession(reason: "disabled", settleDictation: nil)
|
|
typingLog = nil
|
|
_ = TypingLogger.sharedIfEnabled()
|
|
}
|
|
}
|
|
|
|
/// Another field, or the same field with new traits: a new session (which may be skipped).
|
|
private func followTypingLogField() {
|
|
guard typingLog != nil, isShown else { return }
|
|
let traits = TypingLogTraits(textDocumentProxy, document: textDocumentProxy.documentIdentifier)
|
|
guard traits.signature != typingLogTraits?.signature else { return }
|
|
endTypingLogSession(reason: "field_changed", settleDictation: nil)
|
|
beginTypingLogSession(trigger: "field")
|
|
}
|
|
|
|
/// The host changed the text or moved the cursor. Hosts call this in bursts (and some
|
|
/// after Hex's own edits), so the context is written once things settle, at once when
|
|
/// the field was emptied.
|
|
private func noteHostTextChange() {
|
|
guard let log = typingLog, log.isRecording else { return }
|
|
pendingHostContext?.cancel()
|
|
guard textDocumentProxy.hasText else {
|
|
pendingHostContext = nil
|
|
log.context("text_did_change")
|
|
return
|
|
}
|
|
let work = DispatchWorkItem { [weak self] in
|
|
MainActor.assumeIsolated {
|
|
self?.pendingHostContext = nil
|
|
self?.typingLog?.context("text_did_change")
|
|
}
|
|
}
|
|
pendingHostContext = work
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: work)
|
|
}
|
|
|
|
private var typingLogContext: TypingLogContext {
|
|
TypingLogContext(
|
|
before: textDocumentProxy.documentContextBeforeInput,
|
|
after: textDocumentProxy.documentContextAfterInput,
|
|
hasText: textDocumentProxy.hasText
|
|
)
|
|
}
|
|
|
|
private func typingLogSessionInfo(trigger: String, traits: TypingLogTraits) -> TypingLogSessionInfo {
|
|
let bundle = Bundle.main.infoDictionary
|
|
let orientation = fullKeyboard?.orientation ?? FullKeyboardView.orientation(for: traitCollection)
|
|
let dark = textDocumentProxy.keyboardAppearance == .dark || traitCollection.userInterfaceStyle == .dark
|
|
return TypingLogSessionInfo(
|
|
trigger: trigger,
|
|
surface: fullKeyboard == nil ? "bar" : "full",
|
|
traits: traits,
|
|
bounds: view.bounds.size,
|
|
safeArea: view.safeAreaInsets,
|
|
orientation: orientation == .landscape ? "landscape" : "portrait",
|
|
appearance: dark ? "dark" : "light",
|
|
device: TypingLogSessionInfo.deviceModel,
|
|
system: "\(UIDevice.current.systemName) \(UIDevice.current.systemVersion)",
|
|
version: bundle?["CFBundleShortVersionString"] as? String ?? "",
|
|
build: bundle?["CFBundleVersion"] as? String ?? "",
|
|
locale: Locale.current.identifier,
|
|
languages: Array(Locale.preferredLanguages.prefix(4)),
|
|
keyboardLanguage: primaryLanguage,
|
|
inputMode: textDocumentProxy.documentInputMode?.primaryLanguage,
|
|
fullAccess: hasFullAccess,
|
|
settings: [
|
|
TypingLogField("autocorrect", .bool(FullKeyboardSettings().autocorrect)),
|
|
TypingLogField("learnHowIType", .bool(TypingJournalSettings().isEnabled)),
|
|
TypingLogField("cleanupTagger", .bool(CleanupTaggerSettings().isEnabled)),
|
|
TypingLogField("hexKeyPlacement", .string(HexKeyPlacement.current.rawValue)),
|
|
TypingLogField("style", .string(bridge.style.rawValue)),
|
|
]
|
|
)
|
|
}
|
|
|
|
// MARK: - Learn how I type
|
|
|
|
/// Dictations are followed only while Learn how I type is on, in plain text fields
|
|
/// that allow autocorrect.
|
|
private var learnsFromDictation: Bool {
|
|
guard TypingJournalSettings().isEnabled,
|
|
case .keys(let kind, _) = KeyboardFieldRouting.route(for: textDocumentProxy.keyboardType)
|
|
else { return false }
|
|
return TypingJournal.allows(field: kind, target: documentTarget)
|
|
}
|
|
|
|
private var editContext: DictationEditTracker.Context {
|
|
DictationEditTracker.Context(
|
|
before: textDocumentProxy.documentContextBeforeInput,
|
|
after: textDocumentProxy.documentContextAfterInput,
|
|
hasText: textDocumentProxy.hasText
|
|
)
|
|
}
|
|
|
|
private func observeDictationEdits(fromKeys: Bool) {
|
|
guard dictationEdits.isTracking || logDictationEdits.isTracking else { return }
|
|
let context = editContext
|
|
if logDictationEdits.isTracking { logDictationEdits.observe(context, fromKeys: fromKeys) }
|
|
if dictationEdits.isTracking { dictationEdits.observe(context, fromKeys: fromKeys) }
|
|
}
|
|
|
|
/// A return key labelled Send ends the dictation's edit window: what is there is sent.
|
|
private func returnKeyWillInsert() {
|
|
guard textDocumentProxy.returnKeyType == .send else { return }
|
|
if logDictationEdits.isTracking { logDictationEdits.finish(editContext) }
|
|
guard dictationEdits.isTracking else { return }
|
|
dictationEdits.finish(editContext)
|
|
}
|
|
|
|
private func handOffNumericPadIfNeeded() {
|
|
guard fullKeyboard != nil else { return }
|
|
if KeyboardFieldRouting.route(for: textDocumentProxy.keyboardType) == .handOff {
|
|
guard !didHandOffCurrentField else { return }
|
|
didHandOffCurrentField = true
|
|
if needsInputModeSwitchKey {
|
|
advanceToNextInputMode()
|
|
}
|
|
} else {
|
|
didHandOffCurrentField = false
|
|
}
|
|
}
|
|
|
|
private func warmFullKeyboardLexiconIfNeeded() {
|
|
guard let fullKeyboard else { return }
|
|
fullKeyboard.warmUpTextCheckerAsync()
|
|
guard !didWarmLexicon else { return }
|
|
didWarmLexicon = true
|
|
// UIKit calls back on its lexicon-request queue. A main-actor closure here trips
|
|
// Swift's runtime isolation check (EXC_BREAKPOINT, TestFlight build 3), so the
|
|
// callback is nonisolated and hops to the main actor itself.
|
|
requestSupplementaryLexicon { @Sendable [weak self] lexicon in
|
|
Task { @MainActor in
|
|
self?.fullKeyboard?.loadLexicon(lexicon)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func configureView() {
|
|
view.backgroundColor = .clear
|
|
|
|
viewModel.inputViewController = self
|
|
dictationEdits.onResult = { result in
|
|
TypingJournal.sharedIfEnabled()?.recordDictation(result)
|
|
}
|
|
logDictationEdits.onResult = { [weak self] result in
|
|
self?.typingLog?.dictationOutcome(result)
|
|
}
|
|
localDictation.corrections = {
|
|
TypingJournal.sharedIfEnabled()?.dictationCorrections() ?? []
|
|
}
|
|
viewModel.onDictateTap = { [weak self] in self?.startWarmDictation() }
|
|
viewModel.onStopTap = { [weak self] in
|
|
guard let self else { return }
|
|
if self.localDictation.isActive { self.localDictation.stop() } else { self.stopWarmDictation() }
|
|
}
|
|
viewModel.onCancelTap = { [weak self] in
|
|
guard let self else { return }
|
|
if self.localDictation.isActive { self.localDictation.cancel() } else { self.cancelWarmDictation() }
|
|
}
|
|
if KeyboardLocalDictation.isSupported {
|
|
viewModel.onLocalDictateTap = { [weak self] in
|
|
guard let self else { return }
|
|
CleanupTagger.prepareIfEnabled(style: self.bridge.style)
|
|
self.localDictation.start(in: self.view, allowBluetoothMic: self.bridge.allowBluetoothMic)
|
|
}
|
|
}
|
|
viewModel.onStyleSelected = { [weak self] style in
|
|
guard let self else { return }
|
|
self.bridge.style = style
|
|
if self.viewModel.style != style {
|
|
self.viewModel.style = style
|
|
}
|
|
self.refreshColdStartURL(now: Date().timeIntervalSince1970, force: true)
|
|
}
|
|
viewModel.onReturnAppSelected = { [weak self] appID in
|
|
guard let self else { return }
|
|
self.bridge.returnAppID = appID
|
|
if self.viewModel.returnAppID != appID {
|
|
self.viewModel.returnAppID = appID
|
|
}
|
|
self.refreshColdStartURL(now: Date().timeIntervalSince1970, force: true)
|
|
}
|
|
viewModel.onSpace = { [weak self] in
|
|
self?.typingLog?.edit("insert", [
|
|
TypingLogField("inserted", .string(" ")),
|
|
TypingLogField("src", .string("bar")),
|
|
])
|
|
self?.textDocumentProxy.insertText(" ")
|
|
self?.observeDictationEdits(fromKeys: true)
|
|
self?.typingLog?.context("space")
|
|
}
|
|
viewModel.onDelete = { [weak self] in
|
|
if let self, let log = self.typingLog {
|
|
let deleted = self.textDocumentProxy.documentContextBeforeInput?.last.map { String($0) }
|
|
log.edit("delete", [
|
|
deleted.map { TypingLogField("deleted", .string($0)) } ?? TypingLogField("deletedCount", .int(1)),
|
|
TypingLogField("src", .string("bar")),
|
|
])
|
|
}
|
|
self?.textDocumentProxy.deleteBackward()
|
|
self?.observeDictationEdits(fromKeys: true)
|
|
}
|
|
viewModel.onReturn = { [weak self] in
|
|
if let self {
|
|
self.typingLog?.edit("return", [
|
|
TypingLogField("inserted", .string("\n")),
|
|
TypingLogField("returnKeyType", .string(TypingLogTraits.name(self.textDocumentProxy.returnKeyType ?? .default))),
|
|
TypingLogField("send", .bool(self.textDocumentProxy.returnKeyType == .send)),
|
|
TypingLogField("src", .string("bar")),
|
|
])
|
|
}
|
|
self?.returnKeyWillInsert()
|
|
self?.textDocumentProxy.insertText("\n")
|
|
self?.observeDictationEdits(fromKeys: true)
|
|
self?.typingLog?.context("return")
|
|
}
|
|
viewModel.onOpenURL = { [weak self] url in
|
|
guard let self else { return }
|
|
ContainingAppOpener.open(url, from: self)
|
|
}
|
|
|
|
let heightConstraint = view.heightAnchor.constraint(equalToConstant: 140)
|
|
heightConstraint.priority = UILayoutPriority(999)
|
|
heightConstraint.isActive = true
|
|
self.heightConstraint = heightConstraint
|
|
mountSurface()
|
|
}
|
|
|
|
/// The dictation bar, or the full keyboard when its experiment is on (iPhone only).
|
|
/// Only switches while idle.
|
|
private func mountSurface() {
|
|
let wantsFull = FullKeyboardSettings().isEnabled && UIDevice.current.userInterfaceIdiom == .phone
|
|
guard surface == nil || wantsFull != (fullKeyboard != nil) else { return }
|
|
guard surface == nil || viewModel.stagePhase == .idle || viewModel.stagePhase == .failed else { return }
|
|
|
|
children.forEach { child in
|
|
child.willMove(toParent: nil)
|
|
child.removeFromParent()
|
|
}
|
|
surface?.removeFromSuperview()
|
|
fullKeyboard = nil
|
|
didWarmLexicon = false
|
|
|
|
let mounted: UIView
|
|
if wantsFull {
|
|
let keyboard = FullKeyboardView(
|
|
model: viewModel,
|
|
target: DocumentProxyTextTarget { [weak self] in self?.textDocumentProxy }
|
|
)
|
|
keyboard.hostController = self
|
|
keyboard.allowsHaptics = { [weak self] in self?.hasFullAccess ?? false }
|
|
keyboard.onTextEdit = { [weak self] in self?.observeDictationEdits(fromKeys: true) }
|
|
keyboard.onUndoDictation = { [weak self] in
|
|
self?.dictationEdits.didUndo()
|
|
self?.logDictationEdits.didUndo()
|
|
}
|
|
keyboard.onReturnKey = { [weak self] in self?.returnKeyWillInsert() }
|
|
fullKeyboard = keyboard
|
|
mounted = keyboard
|
|
heightConstraint?.constant = keyboard.preferredHeight
|
|
} else {
|
|
let hosting = UIHostingController(
|
|
rootView: KeyboardSurfaceView(model: viewModel, defersDotScopeUntilActive: true)
|
|
)
|
|
hosting.view.backgroundColor = .clear
|
|
addChild(hosting)
|
|
hosting.didMove(toParent: self)
|
|
mounted = hosting.view
|
|
heightConstraint?.constant = 140
|
|
}
|
|
preferredContentSize.height = heightConstraint?.constant ?? 140
|
|
mounted.translatesAutoresizingMaskIntoConstraints = false
|
|
view.addSubview(mounted)
|
|
NSLayoutConstraint.activate([
|
|
mounted.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
mounted.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
mounted.topAnchor.constraint(equalTo: view.topAnchor),
|
|
mounted.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
|
])
|
|
surface = mounted
|
|
attachTypingLog()
|
|
schedulePoll(force: false)
|
|
}
|
|
|
|
/// The dictation bar polls at 25 Hz; the full keyboard at 4 Hz, and 30 Hz while recording.
|
|
private func schedulePoll(force: Bool) {
|
|
let busy = viewModel.stagePhase == .recording || viewModel.stagePhase == .transcribing
|
|
|| viewModel.pendingCommand != nil
|
|
let interval: TimeInterval = fullKeyboard == nil ? 0.04 : (busy ? 1.0 / 30 : 0.25)
|
|
guard force || (pollTimer != nil && interval != pollInterval) else { return }
|
|
pollInterval = interval
|
|
pollTimer?.invalidate()
|
|
pollTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] _ in
|
|
MainActor.assumeIsolated {
|
|
self?.syncFromBridge(animated: true)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func syncFromBridge(animated: Bool) {
|
|
let now = Date().timeIntervalSince1970
|
|
updateKeyboardTraits()
|
|
fullKeyboard?.syncSettings()
|
|
syncTypingLogEnabled()
|
|
|
|
#if targetEnvironment(simulator)
|
|
let simSnap = bridge.snapshot()
|
|
let effectiveFullAccess = hasFullAccess || simSnap.isAvailable(at: now)
|
|
#else
|
|
let effectiveFullAccess = hasFullAccess
|
|
#endif
|
|
|
|
guard effectiveFullAccess else {
|
|
viewModel.applyBridgeState(
|
|
hasFullAccess: false,
|
|
snapshot: .offline,
|
|
isModelInstalled: false,
|
|
style: viewModel.style,
|
|
returnAppID: viewModel.returnAppID,
|
|
now: now,
|
|
clearPending: true,
|
|
animated: animated
|
|
)
|
|
return
|
|
}
|
|
|
|
if now - lastFullAccessNote >= 5 {
|
|
lastFullAccessNote = now
|
|
bridge.fullAccessSeenAt = now
|
|
}
|
|
|
|
if dictationEdits.isTracking {
|
|
dictationEdits.tick { editContext }
|
|
}
|
|
if logDictationEdits.isTracking {
|
|
logDictationEdits.tick { editContext }
|
|
}
|
|
|
|
let localSnapshot = localDictation.snapshot(now: now)
|
|
let snap = localSnapshot ?? bridge.snapshot()
|
|
var insertedResult = false
|
|
|
|
if snap.hasDeliverableResult(at: now),
|
|
let resultID = snap.resultID,
|
|
let rawTranscript = snap.transcript,
|
|
bridge.lastInsertedResultID != resultID {
|
|
let context = editContext
|
|
let insertion = TranscriptFormatter.insertionText(
|
|
for: rawTranscript,
|
|
beforeContext: context.before,
|
|
afterContext: context.after
|
|
)
|
|
if learnsFromDictation {
|
|
dictationEdits.willInsert(heard: snap.heard, inserted: insertion, context: context)
|
|
} else {
|
|
dictationEdits.finish(context)
|
|
}
|
|
if let log = typingLog, log.isRecording {
|
|
// A translation can't be compared with what was heard.
|
|
let heard = snap.stages?.translated == nil ? (snap.heard ?? snap.stages?.raw) : nil
|
|
logDictationEdits.willInsert(heard: heard, inserted: insertion, context: context)
|
|
} else if logDictationEdits.isTracking {
|
|
logDictationEdits.finish(context)
|
|
}
|
|
textDocumentProxy.insertText(insertion)
|
|
fullKeyboard?.didInsertDictation(insertion)
|
|
typingLog?.dictation(
|
|
source: localSnapshot == nil ? "app" : "keyboard",
|
|
resultID: resultID,
|
|
style: bridge.style.rawValue,
|
|
stages: snap.stages,
|
|
delivered: rawTranscript,
|
|
inserted: insertion
|
|
)
|
|
observeDictationEdits(fromKeys: true)
|
|
bridge.lastInsertedResultID = resultID
|
|
localDictation.markDelivered(resultID: resultID)
|
|
insertedResult = true
|
|
coldStartJobID = UUID().uuidString
|
|
}
|
|
|
|
let quickReturnApps = bridge.quickReturnApps
|
|
if viewModel.quickReturnApps != quickReturnApps {
|
|
viewModel.quickReturnApps = quickReturnApps
|
|
}
|
|
|
|
viewModel.applyBridgeState(
|
|
hasFullAccess: true,
|
|
snapshot: snap,
|
|
isModelInstalled: bridge.isModelInstalled,
|
|
style: bridge.style,
|
|
returnAppID: bridge.returnAppID,
|
|
now: now,
|
|
clearPending: insertedResult,
|
|
animated: animated
|
|
)
|
|
|
|
refreshColdStartURL(now: now, force: false)
|
|
schedulePoll(force: false)
|
|
}
|
|
|
|
private func refreshColdStartURL(now: TimeInterval, force: Bool) {
|
|
let bucket = Int(now / 2.0)
|
|
guard force || bucket != lastURLRefreshBucket else { return }
|
|
lastURLRefreshBucket = bucket
|
|
mountSurface()
|
|
|
|
let request: KeyboardLaunchRequest
|
|
if bridge.isModelInstalled {
|
|
request = .startRecording(
|
|
jobID: coldStartJobID,
|
|
requestedAt: now,
|
|
style: bridge.style,
|
|
returnAppID: bridge.returnAppID
|
|
)
|
|
} else {
|
|
request = .startSession(
|
|
requestedAt: now,
|
|
returnAppID: bridge.returnAppID
|
|
)
|
|
}
|
|
viewModel.coldStartURL = request.url
|
|
}
|
|
|
|
private func updateKeyboardTraits() {
|
|
let globe = needsInputModeSwitchKey
|
|
if viewModel.needsGlobeKey != globe {
|
|
viewModel.needsGlobeKey = globe
|
|
}
|
|
// Fields that ask for a dark keyboard get one even in light mode, like the system keyboard.
|
|
overrideUserInterfaceStyle = textDocumentProxy.keyboardAppearance == .dark ? .dark : .unspecified
|
|
let nextReturn: String = switch textDocumentProxy.returnKeyType {
|
|
case .send: "Send"
|
|
case .google, .yahoo, .search: "Search"
|
|
case .go: "Go"
|
|
case .done: "Done"
|
|
case .join: "Join"
|
|
case .next: "Next"
|
|
case .route: "Route"
|
|
case .continue: "Continue"
|
|
case .emergencyCall: "SOS"
|
|
default: "Return"
|
|
}
|
|
if viewModel.returnKeyLabel != nextReturn {
|
|
viewModel.returnKeyLabel = nextReturn
|
|
}
|
|
}
|
|
|
|
private func startWarmDictation() {
|
|
let now = Date().timeIntervalSince1970
|
|
guard viewModel.hasFullAccess, viewModel.currentSnapshot.isAvailable(at: now) else { return }
|
|
let jobID = UUID().uuidString
|
|
viewModel.setPendingCommand(
|
|
KeyboardViewModel.PendingCommand(kind: .start, jobID: jobID, sentAt: now),
|
|
now: now,
|
|
animated: true
|
|
)
|
|
bridge.send(.start, jobID: jobID, style: viewModel.style, sentAt: now)
|
|
}
|
|
|
|
private func stopWarmDictation() {
|
|
let now = Date().timeIntervalSince1970
|
|
guard let jobID = viewModel.currentSnapshot.jobID ?? viewModel.pendingCommand?.jobID else { return }
|
|
viewModel.setPendingCommand(
|
|
KeyboardViewModel.PendingCommand(kind: .stop, jobID: jobID, sentAt: now),
|
|
now: now,
|
|
animated: true
|
|
)
|
|
bridge.send(.stop, jobID: jobID, style: viewModel.style, sentAt: now)
|
|
}
|
|
|
|
private func cancelWarmDictation() {
|
|
let now = Date().timeIntervalSince1970
|
|
guard let jobID = viewModel.currentSnapshot.jobID ?? viewModel.pendingCommand?.jobID else { return }
|
|
viewModel.setPendingCommand(
|
|
KeyboardViewModel.PendingCommand(kind: .cancel, jobID: jobID, sentAt: now),
|
|
now: now,
|
|
animated: true
|
|
)
|
|
bridge.send(.cancel, jobID: jobID, style: viewModel.style, sentAt: now)
|
|
}
|
|
}
|