From 36f51b437df19015ce21ca97d268b343813e2dce Mon Sep 17 00:00:00 2001 From: Sean Perez Date: Thu, 19 Oct 2017 14:55:59 -0600 Subject: [PATCH] Adds conditional checks to fix first image appearance and fixes unit tests --- Example/Tests/SwiftPhotoGalleryTests.swift | 4 +++- Pod/Classes/SwiftPhotoGallery.swift | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Example/Tests/SwiftPhotoGalleryTests.swift b/Example/Tests/SwiftPhotoGalleryTests.swift index e84a596..85b5fb9 100644 --- a/Example/Tests/SwiftPhotoGalleryTests.swift +++ b/Example/Tests/SwiftPhotoGalleryTests.swift @@ -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 @@ -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]) diff --git a/Pod/Classes/SwiftPhotoGallery.swift b/Pod/Classes/SwiftPhotoGallery.swift index eaa4ff0..6310a61 100644 --- a/Pod/Classes/SwiftPhotoGallery.swift +++ b/Pod/Classes/SwiftPhotoGallery.swift @@ -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? @@ -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 } } @@ -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 { @@ -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") }