Compare commits
14 Commits
41841896dd
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c86e482e68 | |||
| ccf0bc876b | |||
| d14ae41706 | |||
| 2d8caf3550 | |||
| 7728a22048 | |||
| ad0ff1ca7a | |||
| 106b60b58f | |||
| 63f5e7ee76 | |||
| cc032110d4 | |||
| f48fd5c3d8 | |||
| 35300e9aa1 | |||
| ad58d9ea5a | |||
| 673e35aec2 | |||
| 74dcaee335 |
@@ -2,7 +2,13 @@ import Foundation
|
||||
|
||||
struct LearnedRecord: Codable {
|
||||
let word: Word
|
||||
let learnedAt: Date
|
||||
var learnedAt: Date
|
||||
var count: Int = 1
|
||||
var correctCount: Int = 0
|
||||
|
||||
var accuracy: Int {
|
||||
count > 0 ? Int(Double(correctCount) / Double(count) * 100) : 0
|
||||
}
|
||||
}
|
||||
|
||||
final class LearnedStore {
|
||||
@@ -25,9 +31,20 @@ final class LearnedStore {
|
||||
}
|
||||
}
|
||||
|
||||
func record(_ word: Word) {
|
||||
guard !records.contains(where: { $0.word.id == word.id }) else { return }
|
||||
records.append(LearnedRecord(word: word, learnedAt: Date()))
|
||||
func record(_ word: Word, correct: Bool) {
|
||||
if let index = records.firstIndex(where: { $0.word.id == word.id }) {
|
||||
records[index].learnedAt = Date()
|
||||
records[index].count += 1
|
||||
if correct { records[index].correctCount += 1 }
|
||||
} else {
|
||||
var rec = LearnedRecord(word: word, learnedAt: Date())
|
||||
if correct { rec.correctCount = 1 }
|
||||
records.append(rec)
|
||||
}
|
||||
}
|
||||
|
||||
func record(for word: Word) -> LearnedRecord? {
|
||||
records.first { $0.word.id == word.id }
|
||||
}
|
||||
|
||||
func isRecorded(_ word: Word) -> Bool {
|
||||
|
||||
@@ -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 {
|
||||
switch mode {
|
||||
case .enToZh: return word.word
|
||||
|
||||
@@ -1,26 +1,49 @@
|
||||
import UIKit
|
||||
|
||||
final class HistoryViewController: UITableViewController {
|
||||
private let sections: [(title: String, items: [LearnedRecord])]
|
||||
private var sections: [(title: String, items: [LearnedRecord])] = []
|
||||
private var speechObserver: NSObjectProtocol?
|
||||
private let timeFormatter: DateFormatter = {
|
||||
let f = DateFormatter()
|
||||
f.locale = Locale(identifier: "zh_CN")
|
||||
f.dateFormat = "HH:mm:ss"
|
||||
return f
|
||||
}()
|
||||
|
||||
init() {
|
||||
sections = LearnedStore.shared.sectioned
|
||||
super.init(style: .insetGrouped)
|
||||
super.init(style: .plain)
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) { fatalError() }
|
||||
|
||||
deinit {
|
||||
if let speechObserver {
|
||||
NotificationCenter.default.removeObserver(speechObserver)
|
||||
}
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
title = "学习记录"
|
||||
navigationItem.largeTitleDisplayMode = .never
|
||||
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
|
||||
tableView.register(WordCardCell.self, forCellReuseIdentifier: "cell")
|
||||
tableView.separatorStyle = .none
|
||||
tableView.backgroundColor = .systemBackground
|
||||
updateEmptyState()
|
||||
tableView.contentInset.top = 8
|
||||
|
||||
speechObserver = NotificationCenter.default.addObserver(
|
||||
forName: SpeechService.speakingDidChangeNotification, object: nil, queue: .main
|
||||
) { [weak self] _ in
|
||||
for cell in self?.tableView.visibleCells ?? [] {
|
||||
(cell as? WordCardCell)?.updateSpeakerState()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func updateEmptyState() {
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
sections = LearnedStore.shared.sectioned
|
||||
if sections.isEmpty {
|
||||
let label = UILabel()
|
||||
label.text = "暂无学习记录"
|
||||
@@ -35,7 +58,10 @@ final class HistoryViewController: UITableViewController {
|
||||
label.centerYAnchor.constraint(equalTo: bg.centerYAnchor)
|
||||
])
|
||||
tableView.backgroundView = bg
|
||||
} else {
|
||||
tableView.backgroundView = nil
|
||||
}
|
||||
tableView.reloadData()
|
||||
}
|
||||
|
||||
override func numberOfSections(in tableView: UITableView) -> Int {
|
||||
@@ -51,14 +77,18 @@ final class HistoryViewController: UITableViewController {
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! WordCardCell
|
||||
let record = sections[indexPath.section].items[indexPath.row]
|
||||
var config = cell.defaultContentConfiguration()
|
||||
config.text = record.word.word
|
||||
config.secondaryText = "\(record.word.phonetic) \(record.word.meaning)"
|
||||
config.image = UIImage(systemName: "text.book.closed")
|
||||
config.imageProperties.tintColor = Theme.Color.accent
|
||||
cell.contentConfiguration = config
|
||||
let time = timeFormatter.string(from: record.learnedAt)
|
||||
cell.configure(with: record.word, count: record.count, accuracy: record.accuracy, time: time)
|
||||
cell.onSpeak = { word in
|
||||
SpeechService.shared.speak(word.word)
|
||||
}
|
||||
cell.onLearn = { [weak self] word in
|
||||
guard let self else { return }
|
||||
let config = QuizConfig(mode: .spelling, words: [word], isStudyMode: true)
|
||||
navigationController?.pushViewController(QuizViewController(config: config), animated: true)
|
||||
}
|
||||
return cell
|
||||
}
|
||||
|
||||
|
||||
@@ -218,6 +218,7 @@ final class QuizViewController: UIViewController {
|
||||
|
||||
if isSpelling {
|
||||
spellingHintLabel.text = "\(question.word.word.count) 个字母,补全空缺部分"
|
||||
spellingView.isUppercase = isUppercase
|
||||
spellingView.configure(blankedWord: question.blankedWord, blankPositions: question.blankPositions)
|
||||
if config.isStudyMode {
|
||||
nextButton.isHidden = true
|
||||
@@ -357,8 +358,8 @@ final class QuizViewController: UIViewController {
|
||||
private func submitSpelling(_ typed: String, for question: QuizQuestion) {
|
||||
guard selectedOption == nil else { return }
|
||||
selectedOption = typed
|
||||
LearnedStore.shared.record(question.word)
|
||||
let correct = typed.lowercased() == question.answer.lowercased()
|
||||
LearnedStore.shared.record(question.word, correct: correct)
|
||||
if !correct {
|
||||
wrongWords.append(question.word)
|
||||
}
|
||||
@@ -425,7 +426,9 @@ final class QuizViewController: UIViewController {
|
||||
isUppercase.toggle()
|
||||
UserDefaults.standard.set(isUppercase, forKey: "spelling_uppercase")
|
||||
updateCaseButton()
|
||||
restart()
|
||||
spellingView.isUppercase = isUppercase
|
||||
questions = QuizGenerator.recase(questions, toUpper: isUppercase)
|
||||
spellingView.updateCase(toUpper: isUppercase)
|
||||
}
|
||||
|
||||
private func updateCaseButton() {
|
||||
|
||||
@@ -17,6 +17,8 @@ final class SpellingInputView: UIView, UITextFieldDelegate {
|
||||
private var cursorView: UIView?
|
||||
private var inputEnabled = true
|
||||
|
||||
var isUppercase = false
|
||||
|
||||
var isComplete: Bool {
|
||||
!filledBlanks.isEmpty && filledBlanks.allSatisfy { !$0.isEmpty }
|
||||
}
|
||||
@@ -133,6 +135,20 @@ final class SpellingInputView: UIView, UITextFieldDelegate {
|
||||
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) {
|
||||
let color = correct ? Theme.Color.correct : Theme.Color.wrong
|
||||
for position in blankPositions {
|
||||
@@ -169,7 +185,8 @@ final class SpellingInputView: UIView, UITextFieldDelegate {
|
||||
onChange?()
|
||||
}
|
||||
} 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
|
||||
refreshBoxes()
|
||||
popBox(atBlank: cursorIndex - 1)
|
||||
|
||||
@@ -5,6 +5,7 @@ final class WordCardCell: UITableViewCell {
|
||||
private let wordLabel = UILabel()
|
||||
private let phoneticLabel = UILabel()
|
||||
private let meaningLabel = UILabel()
|
||||
private let countLabel = UILabel()
|
||||
private let learnButton = UIButton(type: .system)
|
||||
private let speakerButton = UIButton(type: .system)
|
||||
private var word: Word?
|
||||
@@ -37,6 +38,10 @@ final class WordCardCell: UITableViewCell {
|
||||
meaningLabel.font = .preferredFont(forTextStyle: .subheadline)
|
||||
meaningLabel.textColor = .secondaryLabel
|
||||
|
||||
countLabel.font = .preferredFont(forTextStyle: .caption2)
|
||||
countLabel.textColor = Theme.Color.accent
|
||||
countLabel.setContentHuggingPriority(.required, for: .horizontal)
|
||||
|
||||
let textStack = UIStackView(arrangedSubviews: [wordLabel, phoneticLabel, meaningLabel])
|
||||
textStack.axis = .vertical
|
||||
textStack.spacing = 8
|
||||
@@ -57,7 +62,7 @@ final class WordCardCell: UITableViewCell {
|
||||
}, for: .touchUpInside)
|
||||
speakerButton.setContentHuggingPriority(.required, for: .horizontal)
|
||||
|
||||
let row = UIStackView(arrangedSubviews: [textStack, learnButton, speakerButton])
|
||||
let row = UIStackView(arrangedSubviews: [textStack, countLabel, learnButton, speakerButton])
|
||||
row.axis = .horizontal
|
||||
row.alignment = .center
|
||||
row.spacing = 8
|
||||
@@ -83,9 +88,16 @@ final class WordCardCell: UITableViewCell {
|
||||
wordLabel.text = word.word
|
||||
phoneticLabel.text = word.phonetic
|
||||
meaningLabel.text = word.meaning
|
||||
countLabel.isHidden = true
|
||||
updateSpeakerState()
|
||||
}
|
||||
|
||||
func configure(with word: Word, count: Int, accuracy: Int, time: String) {
|
||||
configure(with: word)
|
||||
countLabel.text = "\(time) · \(count)次 · \(accuracy)%"
|
||||
countLabel.isHidden = false
|
||||
}
|
||||
|
||||
func updateSpeakerState() {
|
||||
guard let word else { return }
|
||||
let speaking = SpeechService.shared.isSpeaking(word.word)
|
||||
|
||||
@@ -31,7 +31,10 @@ final class WordDetailViewController: UIViewController {
|
||||
view.backgroundColor = .systemBackground
|
||||
title = categoryName
|
||||
navigationItem.largeTitleDisplayMode = .never
|
||||
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "学习", style: .plain, target: self, action: #selector(startSpelling))
|
||||
navigationItem.rightBarButtonItems = [
|
||||
UIBarButtonItem(title: "学习", style: .plain, target: self, action: #selector(startSpelling)),
|
||||
UIBarButtonItem(image: UIImage(systemName: "clock.arrow.circlepath"), style: .plain, target: self, action: #selector(showHistory))
|
||||
]
|
||||
buildLayout()
|
||||
updateSpeakState()
|
||||
speechObserver = NotificationCenter.default.addObserver(
|
||||
@@ -187,6 +190,10 @@ final class WordDetailViewController: UIViewController {
|
||||
return card
|
||||
}
|
||||
|
||||
@objc private func showHistory() {
|
||||
navigationController?.pushViewController(WordHistoryViewController(word: word), animated: true)
|
||||
}
|
||||
|
||||
@objc private func startSpelling() {
|
||||
Haptics.selection()
|
||||
let config = QuizConfig(mode: .spelling, words: [word], isStudyMode: true)
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
import UIKit
|
||||
|
||||
final class WordHistoryViewController: UIViewController {
|
||||
private let word: Word
|
||||
private let record: LearnedRecord?
|
||||
|
||||
init(word: Word) {
|
||||
self.word = word
|
||||
record = LearnedStore.shared.record(for: word)
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) { fatalError() }
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
title = "学习记录"
|
||||
navigationItem.largeTitleDisplayMode = .never
|
||||
view.backgroundColor = .systemBackground
|
||||
buildLayout()
|
||||
}
|
||||
|
||||
private func buildLayout() {
|
||||
guard let record else {
|
||||
let label = UILabel()
|
||||
label.text = "暂无学习记录"
|
||||
label.textAlignment = .center
|
||||
label.textColor = .secondaryLabel
|
||||
label.font = .preferredFont(forTextStyle: .body)
|
||||
view.addSubview(label)
|
||||
label.translatesAutoresizingMaskIntoConstraints = false
|
||||
NSLayoutConstraint.activate([
|
||||
label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
|
||||
label.centerYAnchor.constraint(equalTo: view.centerYAnchor)
|
||||
])
|
||||
return
|
||||
}
|
||||
|
||||
let wordCard = makeWordCard(record)
|
||||
let statsCard = makeStatsCard(record)
|
||||
|
||||
let stack = UIStackView(arrangedSubviews: [wordCard, statsCard])
|
||||
stack.axis = .vertical
|
||||
stack.spacing = 20
|
||||
|
||||
let scrollView = UIScrollView()
|
||||
scrollView.alwaysBounceVertical = true
|
||||
view.addSubview(scrollView)
|
||||
scrollView.addSubview(stack)
|
||||
scrollView.translatesAutoresizingMaskIntoConstraints = false
|
||||
stack.translatesAutoresizingMaskIntoConstraints = false
|
||||
NSLayoutConstraint.activate([
|
||||
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
|
||||
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
||||
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
||||
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
||||
stack.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor, constant: 20),
|
||||
stack.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor, constant: 16),
|
||||
stack.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor, constant: -16),
|
||||
stack.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor, constant: -16),
|
||||
stack.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor, constant: -32)
|
||||
])
|
||||
}
|
||||
|
||||
private func makeWordCard(_ record: LearnedRecord) -> UIView {
|
||||
let card = UIView()
|
||||
card.backgroundColor = Theme.Color.cardBackground
|
||||
card.layer.cornerRadius = Theme.Metrics.cardRadius
|
||||
|
||||
let wordLabel = UILabel()
|
||||
wordLabel.text = record.word.word
|
||||
wordLabel.font = .preferredFont(forTextStyle: .headline)
|
||||
|
||||
let phoneticLabel = UILabel()
|
||||
phoneticLabel.text = record.word.phonetic
|
||||
phoneticLabel.font = .preferredFont(forTextStyle: .caption1)
|
||||
phoneticLabel.textColor = .secondaryLabel
|
||||
|
||||
let meaningLabel = UILabel()
|
||||
meaningLabel.text = record.word.meaning
|
||||
meaningLabel.font = .preferredFont(forTextStyle: .subheadline)
|
||||
meaningLabel.textColor = .secondaryLabel
|
||||
|
||||
let textStack = UIStackView(arrangedSubviews: [wordLabel, phoneticLabel, meaningLabel])
|
||||
textStack.axis = .vertical
|
||||
textStack.spacing = 8
|
||||
|
||||
card.addSubview(textStack)
|
||||
textStack.translatesAutoresizingMaskIntoConstraints = false
|
||||
NSLayoutConstraint.activate([
|
||||
textStack.topAnchor.constraint(equalTo: card.topAnchor, constant: 14),
|
||||
textStack.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 14),
|
||||
textStack.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -14),
|
||||
textStack.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -14)
|
||||
])
|
||||
return card
|
||||
}
|
||||
|
||||
private func makeStatsCard(_ record: LearnedRecord) -> UIView {
|
||||
let card = UIView()
|
||||
card.backgroundColor = Theme.Color.cardBackground
|
||||
card.layer.cornerRadius = Theme.Metrics.cardRadius
|
||||
|
||||
let title = UILabel()
|
||||
title.text = "学习统计"
|
||||
title.font = .preferredFont(forTextStyle: .caption1)
|
||||
title.textColor = .secondaryLabel
|
||||
|
||||
let countLabel = UILabel()
|
||||
countLabel.text = "\(record.count) 次"
|
||||
countLabel.font = .systemFont(ofSize: 36, weight: .bold)
|
||||
|
||||
let accuracyLabel = UILabel()
|
||||
accuracyLabel.text = "正确率 \(record.accuracy)%"
|
||||
accuracyLabel.font = .preferredFont(forTextStyle: .subheadline)
|
||||
accuracyLabel.textColor = Theme.Color.correct
|
||||
|
||||
let formatter = DateFormatter()
|
||||
formatter.locale = Locale(identifier: "zh_CN")
|
||||
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
|
||||
let dateLabel = UILabel()
|
||||
dateLabel.text = "最近学习:\(formatter.string(from: record.learnedAt))"
|
||||
dateLabel.font = .preferredFont(forTextStyle: .caption1)
|
||||
dateLabel.textColor = .tertiaryLabel
|
||||
|
||||
let statsStack = UIStackView(arrangedSubviews: [countLabel, accuracyLabel])
|
||||
statsStack.axis = .horizontal
|
||||
statsStack.spacing = 16
|
||||
statsStack.alignment = .lastBaseline
|
||||
|
||||
let stack = UIStackView(arrangedSubviews: [title, statsStack, dateLabel])
|
||||
stack.axis = .vertical
|
||||
stack.spacing = 12
|
||||
|
||||
card.addSubview(stack)
|
||||
stack.translatesAutoresizingMaskIntoConstraints = false
|
||||
NSLayoutConstraint.activate([
|
||||
stack.topAnchor.constraint(equalTo: card.topAnchor, constant: 16),
|
||||
stack.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 16),
|
||||
stack.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -16),
|
||||
stack.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -16)
|
||||
])
|
||||
return card
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user