fix(training): 修复课程学习倒计时停顿
This commit is contained in:
@@ -250,8 +250,11 @@ enum TrainingValidation {
|
||||
struct TrainingReadingClock {
|
||||
private var lastUptime: TimeInterval?
|
||||
|
||||
/// 页面内容可见且 App 活跃时开始一个新的计时片段。
|
||||
mutating func resume(at uptime: TimeInterval) { lastUptime = uptime }
|
||||
/// 暂停后开始新的计时片段;持续可见时重复恢复不改变正在累计的起点。
|
||||
mutating func resume(at uptime: TimeInterval) {
|
||||
guard lastUptime == nil else { return }
|
||||
lastUptime = uptime
|
||||
}
|
||||
|
||||
/// 失去前台可见性时丢弃未满一秒的片段。
|
||||
mutating func pause() { lastUptime = nil }
|
||||
|
||||
@@ -103,6 +103,12 @@ final class TrainingRemoteReadingViewModel {
|
||||
&& progress?.canStudy == true && progress?.readingCompleted == false && !isInvalidated
|
||||
}
|
||||
|
||||
/// 上一次请求结束后才允许发送下一次心跳;保存失败交由用户重试。
|
||||
var needsReadingFlush: Bool {
|
||||
guard !isInvalidated, !isSessionBusy, readingErrorMessage == nil, let session else { return false }
|
||||
return finalRequested || activeSecondsTotal - acceptedActiveSecondsTotal >= max(1, session.heartbeatIntervalSeconds)
|
||||
}
|
||||
|
||||
/// 首次只读课程元信息、附件第一页与进度,并为首个附件授权;不请求小节标题或 HTML。
|
||||
func load() async {
|
||||
guard !isLoading, !isInvalidated else { return }
|
||||
@@ -248,14 +254,15 @@ final class TrainingRemoteReadingViewModel {
|
||||
attachmentErrorMessage = nil
|
||||
}
|
||||
|
||||
/// 附件已渲染且前台可见时建立或恢复会话;创建丢响应时沿用原 requestID。
|
||||
/// 附件已渲染且前台可见时建立或恢复会话;正在阅读时重复调用不重启计时。
|
||||
func startReading(at uptime: TimeInterval) async {
|
||||
guard isAttachmentRendered, currentAttachmentLink != nil, canStudy else { return }
|
||||
lastVisibleUptime = uptime
|
||||
guard !isReading else { return }
|
||||
let epoch = generation
|
||||
wantsReading = true
|
||||
lastVisibleUptime = uptime
|
||||
await waitForSessionOperation(epoch: epoch)
|
||||
guard generation == epoch, !isInvalidated, wantsReading, canStudy, isAttachmentRendered else { return }
|
||||
guard generation == epoch, !isInvalidated, wantsReading, canStudy, isAttachmentRendered, !isReading else { return }
|
||||
isSessionBusy = true
|
||||
defer { finishSessionOperation(epoch: epoch) }
|
||||
do {
|
||||
@@ -272,16 +279,21 @@ final class TrainingRemoteReadingViewModel {
|
||||
} catch { await handleReadingError(error, epoch: epoch) }
|
||||
}
|
||||
|
||||
/// 由前台定时器驱动,每次最多记一秒;到间隔或门槛才发送网络心跳。
|
||||
func recordVisibleTime(at uptime: TimeInterval) async {
|
||||
/// 前台可见时每次最多记一秒;网络心跳进行中仍累计,到门槛时同步停止。
|
||||
@discardableResult
|
||||
func accumulateVisibleTime(at uptime: TimeInterval) -> Bool {
|
||||
lastVisibleUptime = uptime
|
||||
guard isReading, wantsReading, canStudy, isAttachmentRendered, !isSessionBusy, pendingHeartbeat == nil,
|
||||
readingClock.takeSecond(at: uptime) else { return }
|
||||
guard session != nil, isReading, wantsReading, canStudy, isAttachmentRendered, readingErrorMessage == nil,
|
||||
readingClock.takeSecond(at: uptime) else { return false }
|
||||
activeSecondsTotal += 1
|
||||
let interval = max(1, session?.heartbeatIntervalSeconds ?? 5)
|
||||
if activeSecondsTotal - acceptedActiveSecondsTotal >= interval || remainingSeconds == 0 {
|
||||
await flush(final: remainingSeconds == 0)
|
||||
}
|
||||
if remainingSeconds == 0 { pauseReading() }
|
||||
return true
|
||||
}
|
||||
|
||||
/// 累计可见读时并等待本次保存;持续刷新的页面可分别调用累计与保存方法。
|
||||
func recordVisibleTime(at uptime: TimeInterval) async {
|
||||
accumulateVisibleTime(at: uptime)
|
||||
if needsReadingFlush { await flush() }
|
||||
}
|
||||
|
||||
/// 页面隐藏、锁屏、文档切换或预览失败时同步停止计时,丢弃未满一秒片段。
|
||||
|
||||
@@ -398,7 +398,12 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
@objc private func appDidBecomeActive() { appIsActive = true; resumeReading() }
|
||||
|
||||
private func resumeReading(retrying: Bool = false) {
|
||||
guard isContentVisible, viewModel.canStudy, (retrying || viewModel.readingErrorMessage == nil), readingTask == nil else { return }
|
||||
guard isContentVisible, viewModel.canStudy, (retrying || viewModel.readingErrorMessage == nil) else { return }
|
||||
if viewModel.isReading {
|
||||
installReadingTimer()
|
||||
return
|
||||
}
|
||||
guard readingTask == nil else { return }
|
||||
let request = retrying ? beginUserRequest(showsLoading: true) : nil
|
||||
guard !retrying || request != nil else { return }
|
||||
let pendingClose = closingTask
|
||||
@@ -427,13 +432,15 @@ final class TrainingCourseViewController: TrainingBaseViewController {
|
||||
}
|
||||
|
||||
private func readingTick() {
|
||||
guard isContentVisible, readingTask == nil, viewModel.isReading else { return }
|
||||
guard isContentVisible else { return }
|
||||
if viewModel.accumulateVisibleTime(at: ProcessInfo.processInfo.systemUptime) { applyState() }
|
||||
guard readingTask == nil, viewModel.needsReadingFlush else { return }
|
||||
readingTask = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
await viewModel.recordVisibleTime(at: ProcessInfo.processInfo.systemUptime)
|
||||
await viewModel.flush()
|
||||
readingTask = nil
|
||||
applyState()
|
||||
if isContentVisible, viewModel.canStudy, !viewModel.isReading { resumeReading() }
|
||||
if isContentVisible, viewModel.canStudy, !viewModel.isReading, viewModel.remainingSeconds > 0 { resumeReading() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user