feat(training): 新增培训中心学习答题演示及成绩回顾
新增首页和个人页培训入口,支持课程阅读、答题卡、未交卷答案恢复、成绩记录及重考。 对齐小程序的培训首页与成绩页,答案解析默认收起并提供返回对应课程的入口。 开发版使用隔离的本机演示数据,正式培训服务尚未接入。
This commit is contained in:
@@ -0,0 +1,261 @@
|
||||
import SnapKit
|
||||
import UIKit
|
||||
|
||||
/// 与小程序培训页面一致的色彩、字体和内容间距。
|
||||
@MainActor
|
||||
enum TrainingUI {
|
||||
static let primary = UIColor(red: 35/255, green: 121/255, blue: 1, alpha: 1)
|
||||
static let background = UIColor(red: 245/255, green: 248/255, blue: 252/255, alpha: 1)
|
||||
static let textColor = UIColor(red: 23/255, green: 32/255, blue: 51/255, alpha: 1)
|
||||
static let muted = UIColor(red: 132/255, green: 145/255, blue: 166/255, alpha: 1)
|
||||
static let border = UIColor(red: 227/255, green: 235/255, blue: 245/255, alpha: 1)
|
||||
static let hintColor = UIColor(red: 189/255, green: 121/255, blue: 24/255, alpha: 1)
|
||||
static let hintBackground = UIColor(red: 1, green: 246/255, blue: 233/255, alpha: 1)
|
||||
static let success = UIColor(red: 21/255, green: 151/255, blue: 108/255, alpha: 1)
|
||||
|
||||
/// 创建支持系统字体缩放的标签。
|
||||
static func text(_ value: String = "", size: CGFloat = 14, weight: UIFont.Weight = .regular, color: UIColor = textColor) -> UILabel {
|
||||
let label = UILabel()
|
||||
label.text = value
|
||||
label.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: size, weight: weight))
|
||||
label.adjustsFontForContentSizeCategory = true
|
||||
label.textColor = color
|
||||
label.numberOfLines = 0
|
||||
return label
|
||||
}
|
||||
|
||||
/// 为答题等现有页面保留语义字体入口。
|
||||
static func label(_ text: String = "", style: UIFont.TextStyle = .body, color: UIColor = textColor) -> UILabel {
|
||||
let label = self.text(text, color: color)
|
||||
label.font = .preferredFont(forTextStyle: style)
|
||||
return label
|
||||
}
|
||||
|
||||
/// 白色内容卡片使用细边框和统一内边距。
|
||||
static func card() -> UIStackView {
|
||||
let stack = UIStackView()
|
||||
stack.axis = .vertical
|
||||
stack.spacing = 10
|
||||
stack.backgroundColor = .white
|
||||
stack.layer.cornerRadius = 11
|
||||
stack.layer.borderWidth = 1
|
||||
stack.layer.borderColor = border.cgColor
|
||||
stack.layoutMargins = UIEdgeInsets(top: 14, left: 14, bottom: 14, right: 14)
|
||||
stack.isLayoutMarginsRelativeArrangement = true
|
||||
return stack
|
||||
}
|
||||
|
||||
/// 提示内容在浅橙背景中展示。
|
||||
static func notice(_ value: String, demo: Bool = false) -> UIStackView {
|
||||
let box = UIStackView(arrangedSubviews: [text(value, size: demo ? 11 : 12, color: hintColor)])
|
||||
box.axis = .vertical
|
||||
box.backgroundColor = hintBackground
|
||||
box.layer.cornerRadius = 6
|
||||
box.isLayoutMarginsRelativeArrangement = true
|
||||
box.layoutMargins = UIEdgeInsets(top: 9, left: 10, bottom: 9, right: 10)
|
||||
return box
|
||||
}
|
||||
|
||||
/// 明确标识本机演示边界。
|
||||
static func demoNotice() -> UIStackView {
|
||||
notice("开发演示 · 记录仅保存在本机,未同步景区后台", demo: true)
|
||||
}
|
||||
|
||||
/// 使用本地时区展示任务截止和考试时间。
|
||||
static func date(_ date: Date) -> String {
|
||||
let formatter = DateFormatter()
|
||||
formatter.locale = Locale(identifier: "zh_CN")
|
||||
formatter.dateFormat = "MM-dd HH:mm"
|
||||
return formatter.string(from: date)
|
||||
}
|
||||
|
||||
/// 培训专用按钮,文字缩放时高度随内容增加。
|
||||
static func button(_ title: String, secondary: Bool = false, plain: Bool = false, pill: Bool = false) -> UIButton {
|
||||
let button = UIButton(type: .system)
|
||||
var configuration = UIButton.Configuration.plain()
|
||||
configuration.title = title
|
||||
configuration.baseForegroundColor = plain || secondary ? primary : .white
|
||||
configuration.background.backgroundColor = plain ? .clear : secondary ? primary.withAlphaComponent(0.08) : primary
|
||||
configuration.background.cornerRadius = pill ? 24 : 11
|
||||
configuration.contentInsets = NSDirectionalEdgeInsets(top: plain ? 10 : 12, leading: 14, bottom: plain ? 10 : 12, trailing: 14)
|
||||
configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
|
||||
var output = incoming
|
||||
output.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: .systemFont(ofSize: 14, weight: plain ? .regular : .semibold))
|
||||
return output
|
||||
}
|
||||
button.configuration = configuration
|
||||
if !plain, !secondary {
|
||||
button.configurationUpdateHandler = { button in
|
||||
var updated = button.configuration
|
||||
updated?.background.backgroundColor = button.isEnabled ? primary : border
|
||||
updated?.baseForegroundColor = button.isEnabled ? .white : muted
|
||||
button.configuration = updated
|
||||
}
|
||||
}
|
||||
button.titleLabel?.adjustsFontForContentSizeCategory = true
|
||||
button.snp.makeConstraints { $0.height.greaterThanOrEqualTo(44) }
|
||||
return button
|
||||
}
|
||||
|
||||
/// 导航栏保持普通白色背景,入口使用文本与箭头。
|
||||
static func navigation(_ controller: UIViewController, title: String) {
|
||||
controller.title = title
|
||||
let appearance = UINavigationBarAppearance()
|
||||
appearance.configureWithOpaqueBackground()
|
||||
appearance.backgroundColor = .white
|
||||
appearance.shadowColor = .clear
|
||||
appearance.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 17, weight: .medium), .foregroundColor: textColor]
|
||||
controller.navigationItem.standardAppearance = appearance
|
||||
controller.navigationItem.scrollEdgeAppearance = appearance
|
||||
controller.navigationItem.compactAppearance = appearance
|
||||
controller.navigationItem.backButtonDisplayMode = .minimal
|
||||
let back = UIButton(type: .system)
|
||||
back.tintColor = textColor
|
||||
back.setImage(UIImage(systemName: "chevron.left", withConfiguration: UIImage.SymbolConfiguration(pointSize: 20, weight: .medium)), for: .normal)
|
||||
back.accessibilityLabel = "返回"
|
||||
back.snp.makeConstraints { $0.size.equalTo(44) }
|
||||
back.addAction(UIAction { [weak controller] _ in controller?.navigationController?.popViewController(animated: true) }, for: .touchUpInside)
|
||||
let item = UIBarButtonItem(customView: back)
|
||||
if #available(iOS 26.0, *) { item.hidesSharedBackground = true }
|
||||
controller.navigationItem.leftBarButtonItem = item
|
||||
}
|
||||
}
|
||||
|
||||
/// 任务卡显示截止、课程小节、提示、进度条和对应的操作入口。
|
||||
@MainActor
|
||||
final class TrainingTaskCell: UITableViewCell {
|
||||
static let reuseIdentifier = "TrainingTaskCell"
|
||||
private let stack = TrainingUI.card()
|
||||
private let deadlineLabel = TrainingUI.text(size: 11.5, color: .systemRed)
|
||||
private let statusLabel = TrainingUI.text(size: 11, color: TrainingUI.primary)
|
||||
private let titleLabel = TrainingUI.text(size: 15.5, weight: .semibold)
|
||||
private let publisherLabel = TrainingUI.text(size: 12.5, color: TrainingUI.muted)
|
||||
private let metaLabel = TrainingUI.text(size: 12.5, color: TrainingUI.muted)
|
||||
private let hintLabel = TrainingUI.text(size: 12, color: TrainingUI.hintColor)
|
||||
private let progressView = UIProgressView(progressViewStyle: .bar)
|
||||
private let progressLabel = TrainingUI.text(size: 12.5, color: TrainingUI.muted)
|
||||
private let actionButton = TrainingUI.button("去学习", pill: true)
|
||||
private let footer = UIStackView()
|
||||
var onAction: (() -> Void)?
|
||||
|
||||
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
||||
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
||||
backgroundColor = .clear
|
||||
selectionStyle = .none
|
||||
contentView.addSubview(stack)
|
||||
let deadline = UIStackView(arrangedSubviews: [deadlineLabel])
|
||||
deadline.backgroundColor = UIColor.systemRed.withAlphaComponent(0.06)
|
||||
deadline.layer.cornerRadius = 12
|
||||
deadline.isLayoutMarginsRelativeArrangement = true
|
||||
deadline.layoutMargins = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8)
|
||||
let top = UIStackView(arrangedSubviews: [deadline, UIView(), statusLabel])
|
||||
top.alignment = .center
|
||||
top.spacing = 8
|
||||
statusLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
|
||||
let hint = UIStackView(arrangedSubviews: [hintLabel])
|
||||
hint.backgroundColor = TrainingUI.hintBackground
|
||||
hint.layer.cornerRadius = 6
|
||||
hint.isLayoutMarginsRelativeArrangement = true
|
||||
hint.layoutMargins = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
|
||||
progressView.progressTintColor = TrainingUI.primary
|
||||
progressView.trackTintColor = TrainingUI.border
|
||||
progressView.layer.cornerRadius = 3
|
||||
progressView.clipsToBounds = true
|
||||
progressView.snp.makeConstraints { $0.height.equalTo(6) }
|
||||
footer.spacing = 12
|
||||
footer.alignment = .center
|
||||
footer.addArrangedSubview(progressLabel)
|
||||
footer.addArrangedSubview(actionButton)
|
||||
progressLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
|
||||
actionButton.setContentHuggingPriority(.defaultHigh, for: .horizontal)
|
||||
actionButton.setContentCompressionResistancePriority(.required, for: .horizontal)
|
||||
actionButton.snp.makeConstraints { $0.width.greaterThanOrEqualTo(140).priority(750) }
|
||||
[top, titleLabel, publisherLabel, metaLabel, hint, progressView, footer].forEach(stack.addArrangedSubview)
|
||||
stack.setCustomSpacing(14, after: top)
|
||||
stack.setCustomSpacing(14, after: metaLabel)
|
||||
stack.snp.makeConstraints { $0.edges.equalToSuperview().inset(UIEdgeInsets(top: 0, left: 13, bottom: 10, right: 13)) }
|
||||
actionButton.addTarget(self, action: #selector(actionTapped), for: .touchUpInside)
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
||||
|
||||
/// 每个任务始终可回顾课程,只有已读完且有效时直接进入考试。
|
||||
func configure(task: TrainingTask) {
|
||||
let presentation = TrainingTaskPresentation(task: task)
|
||||
titleLabel.text = task.title
|
||||
deadlineLabel.text = "截止时间:\(TrainingUI.date(task.endAt))"
|
||||
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) 节"
|
||||
hintLabel.text = presentation.hint
|
||||
progressView.progress = Float(presentation.progressPercent) / 100
|
||||
progressLabel.text = "学习进度 \(presentation.progressPercent)%"
|
||||
var configuration = actionButton.configuration
|
||||
configuration?.title = presentation.actionTitle
|
||||
actionButton.configuration = configuration
|
||||
actionButton.accessibilityIdentifier = "training.task.action.\(task.id)"
|
||||
footer.axis = traitCollection.preferredContentSizeCategory.isAccessibilityCategory ? .vertical : .horizontal
|
||||
}
|
||||
|
||||
@objc private func actionTapped() { onAction?() }
|
||||
}
|
||||
|
||||
/// 考试记录沿用小程序的标题、蓝色得分和通过状态卡片。
|
||||
@MainActor
|
||||
final class TrainingRecordCell: UITableViewCell {
|
||||
static let reuseIdentifier = "TrainingRecordCell"
|
||||
private let stack = TrainingUI.card()
|
||||
private let titleLabel = TrainingUI.text(weight: .semibold)
|
||||
private let scoreLabel = TrainingUI.text(weight: .semibold, color: TrainingUI.primary)
|
||||
private let detailLabel = TrainingUI.text(size: 11.5, color: TrainingUI.muted)
|
||||
private let statusLabel = TrainingUI.text(size: 11.5)
|
||||
|
||||
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
||||
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
||||
backgroundColor = .clear
|
||||
selectionStyle = .none
|
||||
let top = UIStackView(arrangedSubviews: [titleLabel, scoreLabel])
|
||||
top.spacing = 12
|
||||
top.alignment = .top
|
||||
scoreLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
|
||||
scoreLabel.setContentHuggingPriority(.required, for: .horizontal)
|
||||
titleLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
|
||||
let bottom = UIStackView(arrangedSubviews: [detailLabel, statusLabel])
|
||||
bottom.spacing = 12
|
||||
bottom.alignment = .top
|
||||
statusLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
|
||||
statusLabel.setContentHuggingPriority(.required, for: .horizontal)
|
||||
detailLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
|
||||
stack.addArrangedSubview(top)
|
||||
stack.addArrangedSubview(bottom)
|
||||
contentView.addSubview(stack)
|
||||
stack.snp.makeConstraints { $0.edges.equalToSuperview().inset(UIEdgeInsets(top: 0, left: 13, bottom: 10, right: 13)) }
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
||||
|
||||
/// 展示已提交记录,点击后进入该次成绩。
|
||||
func configure(task: TrainingTask, attempt: TrainingAttempt) {
|
||||
titleLabel.text = task.title
|
||||
scoreLabel.text = "\(attempt.scoreText) 分"
|
||||
detailLabel.text = "\(TrainingUI.date(attempt.submittedAt)) · 答对 \(attempt.correctCount) / \(attempt.questionCount) 题"
|
||||
statusLabel.text = attempt.passed ? "已通过" : "未通过"
|
||||
statusLabel.textColor = attempt.passed ? TrainingUI.success : .systemRed
|
||||
}
|
||||
}
|
||||
|
||||
/// 培训页面在账号或景区切换时立即关闭,停止展示旧身份记录。
|
||||
@MainActor
|
||||
class TrainingBaseViewController: BaseViewController {
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
for name in [NotificationName.accountDidSwitch, NotificationName.scenicDidChange] {
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(contextChanged), name: name, object: nil)
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func contextChanged() { navigationController?.popToRootViewController(animated: false) }
|
||||
}
|
||||
Reference in New Issue
Block a user