ReadViewSDK/Sources/RDEpubReaderView/EPUBUI/RDEPUBReaderController+Trial.swift

105 lines
4.3 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.

import UIKit
///
///
/// `pageCountOfReaderView`
/// `numberOfPages(in:)` +1
///
/// - readoor epubreadableChapterCount nil
/// - epub + readableChapterCount N bookPageMap
extension RDEPUBReaderController {
/// 访 delegate
var trialWallView: UIView? {
if let cached = cachedTrialWallView {
return cached
}
guard configuration.trialPolicy != nil else { return nil }
let view = delegate?.epubReaderTrialWallView(self)
cachedTrialWallView = view
return view
}
/// 宿
var isTrialWallEnabled: Bool {
configuration.trialPolicy != nil && trialWallView != nil
}
///
/// readableChapterCount bookPageMap
var readableContentPageCount: Int {
let rawCount = pageCountOfReaderView(readerView: readerView)
guard let policy = configuration.trialPolicy,
let readableChapterCount = policy.readableChapterCount,
readableChapterCount > 0,
let pageMap = readerContext.bookPageMap else {
return rawCount
}
// spineIndex >= readableChapterCount =
let firstLockedStart = pageMap.entries
.first { $0.spineIndex >= readableChapterCount }?
.absolutePageStart
return firstLockedStart ?? rawCount
}
/// 0-based nil
var trialWallPageIndex: Int? {
guard isTrialWallEnabled else { return nil }
return readableContentPageCount
}
/// RDEpubReaderView 使 + 1
var effectiveNumberOfPages: Int {
guard isTrialWallEnabled else {
return pageCountOfReaderView(readerView: readerView)
}
return readableContentPageCount + 1
}
func isReadableTableOfContentsLocation(_ location: RDEPUBLocation) -> Bool {
guard let policy = configuration.trialPolicy,
let readableChapterCount = policy.readableChapterCount else {
return true
}
guard let spineIndex = readerContext.normalizedSpineIndex(for: location) else {
return true
}
return spineIndex < readableChapterCount
}
/// 宿
func makeTrialWallPageView(reusableView: UIView?) -> UIView {
let container = (reusableView as? RDEPUBTrialWallContainerView) ?? RDEPUBTrialWallContainerView()
container.setWallView(trialWallView)
return container
}
}
/// 宿
final class RDEPUBTrialWallContainerView: UIView, RDEpubReaderCachePolicyProviding {
/// 宿 View
/// View
var shouldAvoidReaderPageCaching: Bool { true }
private weak var wallView: UIView?
func setWallView(_ view: UIView?) {
// 使 View
if wallView === view, view?.superview === self {
return
}
wallView?.removeFromSuperview()
guard let view else { return }
view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)
NSLayoutConstraint.activate([
view.leadingAnchor.constraint(equalTo: leadingAnchor),
view.trailingAnchor.constraint(equalTo: trailingAnchor),
view.topAnchor.constraint(equalTo: topAnchor),
view.bottomAnchor.constraint(equalTo: bottomAnchor)
])
wallView = view
}
}