Files
OSGKeyboard/OSGKeyboard/Views/SettingsPreferenceRows.swift
T
rockyandClaude Opus 5 0023741455 feat(app): add crash visibility, memory relief, and clipboard reply work
Squashes the working tree that had accumulated on cloud-agent. Two
strands of work, kept in one commit at the maintainer's request.

Crash and stability (this session):

- Subscribe to MetricKit so crash, hang, CPU, and disk-write diagnostics
  are recorded on device. Keyboard-extension memory kills are the most
  likely way a user loses the keyboard mid-sentence and iOS does not
  classify them as crashes, so they never reach App Store Connect.
- Upload those reports to POST /v1/analytics/diagnostics from internal
  builds only (local Debug and TestFlight), reusing the analytics
  installation identifier and skipping entirely when analytics are off.
  App Store builds keep everything on device.
- Shed the Chinese engine, English lexicon, and in-flight pipeline work
  at the 40 MiB and 48 MiB budget thresholds instead of waiting for a
  system memory warning that jetsam often preempts.
- Install audio taps with the input node's live format through one
  shared adaptive resampler, so an audio-route change mid-recording no
  longer raises an uncatchable format-mismatch exception. Covers the
  dictation preview and the Mac recorder, which still used the pattern
  the Flow capture path had already moved away from.
- Harden the librime bridge: never pass a nil string across a nonnull
  boundary when a user dictionary holds malformed UTF-8, null-check the
  engine API vector, and distrust a candidate count without its array.
- Guard the expanded candidate grid against a list that shrinks while
  the collection view is still laying out against the previous count.
- Resolve localized text without the App Group so the screen shown when
  that container is missing renders its message instead of crashing on
  launch, which is the one situation it exists to handle.
- Gate CI on the App Group and Keychain wiring that the keyboard
  extension hard-depends on, with a self-test proving the check fails.

Clipboard, reply, and semantics (pre-existing working tree):

- Unify the reply center, refresh keyboard AI features, and add native
  event and reminder exporters.
- Expand clipboard semantic routing with authored corpora and a
  product-blind holdout, plus the scripts that build and validate them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 17:31:29 -07:00

363 lines
12 KiB
Swift

// SettingsPreferenceRows.swift
// OSGKeyboard · Main App
//
// Shared preference picker / toggle rows used by Settings home and
// secondary pages (General, Voice session, Daily).
import OSGKeyboardShared
import Speech
import SwiftUI
// MARK: - App language picker row
struct AppLanguagePickerRow: View {
@Binding var selection: AppUILanguage
private var options: [(id: String, label: String)] {
AppUILanguage.allCases.map { language in
(language.rawValue, AppL10n.string(language.labelKey))
}
}
var body: some View {
SettingsMenuPickerRow(
title: AppL10n.string("settings.appLanguage.title"),
options: options,
selection: Binding(
get: { selection.rawValue },
set: { newValue in
selection = AppUILanguage(rawValue: newValue) ?? .auto
}
)
)
}
}
// MARK: - Appearance picker row
struct AppearancePickerRow: View {
@AppStorage(AppearancePreference.storageKey)
private var appearanceRaw = AppearancePreference.system.rawValue
private var options: [(id: String, label: String)] {
AppearancePreference.allCases.map { preference in
(preference.rawValue, AppL10n.string(preference.labelKey))
}
}
var body: some View {
SettingsMenuPickerRow(
title: AppL10n.string("settings.appearance.title"),
options: options,
selection: $appearanceRaw
)
}
}
// MARK: - Handedness picker row
struct HandednessPickerRow: View {
@Binding var selection: HandednessPreference
private var options: [(id: String, label: String)] {
HandednessPreference.allCases.map { preference in
(preference.rawValue, AppL10n.string(preference.labelKey))
}
}
var body: some View {
SettingsMenuPickerRow(
title: AppL10n.string("settings.handedness.title"),
options: options,
selection: Binding(
get: { selection.rawValue },
set: { newValue in
selection = HandednessPreference(rawValue: newValue) ?? .left
}
)
)
}
}
// MARK: - Keyboard haptic picker row
struct KeyboardHapticPickerRow: View {
@Binding var selection: KeyboardHapticIntensity
private var options: [(id: String, label: String)] {
KeyboardHapticIntensity.allCases.map { intensity in
(intensity.rawValue, AppL10n.string(intensity.labelKey))
}
}
var body: some View {
SettingsMenuPickerRow(
title: AppL10n.string("settings.keyboardHaptic.title"),
options: options,
selection: Binding(
get: { selection.rawValue },
set: { newValue in
selection = KeyboardHapticIntensity(rawValue: newValue) ?? .default
}
)
)
}
}
// MARK: - Polish intensity picker row
struct PolishIntensityPickerRow: View {
@ObservedObject var config: ProviderConfig
var body: some View {
SettingsMenuPickerRow(
title: AppL10n.string("settings.polishIntensity.title"),
options: PolishIntensity.allCases.map { intensity in
(
intensity.rawValue,
SharedL10n.string(intensity.labelKey, language: config.uiLanguage)
)
},
selection: Binding(
get: { config.polishIntensity.rawValue },
set: { rawValue in
config.polishIntensity = PolishIntensity(rawValue: rawValue) ?? .default
}
)
)
}
}
// MARK: - AI response length picker row
struct AIResponseLengthPickerRow: View {
@ObservedObject var config: ProviderConfig
var body: some View {
SettingsMenuPickerRow(
title: AppL10n.string("settings.aiAgent.responseLength.title", language: config.uiLanguage),
options: AIResponseLength.allCases.map { length in
(
length.rawValue,
SharedL10n.string(length.labelKey, language: config.uiLanguage)
)
},
selection: Binding(
get: { config.aiResponseLength.rawValue },
set: { rawValue in
config.aiResponseLength = AIResponseLength.resolve(storedRawValue: rawValue)
}
)
)
}
}
// MARK: - Default input mode picker
struct DefaultInputModePickerRow: View {
@Environment(\.themePalette) private var palette: ThemePalette
@ObservedObject private var config = ProviderConfig.shared
@Binding var selection: DefaultInputMode
private var options: [(id: String, label: String)] {
DefaultInputMode.allCases.map { mode in
(mode.rawValue, AppL10n.string(mode.labelKey, language: config.uiLanguage))
}
}
var body: some View {
HStack(alignment: .center, spacing: 12) {
VStack(alignment: .leading, spacing: 3) {
Text(AppL10n.string("settings.typingInput.default.title", language: config.uiLanguage))
.font(TypeStyle.body)
.foregroundStyle(palette.textPrimary)
Text(AppL10n.string("settings.typingInput.default.description", language: config.uiLanguage))
.font(TypeStyle.caption)
.foregroundStyle(palette.textSecondary)
.fixedSize(horizontal: false, vertical: true)
}
Spacer(minLength: 8)
Menu {
Picker("", selection: defaultInputModeSelection) {
ForEach(options, id: \.id) { option in
Text(option.label)
.tag(option.id)
}
}
.labelsHidden()
} label: {
HStack(spacing: 4) {
Text(currentLabel)
.font(TypeStyle.body)
.foregroundStyle(palette.textSecondary)
Image(systemName: "chevron.up.chevron.down")
.font(.system(size: 11, weight: .bold))
.foregroundStyle(palette.textTertiary)
}
}
}
.settingsListRow()
}
private var defaultInputModeSelection: Binding<String> {
Binding(
get: { selection.rawValue },
set: { selection = DefaultInputMode(rawValue: $0) ?? .voice }
)
}
private var currentLabel: String {
options.first(where: { $0.id == selection.rawValue })?.label ?? "—"
}
}
struct RememberLastSurfaceToggleRow: View {
@Environment(\.themePalette) private var palette: ThemePalette
@ObservedObject private var config = ProviderConfig.shared
@Binding var isOn: Bool
var body: some View {
Toggle(isOn: $isOn) {
VStack(alignment: .leading, spacing: 3) {
Text(AppL10n.string("settings.typingInput.rememberLast.title", language: config.uiLanguage))
.font(TypeStyle.body)
.foregroundStyle(palette.textPrimary)
Text(AppL10n.string("settings.typingInput.rememberLast.description", language: config.uiLanguage))
.font(TypeStyle.caption)
.foregroundStyle(palette.textSecondary)
.fixedSize(horizontal: false, vertical: true)
}
}
.tint(palette.accent)
.settingsListRow()
}
}
// MARK: - Menu picker row (generic)
struct SettingsMenuPickerRow: View {
@Environment(\.themePalette) private var palette: ThemePalette
let title: String
let options: [(id: String, label: String)]
@Binding var selection: String
var body: some View {
HStack {
Text(title)
.font(TypeStyle.body)
.foregroundStyle(palette.textPrimary)
Spacer()
Menu {
Picker("", selection: $selection) {
ForEach(options, id: \.id) { option in
Text(option.label)
.tag(option.id)
}
}
.labelsHidden()
} label: {
HStack(spacing: 4) {
Text(currentLabel)
.font(TypeStyle.body)
.foregroundStyle(palette.textSecondary)
Image(systemName: "chevron.up.chevron.down")
.font(.system(size: 11, weight: .bold))
.foregroundStyle(palette.textTertiary)
}
}
}
.settingsListRow()
}
private var currentLabel: String {
options.first(where: { $0.id == selection })?.label ?? "—"
}
}
// MARK: - Locale picker row (with on-device indicator)
struct LocalePickerRow: View {
@Environment(\.themePalette) private var palette: ThemePalette
@ObservedObject private var config = ProviderConfig.shared
let locales: [(id: String, onDevice: Bool)]
@Binding var selection: String
var body: some View {
HStack {
Text(AppL10n.string("settings.asrLocale"))
.font(TypeStyle.body)
.foregroundStyle(palette.textPrimary)
Spacer()
Menu {
Picker("", selection: $selection) {
ForEach(locales, id: \.id) { locale in
Text(label(for: locale.id))
.tag(locale.id)
}
}
.labelsHidden()
} label: {
HStack(spacing: 6) {
// On-device badge for the currently selected locale.
if let current = locales.first(where: { $0.id == selection }), current.onDevice {
Image(systemName: "iphone")
.font(TypeStyle.caption2)
.foregroundStyle(palette.accent)
}
Text(currentLabel)
.font(TypeStyle.body)
.foregroundStyle(palette.textSecondary)
Image(systemName: "chevron.up.chevron.down")
.font(.system(size: 11, weight: .bold))
.foregroundStyle(palette.textTertiary)
}
}
}
.settingsListRow()
}
private func label(for localeId: String) -> String {
ASRLocaleLabels.displayName(for: localeId, language: config.uiLanguage)
}
private var currentLabel: String {
label(for: selection)
}
}
// MARK: - Dynamic ASR locale loading
enum SettingsASRLocales {
/// Falls back to a short static list while `SFSpeechRecognizer` is loading.
static let staticFallback: [(id: String, onDevice: Bool)] = [
("auto", false),
("zh-Hans", false),
("zh-Hant", false),
("en-US", false),
("ja-JP", false),
("ko-KR", false)
]
static func loadDynamic() async -> [(id: String, onDevice: Bool)] {
// Run everything in a background task: `SFSpeechRecognizer.supportedLocales()`
// can return 100+ locales, and we probe supportsOnDeviceRecognition for each.
// Creating `SFSpeechRecognizer` instances in a @Sendable closure is
// safe here; we only read locale metadata (no transcription session).
await Task.detached(priority: .userInitiated) {
var result: [(id: String, onDevice: Bool)] = [("auto", false)]
for locale in SFSpeechRecognizer.supportedLocales()
.sorted(by: { $0.identifier < $1.identifier }) {
let id = locale.identifier
let onDevice = SFSpeechRecognizer(locale: locale)?.supportsOnDeviceRecognition ?? false
result.append((id: id, onDevice: onDevice))
}
return result
}.value
}
}