hex/ios/HEXMobile/CustomVocabularyEditor.swift
Ryan Vogel 56209138f2 iOS: Word replacements, opening and exporting Hex words files, behind Custom vocabulary
Word replacements: a new list (heard -> write, up to 1,000, the words
file's limits, no empty or blank outputs yet) under Settings > Dictation >
Custom vocabulary, shown and applied only while that switch is on. They
apply in DictationPipeline, so in the app and in-keyboard dictation alike,
right before the learned fixes with DictationCorrections.apply's matching
(now taking up to 8 words for them), and win over a learned fix with the
same heard words. Learned fixes are never changed. A dictation Translate
what I say translates gets none; one that goes in as spoken does.

Opening: Info.plist (project.yml) exports anomaly.hex.words (public.json,
.hexwords, application/vnd.anomaly.hex-words+json) and owns it as a
document type copied in (LSSupportsOpeningDocumentsInPlace NO). HexInbox,
a small inbox shaped for another file kind later (#2529), copies the file
privately, reads it, shows HexWordsReviewSheet over whatever is on screen
and deletes both copies when the sheet closes; it logs counts and reasons
only. The sheet shows what is added, already there, clashes (Keep mine by
default), what this iPhone can't use, what the file skipped, a switch per
part, and offers to turn Custom vocabulary on. Strings from the file are
Text(verbatim:), direction-isolated and shortened. Add shows a one-line
confirmation with Undo; "Undo last import" stays in settings until either
list changes or another file is imported.

Export: Export words opens a sheet with "Include fixes Hex learned" (off),
what is left out and why, then the share sheet with hex-words.hexwords,
deleted after sharing. ShareSheet.present gains an optional completion.

Every new string has English and Simplified Chinese. Docs: dictation.md
and settings-and-home.md.

Checked on anomastudio (iOS 27.2 simulator): HexWordsSharedCasesTests
(read 59/59, merge 11/11 phone, write 2/2 phone), WordReplacementsTests,
CustomVocabulary, DictationLearning, DictationSSOT, DictationController,
Greeklish and TranscriptCleanup tests all pass.
2026-11-24 18:13:04 -05:00

179 lines
6.4 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
/// The custom vocabulary list shown under its switch in Settings › Dictation, and on
/// Settings › Keyboard › Vocabulary: one list, which dictation spells (with its switch on) and
/// the full keyboard never autocorrects away, completes and swipes.
struct CustomVocabularyEditor: View {
private let settings = CustomVocabularySettings()
var footer = String(localized: "Fixes spelling, not words Hex mishears")
@State private var terms: [String] = []
@State private var draft = ""
@State private var notice: String?
private var isAtLimit: Bool {
terms.count >= CustomVocabularySettings.maxTermCount
}
var body: some View {
VStack(alignment: .leading, spacing: 10) {
ForEach(terms, id: \.self) { term in
HStack(spacing: 10) {
Text(term)
.font(.system(size: 14))
.foregroundStyle(HexTheme.textBase)
.lineLimit(1)
Spacer(minLength: 8)
Button {
remove(term)
} label: {
Image(systemName: "xmark")
.font(.system(size: 11, weight: .semibold))
.foregroundStyle(HexTheme.textMuted)
.frame(width: 26, height: 26)
.background(HexTheme.layer02, in: Circle())
}
.buttonStyle(.plain)
.accessibilityLabel("Remove \(term)")
}
.padding(.leading, 12)
.padding(.trailing, 6)
.frame(minHeight: 36)
.background(
HexTheme.layer01,
in: RoundedRectangle(
cornerRadius: HexTheme.controlRadius,
style: .continuous
)
)
}
TextField("Add a word or phrase", text: $draft)
.font(.system(size: 14))
.foregroundStyle(HexTheme.textBase)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.submitLabel(.done)
.onSubmit(add)
.onChange(of: draft) { _, _ in
notice = nil
}
.disabled(isAtLimit)
.padding(.horizontal, 12)
.frame(minHeight: 38)
.background(
HexTheme.layer01,
in: RoundedRectangle(
cornerRadius: HexTheme.controlRadius,
style: .continuous
)
)
.opacity(isAtLimit ? 0.5 : 1)
if isAtLimit {
Text("Up to 100 terms")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
}
if let notice {
Text(notice)
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
.fixedSize(horizontal: false, vertical: true)
}
Text(footer)
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
.fixedSize(horizontal: false, vertical: true)
}
.onAppear {
terms = settings.terms
}
.onReceive(NotificationCenter.default.publisher(for: .hexWordsListsDidChange)) { _ in
terms = settings.terms
}
}
private func add() {
guard !isAtLimit else { return }
let candidate = draft.trimmingCharacters(in: .whitespacesAndNewlines)
guard !candidate.isEmpty else { return }
guard candidate.count <= CustomVocabularySettings.maxTermLength else {
notice = String(localized: "Too long")
return
}
let key = VocabularySpelling.normalizedKey(candidate)
guard key.count >= 2 else {
notice = String(localized: "Needs at least two letters or digits")
return
}
let isDuplicate = terms.contains {
VocabularySpelling.normalizedKey($0) == key
}
guard !isDuplicate else {
notice = String(localized: "Already in the list")
return
}
let updated = CustomVocabularySettings.sanitize(terms + [candidate])
terms = updated
settings.terms = updated
draft = ""
notice = nil
}
private func remove(_ term: String) {
terms.removeAll { $0 == term }
settings.terms = terms
notice = nil
}
}
/// Settings › Keyboard › Vocabulary: how many words the list holds, opening its page.
struct KeyboardVocabularyRow: View {
@State private var count = CustomVocabularySettings().terms.count
@State private var showsPage = false
var body: some View {
Button {
showsPage = true
} label: {
HStack(spacing: 12) {
SettingsRowLabel(
title: String(localized: "Vocabulary"),
detail: count == 0
? String(localized: "Words autocorrect keeps and suggests")
: String(localized: "\(count) words autocorrect keeps and suggests")
)
Spacer(minLength: 8)
Image(systemName: "chevron.right")
.font(.system(size: 13, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
}
.padding(.horizontal, 16)
.padding(.vertical, 10)
.frame(minHeight: 50)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.navigationDestination(isPresented: $showsPage) {
SettingsPageScaffold(title: "Vocabulary") {
SettingsCard {
CustomVocabularyEditor(
footer: String(localized: "Hold a word you typed in the keyboard's suggestions to add it. Dictation spells these too while Custom vocabulary is on.")
)
.padding(16)
}
}
}
.onAppear { count = CustomVocabularySettings().terms.count }
.onChange(of: showsPage) { _, shown in
if !shown { count = CustomVocabularySettings().terms.count }
}
}
}