feat(training): 接入真实培训接口与学习答题恢复

This commit is contained in:
2026-10-14 16:18:48 +08:00
parent 8862ae5401
commit 01068bb24e
28 changed files with 4945 additions and 238 deletions
@@ -5,9 +5,11 @@ import UIKit
@MainActor
final class TrainingCenterViewController: TrainingBaseViewController {
/// 差异化列表中的任务与考试记录标识。
private enum Item: Hashable { case task(String), attempt(taskID: String, attemptID: String) }
private let service: any TrainingServing
private let viewModel = TrainingCenterViewModel()
private enum Item: Hashable { case task(String), attempt(String) }
private var reloadTask: Task<Void, Never>?
private var searchTask: Task<Void, Never>?
private let service: any TrainingDataServing
private let viewModel = TrainingRemoteCenterViewModel()
private let tableView = UITableView(frame: .zero, style: .plain)
private let refreshControl = UIRefreshControl()
private let header = UIStackView()
@@ -29,9 +31,9 @@ final class TrainingCenterViewController: TrainingBaseViewController {
private let moreButton = TrainingUI.button("加载更多记录", plain: true)
private var dataSource: UITableViewDiffableDataSource<Int, Item>!
/// 默认连接正式服务,开发演示仅由用户显式打开。
init(service: any TrainingServing = TrainingServiceFactory.production(), showsHistory: Bool = false) {
self.service = service
/// 默认使用当前登录身份连接环境配置中的培训接口。
init(service: (any TrainingDataServing)? = nil, showsHistory: Bool = false) {
self.service = service ?? TrainingRemoteServiceFactory.production()
viewModel.tab = showsHistory ? .records : .home
super.init(nibName: nil, bundle: nil)
}
@@ -41,9 +43,6 @@ final class TrainingCenterViewController: TrainingBaseViewController {
override func setupNavigationBar() {
TrainingUI.navigation(self, title: "培训中心")
#if DEBUG
if !service.isDemo { navigationItem.rightBarButtonItem = UIBarButtonItem(title: "开发演示", style: .plain, target: self, action: #selector(demoTapped)) }
#endif
}
override func setupUI() {
@@ -87,7 +86,6 @@ final class TrainingCenterViewController: TrainingBaseViewController {
body.spacing = 11
body.isLayoutMarginsRelativeArrangement = true
body.layoutMargins = UIEdgeInsets(top: 14, left: 13, bottom: 10, right: 13)
if service.isDemo { body.addArrangedSubview(TrainingUI.demoNotice()) }
let heading = UIStackView(arrangedSubviews: [headingLabel, viewAllButton])
heading.alignment = .center
heading.spacing = 8
@@ -205,9 +203,9 @@ final class TrainingCenterViewController: TrainingBaseViewController {
cell.configure(task: task)
cell.onAction = { [weak self] in self?.openTask(task, directAction: true) }
return cell
case .attempt(let taskID, let attemptID):
guard let task = self.viewModel.tasks.first(where: { $0.id == taskID }), let attempt = task.progress.attempts.first(where: { $0.id == attemptID }), let cell = table.dequeueReusableCell(withIdentifier: TrainingRecordCell.reuseIdentifier, for: indexPath) as? TrainingRecordCell else { return nil }
cell.configure(task: task, attempt: attempt)
case .attempt(let attemptID):
guard let attempt = self.viewModel.records.first(where: { $0.id == attemptID }), let cell = table.dequeueReusableCell(withIdentifier: TrainingRecordCell.reuseIdentifier, for: indexPath) as? TrainingRecordCell else { return nil }
cell.configure(attempt: attempt)
return cell
}
}
@@ -237,7 +235,7 @@ final class TrainingCenterViewController: TrainingBaseViewController {
button.configuration = configuration
button.accessibilityTraits = index == viewModel.tab.rawValue ? [.button, .selected] : .button
}
let items: [Item] = viewModel.tab == .records ? viewModel.records.prefix(viewModel.recordLimit).map { .attempt(taskID: $0.task.id, attemptID: $0.attempt.id) } : viewModel.visibleTasks.map { .task($0.id) }
let items: [Item] = viewModel.tab == .records ? viewModel.records.map { .attempt($0.id) } : viewModel.tasks.map { .task($0.id) }
var snapshot = NSDiffableDataSourceSnapshot<Int, Item>()
snapshot.appendSections([0])
snapshot.appendItems(items)
@@ -247,7 +245,11 @@ final class TrainingCenterViewController: TrainingBaseViewController {
stateLabel.isHidden = !items.isEmpty
retryButton.isHidden = viewModel.errorMessage == nil
retryButton.isEnabled = !viewModel.isLoading
moreButton.isHidden = viewModel.tab != .records || viewModel.records.count <= viewModel.recordLimit
moreButton.isHidden = !viewModel.hasMore
moreButton.isEnabled = !viewModel.isLoadingMore
var moreConfiguration = moreButton.configuration
moreConfiguration?.title = viewModel.isLoadingMore ? "正在加载…" : "加载更多"
moreButton.configuration = moreConfiguration
if !viewModel.isLoading { refreshControl.endRefreshing() }
sizeTableSupplementaryViews()
}
@@ -255,6 +257,8 @@ final class TrainingCenterViewController: TrainingBaseViewController {
/// 页面出现和下拉刷新都读取最新本人任务。
func reload() async {
await viewModel.load(service: service)
guard !Task.isCancelled else { return }
if viewModel.isIdentityInvalid { navigationController?.popToRootViewController(animated: false); return }
await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in
applyState { continuation.resume() }
}
@@ -265,29 +269,38 @@ final class TrainingCenterViewController: TrainingBaseViewController {
view.endEditing(true)
viewModel.selectTab(tab)
searchField.text = ""
applyState()
reloadTapped()
tableView.setContentOffset(.zero, animated: false)
}
@objc private func reloadTapped() {
guard !viewModel.isLoading else { return }
Task { @MainActor in await reload() }
reloadTask?.cancel()
reloadTask = Task { @MainActor [weak self] in await self?.reload() }
}
@objc private func tabTapped(_ button: UIButton) { if let tab = TrainingCenterTab(rawValue: button.tag) { selectTab(tab) } }
@objc private func filterTapped(_ button: UIButton) { viewModel.filter = button.tag == 0 ? .pendingExam : .completed; applyState() }
@objc private func searchChanged() { viewModel.keyword = searchField.text ?? ""; applyState() }
@objc private func moreTapped() { viewModel.recordLimit += 20; applyState() }
@objc private func filterTapped(_ button: UIButton) { viewModel.filter = button.tag == 0 ? .pendingExam : .completed; reloadTapped() }
@objc private func searchChanged() {
viewModel.setKeyword(searchField.text ?? "")
reloadTask?.cancel()
searchTask?.cancel()
moreButton.isHidden = true
searchTask = Task { @MainActor [weak self] in
do { try await Task.sleep(for: .milliseconds(300)) } catch { return }
self?.reloadTapped()
}
}
@objc private func moreTapped() {
Task { @MainActor [weak self] in
guard let self else { return }
await viewModel.loadMore(service: service)
if viewModel.isIdentityInvalid { navigationController?.popToRootViewController(animated: false); return }
applyState()
}
}
@objc private func viewAllTapped() { viewModel.filter = .pendingExam; selectTab(.tasks) }
#if DEBUG
@objc private func demoTapped() {
do { navigationController?.pushViewController(TrainingCenterViewController(service: try TrainingServiceFactory.demo()), animated: true) }
catch { showToast(error.localizedDescription) }
}
#endif
private func openTask(_ task: TrainingTask, directAction: Bool) {
let controller: UIViewController = directAction && TrainingTaskPresentation(task: task).canStartExam ? TrainingExamViewController(taskID: task.id, service: service) : TrainingCourseViewController(taskID: task.id, service: service)
private func openTask(_ task: TrainingTaskSummary, directAction: Bool) {
let controller: UIViewController = directAction && task.learning.canStartExam ? TrainingExamViewController(taskID: task.id, service: service) : TrainingCourseViewController(taskID: task.id, service: service)
navigationController?.pushViewController(controller, animated: true)
}
}
@@ -298,7 +311,7 @@ extension TrainingCenterViewController: UITableViewDelegate {
guard let item = dataSource.itemIdentifier(for: indexPath) else { return }
switch item {
case .task(let id): if let task = viewModel.tasks.first(where: { $0.id == id }) { openTask(task, directAction: false) }
case .attempt(let taskID, let attemptID): navigationController?.pushViewController(TrainingResultViewController(taskID: taskID, attemptID: attemptID, service: service), animated: true)
case .attempt(let attemptID): if let attempt = viewModel.records.first(where: { $0.id == attemptID }) { navigationController?.pushViewController(TrainingResultViewController(taskID: attempt.taskID, attemptID: attempt.id, service: service), animated: true) }
}
}
}