feat(training): 新增已结束筛选并精简课程展示

新增已结束任务筛选与只读回顾资格,保留搜索和游标分页。移除任务小节数量及课程附件文件名、切换入口,并更新接口和界面回归测试。
This commit is contained in:
2026-10-20 14:04:25 +08:00
parent b149295e66
commit 27be1f30ed
14 changed files with 227 additions and 77 deletions
@@ -79,6 +79,7 @@ extension TrainingLearnerStatus: Codable {
switch try container.decode(String.self) {
case "pending": self = .pendingExam
case "completed": self = .completed
case "ended": self = .ended
default: throw DecodingError.dataCorruptedError(in: container, debugDescription: "Unknown training learner status")
}
}
@@ -88,7 +89,7 @@ extension TrainingLearnerStatus: Codable {
try container.encode(apiValue)
}
/// 查询参数使用的正式状态值。
var apiValue: String { self == .completed ? "completed" : "pending" }
var apiValue: String { switch self { case .pendingExam: "pending"; case .completed: "completed"; case .ended: "ended" } }
}
/// 游标分页;小节、附件、任务和成绩复用相同分页边界。
@@ -6,12 +6,12 @@ enum TrainingMode: String, Codable, Sendable { case study, exam }
/// 任务发布状态;关闭后只允许回顾。
enum TrainingTaskStatus: String, Codable, Sendable { case published, closed }
/// 学员列表统一展示的两种状态。
/// 学员任务列表的待办、完成与结束状态。
enum TrainingLearnerStatus: String, CaseIterable, Sendable {
case pendingExam, completed
case pendingExam, completed, ended
/// 面向学员的状态名称。
var title: String { self == .completed ? "已完成" : "待考试" }
var title: String { switch self { case .pendingExam: "待考试"; case .completed: "已完成"; case .ended: "已结束" } }
}
/// 单选和多选题型。
@@ -122,12 +122,12 @@ final class TrainingRemoteExamViewModel {
var canRetryCurrentPage: Bool {
!isBusy && identityIsValid && !hasPendingSubmission && !isResetPending && !recoveryStorageFailed
&& !shouldReturnToCenter && submittedAttempt == nil && paper != nil && !isCurrentPageReady
&& progress?.canStartExam == true && progress?.learnerStatus != .completed
&& progress?.canStartExam == true && progress?.learnerStatus == .pendingExam
}
/// 权威资格及当前页就绪时可编辑,后台保存不会阻断本机选择。
var canEdit: Bool {
!isBusy && identityIsValid && !hasPendingSubmission && !isResetPending && !needsDraftRefresh && !recoveryStorageFailed
&& submittedAttempt == nil && progress?.canStartExam == true && progress?.learnerStatus != .completed
&& submittedAttempt == nil && progress?.canStartExam == true && progress?.learnerStatus == .pendingExam
&& !shouldReturnToCenter && paper != nil && isCurrentPageReady
}
@@ -420,8 +420,8 @@ final class TrainingRemoteExamViewModel {
private func requireExamAllowed() throws {
guard task?.id == taskID, progress?.taskID == taskID else { throw TrainingError.invalidPaper }
guard task?.mode == .exam, task?.learnerStatus != .completed,
progress?.learnerStatus != .completed, progress?.completedAt == nil,
guard task?.mode == .exam, task?.learnerStatus == .pendingExam,
progress?.learnerStatus == .pendingExam, progress?.completedAt == nil,
progress?.canStartExam == true, progress?.readingCompleted == true else {
throw TrainingRemoteError.examNotAllowed("当前任务暂不能考试,请返回课程确认学习进度和任务状态")
}
@@ -98,7 +98,10 @@ final class TrainingRemoteReadingViewModel {
}
/// 当前后端是否允许继续有效学习,完成或只读任务不建立新会话。
var canStudy: Bool { progress?.canStudy == true && progress?.readingCompleted == false && !isInvalidated }
var canStudy: Bool {
task?.summary.learnerStatus == .pendingExam && progress?.learnerStatus == .pendingExam
&& progress?.canStudy == true && progress?.readingCompleted == false && !isInvalidated
}
/// 首次只读课程元信息、附件第一页与进度,并为首个附件授权;不请求小节标题或 HTML。
func load() async {
@@ -100,9 +100,14 @@ final class TrainingCenterViewController: TrainingBaseViewController {
body.addArrangedSubview(heading)
filters.distribution = .fillEqually
filters.spacing = 9
for state in TrainingLearnerStatus.allCases {
for (index, state) in TrainingLearnerStatus.allCases.enumerated() {
let button = TrainingUI.button(state.title, secondary: true)
button.tag = state == .pendingExam ? 0 : 1
var configuration = button.configuration
configuration?.contentInsets = NSDirectionalEdgeInsets(top: 12, leading: 6, bottom: 12, trailing: 6)
configuration?.titleAlignment = .center
configuration?.titleLineBreakMode = .byWordWrapping
button.configuration = configuration
button.tag = index
button.accessibilityIdentifier = "training.filter.\(button.tag)"
button.addTarget(self, action: #selector(filterTapped(_:)), for: .touchUpInside)
filterButtons.append(button)
@@ -231,11 +236,18 @@ final class TrainingCenterViewController: TrainingBaseViewController {
filters.isHidden = viewModel.tab != .tasks
searchField.isHidden = viewModel.tab != .tasks
for (index, button) in filterButtons.enumerated() {
let state = TrainingLearnerStatus.allCases[index]
let selected = state == viewModel.filter
var configuration = button.configuration
configuration?.title = index == 0 ? "待考试 \(viewModel.pendingCount)" : "已完成 \(viewModel.completedCount)"
configuration?.background.backgroundColor = index == (viewModel.filter == .pendingExam ? 0 : 1) ? TrainingUI.primary.withAlphaComponent(0.09) : .white
configuration?.baseForegroundColor = index == (viewModel.filter == .pendingExam ? 0 : 1) ? TrainingUI.primary : TrainingUI.muted
configuration?.title = switch state {
case .pendingExam: "待考试 \(viewModel.pendingCount)"
case .completed: "已完成 \(viewModel.completedCount)"
case .ended: state.title
}
configuration?.background.backgroundColor = selected ? TrainingUI.primary.withAlphaComponent(0.09) : .white
configuration?.baseForegroundColor = selected ? TrainingUI.primary : TrainingUI.muted
button.configuration = configuration
button.accessibilityTraits = selected ? [.button, .selected] : .button
}
for (index, button) in tabButtons.enumerated() {
var configuration = button.configuration
@@ -316,7 +328,11 @@ final class TrainingCenterViewController: TrainingBaseViewController {
}
}
@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; reloadTapped() }
@objc private func filterTapped(_ button: UIButton) {
guard TrainingLearnerStatus.allCases.indices.contains(button.tag) else { return }
viewModel.filter = TrainingLearnerStatus.allCases[button.tag]
reloadTapped()
}
@objc private func searchChanged() {
viewModel.setKeyword(searchField.text ?? "")
reloadTask?.cancel()
@@ -351,7 +367,7 @@ final class TrainingCenterViewController: TrainingBaseViewController {
@objc private func viewAllTapped() { viewModel.filter = .pendingExam; selectTab(.tasks) }
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)
let controller: UIViewController = directAction && task.learnerStatus == .pendingExam && task.learning.canStartExam ? TrainingExamViewController(taskID: task.id, service: service) : TrainingCourseViewController(taskID: task.id, service: service)
navigationController?.pushViewController(controller, animated: true)
}
}
@@ -16,7 +16,6 @@ final class TrainingCourseViewController: TrainingBaseViewController {
private let courseNameLabel = TrainingUI.text(size: 12.5, color: TrainingUI.muted)
private let examInfoLabel = TrainingUI.text(size: 12.5, color: TrainingUI.muted)
private let introductionLabel = TrainingUI.text(size: 12.5, color: TrainingUI.muted)
private let attachmentButton = TrainingUI.button("", plain: true)
private let previewView = TrainingDocumentPreviewView()
private var previewHeightConstraint: Constraint?
private var localDocument: TrainingLocalDocument?
@@ -84,12 +83,7 @@ final class TrainingCourseViewController: TrainingBaseViewController {
courseNameLabel.accessibilityIdentifier = "training.reading.courseName"
examInfoLabel.accessibilityIdentifier = "training.reading.examInfo"
introductionLabel.accessibilityIdentifier = "training.reading.introduction"
attachmentButton.contentHorizontalAlignment = .leading
attachmentButton.accessibilityIdentifier = "training.reading.documentPicker"
attachmentButton.showsMenuAsPrimaryAction = true
attachmentButton.isHidden = true
contentStack.addArrangedSubview(metadataStack)
contentStack.addArrangedSubview(attachmentButton)
contentStack.addArrangedSubview(previewView)
stepView.isHidden = true
bottomStack.axis = .vertical
@@ -289,37 +283,6 @@ final class TrainingCourseViewController: TrainingBaseViewController {
examInfoLabel.text = task.mode == .exam ? "学习 + 考试 · \(task.questionCount) 题 · \(task.passScore ?? 0) 分及格" : "仅学习"
introductionLabel.text = course.introduction
introductionLabel.isHidden = course.introduction.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
attachmentButton.isHidden = viewModel.currentAttachment == nil
var configuration = attachmentButton.configuration
configuration?.title = viewModel.currentAttachment?.name
let canSwitch = viewModel.attachments.count > 1 || viewModel.hasMoreAttachments
configuration?.image = canSwitch ? UIImage(systemName: "chevron.down") : nil
configuration?.imagePlacement = .trailing
configuration?.imagePadding = 8
configuration?.contentInsets = .zero
configuration?.titleLineBreakMode = .byWordWrapping
attachmentButton.configuration = configuration
attachmentButton.isUserInteractionEnabled = canSwitch && userRequestID == nil
var actions = viewModel.attachments.map { attachment in
UIAction(title: attachment.name, state: attachment.id == viewModel.currentAttachmentID ? .on : .off) { [weak self] _ in
guard attachment.id != self?.viewModel.currentAttachmentID else { return }
self?.selectAttachment(attachment.id)
}
}
if viewModel.hasMoreAttachments {
actions.append(UIAction(title: "加载更多附件") { [weak self] _ in self?.loadMoreAttachments() })
}
attachmentButton.menu = UIMenu(children: actions)
}
private func loadMoreAttachments() {
guard let request = beginUserRequest(showsLoading: true) else { return }
Task { @MainActor [weak self] in
guard let self else { return }
await performUserRequest(request) { await self.viewModel.loadAttachments(loadMore: true) }
applyMetadata()
if let message = viewModel.attachmentListErrorMessage { showToast(message) }
}
}
private func applyState() {
@@ -330,7 +293,6 @@ final class TrainingCourseViewController: TrainingBaseViewController {
}
let canInteract = userRequestID == nil
actionButton.isUserInteractionEnabled = canInteract
attachmentButton.isUserInteractionEnabled = canInteract && (viewModel.attachments.count > 1 || viewModel.hasMoreAttachments)
guard canInteract else { return }
if contentLoadFailed || viewModel.attachmentErrorMessage != nil || viewModel.task == nil {
noticeLabel.text = viewModel.errorMessage ?? "正在加载课程…"
@@ -344,9 +306,18 @@ final class TrainingCourseViewController: TrainingBaseViewController {
actionButton.isEnabled = true
return
}
if viewModel.progress?.learnerStatus == .ended {
noticeLabel.text = "任务已结束,仅可回顾课程内容"
setActionTitle("返回培训中心")
actionButton.isEnabled = true
readingTimer?.invalidate()
readingTimer = nil
return
}
if let progress = viewModel.progress, progress.readingCompleted {
noticeLabel.text = viewModel.readingErrorMessage ?? "课程阅读已完成"
if viewModel.task?.summary.mode == .exam && progress.canStartExam { setActionTitle("开始考试") }
if viewModel.task?.summary.mode == .exam && viewModel.task?.summary.learnerStatus == .pendingExam
&& progress.learnerStatus == .pendingExam && progress.canStartExam { setActionTitle("开始考试") }
else { setActionTitle("返回培训中心") }
actionButton.isEnabled = true
readingTimer?.invalidate()
@@ -487,7 +458,8 @@ final class TrainingCourseViewController: TrainingBaseViewController {
} else if viewModel.attachments.isEmpty { returnToCenter() }
else if let progress = viewModel.progress, progress.readingCompleted {
pauseReading()
if viewModel.task?.summary.mode == .exam && progress.canStartExam {
if viewModel.task?.summary.mode == .exam && viewModel.task?.summary.learnerStatus == .pendingExam
&& progress.learnerStatus == .pendingExam && progress.canStartExam {
navigationController?.pushViewController(TrainingExamViewController(taskID: taskID, service: service), animated: true)
} else { returnToCenter() }
} else if !viewModel.canStudy { returnToCenter() }
@@ -239,7 +239,7 @@ final class TrainingResultViewController: TrainingBaseViewController {
contentStack.addArrangedSubview(more)
}
}
let canRetry = !attempt.passed && detail.taskState.canRetry
let canRetry = !attempt.passed && detail.taskState.learnerStatus == .pendingExam && detail.taskState.canRetry
contentStack.addArrangedSubview(TrainingUI.text(canRetry ? "截止前可不限次数重考,每次成绩都会保留。" : "本次记录只读,可回顾课程内容和答案解析。", size: 12, color: TrainingUI.muted))
setActionTitle(canRetry ? "再考一次" : "完成,返回培训中心")
}
@@ -294,7 +294,7 @@ final class TrainingResultViewController: TrainingBaseViewController {
if loadFailed { loadResult(); return }
guard let detail = viewModel.detail, let navigationController else { return }
finishCurrentRequestBeforeNavigation()
if !detail.attempt.passed, detail.taskState.canRetry {
if !detail.attempt.passed, detail.taskState.learnerStatus == .pendingExam, detail.taskState.canRetry {
navigationController.setViewControllers(Array(navigationController.viewControllers.dropLast()) + [TrainingExamViewController(taskID: taskID, service: service, resetDraft: true)], animated: true)
} else if let center = navigationController.viewControllers.last(where: { $0 is TrainingCenterViewController }) {
navigationController.popToViewController(center, animated: true)
+8 -7
View File
@@ -121,7 +121,7 @@ enum TrainingUI {
}
}
/// 任务卡显示截止、课程小节、提示、进度条和对应的操作入口。
/// 任务卡显示截止、考试信息、提示、进度条和对应的操作入口。
@MainActor
final class TrainingTaskCell: UITableViewCell {
static let reuseIdentifier = "TrainingTaskCell"
@@ -188,7 +188,7 @@ final class TrainingTaskCell: UITableViewCell {
statusLabel.text = task.learnerStatus.title
statusLabel.textColor = task.learnerStatus == .completed ? TrainingUI.success : TrainingUI.primary
publisherLabel.text = "发布人:\(task.publisherText)"
metaLabel.text = task.mode == .exam ? "学习 + 考试 · \(task.course.sections.count) 节 · \(task.questions.count) 题 · \(task.passScore) 分及格" : "仅学习 · \(task.course.sections.count) 节"
metaLabel.text = task.mode == .exam ? "学习 + 考试 · \(task.questions.count) 题 · \(task.passScore) 分及格" : "仅学习"
hintLabel.text = presentation.hint
progressView.progress = Float(presentation.progressPercent) / 100
progressLabel.text = "学习进度 \(presentation.progressPercent)%"
@@ -204,19 +204,20 @@ final class TrainingTaskCell: UITableViewCell {
titleLabel.text = task.title
deadlineLabel.text = task.endAt.map { "截止时间:\(TrainingUI.date($0))" } ?? "未设置截止时间"
statusLabel.text = task.learnerStatus.title
statusLabel.textColor = task.learnerStatus == .completed ? TrainingUI.success : TrainingUI.primary
statusLabel.textColor = switch task.learnerStatus { case .pendingExam: TrainingUI.primary; case .completed: TrainingUI.success; case .ended: TrainingUI.muted }
let publisher = task.publisherName?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
publisherLabel.text = "发布人:\(publisher.isEmpty ? "未记录" : publisher)"
metaLabel.text = task.mode == .exam ? "学习 + 考试 · \(task.sectionCount) 节 · \(task.questionCount) 题 · \(task.passScore ?? 0) 分及格" : "仅学习 · \(task.sectionCount) 节"
metaLabel.text = task.mode == .exam ? "学习 + 考试 · \(task.questionCount) 题 · \(task.passScore ?? 0) 分及格" : "仅学习"
let completed = task.learnerStatus == .completed
hintLabel.text = completed ? "已完成本次学习任务" : task.learning.canStartExam ? "课程已学完,可以开始考试" : task.learning.canStudy ? "请阅读课程,完成学习要求" : "任务暂不可提交,可回顾课程内容"
let ended = task.learnerStatus == .ended
hintLabel.text = ended ? "任务已结束,仅可回顾课程内容" : completed ? "已完成本次学习任务" : task.learning.canStartExam ? "课程已学完,可以开始考试" : task.learning.canStudy ? "请阅读课程,完成学习要求" : "任务暂不可提交,可回顾课程内容"
let progress = min(100, max(0, task.learning.progressPercent))
progressView.progress = Float(progress) / 100
progressLabel.text = "学习进度 \(progress)%"
var configuration = actionButton.configuration
configuration?.title = completed ? "回顾学习" : task.learning.canStartExam ? (task.learning.hasDraft ? "继续答题" : "开始考试") : task.learning.canStudy ? "去学习" : "查看课程"
configuration?.title = ended || completed ? "回顾学习" : task.learning.canStartExam ? (task.learning.hasDraft ? "继续答题" : "开始考试") : task.learning.canStudy ? "去学习" : "查看课程"
actionButton.configuration = configuration
actionButton.isEnabled = task.learning.canViewCourse || task.learning.canStartExam
actionButton.isEnabled = task.learning.canViewCourse || (!ended && task.learning.canStartExam)
actionButton.accessibilityIdentifier = "training.task.action.\(task.id)"
footer.axis = traitCollection.preferredContentSizeCategory.isAccessibilityCategory ? .vertical : .horizontal
}