新增拼写功能:出题随机空缺字母,用户输入完整单词

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-07-18 21:24:57 +08:00
parent 1d7b48837c
commit 94ef6ad84a
3 changed files with 95 additions and 27 deletions
+71 -24
View File
@@ -9,6 +9,7 @@ struct QuizView: View {
@State private var questions: [QuizQuestion]
@State private var currentIndex = 0
@State private var selectedOption: String?
@State private var userInput = ""
@State private var wrongWords: [Word] = []
@State private var finished = false
@State private var startDate = Date.now
@@ -67,34 +68,26 @@ struct QuizView: View {
.frame(maxWidth: .infinity)
.padding(.vertical, 24)
VStack(spacing: 12) {
ForEach(question.options, id: \.self) { option in
Button {
select(option, for: question)
} label: {
VStack(spacing: 4) {
Text(option)
.font(.headline)
if config.mode == .zhToEn, let w = config.words.first(where: { $0.word == option }) {
Text(w.phonetic)
.font(.caption)
.foregroundStyle(.secondary)
}
}
.frame(maxWidth: .infinity)
.padding(.vertical, 10)
}
.buttonStyle(.bordered)
.tint(optionColor(option, for: question))
.disabled(selectedOption != nil)
}
if config.mode == .spelling {
spellingView(question)
} else {
optionsView(question)
}
if let selectedOption {
VStack(spacing: 12) {
Text(selectedOption == question.answer ? "回答正确" : "正确答案:\(question.answer)")
.font(.headline)
.foregroundStyle(selectedOption == question.answer ? .green : .red)
if config.mode == .spelling {
Text(selectedOption == question.answer ? "正确" : "正确答案:\(question.answer)")
.font(.title2.weight(.bold))
.foregroundStyle(selectedOption == question.answer ? .green : .red)
Text(question.word.phonetic)
.font(.subheadline)
.foregroundStyle(.secondary)
} else {
Text(selectedOption == question.answer ? "回答正确" : "正确答案:\(question.answer)")
.font(.headline)
.foregroundStyle(selectedOption == question.answer ? .green : .red)
}
Button {
next()
} label: {
@@ -112,6 +105,58 @@ struct QuizView: View {
.padding()
}
private func optionsView(_ question: QuizQuestion) -> some View {
VStack(spacing: 12) {
ForEach(question.options, id: \.self) { option in
Button {
select(option, for: question)
} label: {
VStack(spacing: 4) {
Text(option)
.font(.headline)
if config.mode == .zhToEn, let w = config.words.first(where: { $0.word == option }) {
Text(w.phonetic)
.font(.caption)
.foregroundStyle(.secondary)
}
}
.frame(maxWidth: .infinity)
.padding(.vertical, 10)
}
.buttonStyle(.bordered)
.tint(optionColor(option, for: question))
.disabled(selectedOption != nil)
}
}
}
private func spellingView(_ question: QuizQuestion) -> some View {
VStack(spacing: 16) {
Text(question.blankedWord)
.font(.system(size: 44, weight: .bold, design: .monospaced))
.tracking(6)
Text("\(question.word.word.count) 个字母")
.font(.subheadline)
.foregroundStyle(.secondary)
HStack(spacing: 12) {
TextField("输入完整单词", text: $userInput)
.textFieldStyle(.roundedBorder)
.autocapitalization(.none)
.disableAutocorrection(true)
.disabled(selectedOption != nil)
Button("确认") {
guard !userInput.trimmingCharacters(in: .whitespaces).isEmpty else { return }
selectedOption = userInput.trimmingCharacters(in: .whitespaces).lowercased()
if selectedOption != question.answer {
wrongWords.append(question.word)
}
}
.buttonStyle(.borderedProminent)
.disabled(userInput.trimmingCharacters(in: .whitespaces).isEmpty || selectedOption != nil)
}
}
}
private func optionColor(_ option: String, for question: QuizQuestion) -> Color {
guard let selectedOption else { return .accentColor }
if option == question.answer { return .green }
@@ -131,6 +176,7 @@ struct QuizView: View {
if currentIndex + 1 < questions.count {
currentIndex += 1
selectedOption = nil
userInput = ""
} else {
finished = true
}
@@ -140,6 +186,7 @@ struct QuizView: View {
questions = QuizGenerator.makeQuestions(mode: config.mode, words: config.words)
currentIndex = 0
selectedOption = nil
userInput = ""
wrongWords = []
finished = false
startDate = .now
@@ -25,6 +25,7 @@ struct WordListView: View {
Menu {
Button("英选汉") { startQuiz(.enToZh) }
Button("汉选英") { startQuiz(.zhToEn) }
Button("拼写") { startQuiz(.spelling) }
} label: {
Text("测验")
}