hex/ios/HEXShared/KeyboardInterruptedRecording.swift
Ryan Vogel c7c6dc7f1f iOS keyboard: keep a dictation the screen lock cut short
When the iPhone locks mid-dictation, the keyboard's viewWillDisappear stops
the in-keyboard recording at once and transcribes what was said. On the
iOS 27.2 simulator, iOS then gives the keyboard about 11 s before it
freezes it until the keyboard is shown again. If the keyboard is ended while
frozen (here its process was killed 20 s after the lock), the next keyboard
offered nothing: the text was lost, and the recording sat unheard in the
keyboard's tmp folder.

Now the recording of a dictation the keyboard closed during stays on disk,
recorded in the App Group (KeyboardInterruptedRecording,
keyboard.interruptedRecording, one at a time), until its text is held. Any
keyboard that finds a recording no process of its own is transcribing
transcribes it and holds the text as the first keyboard would have. A
recording older than 24 hours is deleted unheard. A transcription given up
on while the keyboard was away keeps its recording for the next keyboard
instead of deleting it.

A dictation cut short this way also says where it stopped, for 8 s beside
its Paste dictation. In the full keyboard's strip that is a notice:
"Stopped at 0:44" over "The screen locked or you left the app", with Paste.
In the dictation bar it is one line. A keyboard extension can't tell the
screen locking from another app coming forward, so the note names both.
KeyboardHeldDictation gains stoppedAfter. It is optional, so text held by an
earlier build still decodes.

Unchanged: what is said after the lock is not recorded (a keyboard
extension can't record in the background), and the held text is still
offered as Paste dictation, not inserted by itself.

Verified:
- iOS 27.2 simulator on the Mac Studio, probe build with a stand-in final
  pass and the capture-debug.js ramp voice, locked with the simulator's lock
  button.
- Before: lock at 20 s, keyboard process killed 20 s later, unlock at 80 s:
  nothing offered, and the 19.1 s recording orphaned in tmp.
- After, same run: the new keyboard process transcribed the 19.7 s left
  recording and offered it with "Stopped at 0:19".
- After, lock at 45 s, no kill: the note "Stopped at 0:44" showed, and its
  Paste inserted the 44.8 s text into the field.
- KeyboardBridgeTests.testARecordingTheKeyboardClosedDuringWaitsForTheNextKeyboard
  covers the record, its clear and expiry, the note, held-dictation
  decoding and the view model's note. Full HEXMobileTests suite on a fresh
  iOS 27.2 iPhone 17 Pro simulator: 976 tests, 4 skipped, 0 failures.
- scripts/code-shape.py --check passes. KeyboardViewController.swift is at
  1,494 non-test lines.
2026-11-23 17:25:47 -05:00

63 lines
2.9 KiB
Swift

import Foundation
/// An in-keyboard recording the keyboard closed during: the screen locked, or another app came
/// forward. A keyboard extension can't record or run in the background, so the recording stops
/// there and keeps what was said. Its file stays in the keyboard's temporary folder under this
/// record until its text is held for the next keyboard (`KeyboardHeldDictation`): iOS freezes a
/// keyboard seconds after the screen locks and may end it before it has transcribed, and the
/// next keyboard then transcribes the recording instead (`KeyboardLocalDictation`).
nonisolated struct KeyboardInterruptedRecording: Codable, Equatable, Sendable {
let jobID: String
/// The recording's file name in the keyboard's temporary folder.
let fileName: String
let document: UUID?
/// The recording's length.
let seconds: TimeInterval
/// Whether the keyboard closed while it recorded, rather than while its text was worked out.
let cutShort: Bool
let at: TimeInterval
/// How long a later keyboard still transcribes a recording an earlier one left; after
/// that it is deleted unheard.
static let keptFor: TimeInterval = 24 * 60 * 60
private static let key = "keyboard.interruptedRecording"
var url: URL {
FileManager.default.temporaryDirectory.appendingPathComponent(fileName)
}
func isExpired(at now: TimeInterval) -> Bool {
now - at > Self.keptFor
}
/// The one recording left, if any: a newer one takes its place.
static func load(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) -> Self? {
guard let data = defaults?.data(forKey: key) else { return nil }
return try? JSONDecoder().decode(Self.self, from: data)
}
func save(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
if let data = try? JSONEncoder().encode(self) {
defaults?.set(data, forKey: Self.key)
}
}
/// Forgets `jobID`'s recording and deletes its file; another job's is left alone.
static func clear(jobID: String, defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
guard let record = load(defaults: defaults), record.jobID == jobID else { return }
try? FileManager.default.removeItem(at: record.url)
defaults?.removeObject(forKey: key)
}
/// What the keyboard says for a few seconds beside a dictation it offers that the keyboard
/// closing cut short: where it stopped, and why. A keyboard can't tell the screen locking
/// from another app coming forward, so it names both.
static func note(stoppedAfter seconds: TimeInterval) -> (title: String, detail: String) {
let whole = max(0, Int(seconds))
let point = String(format: "%d:%02d", whole / 60, whole % 60)
return (
String(localized: "Stopped at \(point)"),
String(localized: "The screen locked or you left the app")
)
}
}