feat(training): 为请求操作接入通用Loading
This commit is contained in:
@@ -26,6 +26,13 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
private var contentIsReady = false
|
||||
private var contentLoadFailed = false
|
||||
private var pageIsVisible = false
|
||||
private var userRequestID: UUID?
|
||||
|
||||
/// 用户操作独占的请求票据;缓存复用仍防重复操作,但不展示全局进度。
|
||||
private struct UserRequest {
|
||||
let id: UUID
|
||||
let loadingToken: UUID?
|
||||
}
|
||||
|
||||
/// 当前任务标识,成绩页可复用已有课程页面。
|
||||
var taskID: String { viewModel.taskID }
|
||||
@@ -106,10 +113,16 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
override func viewDidAppear(_ animated: Bool) {
|
||||
super.viewDidAppear(animated)
|
||||
pageIsVisible = true
|
||||
if viewModel.task == nil { loadCourse() }
|
||||
else {
|
||||
contentTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
let previousContent = contentTask
|
||||
contentTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
await previousContent?.value
|
||||
guard !Task.isCancelled, pageIsVisible, navigationController?.topViewController === self,
|
||||
userRequestID == nil else { return }
|
||||
if viewModel.task == nil { loadCourse() }
|
||||
else if viewModel.currentSection == nil, let id = viewModel.currentSectionID { selectSection(id) }
|
||||
else {
|
||||
if contentNavigation == nil { renderCurrentSection() }
|
||||
await viewModel.refreshProgress()
|
||||
applyState()
|
||||
resumeReading()
|
||||
@@ -120,6 +133,7 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
override func viewWillDisappear(_ animated: Bool) {
|
||||
super.viewWillDisappear(animated)
|
||||
pageIsVisible = false
|
||||
contentTask?.cancel()
|
||||
pauseReading()
|
||||
}
|
||||
|
||||
@@ -131,20 +145,22 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
}
|
||||
|
||||
private func loadCourse() {
|
||||
guard let request = beginUserRequest(showsLoading: true) else { return }
|
||||
pauseReading()
|
||||
contentIsReady = false
|
||||
contentLoadFailed = false
|
||||
contentNavigation = nil
|
||||
contentTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
await viewModel.load()
|
||||
guard !Task.isCancelled else { return }
|
||||
await performUserRequest(request) { await self.viewModel.load() }
|
||||
guard !Task.isCancelled, pageIsVisible, navigationController?.topViewController === self else { return }
|
||||
renderCurrentSection()
|
||||
applyState()
|
||||
}
|
||||
}
|
||||
|
||||
private func selectSection(_ id: String) {
|
||||
guard let request = beginUserRequest(showsLoading: !viewModel.isSectionCached(id: id)) else { return }
|
||||
isOpeningResource = false
|
||||
pauseReading()
|
||||
contentIsReady = false
|
||||
@@ -152,8 +168,8 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
contentNavigation = nil
|
||||
contentTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
await viewModel.selectSection(id: id)
|
||||
guard !Task.isCancelled else { return }
|
||||
await performUserRequest(request) { await self.viewModel.selectSection(id: id) }
|
||||
guard !Task.isCancelled, pageIsVisible, navigationController?.topViewController === self else { return }
|
||||
renderCurrentSection()
|
||||
applyState()
|
||||
}
|
||||
@@ -186,8 +202,13 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
|
||||
private func applyState() {
|
||||
guard !viewModel.isInvalidated else { leaveUnavailableCourse(); return }
|
||||
sectionsButton.isEnabled = !viewModel.isLoading && !viewModel.sections.isEmpty
|
||||
attachmentsButton.isEnabled = !viewModel.isLoading && (viewModel.course?.attachmentCount ?? 0) > 0
|
||||
let canInteract = userRequestID == nil
|
||||
sectionsButton.isUserInteractionEnabled = canInteract
|
||||
attachmentsButton.isUserInteractionEnabled = canInteract
|
||||
actionButton.isUserInteractionEnabled = canInteract
|
||||
guard canInteract else { return }
|
||||
sectionsButton.isEnabled = !viewModel.sections.isEmpty
|
||||
attachmentsButton.isEnabled = (viewModel.course?.attachmentCount ?? 0) > 0
|
||||
if let progress = viewModel.progress, progress.readingCompleted {
|
||||
noticeLabel.text = viewModel.readingErrorMessage ?? "课程阅读已完成"
|
||||
if viewModel.task?.summary.mode == .exam && progress.canStartExam { setActionTitle("开始考试") }
|
||||
@@ -229,6 +250,26 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
actionButton.configuration = configuration
|
||||
}
|
||||
|
||||
private func beginUserRequest(showsLoading: Bool) -> UserRequest? {
|
||||
guard userRequestID == nil, !viewModel.isInvalidated else { return nil }
|
||||
let request = UserRequest(id: UUID(), loadingToken: showsLoading ? beginRequestLoading() : nil)
|
||||
userRequestID = request.id
|
||||
applyState()
|
||||
return request
|
||||
}
|
||||
|
||||
private func endUserRequest(_ request: UserRequest) {
|
||||
if let token = request.loadingToken { endRequestLoading(token) }
|
||||
guard userRequestID == request.id else { return }
|
||||
userRequestID = nil
|
||||
applyState()
|
||||
}
|
||||
|
||||
private func performUserRequest<Value>(_ request: UserRequest, operation: () async -> Value) async -> Value {
|
||||
defer { endUserRequest(request) }
|
||||
return await operation()
|
||||
}
|
||||
|
||||
@objc private func pauseReading() {
|
||||
readingTimer?.invalidate()
|
||||
readingTimer = nil
|
||||
@@ -252,9 +293,12 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
|
||||
private func resumeReading(retrying: Bool = false) {
|
||||
guard isContentVisible, viewModel.canStudy, (retrying || viewModel.readingErrorMessage == nil), readingTask == nil else { return }
|
||||
let request = retrying ? beginUserRequest(showsLoading: true) : nil
|
||||
guard !retrying || request != nil else { return }
|
||||
let pendingClose = closingTask
|
||||
readingTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
defer { if let request { endUserRequest(request) } }
|
||||
await pendingClose?.value
|
||||
guard isContentVisible, viewModel.canStudy, (retrying || viewModel.readingErrorMessage == nil), !Task.isCancelled else {
|
||||
readingTask = nil
|
||||
@@ -299,6 +343,7 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
}
|
||||
|
||||
@objc private func actionTapped() {
|
||||
guard userRequestID == nil else { return }
|
||||
if let progress = viewModel.progress, progress.readingCompleted {
|
||||
pauseReading()
|
||||
if viewModel.task?.summary.mode == .exam && progress.canStartExam {
|
||||
@@ -334,13 +379,17 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
}
|
||||
|
||||
@objc private func sectionsTapped() {
|
||||
guard userRequestID == nil, !viewModel.sections.isEmpty else { return }
|
||||
isOpeningResource = true
|
||||
pauseReading()
|
||||
let picker = TrainingCoursePickerViewController(title: "章节目录", rows: sectionRows(), hasMore: viewModel.hasMoreSections,
|
||||
selectedID: viewModel.currentSectionID, select: { [weak self] id in self?.selectSection(id) },
|
||||
loadMore: { [weak self] in
|
||||
guard let self else { return ([], false) }
|
||||
await viewModel.loadMoreSections()
|
||||
guard viewModel.hasMoreSections, let request = beginUserRequest(showsLoading: true) else {
|
||||
return (sectionRows(), viewModel.hasMoreSections)
|
||||
}
|
||||
await performUserRequest(request) { await self.viewModel.loadMoreSections() }
|
||||
applyState()
|
||||
if let error = viewModel.errorMessage { showToast(error) }
|
||||
return (sectionRows(), viewModel.hasMoreSections)
|
||||
@@ -353,18 +402,22 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
}
|
||||
|
||||
@objc private func attachmentsTapped() {
|
||||
guard let request = beginUserRequest(showsLoading: !viewModel.hasLoadedAttachments) else { return }
|
||||
isOpeningResource = true
|
||||
pauseReading()
|
||||
contentTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
await viewModel.loadAttachments()
|
||||
guard !Task.isCancelled else { return }
|
||||
await performUserRequest(request) { await self.viewModel.loadAttachments() }
|
||||
guard !Task.isCancelled, pageIsVisible, navigationController?.topViewController === self else { return }
|
||||
if viewModel.isInvalidated { applyState(); return }
|
||||
if let error = viewModel.errorMessage { isOpeningResource = false; showToast(error); applyState(); return }
|
||||
let picker = TrainingCoursePickerViewController(title: "课程附件", rows: attachmentRows(), hasMore: viewModel.hasMoreAttachments,
|
||||
select: { [weak self] id in self?.openAttachment(id) }, loadMore: { [weak self] in
|
||||
guard let self else { return ([], false) }
|
||||
await viewModel.loadAttachments(loadMore: true)
|
||||
guard viewModel.hasMoreAttachments, let request = beginUserRequest(showsLoading: true) else {
|
||||
return (attachmentRows(), viewModel.hasMoreAttachments)
|
||||
}
|
||||
await performUserRequest(request) { await self.viewModel.loadAttachments(loadMore: true) }
|
||||
applyState()
|
||||
if let error = viewModel.errorMessage { showToast(error) }
|
||||
return (attachmentRows(), viewModel.hasMoreAttachments)
|
||||
@@ -387,11 +440,14 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
}
|
||||
|
||||
private func openAttachment(_ id: String) {
|
||||
guard let request = beginUserRequest(showsLoading: true) else { return }
|
||||
isOpeningResource = true
|
||||
pauseReading()
|
||||
contentTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
guard let link = await viewModel.authorizeAttachment(id: id), !Task.isCancelled else {
|
||||
let link = await performUserRequest(request) { await self.viewModel.authorizeAttachment(id: id) }
|
||||
guard pageIsVisible, navigationController?.topViewController === self, !Task.isCancelled else { return }
|
||||
guard let link else {
|
||||
isOpeningResource = false
|
||||
if let error = viewModel.errorMessage { showToast(error) }
|
||||
applyState()
|
||||
|
||||
Reference in New Issue
Block a user