Skip to content
This repository has been archived by the owner on Nov 26, 2020. It is now read-only.

Migration/wkwebview #388

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
platform :ios, '9.0'
platform :ios, '10.0'

use_frameworks!
inhibit_all_warnings!
Expand Down
2 changes: 1 addition & 1 deletion Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ DEPENDENCIES:
- Quick (= 1.3.2)

SPEC REPOS:
https://github.com/cocoapods/specs.git:
https://github.com/CocoaPods/Specs.git:
- AEXML
- FontBlaster
- MenuItemKit
Expand Down
2 changes: 1 addition & 1 deletion FolioReaderKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Pod::Spec.new do |s|
s.social_media_url = 'https://twitter.com/hebertialmeida'

s.swift_version = '4.2'
s.platform = :ios, '9.0'
s.platform = :ios, '10.0'
s.requires_arc = true

s.source_files = [
Expand Down
25 changes: 11 additions & 14 deletions Source/FolioReaderAudioPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -382,22 +382,19 @@ open class FolioReaderAudioPlayer: NSObject {
}

let playbackActiveClass = book.playbackActiveClass
guard let sentence = currentPage.webView?.js("getSentenceWithIndex('\(playbackActiveClass)')") else {
if (readerCenter.isLastPage() == true) {
self.stop()
} else {
readerCenter.changePageToNext()
currentPage.webView?.js("getSentenceWithIndex('\(playbackActiveClass)')") { sentence in
guard let sentence = sentence else {
if (readerCenter.isLastPage() == true) {
self.stop()
} else {
readerCenter.changePageToNext()
}
return
}

return
guard let href = readerCenter.getCurrentChapter()?.href else { return }
// TODO QUESTION: The previous code made it possible to call `playText` with the parameter `href` being an empty string. Was that valid? should this logic be kept?
self.playText(href, text: sentence)
}

guard let href = readerCenter.getCurrentChapter()?.href else {
return
}

// TODO QUESTION: The previous code made it possible to call `playText` with the parameter `href` being an empty string. Was that valid? should this logic be kept?
self.playText(href, text: sentence)
}

func readCurrentSentence() {
Expand Down
53 changes: 22 additions & 31 deletions Source/FolioReaderCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import ZFDragableModalTransition
import WebKit

/// Protocol which is used from `FolioReaderCenter`s.
@objc public protocol FolioReaderCenterDelegate: class {
Expand Down Expand Up @@ -464,17 +465,11 @@ open class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UIColl
guard var html = try? String(contentsOfFile: resource.fullHref, encoding: String.Encoding.utf8) else {
return cell
}

// Inject viewport
let viewportTag = "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, shrink-to-fit=no\">"

let mediaOverlayStyleColors = "\"\(self.readerConfig.mediaOverlayColor.hexString(false))\", \"\(self.readerConfig.mediaOverlayColor.highlightColor().hexString(false))\""

// Inject CSS
let jsFilePath = Bundle.frameworkBundle().path(forResource: "Bridge", ofType: "js")
let cssFilePath = Bundle.frameworkBundle().path(forResource: "Style", ofType: "css")
let cssTag = "<link rel=\"stylesheet\" type=\"text/css\" href=\"\(cssFilePath!)\">"
let jsTag = "<script type=\"text/javascript\" src=\"\(jsFilePath!)\"></script>" +
"<script type=\"text/javascript\">setMediaOverlayStyleColors(\(mediaOverlayStyleColors))</script>"

let toInject = "\n\(cssTag)\n\(jsTag)\n</head>"
let toInject = "\n\(viewportTag)\n</head>"
html = html.replacingOccurrences(of: "</head>", with: toInject)

// Font class name
Expand Down Expand Up @@ -648,18 +643,13 @@ open class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UIColl
}

scrollScrubber?.setSliderVal()

if let readingTime = currentPage.webView?.js("getReadingTime()") {
pageIndicatorView?.totalMinutes = Int(readingTime)!
} else {
pageIndicatorView?.totalMinutes = 0
currentPage.webView?.js("getReadingTime()") { readingTime in
self.pageIndicatorView?.totalMinutes = Int(readingTime ?? "0")!
self.pagesForCurrentPage(currentPage)
self.delegate?.pageDidAppear?(currentPage)
self.delegate?.pageItemChanged?(self.getCurrentPageItemNumber())
completion?()
}
pagesForCurrentPage(currentPage)

delegate?.pageDidAppear?(currentPage)
delegate?.pageItemChanged?(self.getCurrentPageItemNumber())

completion?()
}

func pagesForCurrentPage(_ page: FolioReaderPage?) {
Expand Down Expand Up @@ -1075,9 +1065,10 @@ open class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UIColl
@objc func shareChapter(_ sender: UIBarButtonItem) {
guard let currentPage = currentPage else { return }

if let chapterText = currentPage.webView?.js("getBodyText()") {
currentPage.webView?.js("getBodyText()") { chapterText in
guard let chapterText = chapterText else { return }
let htmlText = chapterText.replacingOccurrences(of: "[\\n\\r]+", with: "<br />", options: .regularExpression)
var subject = readerConfig.localizedShareChapterSubject
var subject = self.readerConfig.localizedShareChapterSubject
var html = ""
var text = ""
var bookTitle = ""
Expand All @@ -1092,7 +1083,7 @@ open class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UIColl
}

// Get chapter name
if let chapter = getCurrentChapterName() {
if let chapter = self.getCurrentChapterName() {
chapterName = chapter
}

Expand All @@ -1104,17 +1095,17 @@ open class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UIColl
// Sharing html and text
html = "<html><body>"
html += "<br /><hr> <p>\(htmlText)</p> <hr><br />"
html += "<center><p style=\"color:gray\">"+readerConfig.localizedShareAllExcerptsFrom+"</p>"
html += "<center><p style=\"color:gray\">"+self.readerConfig.localizedShareAllExcerptsFrom+"</p>"
html += "<b>\(bookTitle)</b><br />"
html += readerConfig.localizedShareBy+" <i>\(authorName)</i><br />"

if let bookShareLink = readerConfig.localizedShareWebLink {
html += self.readerConfig.localizedShareBy+" <i>\(authorName)</i><br />"
if let bookShareLink = self.readerConfig.localizedShareWebLink {
html += "<a href=\"\(bookShareLink.absoluteString)\">\(bookShareLink.absoluteString)</a>"
shareItems.append(bookShareLink as AnyObject)
}

html += "</center></body></html>"
text = "\(chapterName)\n\n“\(chapterText)” \n\n\(bookTitle) \n\(readerConfig.localizedShareBy) \(authorName)"
text = "\(chapterName)\n\n“\(chapterText)” \n\n\(bookTitle) \n\(self.readerConfig.localizedShareBy) \(authorName)"

let act = FolioReaderSharingProvider(subject: subject, text: text, html: html)
shareItems.insert(contentsOf: [act, "" as AnyObject], at: 0)
Expand All @@ -1127,7 +1118,7 @@ open class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UIColl
actv.barButtonItem = sender
}

present(activityViewController, animated: true, completion: nil)
self.present(activityViewController, animated: true, completion: nil)
}
}

Expand Down Expand Up @@ -1281,7 +1272,7 @@ open class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UIColl
// Perform the page after a short delay as the collection view hasn't completed it's transition if this method is called (the index paths aren't right during fast scrolls).
delay(0.2, closure: { [weak self] in
if (self?.readerConfig.scrollDirection == .horizontalWithVerticalContent),
let cell = ((scrollView.superview as? UIWebView)?.delegate as? FolioReaderPage) {
let cell = ((scrollView.superview as? WKWebView)?.navigationDelegate as? FolioReaderPage) {
let currentIndexPathRow = cell.pageNumber - 1
Copy link
Author

@OrbitalMan OrbitalMan Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @ThakarRajesh! I have already fixed the UIWebView reference - please take a look at commit 49076e4

self?.currentWebViewScrollPositions[currentIndexPathRow] = scrollView.contentOffset
}
Expand Down
Loading