Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Adds conditional checks to fix first image appearance and fixes unit tests #62

Merged
merged 1 commit into from
Oct 19, 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
4 changes: 3 additions & 1 deletion Example/Tests/SwiftPhotoGalleryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class SwiftPhotoGalleryTests: XCTestCase {

func testProgramaticInitialization() {
testGallery.viewDidLoad()

testGallery.isRevolvingCarouselEnabled = false

expect(self.testGallery.delegate).toNot(beNil())
expect(self.testGallery.delegate) === testHelper

Expand All @@ -44,6 +45,7 @@ class SwiftPhotoGalleryTests: XCTestCase {

func testFirstImagesLoadedAfterInitialization() {
testGallery.viewDidLoad()
testGallery.isRevolvingCarouselEnabled = false

let indexPath = IndexPath(item: 0, section: 0)
testGallery.imageCollectionView.reloadItems(at: [indexPath])
Expand Down
19 changes: 16 additions & 3 deletions Pod/Classes/SwiftPhotoGallery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import UIKit
public class SwiftPhotoGallery: UIViewController {

fileprivate var animateImageTransition = false
fileprivate var isViewFirstAppearing = true

public weak var dataSource: SwiftPhotoGalleryDataSource?
public weak var delegate: SwiftPhotoGalleryDelegate?
Expand Down Expand Up @@ -72,10 +73,11 @@ public class SwiftPhotoGallery: UIViewController {
}
get {
pageBeforeRotation = Int(imageCollectionView.contentOffset.x / imageCollectionView.frame.size.width)
guard isRevolvingCarouselEnabled else {
if isRevolvingCarouselEnabled {
return Int(imageCollectionView.contentOffset.x / imageCollectionView.frame.size.width) - 1
} else {
return Int(imageCollectionView.contentOffset.x / imageCollectionView.frame.size.width)
}
return Int(imageCollectionView.contentOffset.x / imageCollectionView.frame.size.width) - 1
}
}

Expand Down Expand Up @@ -170,6 +172,13 @@ public class SwiftPhotoGallery: UIViewController {
setupGestureRecognizers()
}

public override func viewDidAppear(_ animated: Bool) {
if currentPage < 0 {
currentPage = 0
isViewFirstAppearing = false
}
}

#if os(iOS)
public override var prefersStatusBarHidden: Bool {
get {
Expand Down Expand Up @@ -416,7 +425,11 @@ extension SwiftPhotoGallery: UICollectionViewDataSource {
cell.image = getImage(currentPage: 0)
case UICollectionElementKindSectionHeader:
cell = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SwiftPhotoGalleryCell", for: indexPath) as! SwiftPhotoGalleryCell
cell.image = getImage(currentPage: numberOfImages - 1)
if isViewFirstAppearing {
cell.image = getImage(currentPage: 0)
} else {
cell.image = getImage(currentPage: numberOfImages - 1)
}
default:
assertionFailure("Unexpected element kind")
}
Expand Down