ReadViewSDK/Sources/RDReaderView/EPUBUI/ReaderController/RDEPUBJumpSession.swift
shenlei c65c190b71 feat: EPUB阅读器搜索、注释、CFI模块及大书远距跳转优化
- 实现EPUB阅读器搜索功能及选中注释功能
- 优化CFI模块,修复代码审查发现的11个问题
- 实现大书远距目录跳转与后台补全优化方案
- 优化设置面板与章节运行时联动
- 重构及大量改进优化
2026-06-22 20:26:34 +08:00

208 lines
5.5 KiB
Swift

import Foundation
struct RDEPUBJumpSession {
let anchorSpineIndex: Int
let createdAt: CFAbsoluteTime
let protectedSpineIndices: Set<Int>
let sequenceNumber: Int
let expiresAt: CFAbsoluteTime
let reason: Reason
enum Reason {
case tableOfContentsJump
case bookmarkJump
case searchJump
}
enum EndReason {
case coverageComplete
case navigatedAway
case timeout
case superseded
}
}
public struct RDEPUBJumpSessionPolicy: Equatable {
public let exitPageThreshold: Int
public let timeout: TimeInterval
public let idleGracePeriod: TimeInterval
public let protectedNeighborRadius: Int
public static let `default` = RDEPUBJumpSessionPolicy(
exitPageThreshold: 6,
timeout: 20,
idleGracePeriod: 1.5,
protectedNeighborRadius: 1
)
public init(
exitPageThreshold: Int = 6,
timeout: TimeInterval = 20,
idleGracePeriod: TimeInterval = 1.5,
protectedNeighborRadius: Int = 1
) {
self.exitPageThreshold = exitPageThreshold
self.timeout = timeout
self.idleGracePeriod = idleGracePeriod
self.protectedNeighborRadius = protectedNeighborRadius
}
}
final class RDEPUBJumpSessionManager {
private unowned let context: RDEPUBReaderContext
private(set) var activeSession: RDEPUBJumpSession?
private var nextSequenceNumber: Int = 0
private var consecutivePageCount: Int = 0
private var lastPageDirection: PageDirection?
private var lastActivityTime: CFAbsoluteTime = 0
enum PageDirection {
case forward
case backward
}
init(context: RDEPUBReaderContext) {
self.context = context
}
@discardableResult
func createSession(
anchorSpineIndex: Int,
reason: RDEPUBJumpSession.Reason,
totalSpineCount: Int
) -> RDEPUBJumpSession {
let policy = context.configuration.jumpSessionPolicy
let now = CFAbsoluteTimeGetCurrent()
var protectedIndices: Set<Int> = [anchorSpineIndex]
for offset in 1...policy.protectedNeighborRadius {
let lower = anchorSpineIndex - offset
let upper = anchorSpineIndex + offset
if lower >= 0 {
protectedIndices.insert(lower)
}
if upper < totalSpineCount {
protectedIndices.insert(upper)
}
}
nextSequenceNumber += 1
let session = RDEPUBJumpSession(
anchorSpineIndex: anchorSpineIndex,
createdAt: now,
protectedSpineIndices: protectedIndices,
sequenceNumber: nextSequenceNumber,
expiresAt: now + policy.timeout,
reason: reason
)
activeSession = session
consecutivePageCount = 0
lastPageDirection = nil
lastActivityTime = now
RDEPUBBackgroundTrace.log(
"JumpSession",
"created anchor=\(anchorSpineIndex) protected=\(protectedIndices.count) seq=\(nextSequenceNumber)"
)
return session
}
func recordPageChange(fromSpineIndex: Int, toSpineIndex: Int) {
guard activeSession != nil else { return }
let direction: PageDirection = toSpineIndex >= fromSpineIndex ? .forward : .backward
lastActivityTime = CFAbsoluteTimeGetCurrent()
if direction == lastPageDirection {
consecutivePageCount += 1
} else {
consecutivePageCount = 1
lastPageDirection = direction
}
}
func shouldAllowPageMapTakeover(candidateSpineIndices: Set<Int>) -> Bool {
guard let session = activeSession else {
return true
}
let protectedIndices = session.protectedSpineIndices
let coverageRatio = Double(protectedIndices.intersection(candidateSpineIndices).count) /
Double(protectedIndices.count)
return coverageRatio >= 0.8
}
func checkSessionEnd(currentSpineIndex: Int, isIdle: Bool) -> RDEPUBJumpSession.EndReason? {
guard let session = activeSession else { return nil }
let now = CFAbsoluteTimeGetCurrent()
let policy = context.configuration.jumpSessionPolicy
if now >= session.expiresAt {
if isIdle || (now - lastActivityTime) >= policy.idleGracePeriod {
RDEPUBBackgroundTrace.log(
"JumpSession",
"end: timeout seq=\(session.sequenceNumber)"
)
return .timeout
}
}
if !session.protectedSpineIndices.contains(currentSpineIndex) {
if consecutivePageCount >= policy.exitPageThreshold {
RDEPUBBackgroundTrace.log(
"JumpSession",
"end: navigated-away seq=\(session.sequenceNumber) pages=\(consecutivePageCount)"
)
return .navigatedAway
}
} else {
consecutivePageCount = 0
}
return nil
}
func endSession(_ reason: RDEPUBJumpSession.EndReason) {
guard let session = activeSession else { return }
RDEPUBBackgroundTrace.log(
"JumpSession",
"ended seq=\(session.sequenceNumber) reason=\(reason)"
)
activeSession = nil
consecutivePageCount = 0
lastPageDirection = nil
}
func clearSession() {
activeSession = nil
consecutivePageCount = 0
lastPageDirection = nil
nextSequenceNumber = 0
}
}