From aaed9da964c17c26bc390cc555115f21d3e57bd2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B1=89=E7=A7=8B?= <497055328@qq.com>
Date: Mon, 19 Oct 2026 17:39:54 +0800
Subject: [PATCH] =?UTF-8?q?ui(training):=20=E7=BB=9F=E4=B8=80=E8=AF=BE?=
=?UTF-8?q?=E7=A8=8B=E5=AD=A6=E4=B9=A0=E4=B8=8E=E5=9C=A8=E7=BA=BF=E8=80=83?=
=?UTF-8?q?=E8=AF=95=E6=AD=A5=E9=AA=A4=E6=A0=8F=E6=A0=B7=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../TrainingCourseViewController.swift | 20 ++++--
.../Training/TrainingExamViewController.swift | 10 ++-
suixinkan/UI/Training/TrainingStepView.swift | 53 ++++++++++++++++
suixinkanTests/TrainingUIRenderingTests.swift | 61 ++++++++++++++++++-
4 files changed, 134 insertions(+), 10 deletions(-)
create mode 100644 suixinkan/UI/Training/TrainingStepView.swift
diff --git a/suixinkan/UI/Training/TrainingCourseViewController.swift b/suixinkan/UI/Training/TrainingCourseViewController.swift
index c3b55ab..91813cc 100644
--- a/suixinkan/UI/Training/TrainingCourseViewController.swift
+++ b/suixinkan/UI/Training/TrainingCourseViewController.swift
@@ -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 {
1 课程学习\(task.mode == .exam ? "2 在线考试" : "完成学习")
+
\(escape(task.title))
\(escape(course.title))
\(metadata)
\(escape(course.introduction))
第 \(section.order) / \(course.sectionCount) 节
\(escape(section.title))
\(section.contentHTML)
""", 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
diff --git a/suixinkan/UI/Training/TrainingExamViewController.swift b/suixinkan/UI/Training/TrainingExamViewController.swift
index 65a8fcc..fa6f838 100644
--- a/suixinkan/UI/Training/TrainingExamViewController.swift
+++ b/suixinkan/UI/Training/TrainingExamViewController.swift
@@ -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)
}
diff --git a/suixinkan/UI/Training/TrainingStepView.swift b/suixinkan/UI/Training/TrainingStepView.swift
new file mode 100644
index 0000000..b12d70c
--- /dev/null
+++ b/suixinkan/UI/Training/TrainingStepView.swift
@@ -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 在线考试" : "完成学习"
+ }
+}
diff --git a/suixinkanTests/TrainingUIRenderingTests.swift b/suixinkanTests/TrainingUIRenderingTests.swift
index 123da17..41a1efd 100644
--- a/suixinkanTests/TrainingUIRenderingTests.swift
+++ b/suixinkanTests/TrainingUIRenderingTests.swift
@@ -56,6 +56,63 @@ struct TrainingUIRenderingTests {
Attachment.record(cardImage, named: "320点长标题任务卡", as: .png)
}
+ @Test("课程与考试步骤等宽居中,窄屏和大字体不裁切")
+ func learningAndExamStepLayout() async throws {
+ for width: CGFloat in [320, 390] {
+ let domain = "training_step_layout_\(UUID().uuidString)"
+ let defaults = try #require(UserDefaults(suiteName: domain))
+ defer { defaults.removePersistentDomain(forName: domain) }
+ let context = TrainingContext(accountScope: domain, scenicID: 1, storeID: 1)
+ let model = TrainingRemoteExamViewModel(taskID: "task_1", context: context,
+ pendingStore: TrainingPendingSubmissionStore(defaults: defaults), currentContext: { context })
+ let source = TrainingDataStub()
+ let course = TrainingCourseViewController(taskID: "task_1", service: source)
+ let exam = TrainingExamViewController(taskID: "task_1", service: source, viewModel: model)
+ for (controller, selectedTitle) in [(course as UIViewController, "1 课程学习"), (exam, "2 在线考试")] {
+ let navigation = UINavigationController(rootViewController: controller)
+ let window = try makeWindow(navigation, width: width)
+ defer { window.isHidden = true }
+ let deadline = ContinuousClock.now.advanced(by: .seconds(5))
+ while (!allViews(controller.view).contains { $0 is TrainingStepView }
+ || (controller === exam && !model.canEdit)), ContinuousClock.now < deadline {
+ try await Task.sleep(for: .milliseconds(10))
+ }
+ let steps = allViews(controller.view).compactMap { $0 as? TrainingStepView }
+ #expect(steps.count == 1)
+ let step = try #require(steps.first)
+ navigation.view.layoutIfNeeded()
+ let labels = allViews(step).compactMap { $0 as? UILabel }
+ #expect(labels.map(\.text) == ["1 课程学习", "2 在线考试"])
+ let first = try #require(labels.first)
+ let second = try #require(labels.last)
+ #expect(abs(first.bounds.width - second.bounds.width) < 1)
+ #expect(first.convert(first.bounds, to: step).maxX <= second.convert(second.bounds, to: step).minX)
+ #expect(labels.allSatisfy { $0.textAlignment == .center })
+ #expect(labels.filter { $0.accessibilityTraits.contains(.selected) }.map(\.text) == [selectedTitle])
+ #expect(labels.first { $0.text == selectedTitle }?.textColor == TrainingUI.primary)
+ #expect(step.bounds.height >= 44)
+ Attachment.record(render(step), named: "\(Int(width))点-\(selectedTitle)", as: .png)
+
+ if width == 320 {
+ navigation.setOverrideTraitCollection(UITraitCollection(preferredContentSizeCategory: .accessibilityExtraExtraExtraLarge), forChild: controller)
+ navigation.view.layoutIfNeeded()
+ #expect(first.font.pointSize > 13)
+ for label in labels {
+ let frame = label.convert(label.bounds, to: step)
+ #expect(frame.minY >= 0 && frame.maxY <= step.bounds.height + 1)
+ #expect(label.sizeThatFits(CGSize(width: label.bounds.width, height: .greatestFiniteMagnitude)).height <= label.bounds.height + 1)
+ }
+ Attachment.record(render(step), named: "大字体-\(selectedTitle)", as: .png)
+ }
+ if controller === course {
+ step.configure(mode: .study)
+ #expect(second.text == "完成学习")
+ #expect(first.accessibilityTraits.contains(.selected))
+ }
+ }
+ }
+ }
+
@Test("未通过成绩可展开解析并返回已有课程,旧失败成绩在通过后不可重考")
func resultLearningAndReviewNavigation() async throws {
let source = TrainingDataStub()
@@ -120,10 +177,10 @@ struct TrainingUIRenderingTests {
#expect(navigation.topViewController === center)
}
- private func makeWindow(_ navigation: UINavigationController) throws -> UIWindow {
+ private func makeWindow(_ navigation: UINavigationController, width: CGFloat = 390) throws -> UIWindow {
let scene = try #require(UIApplication.shared.connectedScenes.first as? UIWindowScene)
let window = UIWindow(windowScene: scene)
- window.frame = CGRect(x: 0, y: 0, width: 390, height: 844)
+ window.frame = CGRect(x: 0, y: 0, width: width, height: 844)
window.rootViewController = navigation
window.makeKeyAndVisible()
navigation.view.layoutIfNeeded()