hex/ios/HEXMobile/Settings/SpeechModelChoiceRow.swift
Ryan Vogel 9e41970efe
iOS: both speech models download on Wi-Fi, with Use mobile data (#2846)
* iOS: both speech models download on Wi-Fi, with Use mobile data

What: Settings › Dictation › Model's two downloads now follow one rule
(SpeechModelDownloadNetwork). On Wi-Fi (KeyboardParakeetDownload.isUnmetered: not
cellular, a hotspot or Low Data Mode) a picked model downloads as before. Off Wi-Fi it
waits: English's line already said "Downloads on Wi-Fi."; Multilingual's now says the
same (ParakeetUltraDownload.State.waitingForWiFi) instead of starting at once. Both lines
get a Use mobile data action that starts that download now. Wi-Fi coming back starts a
waiting download by itself; a multilingual download running on Wi-Fi now stops where it
is when Wi-Fi goes (its files resume), as English's always did. Use mobile data is one
decision for that model's download, kept in the App Group until the model is on the
iPhone (a stop, Try again or relaunch carries on); picking the other model forgets it.
Multilingual's row text now reads "A 632.2 MB download on Wi-Fi." (zh-Hans updated);
the one new string is "Use mobile data" (zh-Hans 使用蜂窝数据). English's own network
monitor moved into SpeechModelDownloadNetwork, which both downloads observe.

Unchanged: a download already running on Wi-Fi; unpicked (the old model stays until the
new one is downloaded and has loaded once); picking English still stops and removes a
partial multilingual download; the keyboard extension and its notice; the Speech model
in keyboard switch turned on by hand still downloads English on any connection.

Why: TestFlight build 151 (no Wi-Fi at home): English waited for Wi-Fi but Multilingual
started a 632 MB download over mobile data the moment it was picked. Ryan's decision:
wait for Wi-Fi, with a Use mobile data button, as on Android.

Verified: tests in DictationControllerTests (both wait off Wi-Fi, Use mobile data starts
them, Wi-Fi returning starts a waiting one, leaving Wi-Fi stops one, both start at once on
Wi-Fi, picking the other model asks again); suite run separately.

* iOS: test that English picked mid-wait leaves a waiting multilingual download stopped

What: the multilingual Wi-Fi test now also stops a download waiting for Wi-Fi (what picking
English does) and checks Wi-Fi coming back doesn't start it, and that picking Multilingual
again waits again.
Why: the task asks for switching models mid-wait to be covered.
Verified: suite run separately.

* iOS: snapshot scenes for Settings › Dictation › Model's download states

What: --snap-state settings-dictation-model-<english|multilingual>-<waiting|downloading|ready>
opens Settings › Dictation with that model picked and its download in that state
(DictationController.applySpeechModelFixture, ParakeetUltraDownload.applyFixture(share:)).
Waiting goes through the real rule: with --simulate-cellular the connection reads as off
Wi-Fi and the keyboard limit is recorded, so only the connection holds the download.
SettingsSnapshotFixture.applyDefaults keeps Multilingual picked for -multilingual- scenes.
Why: screenshots of the Use mobile data row in each state; the simulator has no cellular.
Verified: scenes rendered in a simbox (screenshots in the PR).

* iOS: Model snapshot scenes record the keyboard limit only for waiting

What: settings-dictation-model-*-downloading and -ready no longer record a keyboard limit.
Why: with a limit and the simulator's Wi-Fi, English's real download started under the
downloading scene and finished during the screenshot run, so a later downloading shot
read Downloaded. Without a limit nothing downloads, as with Home's model-card scenes.
Verified: the 12 Model row screenshots retaken in the simbox.

* iOS: check the multilingual Wi-Fi test's resume request, not the last request

What: testAMultilingualDownloadFollowsWiFiComingAndGoing now checks the first two requests
are the fresh one and the bytes=800- resume, instead of the last request.
Why: the stub model has two files, so the last request is the second file's, with no range;
the suite failed on that assertion alone.
Verified: full suite rerun in the simbox.
2026-11-25 09:10:45 -05:00

108 lines
4.8 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
/// Settings › Dictation › Model: the speech model the keyboard dictates with, English or
/// Multilingual. Multilingual is `MultilingualParakeetSettings` (the setting Experiments had),
/// so Hex's own dictation follows it too. English unless picked.
enum SpeechModelChoice: String, CaseIterable, Hashable {
case english
case multilingual
static var current: SpeechModelChoice {
MultilingualParakeetSettings().isEnabled ? .multilingual : .english
}
var label: String {
switch self {
case .english: String(localized: "English")
case .multilingual: String(localized: "Multilingual")
}
}
/// What the choice means, before anything downloads: its languages and its download.
@MainActor
var detail: String {
switch self {
case .english:
String(localized: "For English. A \(KeyboardParakeetDownload.format(KeyboardParakeetDownload.bytes)) download on Wi-Fi.")
case .multilingual:
String(localized: "25 languages, even mixed in one sentence: \(Self.multilingualLanguages). A \(ParakeetUltraTranscriber.downloadSize) download on Wi-Fi. Hex dictates them with it too.")
}
}
/// The multilingual model's 25 languages by name, in the iPhone's language.
static var multilingualLanguages: String {
let names = MultilingualParakeetSettings.languages.compactMap { Locale.current.localizedString(forLanguageCode: $0) }
return ListFormatter.localizedString(byJoining: names.sorted { $0.localizedStandardCompare($1) == .orderedAscending })
}
/// Picked in Settings: Multilingual downloads beside the engine Hex dictates with, on Wi-Fi
/// or after Use mobile data; English stops that download (deleted unless complete) and gets
/// English's model as Speech model in keyboard does. The model picked before stays, and keeps
/// dictating English, until the new one is downloaded and has loaded once (`unpicked`).
/// Use mobile data was for the other model's download: this one asks again.
@MainActor
func select() {
guard self != Self.current else { return }
SpeechModelDownloadNetwork.shared.forgetMobileData()
MultilingualParakeetSettings().isEnabled = self == .multilingual
DictationController.shared.multilingualParakeetChanged()
DictationController.shared.startKeyboardParakeetDownloadIfWanted()
}
}
extension SpeechModelChoice {
/// The model to remove now, if any: Settings › Dictation › Model keeps only the picked one,
/// but the other goes only once the picked one is downloaded and has loaded once (in Hex
/// or the keyboard), so nobody is left with neither; a download that fails or stops leaves
/// the other in place and in use. Never one Hex itself dictates with now.
static func unpicked(
picked: SpeechModelChoice,
englishDownloaded: Bool,
englishLoaded: Bool,
multilingualDownloaded: Bool,
multilingualLoaded: Bool,
hexDictatesWith: KeyboardParakeetPlan.Model?
) -> KeyboardParakeetPlan.Model? {
switch picked {
case .multilingual:
guard englishDownloaded, multilingualDownloaded, multilingualLoaded, hexDictatesWith != .parakeetV2 else { return nil }
return .parakeetV2
case .english:
guard multilingualDownloaded, englishDownloaded, englishLoaded, hexDictatesWith != .parakeetUltra else { return nil }
return .parakeetUltra
}
}
}
/// The Model row in the speech model card: English · Multilingual, what the picked one means,
/// and that a downloaded model stays.
struct SpeechModelChoiceRow: View {
let choice: SpeechModelChoice
let onSelect: (SpeechModelChoice) -> Void
var body: some View {
VStack(alignment: .leading, spacing: 8) {
HStack(spacing: 12) {
SettingsRowLabel(title: String(localized: "Model"))
Spacer(minLength: 8)
SettingsSegments(
items: SpeechModelChoice.allCases,
selected: choice,
label: \.label,
onSelect: onSelect
)
}
Text(choice.detail)
.font(.system(size: 13))
.foregroundStyle(HexTheme.textMuted)
.fixedSize(horizontal: false, vertical: true)
.accessibilityIdentifier("speech-model-choice-detail")
Text("Other languages use Apple's speech recognition. Only the picked model stays on this iPhone: switching downloads the new one, and the old one goes once the new one is ready.")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textFaint)
.fixedSize(horizontal: false, vertical: true)
}
.padding(.horizontal, 16)
.padding(.vertical, 12)
}
}