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

Make the changePage and scrollTo functionality accesable to the public #128

Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 19 additions & 13 deletions Source/FolioReaderCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -613,17 +613,6 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
)
}

func changePageWith(page page: Int, animated: Bool = false, completion: (() -> Void)? = nil) {
if page > 0 && page-1 < totalPages {
let indexPath = NSIndexPath(forRow: page-1, inSection: 0)
changePageWith(indexPath: indexPath, animated: animated, completion: { () -> Void in
self.updateCurrentPage({ () -> Void in
completion?()
})
})
}
}

func changePageWith(page page: Int, andFragment fragment: String, animated: Bool = false, completion: (() -> Void)? = nil) {
if currentPageNumber == page {
if let currentPage = currentPage where fragment != "" {
Expand All @@ -640,7 +629,7 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
}
}

func changePageWith(href href: String, animated: Bool = false, completion: (() -> Void)? = nil) {
func changePageWith(href href: String, animated: Bool = false, completion: (() -> Void)? = nil) {
let item = findPageByHref(href)
let indexPath = NSIndexPath(forRow: item, inSection: 0)
changePageWith(indexPath: indexPath, animated: animated, completion: { () -> Void in
Expand Down Expand Up @@ -681,7 +670,7 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
completion?()
}
}

func indexPathIsValid(indexPath: NSIndexPath) -> Bool {
let section = indexPath.section
let row = indexPath.row
Expand Down Expand Up @@ -772,6 +761,23 @@ public class FolioReaderCenter: UIViewController, UICollectionViewDelegate, UICo
}
return nil
}

// MARK: Public page methods

public func changePageWith(page page: Int, animated: Bool = false, completion: (() -> Void)? = nil) {
Copy link
Member

Choose a reason for hiding this comment

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

Please add some docs for public functions, so you help me on generated docs 😀

if page > 0 && page-1 < totalPages {
let indexPath = NSIndexPath(forRow: page-1, inSection: 0)
changePageWith(indexPath: indexPath, animated: animated, completion: { () -> Void in
self.updateCurrentPage({ () -> Void in
completion?()
})
})
}
}

public func getCurrentPage() -> FolioReaderPage? {
Copy link
Member

Choose a reason for hiding this comment

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

I think is better to make currentPage public get with a private set, so we don't need it.

public private(set) var currentPage: FolioReaderPage?

And use it

FolioReader.sharedInstance.readerCenter.currentPage

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's fine for me.

return self.currentPage
}

// MARK: - Audio Playing

Expand Down
9 changes: 8 additions & 1 deletion Source/FolioReaderContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var book: FRBook!
/// Reader container
public class FolioReaderContainer: UIViewController {
var centerNavigationController: UINavigationController!
var centerViewController: FolioReaderCenter!
var centerViewController: FolioReaderCenter!
var audioPlayer: FolioReaderAudioPlayer!
var shouldHideStatusBar = true
var shouldRemoveEpub = true
Expand Down Expand Up @@ -188,4 +188,11 @@ public class FolioReaderContainer: UIViewController {
override public func preferredStatusBarStyle() -> UIStatusBarStyle {
return isNight(.LightContent, .Default)
}

// MARK: - Public

public func readerCenter() -> FolioReaderCenter {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this? it is already exposed thought singleton.

You can use it:

let readerCenter = FolioReader.sharedInstance.readerCenter

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, didn't see this one.
In general the new method would support an non singleton usage. But as there might be many places in the FolioReader which doesn't support a usage with multiple instances this doesn't matter at the moment.

return self.centerViewController
}

}
162 changes: 82 additions & 80 deletions Source/FolioReaderPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protocol FolioReaderPageDelegate: class {
func pageDidLoad(page: FolioReaderPage)
}

class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecognizerDelegate {
public class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecognizerDelegate {

weak var delegate: FolioReaderPageDelegate?
var pageNumber: Int!
Expand All @@ -31,7 +31,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni

// MARK: - View life cicle

override init(frame: CGRect) {
override public init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()

Expand Down Expand Up @@ -62,15 +62,15 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
webView.addGestureRecognizer(tapGestureRecognizer)
}

required init?(coder aDecoder: NSCoder) {
required public init?(coder aDecoder: NSCoder) {
fatalError("storyboards are incompatible with truth and beauty")
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

override func layoutSubviews() {
override public func layoutSubviews() {
super.layoutSubviews()

webView.setupScrollDirection()
Expand Down Expand Up @@ -127,7 +127,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni

// MARK: - UIWebView Delegate

func webViewDidFinishLoad(webView: UIWebView) {
public func webViewDidFinishLoad(webView: UIWebView) {
refreshPageMode()

if readerConfig.enableTTS && !book.hasAudio() {
Expand All @@ -152,7 +152,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
delegate?.pageDidLoad(self)
}

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
public func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

guard let url = request.URL else { return false }

Expand Down Expand Up @@ -241,7 +241,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni

// MARK: Gesture recognizer

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {

if gestureRecognizer.view is UIWebView {
if otherGestureRecognizer is UILongPressGestureRecognizer {
Expand All @@ -255,7 +255,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
return false
}

func handleTapGesture(recognizer: UITapGestureRecognizer) {
public func handleTapGesture(recognizer: UITapGestureRecognizer) {
// webView.setMenuVisible(false)

if FolioReader.sharedInstance.readerCenter.navigationController!.navigationBarHidden {
Expand Down Expand Up @@ -283,82 +283,84 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni
// Reset menu
menuIsVisible = false
}
// MARK: - Scroll and positioning
/**
Scrolls the page to a given offset
- parameter offset: The offset to scroll
- parameter animated: Enable or not scrolling animation
*/
func scrollPageToOffset(offset: CGFloat, animated: Bool) {
let pageOffsetPoint = isDirection(CGPoint(x: 0, y: offset), CGPoint(x: offset, y: 0))

// MARK: - Public scroll postion setter

/**
Scrolls the page to a given offset

- parameter offset: The offset to scroll
- parameter animated: Enable or not scrolling animation
*/
public func scrollPageToOffset(offset: CGFloat, animated: Bool) {
let pageOffsetPoint = isDirection(CGPoint(x: 0, y: offset), CGPoint(x: offset, y: 0))
webView.scrollView.setContentOffset(pageOffsetPoint, animated: animated)
}

/**
Scrolls the page to bottom
*/
func scrollPageToBottom() {
let bottomOffset = isDirection(
CGPointMake(0, webView.scrollView.contentSize.height - webView.scrollView.bounds.height),
CGPointMake(webView.scrollView.contentSize.width - webView.scrollView.bounds.width, 0),
CGPointMake(webView.scrollView.contentSize.width - webView.scrollView.bounds.width, 0)
)

if bottomOffset.forDirection() >= 0 {
dispatch_async(dispatch_get_main_queue(), {
self.webView.scrollView.setContentOffset(bottomOffset, animated: false)
})
}
}

/**
Handdle #anchors in html, get the offset and scroll to it

- parameter anchor: The #anchor
- parameter avoidBeginningAnchors: Sometimes the anchor is on the beggining of the text, there is not need to scroll
- parameter animated: Enable or not scrolling animation
*/
func handleAnchor(anchor: String, avoidBeginningAnchors: Bool, animated: Bool) {
if !anchor.isEmpty {
let offset = getAnchorOffset(anchor)

if readerConfig.scrollDirection == .vertical {
let isBeginning = offset < frame.forDirection()/2

if !avoidBeginningAnchors {
scrollPageToOffset(offset, animated: animated)
} else if avoidBeginningAnchors && !isBeginning {
scrollPageToOffset(offset, animated: animated)
}
} else {
scrollPageToOffset(offset, animated: animated)
}
}
}

/**
Get the #anchor offset in the page

- parameter anchor: The #anchor id
- returns: The element offset ready to scroll
*/
func getAnchorOffset(anchor: String) -> CGFloat {
let horizontal = readerConfig.scrollDirection == .horizontal
if let strOffset = webView.js("getAnchorOffset('\(anchor)', \(horizontal.description))") {
return CGFloat((strOffset as NSString).floatValue)
}

return CGFloat(0)
}


/**
Scrolls the page to bottom
*/
public func scrollPageToBottom() {
let bottomOffset = isDirection(
CGPointMake(0, webView.scrollView.contentSize.height - webView.scrollView.bounds.height),
CGPointMake(webView.scrollView.contentSize.width - webView.scrollView.bounds.width, 0),
CGPointMake(webView.scrollView.contentSize.width - webView.scrollView.bounds.width, 0)
)

if bottomOffset.forDirection() >= 0 {
dispatch_async(dispatch_get_main_queue(), {
self.webView.scrollView.setContentOffset(bottomOffset, animated: false)
})
}
}

/**
Handdle #anchors in html, get the offset and scroll to it

- parameter anchor: The #anchor
- parameter avoidBeginningAnchors: Sometimes the anchor is on the beggining of the text, there is not need to scroll
- parameter animated: Enable or not scrolling animation
*/
public func handleAnchor(anchor: String, avoidBeginningAnchors: Bool, animated: Bool) {
if !anchor.isEmpty {
let offset = getAnchorOffset(anchor)

if readerConfig.scrollDirection == .vertical {
let isBeginning = offset < frame.forDirection()/2

if !avoidBeginningAnchors {
scrollPageToOffset(offset, animated: animated)
} else if avoidBeginningAnchors && !isBeginning {
scrollPageToOffset(offset, animated: animated)
}
} else {
scrollPageToOffset(offset, animated: animated)
}
}
}

// MARK: Helper

/**
Get the #anchor offset in the page

- parameter anchor: The #anchor id
- returns: The element offset ready to scroll
*/
func getAnchorOffset(anchor: String) -> CGFloat {
let horizontal = readerConfig.scrollDirection == .horizontal
if let strOffset = webView.js("getAnchorOffset('\(anchor)', \(horizontal.description))") {
return CGFloat((strOffset as NSString).floatValue)
}

return CGFloat(0)
}

// MARK: Mark ID

/**
Audio Mark ID - marks an element with an ID with the given class and scrolls to it

- parameter ID: The ID
*/
func audioMarkID(ID: String) {
Expand All @@ -368,7 +370,7 @@ class FolioReaderPage: UICollectionViewCell, UIWebViewDelegate, UIGestureRecogni

// MARK: UIMenu visibility

override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
override public func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if UIMenuController.sharedMenuController().menuItems?.count == 0 {
webView.isColors = false
webView.createMenu(options: false)
Expand Down