perf(training): 优化答题自动保存与培训页面交互
This commit is contained in:
@@ -90,6 +90,11 @@ final class TrainingExamViewController: TrainingBaseViewController {
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
viewModel.onDraftSaveStateChange = { [weak self] in
|
||||
guard let self, !self.isPerformingOperation else { return }
|
||||
self.applyState()
|
||||
self.scheduleNavigationIfNeeded()
|
||||
}
|
||||
loadExam()
|
||||
}
|
||||
|
||||
@@ -111,7 +116,10 @@ final class TrainingExamViewController: TrainingBaseViewController {
|
||||
override func viewWillDisappear(_ animated: Bool) {
|
||||
super.viewWillDisappear(animated)
|
||||
// 自有答题卡和确认弹窗关闭后继续当前题,真实导航返回才刷新考试资格。
|
||||
if navigationController?.topViewController !== self { needsReloadOnReturn = true }
|
||||
if navigationController?.topViewController !== self {
|
||||
needsReloadOnReturn = true
|
||||
Task { @MainActor in _ = await viewModel.flushDraft(service: service) }
|
||||
}
|
||||
}
|
||||
|
||||
override func onSessionDidExpire() { navigationController?.popToRootViewController(animated: true) }
|
||||
@@ -128,7 +136,7 @@ final class TrainingExamViewController: TrainingBaseViewController {
|
||||
private func applyState() {
|
||||
setOperationInteractionEnabled(true)
|
||||
let pendingTitle = viewModel.hasPendingSubmission ? "恢复交卷结果"
|
||||
: viewModel.hasPendingMutation ? "重试保存" : viewModel.canRetryCurrentPage ? "重试加载本题" : nil
|
||||
: viewModel.hasDraftConflict ? "确认本机答案" : viewModel.hasPendingMutation ? "重试保存" : viewModel.canRetryCurrentPage ? "重试加载本题" : nil
|
||||
guard let question = viewModel.currentQuestion else {
|
||||
clearOptions()
|
||||
positionLabel.text = ""
|
||||
@@ -151,8 +159,10 @@ final class TrainingExamViewController: TrainingBaseViewController {
|
||||
for button in optionButtons.values { button.isEnabled = viewModel.canEdit }
|
||||
previousButton.isEnabled = viewModel.canEdit && viewModel.currentIndex > 0
|
||||
nextButton.isEnabled = viewModel.canEdit && viewModel.currentIndex < viewModel.questionCount - 1
|
||||
statusLabel.text = viewModel.errorMessage ?? "已答 \(viewModel.answeredCount)/\(viewModel.questionCount) · 答案已保存,可稍后继续"
|
||||
statusLabel.textColor = viewModel.errorMessage == nil ? AppColor.textSecondary : AppColor.danger
|
||||
let saveState = viewModel.draftSaveMessage
|
||||
statusLabel.text = viewModel.hasPendingMutation || viewModel.errorMessage == nil
|
||||
? "已答 \(viewModel.answeredCount)/\(viewModel.questionCount) · \(saveState)" : viewModel.errorMessage
|
||||
statusLabel.textColor = viewModel.hasPendingMutation || viewModel.errorMessage != nil ? AppColor.danger : AppColor.textSecondary
|
||||
submitButton.setTitle(pendingTitle ?? (viewModel.isBusy ? "正在保存…" : "交卷"), for: .normal)
|
||||
submitButton.isEnabled = !viewModel.isBusy && (pendingTitle != nil || viewModel.canEdit)
|
||||
}
|
||||
@@ -259,18 +269,20 @@ final class TrainingExamViewController: TrainingBaseViewController {
|
||||
}
|
||||
|
||||
private func selectOption(_ id: String) {
|
||||
guard !isPerformingOperation, let selected = viewModel.selectionPreview(optionID: id) else { return }
|
||||
updateOptionSelection(selected)
|
||||
beginOperation(message: "正在保存答案…")
|
||||
Task { @MainActor in
|
||||
await viewModel.select(optionID: id, service: service)
|
||||
finishOperation()
|
||||
}
|
||||
guard !isPerformingOperation, viewModel.selectLocally(optionID: id, service: service) else { return }
|
||||
// desired 已同步更新;连续多选不等待 Task 或网络,保留同题按钮实例。
|
||||
applyState()
|
||||
}
|
||||
|
||||
private func goToQuestion(_ index: Int) {
|
||||
guard !isPerformingOperation, viewModel.canEdit,
|
||||
(0..<viewModel.questionCount).contains(index), index != viewModel.currentIndex else { return }
|
||||
if viewModel.goToCachedQuestion(index, service: service) {
|
||||
applyState()
|
||||
scrollView.setContentOffset(.zero, animated: false)
|
||||
UIAccessibility.post(notification: .screenChanged, argument: questionLabel)
|
||||
return
|
||||
}
|
||||
beginOperation(message: "正在加载题目…")
|
||||
Task { @MainActor in
|
||||
await viewModel.goToQuestion(index, service: service)
|
||||
@@ -295,9 +307,9 @@ final class TrainingExamViewController: TrainingBaseViewController {
|
||||
|
||||
@objc private func submitTapped() {
|
||||
guard !isPerformingOperation, !viewModel.isBusy else { return }
|
||||
if viewModel.hasDraftConflict { confirmDraftConflict(); return }
|
||||
if viewModel.hasPendingMutation {
|
||||
beginOperation(message: "正在重试保存…")
|
||||
Task { @MainActor in await viewModel.retryMutation(service: service); finishOperation() }
|
||||
Task { @MainActor in await viewModel.retryMutation(service: service); applyState() }
|
||||
return
|
||||
}
|
||||
if viewModel.hasPendingSubmission { performSubmit(); return }
|
||||
@@ -320,6 +332,23 @@ final class TrainingExamViewController: TrainingBaseViewController {
|
||||
present(alert, animated: true)
|
||||
}
|
||||
|
||||
private func confirmDraftConflict() {
|
||||
guard presentedViewController == nil else { return }
|
||||
let alert = UIAlertController(title: "答案已在其他设备变更", message: "本机尚未保存的选择已保留。应用本机选择会更新对应题目的答案,也可以使用其他设备已保存的答案。", preferredStyle: .alert)
|
||||
alert.addAction(UIAlertAction(title: "继续检查", style: .cancel))
|
||||
for (title, useLocal) in [("使用已保存答案", false), ("应用本机选择", true)] {
|
||||
alert.addAction(UIAlertAction(title: title, style: .default) { [weak self] _ in
|
||||
guard let self else { return }
|
||||
self.beginOperation(message: "正在确认草稿…", showsLoading: false)
|
||||
Task { @MainActor in
|
||||
await self.viewModel.resolveDraftConflict(useLocalAnswers: useLocal, service: self.service)
|
||||
self.finishOperation()
|
||||
}
|
||||
})
|
||||
}
|
||||
present(alert, animated: true)
|
||||
}
|
||||
|
||||
private func performSubmit() {
|
||||
guard !isPerformingOperation, !viewModel.isBusy else { return }
|
||||
beginOperation(message: "正在提交并确认成绩…")
|
||||
|
||||
Reference in New Issue
Block a user