ReadViewSDK/Sources/RDReaderView/EPUBCore/RDEPUBReadingSession.swift
shen 54798ba578 refactor: 添加中文注释 + 优化模块结构
- 给全部 78 个 Swift 源文件添加详细的中文注释(文件级、类级、方法级)
- 删除 LegacyRDReaderController/ 死代码目录(16 文件 4592 行)
- 根目录翻页容器文件移入 ReaderView/ 目录
- Resources/ 移入 EPUBCore/Resources/(与使用者归属一致)
- RDEPUBTextIndexTable.swift 移入 EPUBTextRendering/(消除反向依赖)
- RDURLReaderController.swift 移入 EPUBUI/(入口控制器归入 UI 层)
- 更新 podspec 资源路径
2026-05-25 10:19:14 +08:00

366 lines
14 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// RDEPUBReadingSession.swift
// EPUB
// initializing/loading/idle/jumping/moving/repaginating
//
//
import Foundation
/// EPUB
/// initializing loading idle jumping/moving/repaginating
public final class RDEPUBReadingSession {
/// +
public typealias PaginationSnapshot = (pages: [EPUBPage], chapters: [EPUBChapterInfo])
///
public let publication: RDEPUBPublication
///
public private(set) var navigatorState: RDEPUBNavigatorState = .initializing
///
public private(set) var activePages: [EPUBPage] = []
///
public private(set) var activeChapters: [EPUBChapterInfo] = []
///
public private(set) var stagedPages: [EPUBPage]?
///
public private(set) var stagedChapters: [EPUBChapterInfo]?
///
public private(set) var stagedRestoreLocation: RDEPUBLocation?
/// fragment
public private(set) var pendingNavigationLocation: RDEPUBLocation?
///
public private(set) var pendingNavigationPageNum: Int?
///
public private(set) var currentViewport: RDEPUBViewport?
/// + + +
public private(set) var currentReadingContext: RDEPUBReadingContext?
public init(publication: RDEPUBPublication) {
self.publication = publication
}
/// 访
public var resourceResolver: RDEPUBResourceResolver {
publication.resourceResolver
}
///
public func transition(to state: RDEPUBNavigatorState) {
navigatorState = state
}
/// initializing
public func resetRuntimeState() {
navigatorState = .initializing
activePages = []
activeChapters = []
clearPendingNavigation()
clearStagedSnapshot()
currentViewport = nil
currentReadingContext = nil
}
///
public func setActiveSnapshot(_ snapshot: PaginationSnapshot) {
activePages = snapshot.pages
activeChapters = snapshot.chapters
}
///
public func stageSnapshot(_ snapshot: PaginationSnapshot, restoreLocation: RDEPUBLocation?) {
stagedPages = snapshot.pages
stagedChapters = snapshot.chapters
stagedRestoreLocation = restoreLocation
}
///
public func stagedSnapshot() -> PaginationSnapshot? {
guard let stagedPages, let stagedChapters else {
return nil
}
return (stagedPages, stagedChapters)
}
///
public func consumeStagedSnapshotIfAllowed() -> (snapshot: PaginationSnapshot, restoreLocation: RDEPUBLocation?)? {
guard navigatorState.isStableForSnapshotApplication,
let stagedPages,
let stagedChapters else {
return nil
}
let restoreLocation = stagedRestoreLocation
clearStagedSnapshot()
return ((stagedPages, stagedChapters), restoreLocation)
}
///
public func clearStagedSnapshot() {
stagedPages = nil
stagedChapters = nil
stagedRestoreLocation = nil
}
///
public func clearPendingNavigation() {
pendingNavigationLocation = nil
pendingNavigationPageNum = nil
}
/// spine
public func pendingLocation(forPageNumber pageNumber: Int, spineIndex: Int?) -> RDEPUBLocation? {
guard pendingNavigationPageNum == pageNumber,
let pendingNavigationLocation else {
return nil
}
guard let spineIndex else {
return pendingNavigationLocation
}
let pageHref = resourceResolver.href(forSpineIndex: spineIndex)
guard resourceResolver.normalizedHref(pageHref ?? "") == resourceResolver.normalizedHref(pendingNavigationLocation.href) else {
return nil
}
return pendingNavigationLocation
}
/// spine spread
public func pageContains(spineIndex: Int, in page: EPUBPage) -> Bool {
if let fixedSpread = page.fixedSpread {
return fixedSpread.resources.contains(where: { $0.spineIndex == spineIndex })
}
return page.spineIndex == spineIndex
}
/// 退
public func fallbackLocation(for page: EPUBPage, bookIdentifier: String?) -> RDEPUBLocation? {
if let fixedSpread = page.fixedSpread {
return RDEPUBLocation(
bookIdentifier: bookIdentifier,
href: fixedSpread.primaryResource.href,
progression: 0,
lastProgression: 1
)
}
guard let href = resourceResolver.href(forSpineIndex: page.spineIndex) else {
return nil
}
let progression: Double
if page.totalPagesInChapter <= 1 {
progression = 0
} else {
progression = Double(page.pageIndexInChapter) / Double(page.totalPagesInChapter - 1)
}
return RDEPUBLocation(
bookIdentifier: bookIdentifier,
href: href,
progression: progression,
lastProgression: progression
)
}
///
public func currentVisibleLocation(currentPageNumber: Int, bookIdentifier: String?) -> RDEPUBLocation? {
guard currentPageNumber > 0,
activePages.indices.contains(currentPageNumber - 1) else {
return nil
}
return fallbackLocation(for: activePages[currentPageNumber - 1], bookIdentifier: bookIdentifier)
}
/// spine
public func initialSpineIndex(for location: RDEPUBLocation?) -> Int {
guard let location,
let normalizedHref = resourceResolver.normalizedHref(location.href),
let spineIndex = publication.spine.firstIndex(where: { resourceResolver.normalizedHref($0.href) == normalizedHref }) else {
return 0
}
return spineIndex
}
///
public func pageIndex(for location: RDEPUBLocation, bookIdentifier: String?) -> Int? {
guard location.bookIdentifier == nil || location.bookIdentifier == bookIdentifier else {
return nil
}
guard let targetHref = resourceResolver.normalizedHref(location.href) else {
return nil
}
if publication.layout == .fixed {
return activePages.firstIndex(where: { page in
if let fixedSpread = page.fixedSpread {
return fixedSpread.contains(normalizedHref: targetHref, normalizer: { self.resourceResolver.normalizedHref($0) })
}
guard let href = self.resourceResolver.href(forSpineIndex: page.spineIndex) else {
return false
}
return self.resourceResolver.normalizedHref(href) == targetHref
})
}
guard let spineIndex = publication.spine.firstIndex(where: { resourceResolver.normalizedHref($0.href) == targetHref }) else {
return nil
}
let candidates = activePages.enumerated().filter { $0.element.spineIndex == spineIndex }
guard !candidates.isEmpty else {
return nil
}
if candidates.count == 1 {
return candidates[0].offset
}
let navigationProgression = location.navigationProgression
let localIndex = min(
candidates.count - 1,
max(0, Int(round(navigationProgression * Double(candidates.count - 1))))
)
return candidates[localIndex].offset
}
/// jumping
public func queueNavigation(
to location: RDEPUBLocation,
relativeToSpineIndex spineIndex: Int? = nil,
bookIdentifier: String?
) -> Int? {
guard let normalizedLocation = resourceResolver.normalizedLocation(
location,
relativeToSpineIndex: spineIndex,
bookIdentifier: bookIdentifier
), let pageIndex = pageIndex(for: normalizedLocation, bookIdentifier: bookIdentifier) else {
return nil
}
pendingNavigationLocation = normalizedLocation.fragment == nil ? nil : normalizedLocation
pendingNavigationPageNum = normalizedLocation.fragment == nil ? nil : pageIndex + 1
transition(to: .jumping)
return pageIndex + 1
}
/// idle
public func updateReadingContext(
pageNumber: Int,
location: RDEPUBLocation,
spineIndex: Int,
chapterIndex: Int?,
bookIdentifier: String?
) {
let normalizedLocation = resourceResolver.normalizedLocation(
location,
relativeToSpineIndex: spineIndex,
bookIdentifier: bookIdentifier
) ?? RDEPUBLocation(
bookIdentifier: bookIdentifier,
href: location.href,
progression: location.progression,
lastProgression: location.lastProgression,
fragment: location.fragment
)
let resource = RDEPUBViewportResource(
href: normalizedLocation.href,
spineIndex: spineIndex,
progression: normalizedLocation.progression,
lastProgression: normalizedLocation.lastProgression,
fragment: normalizedLocation.fragment
)
let viewport = RDEPUBViewport(
resources: [resource],
visiblePageNumber: pageNumber,
chapterIndex: chapterIndex,
isFixedLayout: publication.layout == .fixed
)
currentViewport = viewport
currentReadingContext = RDEPUBReadingContext(
location: normalizedLocation,
viewport: viewport,
pageNumber: pageNumber,
chapterIndex: chapterIndex
)
if pendingNavigationPageNum == pageNumber {
clearPendingNavigation()
}
if navigatorState == .jumping || navigatorState == .moving {
transition(to: .idle)
}
}
/// 退
public func currentReadingLocation(bookIdentifier: String?) -> RDEPUBLocation? {
currentReadingContext?.location ?? currentVisibleLocation(currentPageNumber: currentViewport?.visiblePageNumber ?? 0, bookIdentifier: bookIdentifier)
}
///
/// spread spine
public func makePaginationSnapshot(
pageCounts: [Int],
preferences: RDEPUBPreferences,
layoutContext: RDEPUBNavigatorLayoutContext
) -> PaginationSnapshot {
if publication.layout == .fixed {
let spreads = publication.makeFixedSpreads(
preferences: preferences,
viewportSize: layoutContext.viewportSize
)
let chapters = spreads.enumerated().map { spreadIndex, spread in
EPUBChapterInfo(
spineIndex: spread.primaryResource.spineIndex,
title: spread.primaryResource.title,
pageCount: 1
)
}
let pages = spreads.enumerated().map { spreadIndex, spread in
EPUBPage(
spineIndex: spread.primaryResource.spineIndex,
chapterIndex: spreadIndex,
pageIndexInChapter: 0,
totalPagesInChapter: 1,
chapterTitle: spread.primaryResource.title,
fixedSpread: spread
)
}
return (pages, chapters)
}
var pages: [EPUBPage] = []
var chapters: [EPUBChapterInfo] = []
for (spineIndex, pageCount) in pageCounts.enumerated() {
guard pageCount > 0, spineIndex < publication.spine.count else { continue }
let chapterIndex = chapters.count
let title = publication.spine[spineIndex].title
chapters.append(
EPUBChapterInfo(
spineIndex: spineIndex,
title: title,
pageCount: pageCount
)
)
for pageIndex in 0..<pageCount {
pages.append(
EPUBPage(
spineIndex: spineIndex,
chapterIndex: chapterIndex,
pageIndexInChapter: pageIndex,
totalPagesInChapter: pageCount,
chapterTitle: title,
fixedSpread: nil
)
)
}
}
return (pages, chapters)
}
}