import SnapKit import UIKit protocol RDReaderEPUBContentViewDelegate: AnyObject { func epubContentView(_ contentView: RDReaderEPUBContentView, didUpdateLocation location: RDEPUBLocation, spineIndex: Int) func epubContentView(_ contentView: RDReaderEPUBContentView, didChangeSelection selection: RDEPUBSelection?, spineIndex: Int) func epubContentView(_ contentView: RDReaderEPUBContentView, didActivateInternalLink location: RDEPUBLocation, fromSpineIndex: Int) func epubContentView(_ contentView: RDReaderEPUBContentView, didActivateExternalLink url: URL) func epubContentView(_ contentView: RDReaderEPUBContentView, didLogJavaScriptError message: String) } final class RDReaderEPUBContentView: UIView { weak var delegate: RDReaderEPUBContentViewDelegate? lazy var pageNumLabel: UILabel = { let label = UILabel() label.font = UIFont.systemFont(ofSize: 13) return label }() private let epubWebView = RDEPUBWebView() override init(frame: CGRect) { super.init(frame: frame) clipsToBounds = true layer.masksToBounds = true addSubview(epubWebView) addSubview(pageNumLabel) epubWebView.delegate = self epubWebView.clipsToBounds = true epubWebView.layer.masksToBounds = true epubWebView.snp.makeConstraints { make in make.edges.equalToSuperview() } pageNumLabel.snp.makeConstraints { make in make.right.equalToSuperview().offset(-30) make.bottom.equalToSuperview().offset(-20) } } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func load(publication: RDEPUBPublication, request: RDEPUBRenderRequest) { epubWebView.load(publication: publication, request: request) } func loadPage( parser: RDEPUBParser, spineIndex: Int, pageIndex: Int, totalPagesInChapter: Int, viewportSize: CGSize, padding: UIEdgeInsets, fontSize: CGFloat, lineHeightMultiple: CGFloat, themeBackgroundColor: String?, themeTextColor: String?, targetLocation: RDEPUBLocation?, highlights: [RDEPUBHighlight] ) { epubWebView.loadPage( parser: parser, spineIndex: spineIndex, pageIndex: pageIndex, totalPagesInChapter: totalPagesInChapter, viewportSize: viewportSize, padding: padding, fontSize: fontSize, lineHeightMultiple: lineHeightMultiple, themeBackgroundColor: themeBackgroundColor, themeTextColor: themeTextColor, targetLocation: targetLocation, highlights: highlights ) } func loadFixedSpread( parser: RDEPUBParser, spread: EPUBFixedSpread, viewportSize: CGSize, contentInset: UIEdgeInsets, backgroundColor: UIColor? ) { epubWebView.loadFixedSpread( parser: parser, spread: spread, viewportSize: viewportSize, contentInset: contentInset, backgroundColor: backgroundColor ) } func releaseResources() { epubWebView.reset() epubWebView.delegate = self } } extension RDReaderEPUBContentView: RDEPUBWebViewDelegate { func epubWebView(_ webView: RDEPUBWebView, didUpdateLocation location: RDEPUBLocation, spineIndex: Int) { delegate?.epubContentView(self, didUpdateLocation: location, spineIndex: spineIndex) } func epubWebView(_ webView: RDEPUBWebView, didChangeSelection selection: RDEPUBSelection?, spineIndex: Int) { delegate?.epubContentView(self, didChangeSelection: selection, spineIndex: spineIndex) } func epubWebView(_ webView: RDEPUBWebView, didActivateInternalLink location: RDEPUBLocation, fromSpineIndex: Int) { delegate?.epubContentView(self, didActivateInternalLink: location, fromSpineIndex: fromSpineIndex) } func epubWebView(_ webView: RDEPUBWebView, didActivateExternalLink url: URL) { delegate?.epubContentView(self, didActivateExternalLink: url) } func epubWebView(_ webView: RDEPUBWebView, didLogJavaScriptError message: String) { delegate?.epubContentView(self, didLogJavaScriptError: message) } func epubWebViewDidFinishRendering(_ webView: RDEPUBWebView) { } }