Compare commits

..

2 Commits

Author SHA1 Message Date
fish c86e482e68 修复大小写切换后新输入字母不跟随转换,输入时根据当前大小写状态存储
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-07-21 22:46:18 +08:00
fish ccf0bc876b 大小写切换改为原地更新字符,不重建页面
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-07-21 22:41:04 +08:00
3 changed files with 43 additions and 10 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
@@ -218,6 +218,7 @@ final class QuizViewController: UIViewController {
if isSpelling { if isSpelling {
spellingHintLabel.text = "\(question.word.word.count) 个字母,补全空缺部分" spellingHintLabel.text = "\(question.word.word.count) 个字母,补全空缺部分"
spellingView.isUppercase = isUppercase
spellingView.configure(blankedWord: question.blankedWord, blankPositions: question.blankPositions) spellingView.configure(blankedWord: question.blankedWord, blankPositions: question.blankPositions)
if config.isStudyMode { if config.isStudyMode {
nextButton.isHidden = true nextButton.isHidden = true
@@ -425,15 +426,9 @@ 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 spellingView.isUppercase = isUppercase
let words = config.words.map { word in questions = QuizGenerator.recase(questions, toUpper: isUppercase)
let text = isUppercase ? word.word.uppercased() : word.word.lowercased() spellingView.updateCase(toUpper: isUppercase)
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() {
@@ -17,6 +17,8 @@ final class SpellingInputView: UIView, UITextFieldDelegate {
private var cursorView: UIView? private var cursorView: UIView?
private var inputEnabled = true private var inputEnabled = true
var isUppercase = false
var isComplete: Bool { var isComplete: Bool {
!filledBlanks.isEmpty && filledBlanks.allSatisfy { !$0.isEmpty } !filledBlanks.isEmpty && filledBlanks.allSatisfy { !$0.isEmpty }
} }
@@ -133,6 +135,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 {
@@ -169,7 +185,8 @@ final class SpellingInputView: UIView, UITextFieldDelegate {
onChange?() onChange?()
} }
} else if let char = string.last, char.isLetter, cursorIndex < blankPositions.count { } else if let char = string.last, char.isLetter, cursorIndex < blankPositions.count {
filledBlanks[cursorIndex] = String(char).lowercased() let converted = isUppercase ? char.uppercased() : char.lowercased()
filledBlanks[cursorIndex] = converted
cursorIndex += 1 cursorIndex += 1
refreshBoxes() refreshBoxes()
popBox(atBlank: cursorIndex - 1) popBox(atBlank: cursorIndex - 1)