Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Do not load thumbnails strip at controller creation #38

Merged
merged 4 commits into from
Feb 23, 2017
Merged
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: 2 additions & 0 deletions PDFReader/Demo/StartViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ internal final class StartViewController: UIViewController {
/// Presents a document
///
/// - parameter document: document to present
///
/// Add `thumbnailsEnabled:false` to `createNew` to not load the thumbnails in the controller.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please update the README with this new flag instead of this comment

fileprivate func showDocument(_ document: PDFDocument) {
let image = UIImage(named: "")
let controller = PDFViewController.createNew(with: document, title: "", actionButtonImage: image, actionStyle: .activitySheet)
Expand Down
43 changes: 36 additions & 7 deletions Sources/Classes/PDFViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension PDFViewController {
/// - parameter backButton: button to override the default controller back button
///
/// - returns: a `PDFViewController`
public class func createNew(with document: PDFDocument, title: String? = nil, actionButtonImage: UIImage? = nil, actionStyle: ActionStyle = .print, backButton: UIBarButtonItem? = nil) -> PDFViewController {
public class func createNew(with document: PDFDocument, title: String? = nil, actionButtonImage: UIImage? = nil, actionStyle: ActionStyle = .print, backButton: UIBarButtonItem? = nil, thumbnailsEnabled: Bool? = true) -> PDFViewController {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is thumbnailsEnabled here optional? If its not optional, we wouldn't need to force unwrap it below (it should still have a default value)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, please update the documentation with this new parameter

let storyboard = UIStoryboard(name: "PDFReader", bundle: Bundle(for: PDFViewController.self))
let controller = storyboard.instantiateInitialViewController() as! PDFViewController
controller.document = document
Expand All @@ -30,6 +30,7 @@ extension PDFViewController {
}

controller.backButton = backButton
controller.thumbnailsEnabled = thumbnailsEnabled!

if let actionButtonImage = actionButtonImage {
controller.actionButton = UIBarButtonItem(image: actionButtonImage, style: .plain, target: controller, action: #selector(actionButtonPressed))
Expand Down Expand Up @@ -94,6 +95,30 @@ public final class PDFViewController: UIViewController {
}
}

/// Set this to `false` if you don't want to load the thumbnails strip.
fileprivate var thumbnailsEnabled = true {
didSet {
if thumbnailCollectionControllerHeight == nil {
_ = view
}
if !thumbnailsEnabled {
thumbnailCollectionControllerHeight.constant = 0
}
}
}

/// Slides horizontally (from left to right, default) or vertically (from top to bottom)
public var scrollDirection: UICollectionViewScrollDirection = .horizontal {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is supposed to be in #40 instead

didSet {
if collectionView == nil { // if the user of the controller is trying to change the scrollDiecton before it
_ = view // is on the sceen, we need to show it ofscreen to access it's collectionView.
}
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = scrollDirection
}
}
}

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

Expand Down Expand Up @@ -121,6 +146,10 @@ public final class PDFViewController: UIViewController {
return .slide
}

public override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return thumbnailsEnabled
}

override public func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let controller = segue.destination as? PDFThumbnailCollectionViewController {
thumbnailCollectionController = controller
Expand Down Expand Up @@ -198,11 +227,6 @@ extension PDFViewController: UICollectionViewDataSource {
}

extension PDFViewController: PDFPageCollectionViewCellDelegate {
/// Whether or not the thumbnail controller is currently being displayed
private var isThumbnailControllerShown: Bool {
return thumbnailCollectionControllerBottom.constant == -thumbnailCollectionControllerHeight.constant
}

/// Toggles the hiding/showing of the thumbnail controller
///
/// - parameter shouldHide: whether or not the controller show hide
Expand All @@ -211,7 +235,12 @@ extension PDFViewController: PDFPageCollectionViewCellDelegate {
}

func handleSingleTap(_ cell: PDFPageCollectionViewCell, pdfPageView: PDFPageView) {
let shouldHide = !isThumbnailControllerShown
var shouldHide: Bool {
guard let isNavigationBarHidden = navigationController?.isNavigationBarHidden else {
return false
}
return !isNavigationBarHidden
}
UIView.animate(withDuration: 0.25) {
self.hideThumbnailController(shouldHide)
self.navigationController?.setNavigationBarHidden(shouldHide, animated: true)
Expand Down