hex/ios/HEXMobile/Onboarding.swift
Ryan Vogel 9427d069bf
iOS: onboarding follows the desktop flow (#2136)
* iOS: launch screen in the app's first-frame black

The launch screen's LaunchBackground was a dark teal (sRGB 7,17,20, shown
as about 2,17,19) for the ~1.5 s before the app's first frame, which is
HexTheme.bgDeep (0x0A0B0D). It now uses bgDeep's exact components, so
launch hands over to the app without a colour jump.

Verified: built for Simulator; fresh-install launch recorded.

* iOS: onboarding follows the desktop's pages, with the polish fixes

Dax found the iPhone onboarding annoying; Ryan and Dax asked for the
desktop's flow. The three pages (Welcome, Set up Hex, Try the keyboard)
become the desktop's six, in its order and copy style:

1. Setting up Hex: the speech model starts downloading as onboarding
   opens (whichever engine transcriberFactory picks). Before, only home's
   "Set up" started a Parakeet download, so onboarding said "You're all
   set" while home said "Finish setup · 1 step". A launch that found
   Parakeet ended mid-load stays failed (ParakeetLoadGuard) until Try
   again. Moves on after a beat; a failed start holds it with Try again.
2. With Hex: a short demo, a reply dictated with the Hex key. Skip.
3. Let Hex hear you: Microphone and Hex keyboard rows with a one-line
   detail that stays (steady heights) and one-line copy ("With Full
   Access", no mid-phrase wrap). A done row shows only its check (the
   Keyboard row used to keep its Open Settings pill). The pill and the
   page's button say Continue before iOS's prompt (App Review 5.1.1(iv)),
   Continue never needs the microphone, and the page moves on once both
   rows are done. No Skip.
4. Almost ready: only while the model downloads, with its progress;
   Continue without a model only after a failure or 30 s without progress.
5. Your turn: the practice. The field keeps the keyboard up (the old
   page unfocused it ~150 ms after the first letter) and counts once a
   dictation arrives (lastInsertedResultID changes) or three words are
   typed with Full Access. An empty dictation says "Hex didn't hear any
   words. Try again a little louder." on this page only; the keyboard
   notes it in the App Group (keyboard.lastEmptyDictationAt) and still
   shows nothing itself. Skip.
6. Hex is ready: only once every step (model included) is done, else
   "Almost ready" with what's left. Start using Hex never waits.

The download's pill (language and percent) sits in the top bar until the
model is ready; Skip shows on the demo and the practice only. The page
counter and Back are gone, as on the desktop. Saved pages from earlier
builds resume on their new equivalents. Snapshot scenes are renamed
onboarding-setup/-demo/-permissions(-done)/-waiting/-failed/-try(-done,
-empty,-off)/-ready, plus onboarding-tour. zh-Hans strings added.

Verified: xcodebuild test on an iOS 27.2 simulator (385 tests, 0
failures, 1 skipped), including new KeyboardBridgeTests for the empty
dictation note, the practice rule, and saved-page migration; English and
Chinese snapshots of every page; a fresh-install recording.

* iOS onboarding: the practice field takes focus when Hex comes back

Coming back from Settings with both permission rows done moves straight
on to Your turn, but the page opened before Hex was active again, so its
field never took focus and the keyboard stayed down. The page now
focuses the field again when the scene becomes active (unless it already
has focus).

Verified: fresh-install recording on an iOS 27.2 simulator: back from
Settings, Your turn opens with the keyboard up.

* iOS: Ready waits for the model the Hex key itself dictates with

On iOS 26 the keyboard transcribes inside itself with Apple Speech,
whatever Hex uses. When Hex dictates with Parakeet (an English iPhone
without SpeechTranscriber, or a cached Parakeet model), onboarding and
home said Ready while the keyboard's Apple Speech model wasn't there,
and the first Hex key tap said "Open Hex to download English."

KeyboardSpeechPreparation (owned by ContentView, started at launch)
prepares the keyboard's engine:
- Hex dictates with Apple Speech: Hex's model is the same one, so it
  follows Hex's.
- Otherwise it resolves the keyboard's language (AppleSpeechLanguages,
  as the keyboard does) and installs it with AppleSpeechAssets.install,
  the keyboard's own call, which also clears keyboardNeedsHex.
- Apple has no model for it on this iPhone, or Speech lists nothing: it
  marks keyboardNeedsHex, so the Hex key opens Hex (which dictates with
  its own model) from the first tap instead of after a failed one.
  Re-marked at each launch, as the mark lasts a day.
- A failed download (connection) shows on onboarding with Try again.

Why not have the keyboard use Hex's Parakeet: it can't load within a
keyboard extension's memory limit, and handing every dictation to Hex
leaves the app being typed in each time. In-keyboard Apple Speech stays
the path, with the handoff only where Apple has no model.

Onboarding's model stage combines both (OnboardingModel.combine): ready
only when both are, a failure first, fractions averaged only when the
keyboard actually downloads. Home's status line and the setup sheet's
Speech model row count it the same way.

Verified: xcodebuild test, iOS 27.2 simulator: 386 tests, 0 failures,
1 skipped (new: the combine rules). In the simulator (no Apple Speech
assets: "not subscribed to assistant.en_US") it marks the handoff and
onboarding goes on to Parakeet's Ready; a real iPhone must confirm the
Apple Speech download path.

* iOS onboarding: the desktop's motion, the pill flying between places

The desktop onboarding's motion (onboarding_kit/landing.rs, pill.rs):
- Each page's parts roll in 8 pt with a fade on a 0.34 s zero-bounce
  spring, 0.05 s apart (onboardingStagger), and the leaving page lifts
  away 8 pt on 0.26 s. Replaces the old 28 pt sideways page slide.
- The download pill flies from under Setting up Hex to the top bar and
  stays there across pages (matchedGeometryEffect), on the desktop's
  0.42 s flight spring.
- Reduce Motion: parts fade without moving or staggering, the pill
  cross-fades, pages fade.
Stills (--snap-state) show every part at once.

"Watch another one" stays off: on the desktop it fills a long download
on Almost ready; on iPhone the demo is ~6 s and moves on by itself, so a
replay control would only add a label.

Verified: xcodebuild test (386, 0 failures, 1 skipped); fresh-install
recording on an iOS 27.2 simulator, frames checked at each page change.

* iOS onboarding: steady progress and an untruncated pill in flight

Two things the recording showed:
- In the simulator the keyboard's Apple Speech download starts, then
  can't be had and hands off to Hex; that short attempt still counted as
  half of onboarding's progress, so the percent jumped from 0% to 50%.
  A handoff no longer counts as a download.
- The pill flew with its frame, so the small pill's language truncated
  ("Engli… 2%") for about half a second after landing. It now flies by
  position only; each pill keeps its own size.

Verified: fresh-install recording on an iOS 27.2 simulator, frames at
15 fps around the Setting up Hex to demo change, and the percent climbing
from 0% in steps.
2026-11-21 03:27:38 +00:00

918 lines
36 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
import UIKit
/// Where the speech model stands, as onboarding shows it.
struct OnboardingModel: Equatable {
enum Stage: Equatable {
case starting
case downloading(Double)
case ready
case failed(String)
}
var stage: Stage
/// The language it's for, in Hex's interface language ("English").
var language: String
var isReady: Bool { stage == .ready }
var isFailed: Bool {
if case .failed = stage { return true }
return false
}
var fraction: Double {
switch stage {
case .downloading(let fraction): fraction
case .ready: 1
default: 0
}
}
/// Hex's own model and the one the Hex key dictates with (KeyboardSpeechPreparation): ready
/// only once both are, so Ready means the Hex key works in other apps.
@MainActor
init(dictation: DictationController, keyboard: KeyboardSpeechPreparation) {
let app: Stage
if dictation.isModelReady {
app = .ready
} else {
switch dictation.phase {
case .preparingModel: app = .downloading(dictation.modelPreparation.fractionCompleted)
case .failed(let message): app = .failed(message)
default: app = .starting
}
}
let keys: Stage? = switch keyboard.state {
case .ready, .followsApp: keyboard.downloads ? .ready : nil
case .downloading(let fraction): .downloading(fraction)
case .failed(let message): .failed(message)
case .checking: .starting
}
stage = Self.combine(app, keys)
let id: String
if dictation.isModelReady {
id = dictation.transcriptionLanguageID
} else if dictation.engineName.hasPrefix("Parakeet") {
id = "en-US"
} else {
id = LanguageSettings().effectiveDictationLanguage ?? Locale.preferredLanguages.first ?? "en-US"
}
language = SpeechModelUnavailable.interfaceName(id)
}
init(stage: Stage, language: String) {
self.stage = stage
self.language = language
}
/// One stage for two downloads: a failure first, then ready only when both are, else the
/// two fractions averaged (a ready one counts as whole). No "b": the keyboard has nothing
/// to download.
static func combine(_ a: Stage, _ b: Stage?) -> Stage {
guard let b else { return a }
if case .failed = a { return a }
if case .failed = b { return b }
if a == .ready, b == .ready { return .ready }
if a == .starting, b == .starting { return .starting }
func fraction(_ stage: Stage) -> Double {
switch stage {
case .ready: 1
case .downloading(let value): value
default: 0
}
}
return .downloading((fraction(a) + fraction(b)) / 2)
}
}
struct OnboardingView: View {
@Environment(\.scenePhase) private var scenePhase
@Environment(\.accessibilityReduceMotion) private var reduceMotion
let dictation: DictationController
let monitor: KeyboardSetupMonitor
let keyboardSpeech: KeyboardSpeechPreparation
@Binding var page: OnboardingPage?
@Namespace private var pillSpace
@State private var tryText = OnboardingFixture.current == .tryDone ? "On my way, see you at ten." : ""
@State private var fixtureModel: OnboardingModel? = OnboardingView.initialFixtureModel
private static var initialFixtureModel: OnboardingModel? {
guard let fixture = OnboardingFixture.current else { return nil }
let english = SpeechModelUnavailable.interfaceName("en-US")
if fixture == .failed {
return OnboardingModel(
stage: .failed(String(localized: "Hex couldn't download the speech model. Check your connection and try again.")),
language: english
)
}
if let fraction = fixture.modelFraction {
return OnboardingModel(stage: .downloading(fraction), language: english)
}
return OnboardingModel(stage: .ready, language: english)
}
private var model: OnboardingModel {
fixtureModel ?? OnboardingModel(dictation: dictation, keyboard: keyboardSpeech)
}
var body: some View {
ZStack {
switch page {
case .setup?:
OnboardingSetupPage(model: model, onRetry: retryModel) { go(to: .demo) }
.transition(pageTransition)
case .demo?:
OnboardingDemoPage(model: model) { go(to: .permissions) }
.transition(pageTransition)
case .permissions?:
OnboardingPermissionsPage(monitor: monitor, model: model) {
go(to: model.isReady ? .practice : .waiting)
}
.transition(pageTransition)
case .waiting?:
OnboardingWaitingPage(
model: model,
onRetry: retryModel,
onWithoutModel: finish,
onReady: { go(to: .practice) }
)
.transition(pageTransition)
case .practice?:
OnboardingPracticePage(monitor: monitor, model: model, text: $tryText) {
go(to: .ready)
}
.transition(pageTransition)
case .ready?:
OnboardingReadyPage(
monitor: monitor,
model: model,
onRetry: retryModel,
onTryAgain: { go(to: .practice) },
onStart: finish
)
.transition(pageTransition)
case nil:
EmptyView()
}
}
.frame(maxWidth: 500)
.padding(.horizontal, 20)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.environment(\.onboardingPillSpace, reduceMotion ? nil : pillSpace)
.task {
while !Task.isCancelled {
monitor.refresh()
try? await Task.sleep(for: .seconds(1))
}
}
.onChange(of: scenePhase) { _, phase in
if phase == .active {
monitor.refresh()
}
}
.task { await startModelDownload() }
.task { await runTour() }
}
/// The model starts downloading as onboarding opens, as on the desktop. Launch already
/// prepares a cached Parakeet or Apple Speech; this starts the Parakeet download that home's
/// Set up used to wait for. A launch that found Parakeet ended mid-load stays failed
/// (ParakeetLoadGuard): only Try again loads it again.
private func startModelDownload() async {
guard !OnboardingFixture.isSnapshot else { return }
// Launch decides Apple Speech or Parakeet in a task of its own; let it answer first.
try? await Task.sleep(for: .milliseconds(600))
guard !Task.isCancelled, !dictation.isModelReady, dictation.phase == .modelRequired else { return }
dictation.prepareModel()
}
private func retryModel() {
guard !OnboardingFixture.isSnapshot else { return }
if !dictation.isModelReady {
dictation.prepareModel()
}
keyboardSpeech.start(retry: true) { dictation.engineName }
}
/// "onboarding-tour": steps through every page on the fixture timeline.
private func runTour() async {
guard OnboardingFixture.current == .tour else { return }
typealias Tour = OnboardingFixture.Tour
if Tour.start == nil {
Tour.start = Date().addingTimeInterval(Tour.leadIn)
}
let english = SpeechModelUnavailable.interfaceName("en-US")
while !Task.isCancelled {
let t = Tour.elapsed
monitor.refresh()
fixtureModel = t < Tour.modelReady
? OnboardingModel(stage: .downloading(min(0.97, 0.05 + 0.92 * max(0, t) / Tour.modelReady)), language: english)
: OnboardingModel(stage: .ready, language: english)
if t >= Tour.done {
finish()
return
} else if t >= Tour.toReady, page == .practice {
go(to: .ready)
} else if t >= Tour.text, tryText.isEmpty {
tryText = "On my way, see you at ten."
} else if t >= Tour.toTry, page == .waiting {
go(to: .practice)
} else if t >= Tour.toWaiting, page == .permissions {
go(to: .waiting)
} else if t >= Tour.toPermissions, page == .demo {
go(to: .permissions)
} else if t >= Tour.toDemo, page == .setup {
go(to: .demo)
}
try? await Task.sleep(for: .milliseconds(33))
}
}
/// The page leaving lifts away as the desktop's roll does; the arriving page's parts roll in
/// themselves, staggered (onboardingStagger).
private var pageTransition: AnyTransition {
guard !reduceMotion else { return .opacity }
return .asymmetric(
insertion: .identity,
removal: AnyTransition.opacity
.combined(with: .offset(y: -OnboardingMotion.travel))
.animation(OnboardingMotion.exit)
)
}
private func go(to next: OnboardingPage) {
guard page != next else { return }
OnboardingProgress.save(next)
// The pill flies on the desktop's flight spring; the rest follows its own springs.
withAnimation(reduceMotion ? HexMotion.reducedFade : OnboardingMotion.fly) {
page = next
}
}
private func finish() {
OnboardingProgress.complete()
withAnimation(HexMotion.settleAnimation(reduceMotion: reduceMotion)) {
page = nil
}
}
}
// MARK: - Setting up Hex
/// Page 1: the model downloads by itself from the start, and the page moves on to the demo
/// after a beat. Only a failed start holds it, with Try again.
private struct OnboardingSetupPage: View {
let model: OnboardingModel
let onRetry: () -> Void
let onNext: () -> Void
var body: some View {
VStack(spacing: 0) {
Spacer(minLength: 24)
VStack(spacing: 14) {
HexMarkView(size: 56)
.padding(.bottom, 10)
.onboardingStagger(0)
OnboardingTitle("Setting up Hex")
.onboardingStagger(1)
HStack(spacing: 6) {
Text(model.isReady ? "Ready" : "Downloading")
OnboardingLanguageCapsule(language: model.language)
Text("speech model")
}
.font(.system(size: 15))
.foregroundStyle(HexTheme.textMuted)
.lineLimit(1)
.minimumScaleFactor(0.8)
.onboardingStagger(2)
if case .failed(let message) = model.stage {
OnboardingFailure(message: message, onRetry: onRetry)
.padding(.top, 6)
OnboardingQuietButton(title: "Continue", action: onNext)
} else {
OnboardingProgressPill(model: model, large: true)
.padding(.top, 6)
.onboardingStagger(3)
}
}
.frame(maxWidth: .infinity)
Spacer(minLength: 24)
OnboardingPrivacyLine()
.padding(.bottom, 16)
.onboardingStagger(4)
}
.task(id: model.isFailed) {
guard OnboardingFixture.autoAdvances, !model.isFailed else { return }
try? await Task.sleep(for: .seconds(2.6))
guard !Task.isCancelled else { return }
onNext()
}
}
}
// MARK: - The demo
/// Page 2, optional: a reply dictated with the Hex key, then on to permissions. Skip passes it.
private struct OnboardingDemoPage: View {
@Environment(\.accessibilityReduceMotion) private var reduceMotion
let model: OnboardingModel
let onNext: () -> Void
@State private var beat = OnboardingFixture.current == .demo ? 3 : 0
@State private var shownWords = OnboardingFixture.current == .demo ? Int.max : 0
private var reply: String { String(localized: "On my way. Oat or regular?") }
private var replyWords: [Substring] { reply.split(separator: " ", omittingEmptySubsequences: true) }
private var typed: String {
replyWords.prefix(shownWords).joined(separator: " ")
}
var body: some View {
VStack(spacing: 0) {
OnboardingTopBar(model: model, onSkip: onNext)
VStack(spacing: 10) {
OnboardingTitle("With Hex")
Text("Tap the Hex key and say it.")
.font(.system(size: 15))
.foregroundStyle(HexTheme.textMuted)
}
.padding(.top, 12)
.padding(.bottom, 24)
.onboardingStagger(1)
stage
.onboardingStagger(2)
Spacer(minLength: 16)
}
.task { await play() }
}
private var stage: some View {
VStack(alignment: .leading, spacing: 14) {
HStack(spacing: 10) {
Text(verbatim: "S")
.font(.system(size: 14, weight: .semibold))
.foregroundStyle(HexTheme.textBase)
.frame(width: 30, height: 30)
.background(Color.white.opacity(0.12), in: Circle())
Text(verbatim: "Sam")
.font(.system(size: 15, weight: .semibold))
.foregroundStyle(HexTheme.textBase)
Spacer()
}
bubble(String(localized: "Are you near the store? Could you grab some milk?"), mine: false)
if beat >= 4 {
bubble(reply, mine: true)
.transition(.opacity.combined(with: .offset(y: 6)))
}
HStack(spacing: 10) {
Group {
if beat >= 4 || typed.isEmpty {
Text("Message").foregroundStyle(HexTheme.textFaint)
} else {
Text(typed).foregroundStyle(HexTheme.textBase)
}
}
.font(.system(size: 15))
.lineLimit(1)
Spacer(minLength: 8)
// The Hex key, lit while it listens.
HexMarkView(size: 16, color: beat == 1 || beat == 2 ? HexTheme.textContrast : HexTheme.textBase)
.frame(width: 34, height: 30)
.background(beat == 1 || beat == 2 ? HexTheme.bgContrast : Color.white.opacity(0.09), in: Capsule())
}
.padding(.leading, 14)
.padding(.trailing, 6)
.frame(height: 42)
.background(Color.white.opacity(0.06), in: Capsule())
}
.padding(16)
.background(HexTheme.bgBase, in: RoundedRectangle(cornerRadius: HexTheme.cardRadius, style: .continuous))
.animation(HexMotion.settleAnimation(reduceMotion: reduceMotion), value: beat)
.animation(HexMotion.reducedFade, value: shownWords)
.accessibilityElement(children: .combine)
}
private func bubble(_ text: String, mine: Bool) -> some View {
HStack {
if mine { Spacer(minLength: 40) }
Text(text)
.font(.system(size: 15))
.foregroundStyle(mine ? HexTheme.textContrast : HexTheme.textBase)
.padding(.horizontal, 12)
.padding(.vertical, 8)
.background(mine ? HexTheme.bgContrast : Color.white.opacity(0.09), in: RoundedRectangle(cornerRadius: 16, style: .continuous))
if !mine { Spacer(minLength: 40) }
}
}
/// Hex key down, the words arrive, sent, then on to permissions.
private func play() async {
guard OnboardingFixture.current != .demo else { return }
if reduceMotion {
beat = 4
shownWords = Int.max
} else {
try? await Task.sleep(for: .seconds(0.9))
beat = 1
try? await Task.sleep(for: .seconds(0.9))
beat = 2
for index in 1...replyWords.count {
guard !Task.isCancelled else { return }
shownWords = index
try? await Task.sleep(for: .seconds(0.2))
}
beat = 3
try? await Task.sleep(for: .seconds(0.6))
beat = 4
}
try? await Task.sleep(for: .seconds(1.8))
guard !Task.isCancelled, OnboardingFixture.autoAdvances else { return }
onNext()
}
}
// MARK: - Permissions
/// Page 3: the microphone and the Hex keyboard. No Skip, as on the desktop, but Continue never
/// waits on them: someone who declines the microphone can still type with Hex and allow it
/// later (App Review 5.1.1(iv)). Full Access can't be verified from the app; the practice
/// checks it. Moves on by itself once both rows are done.
private struct OnboardingPermissionsPage: View {
@Environment(\.openURL) private var openURL
let monitor: KeyboardSetupMonitor
let model: OnboardingModel
let onContinue: () -> Void
@State private var showsKeyboardFallback = false
private var microphoneDone: Bool { monitor.microphone == .granted }
private var keyboardDone: Bool { monitor.keyboardAdded == true }
/// Before iOS's microphone prompt, Hex's own buttons say Continue or Open Settings, never
/// Allow (App Review 5.1.1(iv)).
private func continueTapped() {
if monitor.microphone == .undetermined {
monitor.requestMicrophone { onContinue() }
} else {
onContinue()
}
}
var body: some View {
VStack(spacing: 0) {
OnboardingTopBar(model: model, onSkip: nil)
Spacer(minLength: 16)
VStack(spacing: 10) {
OnboardingTitle("Let Hex hear you")
Text("Hex listens only after you tap the Hex key.")
.font(.system(size: 15))
.foregroundStyle(HexTheme.textMuted)
.multilineTextAlignment(.center)
}
.padding(.bottom, 24)
.onboardingStagger(1)
VStack(spacing: 0) {
OnboardingPermissionRow(
icon: "mic",
title: "Microphone",
detail: "So Hex can hear you",
done: microphoneDone,
// iOS asks right after this tap, so the pill says Continue, never Allow.
actionTitle: monitor.microphone == .denied ? "Open Settings" : "Continue"
) {
if monitor.microphone == .denied {
openSettings(openURL)
} else {
monitor.requestMicrophone()
}
}
Rectangle().fill(HexTheme.borderWeak).frame(height: 0.5)
OnboardingPermissionRow(
icon: "keyboard",
title: "Hex keyboard",
detail: "With Full Access",
done: keyboardDone,
actionTitle: "Open Settings"
) {
openSettings(openURL)
}
}
.padding(.horizontal, 16)
.background(HexTheme.bgBase, in: RoundedRectangle(cornerRadius: HexTheme.cardRadius, style: .continuous))
.onboardingStagger(2)
VStack(alignment: .leading, spacing: 8) {
Text("Keyboards › Hex Keyboard › Allow Full Access")
Button {
withAnimation(HexMotion.reducedFade) { showsKeyboardFallback.toggle() }
} label: {
Text("Don't see Keyboards?")
.underline()
}
.buttonStyle(.plain)
if showsKeyboardFallback {
Text("General › Keyboard › Keyboards › Add New Keyboard › Hex")
.transition(.opacity)
}
}
.font(.system(size: 13))
.foregroundStyle(HexTheme.textMuted)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal, 4)
.padding(.top, 14)
.opacity(keyboardDone ? 0 : 1)
.accessibilityHidden(keyboardDone)
.onboardingStagger(3)
Spacer(minLength: 16)
OnboardingPrivacyLine()
.padding(.bottom, 14)
.onboardingStagger(4)
OnboardingPrimaryButton(title: "Continue", enabled: monitor.keyboardAdded != false, action: continueTapped)
.padding(.bottom, 12)
.onboardingStagger(5)
}
.task(id: microphoneDone && keyboardDone) {
guard OnboardingFixture.autoAdvances, microphoneDone, keyboardDone else { return }
try? await Task.sleep(for: .seconds(0.8))
guard !Task.isCancelled else { return }
onContinue()
}
}
}
// MARK: - Almost ready
/// Page 4, only while the model still downloads: the practice needs it. Continue without a
/// model shows only once the download failed or stalled.
private struct OnboardingWaitingPage: View {
let model: OnboardingModel
let onRetry: () -> Void
let onWithoutModel: () -> Void
let onReady: () -> Void
var body: some View {
VStack(spacing: 0) {
Spacer(minLength: 24)
OnboardingModelWait(model: model, onRetry: onRetry, onWithoutModel: onWithoutModel)
.onboardingStagger(1)
Spacer(minLength: 24)
}
.task(id: model.isReady) {
guard OnboardingFixture.autoAdvances, model.isReady else { return }
try? await Task.sleep(for: .seconds(0.7))
guard !Task.isCancelled else { return }
onReady()
}
}
}
/// "Almost ready" with the download's progress, Try again after a failure, and Continue without
/// a model after a failure or 30 s without progress.
struct OnboardingModelWait: View {
let model: OnboardingModel
let onRetry: () -> Void
let onWithoutModel: () -> Void
@State private var lastFraction = -1.0
@State private var stalled = OnboardingFixture.current == .failed
/// A download that gained nothing for this long has stalled.
private static let stallAfter: Duration = .seconds(30)
var body: some View {
VStack(spacing: 14) {
OnboardingTitle("Almost ready")
Text("Finishing the speech model so Hex can hear you.")
.font(.system(size: 15))
.foregroundStyle(HexTheme.textMuted)
.multilineTextAlignment(.center)
.fixedSize(horizontal: false, vertical: true)
if case .failed(let message) = model.stage {
OnboardingFailure(message: message, onRetry: onRetry)
.padding(.top, 6)
} else {
VStack(alignment: .leading, spacing: 8) {
ProgressView(value: model.fraction)
.tint(HexTheme.textBase)
Text(model.fraction, format: .percent.precision(.fractionLength(0)))
.font(.system(size: 13))
.monospacedDigit()
.foregroundStyle(HexTheme.textMuted)
}
.frame(maxWidth: 260)
.padding(.top, 10)
}
if model.isFailed || stalled {
VStack(spacing: 6) {
OnboardingQuietButton(title: "Continue without a model", action: onWithoutModel)
Text("Dictation won't work until the speech model downloads.")
.font(.system(size: 13))
.foregroundStyle(HexTheme.textFaint)
.multilineTextAlignment(.center)
}
.padding(.top, 10)
}
}
.frame(maxWidth: .infinity)
.task(id: model.fraction) {
guard !model.isReady, !model.isFailed else { return }
if lastFraction != model.fraction {
lastFraction = model.fraction
if OnboardingFixture.current != .failed { stalled = false }
}
try? await Task.sleep(for: Self.stallAfter)
guard !Task.isCancelled else { return }
stalled = true
}
}
}
// MARK: - Your turn
/// Page 5, optional: the practice, with the Hex keyboard in a field that keeps the keyboard up.
/// It counts once a dictation arrives or a few words are typed, and the done state keeps the
/// keyboard.
private struct OnboardingPracticePage: View {
@Environment(\.openURL) private var openURL
@Environment(\.scenePhase) private var scenePhase
@Environment(\.accessibilityReduceMotion) private var reduceMotion
let monitor: KeyboardSetupMonitor
let model: OnboardingModel
@Binding var text: String
let onNext: () -> Void
@State private var dictated = OnboardingFixture.current == .tryDone
@State private var heardNothing = OnboardingFixture.current == .tryEmpty
@State private var keyboardActivity = 0
@State private var lastSwitchAt: Date?
@State private var keyboardCameUpAt = Date.distantPast
@State private var fullAccessLooksOff = OnboardingFixture.current == .tryOff
@FocusState private var fieldFocused: Bool
/// How long the keyboard gets to report Full Access after a switch or new text.
private static let fullAccessGrace: Duration = .seconds(4)
/// The keyboard coming up (focus, or back from Settings) also posts an input mode change.
private static let keyboardSettle: TimeInterval = 1.5
private var hasText: Bool {
!text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
private var complete: Bool {
OnboardingPractice.isDone(text: text, dictated: dictated, fullAccessOn: monitor.fullAccessOn)
}
var body: some View {
VStack(spacing: 0) {
OnboardingTopBar(model: model, onSkip: complete ? nil : onNext)
ScrollView {
VStack(alignment: .leading, spacing: 16) {
HStack(alignment: .center, spacing: 10) {
if complete {
Image(systemName: "checkmark.circle.fill")
.font(.system(size: 24, weight: .medium))
.foregroundStyle(HexTheme.success)
.transition(.opacity.combined(with: .scale(scale: 0.9)))
}
OnboardingTitle(complete ? "That's Hex" : "Your turn")
.contentTransition(.opacity)
}
.onboardingStagger(1)
Group {
if complete {
Text("It types wherever your cursor is, in any app.")
} else {
Text("Switch to Hex with \(Image(systemName: "globe")), then tap the Hex key.")
}
}
.font(.system(size: 15))
.foregroundStyle(HexTheme.textMuted)
.fixedSize(horizontal: false, vertical: true)
.padding(.top, -8)
.onboardingStagger(2)
TextField("Say anything…", text: $text, axis: .vertical)
.font(.system(size: 17))
.foregroundStyle(HexTheme.textBase)
.focused($fieldFocused)
.lineLimit(2...5)
.padding(.horizontal, 16)
.padding(.vertical, 14)
.background(HexTheme.bgBase, in: RoundedRectangle(cornerRadius: HexTheme.cardRadius, style: .continuous))
.onboardingStagger(3)
if heardNothing && !complete {
HStack(alignment: .top, spacing: 8) {
Image(systemName: "waveform")
.font(.system(size: 13, weight: .medium))
.padding(.top, 1)
Text("Hex didn't hear any words. Try again a little louder.")
}
.font(.system(size: 13))
.foregroundStyle(HexTheme.textMuted)
.padding(.horizontal, 4)
.transition(.opacity)
}
if fullAccessLooksOff && !monitor.fullAccessOn {
fullAccessNote
.transition(.opacity.combined(with: .scale(scale: 0.98)))
}
}
.padding(.top, 12)
.padding(.bottom, 16)
}
.scrollBounceBehavior(.basedOnSize)
OnboardingPrimaryButton(title: "Continue", action: onNext)
.hexControlReveal(complete, reduceMotion: reduceMotion, scale: 0.96)
.allowsHitTesting(complete)
.padding(.top, 8)
.padding(.bottom, 12)
}
.animation(HexMotion.settleAnimation(reduceMotion: reduceMotion), value: complete)
.animation(HexMotion.settleAnimation(reduceMotion: reduceMotion), value: fullAccessLooksOff)
.animation(HexMotion.reducedFade, value: heardNothing)
.onAppear {
guard OnboardingFixture.current != .tryDone else { return }
DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
fieldFocused = true
}
}
.task { await watchKeyboardDictations() }
.onReceive(NotificationCenter.default.publisher(for: UITextInputMode.currentInputModeDidChangeNotification)) { _ in
lastSwitchAt = Date()
noteKeyboardActivity()
}
.onChange(of: fieldFocused) { _, focused in
if focused { keyboardCameUpAt = Date() }
}
.onChange(of: scenePhase) { _, phase in
guard phase == .active else { return }
keyboardCameUpAt = Date()
// The page can open while Hex comes back from Settings, before it can take focus.
if !fieldFocused && OnboardingFixture.current != .tryDone {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) { fieldFocused = true }
}
}
.onChange(of: text) { _, _ in
if hasText {
heardNothing = false
noteKeyboardActivity()
}
}
.onChange(of: monitor.fullAccessOn) { _, on in
if on { fullAccessLooksOff = false }
}
.task(id: keyboardActivity) {
// iOS tells only the keyboard about Full Access, and the keyboard writes it to the
// App Group as soon as it opens with it; nothing a few seconds after a keyboard
// switch or new text means it's off (or the Hex keyboard isn't the one up).
guard keyboardActivity > 0 else { return }
try? await Task.sleep(for: Self.fullAccessGrace)
guard !Task.isCancelled, !monitor.fullAccessOn else { return }
if !hasText, let switchedAt = lastSwitchAt,
abs(switchedAt.timeIntervalSince(keyboardCameUpAt)) < Self.keyboardSettle {
return
}
fullAccessLooksOff = true
}
}
/// The keyboard's dictations, through the App Group: a new inserted result means one
/// arrived; a new empty one means it heard nothing (the keyboard itself stays quiet).
private func watchKeyboardDictations() async {
guard !OnboardingFixture.isSnapshot else { return }
let bridge = KeyboardBridge()
let insertedAtStart = bridge.lastInsertedResultID
var emptyAtStart = bridge.lastEmptyDictationAt
while !Task.isCancelled {
try? await Task.sleep(for: .milliseconds(250))
if bridge.lastInsertedResultID != insertedAtStart {
dictated = true
heardNothing = false
}
let empty = bridge.lastEmptyDictationAt
if let empty, empty != emptyAtStart {
emptyAtStart = empty
if !dictated { heardNothing = true }
}
}
}
private func noteKeyboardActivity() {
guard !monitor.fullAccessOn else { return }
keyboardActivity += 1
}
private var fullAccessNote: some View {
VStack(alignment: .leading, spacing: 14) {
HStack(alignment: .top, spacing: 10) {
Image(systemName: "exclamationmark.circle.fill")
.font(.system(size: 16, weight: .medium))
.foregroundStyle(HexTheme.warmMic)
.padding(.top, 1)
Text("Turn on Allow Full Access")
.font(.system(size: 15, weight: .medium))
.foregroundStyle(HexTheme.textBase)
}
.accessibilityElement(children: .combine)
OnboardingSecondaryButton(title: "Open Settings", icon: "gearshape.fill") {
openSettings(openURL)
}
}
.padding(16)
.background(HexTheme.bgBase, in: RoundedRectangle(cornerRadius: HexTheme.cardRadius, style: .continuous))
}
}
// MARK: - Hex is ready
/// Page 6. "Hex is ready" only once every step is done (the model included); until then it
/// says what's left. Start using Hex never waits on it (App Review 5.1.1(iv)).
private struct OnboardingReadyPage: View {
@Environment(\.openURL) private var openURL
let monitor: KeyboardSetupMonitor
let model: OnboardingModel
let onRetry: () -> Void
let onTryAgain: () -> Void
let onStart: () -> Void
private enum Left {
case nothing
case model
case microphone
case keyboard
case fullAccess
}
private var left: Left {
if !model.isReady { return .model }
if monitor.microphone != .granted { return .microphone }
if monitor.keyboardAdded == false { return .keyboard }
if !monitor.fullAccessOn { return .fullAccess }
return .nothing
}
var body: some View {
VStack(spacing: 0) {
Spacer(minLength: 24)
Group {
switch left {
case .model:
OnboardingModelWait(model: model, onRetry: onRetry, onWithoutModel: onStart)
default:
VStack(spacing: 14) {
if left == .nothing {
HexMarkView(size: 56)
.padding(.bottom, 10)
}
OnboardingTitle(left == .nothing ? "Hex is ready" : "Almost ready")
subtitle
.font(.system(size: 15))
.foregroundStyle(HexTheme.textMuted)
.multilineTextAlignment(.center)
.fixedSize(horizontal: false, vertical: true)
if left == .microphone || left == .keyboard {
OnboardingQuietButton(title: "Open Settings") { openSettings(openURL) }
.padding(.top, 4)
}
}
.frame(maxWidth: .infinity)
}
}
.onboardingStagger(1)
Spacer(minLength: 24)
if left != .model {
OnboardingQuietButton(title: "Try again", action: onTryAgain)
.padding(.bottom, 8)
.onboardingStagger(2)
}
OnboardingPrimaryButton(title: "Start using Hex", action: onStart)
.padding(.bottom, 12)
.onboardingStagger(3)
}
}
@ViewBuilder
private var subtitle: some View {
switch left {
case .nothing, .model: Text("Tap the Hex key in any app to talk.")
case .microphone: Text("Turn on the microphone in Settings so Hex can hear you.")
case .keyboard: Text("Add the Hex keyboard in Settings.")
case .fullAccess: Text("Use the Hex keyboard once to finish.")
}
}
}