fix(training): 修复课程学习倒计时停顿
This commit is contained in:
@@ -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() }
|
||||
}
|
||||
|
||||
/// 页面隐藏、锁屏、文档切换或预览失败时同步停止计时,丢弃未满一秒片段。
|
||||
|
||||
Reference in New Issue
Block a user