hex/ios/HEXShared/HexWords/HexWordsImport.swift
Ryan Vogel f21a71a31d iOS: read, merge and write Hex words files (.hexwords), with the shared cases
Adds ios/HEXShared/HexWords/, the iPhone's implementation of
docs/specs/hex-words.md:

- HexWordsCheck: the spec's check pass, one non-recursive walk over the
  bytes before JSONSerialization: more than 8 levels (tooDeep), a repeated
  key compared byte for byte after decoding escapes (duplicateKey), and
  everything strict RFC 8259 forbids that JSONSerialization lets through,
  escaped lone surrogates included (notJSON).
- HexWordsReader: size, BOM, UTF-8 (checked by a lossless round trip),
  empty, check pass, then JSONSerialization. Objects stay NSDictionary,
  because bridging keys that differ only in normalization into one Swift
  dictionary would trap. Booleans are told from numbers by CFBoolean, so
  "learned": 1 is badField and "version": true is badVersion. Lengths
  count unicodeScalars, letters are general category L or N, whitespace
  is the spec's exact list, and the key lowercases scalar by scalar then
  maps final sigma to sigma.
- HexWordsMerge: the phone column of the merge table.
- HexWordsWriter: the phone's export, by the reader's own rules and order.
- WordReplacementsSettings and HexWordsImport (Add and Undo) in the App
  Group.

HexWordsSharedCasesTests loads tests/fixtures/hex-words in place (a folder
resource in project.yml) and runs every read case, the phone's merge and
write cases (the desktop's are the desktop's), Undo after each merge, and
a small JSON Schema check of what the writer writes.

Checked on anomastudio (iOS 27.2 simulator, simlanes): read.json 54/54,
merge.json 11/11 phone cases, write.json 2/2 phone cases.
2026-11-24 18:11:43 -05:00

77 lines
3.4 KiB
Swift

import Foundation
/// The copy of both lists from before an import, kept in the App Group until the person edits
/// either list or imports again, so Undo survives a restart. It holds the lists, never the file.
nonisolated struct HexWordsUndo: Codable, Equatable, Sendable {
var before: HexWordsLists
var after: HexWordsLists
/// The sheet turned Custom vocabulary on; Undo turns it off again.
var turnedOnCustomVocabulary: Bool
}
/// Adds a file that was read to the iPhone's lists, and puts them back.
nonisolated struct HexWordsImport {
private static let undoKey = "hexWords.undo"
private let defaults: UserDefaults?
init(defaults: UserDefaults? = UserDefaults(suiteName: KeyboardBridge.appGroup)) {
self.defaults = defaults
}
private var vocabulary: CustomVocabularySettings { CustomVocabularySettings(defaults: defaults) }
private var replacements: WordReplacementsSettings { WordReplacementsSettings(defaults: defaults) }
var lists: HexWordsLists {
HexWordsLists(vocabulary: vocabulary.terms, replacements: replacements.entries)
}
/// What Add would do with these choices, without changing anything.
func preview(
_ file: HexWordsFile,
parts: HexWordsMerge.Parts = HexWordsMerge.Parts(),
useTheirs: Set<String> = [],
limits: HexWordsMerge.Limits = HexWordsMerge.Limits()
) -> HexWordsMergeResult {
HexWordsMerge.phone(before: lists, incoming: file, limits: limits, parts: parts, useTheirs: useTheirs)
}
/// Add: merges into both lists, turns Custom vocabulary on when the person asked the sheet
/// to, and keeps what Undo needs. Nothing else changes.
@discardableResult
func add(
_ file: HexWordsFile,
parts: HexWordsMerge.Parts = HexWordsMerge.Parts(),
useTheirs: Set<String> = [],
turnOnCustomVocabulary: Bool = false,
limits: HexWordsMerge.Limits = HexWordsMerge.Limits()
) -> HexWordsMergeResult {
let before = lists
let result = HexWordsMerge.phone(before: before, incoming: file, limits: limits, parts: parts, useTheirs: useTheirs)
vocabulary.terms = result.after.vocabulary
replacements.entries = result.after.replacements
let turnsOn = turnOnCustomVocabulary && !vocabulary.isEnabled
if turnsOn { vocabulary.isEnabled = true }
let undo = HexWordsUndo(before: before, after: lists, turnedOnCustomVocabulary: turnsOn)
defaults?.set(try? JSONEncoder().encode(undo), forKey: Self.undoKey)
return result
}
/// The last import's copy, while both lists are still as that import left them.
var availableUndo: HexWordsUndo? {
guard let data = defaults?.data(forKey: Self.undoKey),
let undo = try? JSONDecoder().decode(HexWordsUndo.self, from: data) else { return nil }
return undo.after == lists ? undo : nil
}
/// Puts both lists (and a switch the sheet turned on) back exactly. False when the lists
/// changed since, which ends Undo.
@discardableResult
func undo() -> Bool {
defer { defaults?.removeObject(forKey: Self.undoKey) }
guard let undo = availableUndo else { return false }
vocabulary.terms = undo.before.vocabulary
replacements.entries = undo.before.replacements
if undo.turnedOnCustomVocabulary { vocabulary.isEnabled = false }
return true
}
}