ui(training): 统一课程学习与在线考试步骤栏样式

This commit is contained in:
2026-10-19 17:39:54 +08:00
parent fdb4550ba8
commit aaed9da964
4 changed files with 134 additions and 10 deletions
@@ -9,7 +9,9 @@ final class TrainingCourseViewController: TrainingBaseViewController {
private let service: any TrainingDataServing
private let viewModel: TrainingRemoteReadingViewModel
private let webView: WKWebView
private let headerStack = UIStackView()
private let toolbar = UIStackView()
private let stepView = TrainingStepView(currentStep: .learning)
private let sectionsButton = TrainingUI.button("章节目录", secondary: true)
private let attachmentsButton = TrainingUI.button("课程附件", secondary: true)
private let bottomStack = UIStackView()
@@ -70,6 +72,9 @@ final class TrainingCourseViewController: TrainingBaseViewController {
toolbar.distribution = .fillEqually
toolbar.addArrangedSubview(sectionsButton)
toolbar.addArrangedSubview(attachmentsButton)
headerStack.axis = .vertical
headerStack.spacing = 20
headerStack.addArrangedSubview(toolbar)
bottomStack.axis = .vertical
bottomStack.spacing = 8
bottomStack.layoutMargins = UIEdgeInsets(top: 10, left: 12, bottom: 10, right: 12)
@@ -80,19 +85,19 @@ final class TrainingCourseViewController: TrainingBaseViewController {
actionButton.accessibilityIdentifier = "training.reading.action"
sectionsButton.accessibilityIdentifier = "training.reading.sections"
attachmentsButton.accessibilityIdentifier = "training.reading.attachments"
view.addSubview(toolbar)
view.addSubview(headerStack)
view.addSubview(webView)
view.addSubview(bottomStack)
applyState()
}
override func setupConstraints() {
toolbar.snp.makeConstraints {
headerStack.snp.makeConstraints {
$0.top.equalTo(view.safeAreaLayoutGuide).offset(8)
$0.leading.trailing.equalToSuperview().inset(13)
}
webView.snp.makeConstraints {
$0.top.equalTo(toolbar.snp.bottom).offset(8)
$0.top.equalTo(headerStack.snp.bottom).offset(8)
$0.leading.trailing.equalTo(view.safeAreaLayoutGuide)
$0.bottom.equalTo(bottomStack.snp.top)
}
@@ -185,11 +190,10 @@ final class TrainingCourseViewController: TrainingBaseViewController {
<!doctype html><html lang="zh-CN"><head><meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https: data:; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'">
<style>
*{box-sizing:border-box}body{font:15px -apple-system,BlinkMacSystemFont,sans-serif;color:#34435b;background:#f5f8fc;margin:0;padding:14px 13px;line-height:1.95;overflow-wrap:anywhere}
*{box-sizing:border-box}body{font:15px -apple-system,BlinkMacSystemFont,sans-serif;color:#34435b;background:#f5f8fc;margin:0;padding:0 13px 14px;line-height:1.95;overflow-wrap:anywhere}
h1{font-size:17px;font-weight:600;line-height:1.6;color:#172033;margin:14px 0 6px}h2{font-size:15.5px;font-weight:600;color:#172033;margin:0 0 10px}img{max-width:100%;height:auto}table{max-width:100%;display:block;overflow:auto}a{color:#2379ff}
.step{display:flex;gap:20px;background:#edf1f7;padding:9px 12px;border-radius:8px;font-size:13px;color:#8994a6}.step b{font-weight:500;color:#2379ff}
.muted{font-size:12.5px;color:#8491a6;line-height:1.65;margin:6px 0}.reading{background:white;border:1px solid #e3ebf5;border-radius:11px;padding:14px;margin-top:14px}
</style></head><body><div class='step'><b>1 课程学习</b><span>\(task.mode == .exam ? "2 在线考试" : "完成学习")</span></div>
</style></head><body>
<h1>\(escape(task.title))</h1><p class='muted'>\(escape(course.title))</p><p class='muted'>\(metadata)</p><p class='muted'>\(escape(course.introduction))</p>
<p class='muted'>第 \(section.order) / \(course.sectionCount) 节</p><div class='reading'><h2>\(escape(section.title))</h2>\(section.contentHTML)</div></body></html>
""", baseURL: nil)
@@ -202,6 +206,10 @@ final class TrainingCourseViewController: TrainingBaseViewController {
private func applyState() {
guard !viewModel.isInvalidated else { leaveUnavailableCourse(); return }
if let task = viewModel.task?.summary {
stepView.configure(mode: task.mode)
if stepView.superview == nil { headerStack.addArrangedSubview(stepView) }
}
let canInteract = userRequestID == nil
sectionsButton.isUserInteractionEnabled = canInteract
attachmentsButton.isUserInteractionEnabled = canInteract
@@ -16,6 +16,8 @@ final class TrainingExamViewController: TrainingBaseViewController {
private var renderedQuestion: TrainingQuestionItem?
private var optionButtons: [String: UIButton] = [:]
private let scrollView = UIScrollView()
private let pageStack = UIStackView()
private let stepView = TrainingStepView(currentStep: .exam)
private let contentStack = TrainingUI.card()
private let positionLabel = TrainingUI.label(style: .subheadline, color: AppColor.primary)
private let questionLabel = TrainingUI.label(style: .title3)
@@ -46,6 +48,10 @@ final class TrainingExamViewController: TrainingBaseViewController {
override func setupUI() {
scrollView.alwaysBounceVertical = true
pageStack.axis = .vertical
pageStack.spacing = 16
pageStack.addArrangedSubview(stepView)
pageStack.addArrangedSubview(contentStack)
optionStack.axis = .vertical
optionStack.spacing = 12
navigationStack.axis = .horizontal
@@ -64,7 +70,7 @@ final class TrainingExamViewController: TrainingBaseViewController {
bottomStack.addArrangedSubview(statusLabel)
bottomStack.addArrangedSubview(submitButton)
submitButton.accessibilityIdentifier = "training.exam.submit"
scrollView.addSubview(contentStack)
scrollView.addSubview(pageStack)
view.addSubview(scrollView)
view.addSubview(bottomStack)
applyState()
@@ -75,7 +81,7 @@ final class TrainingExamViewController: TrainingBaseViewController {
$0.top.leading.trailing.equalTo(view.safeAreaLayoutGuide)
$0.bottom.equalTo(bottomStack.snp.top)
}
contentStack.snp.makeConstraints {
pageStack.snp.makeConstraints {
$0.edges.equalTo(scrollView.contentLayoutGuide).inset(16)
$0.width.equalTo(scrollView.frameLayoutGuide).offset(-32)
}
@@ -0,0 +1,53 @@
import SnapKit
import UIKit
/// 学习与考试共用的进度提示,两项等宽居中,仅突出当前步骤。
@MainActor
final class TrainingStepView: UIView {
/// 当前页面对应的学习流程阶段。
enum Step {
case learning
case exam
}
private let learningLabel: UILabel
private let nextLabel: UILabel
/// 创建只读步骤栏,任务模式可在课程详情返回后更新。
init(currentStep: Step, mode: TrainingMode = .exam) {
learningLabel = TrainingUI.text("1 课程学习", size: 13,
weight: currentStep == .learning ? .semibold : .regular,
color: currentStep == .learning ? TrainingUI.primary : TrainingUI.muted)
nextLabel = TrainingUI.text(size: 13,
weight: currentStep == .exam ? .semibold : .regular,
color: currentStep == .exam ? TrainingUI.primary : TrainingUI.muted)
super.init(frame: .zero)
backgroundColor = UIColor(red: 237/255, green: 241/255, blue: 247/255, alpha: 1)
layer.cornerRadius = 8
accessibilityIdentifier = "training.steps"
learningLabel.accessibilityIdentifier = "training.steps.learning"
nextLabel.accessibilityIdentifier = "training.steps.next"
for label in [learningLabel, nextLabel] { label.textAlignment = .center }
let currentLabel = currentStep == .learning ? learningLabel : nextLabel
currentLabel.accessibilityTraits.insert(.selected)
currentLabel.accessibilityValue = "当前步骤"
let stack = UIStackView(arrangedSubviews: [learningLabel, nextLabel])
stack.axis = .horizontal
stack.alignment = .center
stack.distribution = .fillEqually
addSubview(stack)
stack.snp.makeConstraints {
$0.edges.equalToSuperview().inset(UIEdgeInsets(top: 10, left: 12, bottom: 10, right: 12))
}
snp.makeConstraints { $0.height.greaterThanOrEqualTo(44) }
configure(mode: mode)
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
/// 仅学习任务展示完成学习,考试任务展示在线考试。
func configure(mode: TrainingMode) {
nextLabel.text = mode == .exam ? "2 在线考试" : "完成学习"
}
}