From 314cda32056f8e0d05da8ab3d55129f0f99d93f6 Mon Sep 17 00:00:00 2001 From: Hans Seiffert Date: Tue, 20 Sep 2016 15:52:52 +0200 Subject: [PATCH 1/3] Provide a delegate method to adjust the html content before the actual loading --- Source/FolioReaderCenter.swift | 11 ++++- Source/FolioReaderPage.swift | 76 +++++++++++++++++++++------------- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/Source/FolioReaderCenter.swift b/Source/FolioReaderCenter.swift index 082442464..86441c16b 100755 --- a/Source/FolioReaderCenter.swift +++ b/Source/FolioReaderCenter.swift @@ -1094,12 +1094,21 @@ extension FolioReaderCenter: FolioReaderPageDelegate { self.pageDelegate?.pageDidLoad(page) } + + public func htmlContentForPage(page: FolioReaderPage, htmlContent: String) -> String { + // Pass the event to the `pageDelegate` property + var tempHtmlContent = htmlContent + if let modifiedHtmlContent = pageDelegate?.htmlContentForPage?(page, htmlContent: htmlContent) { + tempHtmlContent = modifiedHtmlContent + } + return tempHtmlContent + } } // MARK: FolioReaderChapterListDelegate extension FolioReaderCenter: FolioReaderChapterListDelegate { - + func chapterList(chapterList: FolioReaderChapterList, didSelectRowAtIndexPath indexPath: NSIndexPath, withTocReference reference: FRTocReference) { let item = findPageByResource(reference) diff --git a/Source/FolioReaderPage.swift b/Source/FolioReaderPage.swift index eb566ee18..e8e7f4c0f 100755 --- a/Source/FolioReaderPage.swift +++ b/Source/FolioReaderPage.swift @@ -12,13 +12,23 @@ import UIMenuItem_CXAImageSupport import JSQWebViewController /// Protocol which is used from `FolioReaderPage`s. -public protocol FolioReaderPageDelegate: class { +@objc public protocol FolioReaderPageDelegate: class { /** Notify that page did loaded - parameter page: The loaded page */ func pageDidLoad(page: FolioReaderPage) + + /** + Passes and returns the HTML content as `String`. Implement this method if you want to modify the HTML content of a `FolioReaderPage`. + + - parameter page: The current `FolioReaderPage` + - parameter htmlContent: The current HTML content as `String` + + - returns: The adjusted HTML content as `String`. This is the content which will be loaded into the given `FolioReaderPage` + */ + optional func htmlContentForPage(page: FolioReaderPage, htmlContent: String) -> String } public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecognizerDelegate { @@ -98,35 +108,45 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture ) } - func loadHTMLString(string: String!, baseURL: NSURL!) { - - var html = (string as NSString) - - // Restore highlights - let highlights = Highlight.allByBookId((kBookId as NSString).stringByDeletingPathExtension, andPage: pageNumber) - - if highlights.count > 0 { - for item in highlights { - let style = HighlightStyle.classForStyle(item.type) - let tag = "\(item.content)" - var locator = item.contentPre + item.content + item.contentPost - locator = Highlight.removeSentenceSpam(locator) /// Fix for Highlights - let range: NSRange = html.rangeOfString(locator, options: .LiteralSearch) - - if range.location != NSNotFound { - let newRange = NSRange(location: range.location + item.contentPre.characters.count, length: item.content.characters.count) - html = html.stringByReplacingCharactersInRange(newRange, withString: tag) - } - else { - print("highlight range not found") - } - } - } - + func loadHTMLString(htmlContent: String!, baseURL: NSURL!) { + // Insert the stored highlights to the HTML + var tempHtmlContent = htmlContentWithInsertHighlights(htmlContent) + // Let the delegate adjust the html string + if let modifiedHtmlContent = self.delegate?.htmlContentForPage?(self, htmlContent: tempHtmlContent) { + tempHtmlContent = modifiedHtmlContent + } + // Load the html into the webview webView.alpha = 0 - webView.loadHTMLString(html as String, baseURL: baseURL) + webView.loadHTMLString(tempHtmlContent, baseURL: baseURL) } - + + // MARK: - Highlights + + private func htmlContentWithInsertHighlights(htmlContent: String) -> String { + var tempHtmlContent = htmlContent as NSString + // Restore highlights + let highlights = Highlight.allByBookId((kBookId as NSString).stringByDeletingPathExtension, andPage: pageNumber) + + if highlights.count > 0 { + for item in highlights { + let style = HighlightStyle.classForStyle(item.type) + let tag = "\(item.content)" + var locator = item.contentPre + item.content + item.contentPost + locator = Highlight.removeSentenceSpam(locator) /// Fix for Highlights + let range: NSRange = tempHtmlContent.rangeOfString(locator, options: .LiteralSearch) + + if range.location != NSNotFound { + let newRange = NSRange(location: range.location + item.contentPre.characters.count, length: item.content.characters.count) + tempHtmlContent = tempHtmlContent.stringByReplacingCharactersInRange(newRange, withString: tag) + } + else { + print("highlight range not found") + } + } + } + return tempHtmlContent as String + } + // MARK: - UIWebView Delegate public func webViewDidFinishLoad(webView: UIWebView) { From c5c33101469cd49a70aff0d83f6acd1c5e824ce6 Mon Sep 17 00:00:00 2001 From: Hans Seiffert Date: Tue, 20 Sep 2016 17:06:12 +0200 Subject: [PATCH 2/3] Refactor `htmlContentForPage` to be used with the `FolioReaderCenterDelegate` --- Source/FolioReaderCenter.swift | 111 +++++++++++++++++++-------------- Source/FolioReaderPage.swift | 16 +---- 2 files changed, 65 insertions(+), 62 deletions(-) diff --git a/Source/FolioReaderCenter.swift b/Source/FolioReaderCenter.swift index 86441c16b..e6851051e 100755 --- a/Source/FolioReaderCenter.swift +++ b/Source/FolioReaderCenter.swift @@ -18,11 +18,31 @@ var nextPageNumber: Int! var pageScrollDirection = ScrollDirection() var isScrolling = false +/// Protocol which is used from `FolioReaderCenter`s. +@objc public protocol FolioReaderCenterDelegate: class { + + /** + Notifies that a page appeared. This is triggered is a page is chosen and displayed. + + - parameter page: The appeared page + */ + func pageDidAppear(page: FolioReaderPage) + + /** + Passes and returns the HTML content as `String`. Implement this method if you want to modify the HTML content of a `FolioReaderPage`. + + - parameter page: The `FolioReaderPage` + - parameter htmlContent: The current HTML content as `String` + + - returns: The adjusted HTML content as `String`. This is the content which will be loaded into the given `FolioReaderPage` + */ + optional func htmlContentForPage(page: FolioReaderPage, htmlContent: String) -> String +} public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { /// This delegate receives the events from the current `FolioReaderPage`s delegate. - public weak var pageDelegate: FolioReaderPageDelegate? + public weak var delegate: FolioReaderCenterDelegate? var collectionView: UICollectionView! let collectionViewLayout = UICollectionViewFlowLayout() @@ -344,38 +364,44 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo // Configure the cell let resource = book.spine.spineReferences[indexPath.row].resource - var html = try? String(contentsOfFile: resource.fullHref, encoding: NSUTF8StringEncoding) - let mediaOverlayStyleColors = "\"\(readerConfig.mediaOverlayColor.hexString(false))\", \"\(readerConfig.mediaOverlayColor.highlightColor().hexString(false))\"" - - // Inject CSS - let jsFilePath = NSBundle.frameworkBundle().pathForResource("Bridge", ofType: "js") - let cssFilePath = NSBundle.frameworkBundle().pathForResource("Style", ofType: "css") - let cssTag = "" - let jsTag = "" + - "" - - let toInject = "\n\(cssTag)\n\(jsTag)\n" - html = html?.stringByReplacingOccurrencesOfString("", withString: toInject) - - // Font class name - var classes = FolioReader.currentFont.cssIdentifier - classes += " "+FolioReader.currentMediaOverlayStyle.className() - - // Night mode - if FolioReader.nightMode { - classes += " nightMode" - } - - // Font Size - classes += " \(FolioReader.currentFontSize.cssIdentifier)" - - html = html?.stringByReplacingOccurrencesOfString("" + let jsTag = "" + + "" + + let toInject = "\n\(cssTag)\n\(jsTag)\n" + html = html.stringByReplacingOccurrencesOfString("", withString: toInject) + + // Font class name + var classes = FolioReader.currentFont.cssIdentifier + classes += " "+FolioReader.currentMediaOverlayStyle.className() + + // Night mode + if FolioReader.nightMode { + classes += " nightMode" + } + + // Font Size + classes += " \(FolioReader.currentFontSize.cssIdentifier)" + + html = html.stringByReplacingOccurrencesOfString(" CGSize { return CGSizeMake(collectionView.frame.width, collectionView.frame.height) } @@ -503,10 +529,8 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo currentPageNumber = page.pageNumber } else { let currentIndexPath = getCurrentIndexPath() - if currentIndexPath != NSIndexPath(forRow: 0, inSection: 0) { - currentPage = collectionView.cellForItemAtIndexPath(currentIndexPath) as? FolioReaderPage - } - + currentPage = collectionView.cellForItemAtIndexPath(currentIndexPath) as? FolioReaderPage + previousPageNumber = currentIndexPath.row currentPageNumber = currentIndexPath.row+1 } @@ -532,7 +556,11 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo } pagesForCurrentPage(page) } - + + if let currentPage = currentPage { + self.delegate?.pageDidAppear(currentPage) + } + completion?() } @@ -1091,18 +1119,7 @@ extension FolioReaderCenter: FolioReaderPageDelegate { let offsetPoint = self.currentWebViewScrollPositions[page.pageNumber - 1] { page.webView.scrollView.setContentOffset(offsetPoint, animated: false) } - - self.pageDelegate?.pageDidLoad(page) } - - public func htmlContentForPage(page: FolioReaderPage, htmlContent: String) -> String { - // Pass the event to the `pageDelegate` property - var tempHtmlContent = htmlContent - if let modifiedHtmlContent = pageDelegate?.htmlContentForPage?(page, htmlContent: htmlContent) { - tempHtmlContent = modifiedHtmlContent - } - return tempHtmlContent - } } // MARK: FolioReaderChapterListDelegate diff --git a/Source/FolioReaderPage.swift b/Source/FolioReaderPage.swift index e8e7f4c0f..f89a13188 100755 --- a/Source/FolioReaderPage.swift +++ b/Source/FolioReaderPage.swift @@ -12,23 +12,13 @@ import UIMenuItem_CXAImageSupport import JSQWebViewController /// Protocol which is used from `FolioReaderPage`s. -@objc public protocol FolioReaderPageDelegate: class { +protocol FolioReaderPageDelegate: class { /** Notify that page did loaded - parameter page: The loaded page */ func pageDidLoad(page: FolioReaderPage) - - /** - Passes and returns the HTML content as `String`. Implement this method if you want to modify the HTML content of a `FolioReaderPage`. - - - parameter page: The current `FolioReaderPage` - - parameter htmlContent: The current HTML content as `String` - - - returns: The adjusted HTML content as `String`. This is the content which will be loaded into the given `FolioReaderPage` - */ - optional func htmlContentForPage(page: FolioReaderPage, htmlContent: String) -> String } public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecognizerDelegate { @@ -111,10 +101,6 @@ public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGesture func loadHTMLString(htmlContent: String!, baseURL: NSURL!) { // Insert the stored highlights to the HTML var tempHtmlContent = htmlContentWithInsertHighlights(htmlContent) - // Let the delegate adjust the html string - if let modifiedHtmlContent = self.delegate?.htmlContentForPage?(self, htmlContent: tempHtmlContent) { - tempHtmlContent = modifiedHtmlContent - } // Load the html into the webview webView.alpha = 0 webView.loadHTMLString(tempHtmlContent, baseURL: baseURL) From 41067df7eb8d3685e1cd965f1c8e516ccdb759ff Mon Sep 17 00:00:00 2001 From: Hans Seiffert Date: Tue, 20 Sep 2016 17:11:22 +0200 Subject: [PATCH 3/3] Make `pageDidAppear` delegate method optional --- Source/FolioReaderCenter.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/FolioReaderCenter.swift b/Source/FolioReaderCenter.swift index e6851051e..5597c7b26 100755 --- a/Source/FolioReaderCenter.swift +++ b/Source/FolioReaderCenter.swift @@ -26,7 +26,7 @@ var isScrolling = false - parameter page: The appeared page */ - func pageDidAppear(page: FolioReaderPage) + optional func pageDidAppear(page: FolioReaderPage) /** Passes and returns the HTML content as `String`. Implement this method if you want to modify the HTML content of a `FolioReaderPage`. @@ -558,7 +558,7 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo } if let currentPage = currentPage { - self.delegate?.pageDidAppear(currentPage) + self.delegate?.pageDidAppear?(currentPage) } completion?()