学习记录新增正确率统计,每次拼写验证追踪对错并计算百分比

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-07-21 21:50:40 +08:00
parent f48fd5c3d8
commit cc032110d4
4 changed files with 14 additions and 6 deletions
@@ -4,6 +4,11 @@ struct LearnedRecord: Codable {
let word: Word let word: Word
var learnedAt: Date var learnedAt: Date
var count: Int = 1 var count: Int = 1
var correctCount: Int = 0
var accuracy: Int {
count > 0 ? Int(Double(correctCount) / Double(count) * 100) : 0
}
} }
final class LearnedStore { final class LearnedStore {
@@ -26,12 +31,15 @@ final class LearnedStore {
} }
} }
func record(_ word: Word) { func record(_ word: Word, correct: Bool) {
if let index = records.firstIndex(where: { $0.word.id == word.id }) { if let index = records.firstIndex(where: { $0.word.id == word.id }) {
records[index].learnedAt = Date() records[index].learnedAt = Date()
records[index].count += 1 records[index].count += 1
if correct { records[index].correctCount += 1 }
} else { } else {
records.append(LearnedRecord(word: word, learnedAt: Date())) var rec = LearnedRecord(word: word, learnedAt: Date())
if correct { rec.correctCount = 1 }
records.append(rec)
} }
} }
@@ -67,7 +67,7 @@ final class HistoryViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! WordCardCell let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! WordCardCell
let record = sections[indexPath.section].items[indexPath.row] let record = sections[indexPath.section].items[indexPath.row]
cell.configure(with: record.word, count: record.count) cell.configure(with: record.word, count: record.count, accuracy: record.accuracy)
cell.onSpeak = { word in cell.onSpeak = { word in
SpeechService.shared.speak(word.word) SpeechService.shared.speak(word.word)
} }
@@ -357,8 +357,8 @@ final class QuizViewController: UIViewController {
private func submitSpelling(_ typed: String, for question: QuizQuestion) { private func submitSpelling(_ typed: String, for question: QuizQuestion) {
guard selectedOption == nil else { return } guard selectedOption == nil else { return }
selectedOption = typed selectedOption = typed
LearnedStore.shared.record(question.word)
let correct = typed.lowercased() == question.answer.lowercased() let correct = typed.lowercased() == question.answer.lowercased()
LearnedStore.shared.record(question.word, correct: correct)
if !correct { if !correct {
wrongWords.append(question.word) wrongWords.append(question.word)
} }
@@ -92,9 +92,9 @@ final class WordCardCell: UITableViewCell {
updateSpeakerState() updateSpeakerState()
} }
func configure(with word: Word, count: Int) { func configure(with word: Word, count: Int, accuracy: Int) {
configure(with: word) configure(with: word)
countLabel.text = "\(count)" countLabel.text = "\(count) · \(accuracy)%"
countLabel.isHidden = false countLabel.isHidden = false
} }