diff --git a/LearnEnglish/LearnEnglish/Views/HistoryViewController.swift b/LearnEnglish/LearnEnglish/Views/HistoryViewController.swift index e154618..95ba835 100644 --- a/LearnEnglish/LearnEnglish/Views/HistoryViewController.swift +++ b/LearnEnglish/LearnEnglish/Views/HistoryViewController.swift @@ -2,6 +2,7 @@ import UIKit final class HistoryViewController: UITableViewController { private let sections: [(title: String, items: [LearnedRecord])] + private var speechObserver: NSObjectProtocol? init() { sections = LearnedStore.shared.sectioned @@ -11,13 +12,26 @@ final class HistoryViewController: UITableViewController { @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() + + speechObserver = NotificationCenter.default.addObserver( + forName: SpeechService.speakingDidChangeNotification, object: nil, queue: .main + ) { [weak self] _ in + self?.updateVisibleSpeakers() + } } private func updateEmptyState() { @@ -38,6 +52,12 @@ final class HistoryViewController: UITableViewController { } } + private func updateVisibleSpeakers() { + for cell in tableView.visibleCells.compactMap({ $0 as? WordCardCell }) { + cell.updateSpeakerState() + } + } + override func numberOfSections(in tableView: UITableView) -> Int { sections.count } @@ -51,14 +71,17 @@ 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 + cell.configure(with: record.word) + 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 }