大小写切换改为原地更新字符,不重建页面

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-07-21 22:41:04 +08:00
parent d14ae41706
commit ccf0bc876b
3 changed files with 37 additions and 9 deletions
@@ -51,6 +51,27 @@ enum QuizGenerator {
} }
} }
static func recase(_ questions: [QuizQuestion], toUpper: Bool) -> [QuizQuestion] {
questions.map { q in
let word = q.word
let text = toUpper ? word.word.uppercased() : word.word.lowercased()
let recased = Word(categoryId: word.categoryId, word: text, phonetic: word.phonetic,
meaning: word.meaning, example: word.example, exampleMeaning: word.exampleMeaning)
let blanked = q.blankedWord.map { ch -> String in
ch == "_" ? "_" : ch.uppercased()
}.joined()
let blankedWord = toUpper ? blanked.uppercased() : blanked.lowercased()
return QuizQuestion(
word: recased,
prompt: q.prompt,
options: q.options,
answer: text,
blankedWord: blankedWord,
blankPositions: q.blankPositions
)
}
}
private static func promptText(_ word: Word, _ mode: QuizMode) -> String { private static func promptText(_ word: Word, _ mode: QuizMode) -> String {
switch mode { switch mode {
case .enToZh: return word.word case .enToZh: return word.word
@@ -425,15 +425,8 @@ final class QuizViewController: UIViewController {
isUppercase.toggle() isUppercase.toggle()
UserDefaults.standard.set(isUppercase, forKey: "spelling_uppercase") UserDefaults.standard.set(isUppercase, forKey: "spelling_uppercase")
updateCaseButton() updateCaseButton()
selectedOption = nil questions = QuizGenerator.recase(questions, toUpper: isUppercase)
let words = config.words.map { word in spellingView.updateCase(toUpper: isUppercase)
let text = isUppercase ? word.word.uppercased() : word.word.lowercased()
return Word(categoryId: word.categoryId, word: text, phonetic: word.phonetic,
meaning: word.meaning, example: word.example, exampleMeaning: word.exampleMeaning)
}
questions = QuizGenerator.makeQuestions(mode: config.mode, words: words)
showQuestion()
spellingView.activate()
} }
private func updateCaseButton() { private func updateCaseButton() {
@@ -133,6 +133,20 @@ final class SpellingInputView: UIView, UITextFieldDelegate {
hiddenField.inputAccessoryView = nil hiddenField.inputAccessoryView = nil
} }
func updateCase(toUpper: Bool) {
chars = chars.map {
guard $0 != "_" else { return $0 }
return toUpper ? $0.uppercased() : $0.lowercased()
}
filledBlanks = filledBlanks.map {
$0.isEmpty ? $0 : (toUpper ? $0.uppercased() : $0.lowercased())
}
for (index, box) in boxViews.enumerated() where chars[index] != "_" {
(box.viewWithTag(100) as? UILabel)?.text = chars[index]
}
refreshBoxes()
}
func showResultState(correct: Bool) { func showResultState(correct: Bool) {
let color = correct ? Theme.Color.correct : Theme.Color.wrong let color = correct ? Theme.Color.correct : Theme.Color.wrong
for position in blankPositions { for position in blankPositions {