Skip to content

Commit

Permalink
Merge pull request #137 from NYTimes/cdz/134-dismiss-delegates
Browse files Browse the repository at this point in the history
Send delegate messages for all user-initiated dismissals
  • Loading branch information
David Galbraith committed Jan 26, 2016
2 parents 3f944c2 + c318d3b commit fd0ed13
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
5 changes: 0 additions & 5 deletions Example/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,3 @@ end
target 'NYTPhotoViewer-Swift', :exclusive => true do
pod "NYTPhotoViewer/AnimatedGifSupport", :path => "../"
end

target 'NYTPhotoViewer-SwiftTests', :exclusive => true do
pod "NYTPhotoViewer/AnimatedGifSupport", :path => "../"
pod 'OCMock', '~> 3.2'
end
1 change: 0 additions & 1 deletion Example/Tests/NYTPhotosOverlayViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#import <NYTPhotoViewer/NYTPhotosOverlayView.h>

@interface NYTPhotosOverlayViewTests : XCTestCase

@end

@implementation NYTPhotosOverlayViewTests
Expand Down
45 changes: 45 additions & 0 deletions Example/Tests/NYTPhotosViewControllerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@
@import UIKit;
@import XCTest;

#import <OCMock/OCMock.h>

#import <NYTPhotoViewer/NYTPhotosViewController.h>
#import "NYTExamplePhoto.h"

@interface NYTPhotosViewControllerTests : XCTestCase
@end

@interface NYTPhotosViewController (Testing)

- (void)dismissViewControllerAnimated:(BOOL)animated userInitiated:(BOOL)isUserInitiated completion:(void (^)(void))completion;
- (void)didPanWithGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer;
- (void)doneButtonTapped:(id)sender;

@end

Expand Down Expand Up @@ -212,6 +221,42 @@ - (void)testPageViewIsntLoadedAfterInit {
XCTAssertFalse(photosViewController.pageViewController.isViewLoaded);
}

- (void)testDoneButtonDismissalUserInitiatedFlagIsTrue {
NSArray *photos = [self newTestPhotos];
NYTPhotosViewController *photosViewController = [[NYTPhotosViewController alloc] initWithPhotos:photos];

id photosVCMock = OCMPartialMock(photosViewController);

[photosViewController doneButtonTapped:nil];

OCMVerify([photosVCMock dismissViewControllerAnimated:YES userInitiated:YES completion:[OCMArg any]]);
}

- (void)testGestureBasedDismissalUserInitiatedFlagIsTrue {
NSArray *photos = [self newTestPhotos];
NYTPhotosViewController *photosViewController = [[NYTPhotosViewController alloc] initWithPhotos:photos];

id photosVCMock = OCMPartialMock(photosViewController);

id gestureRecognizerMock = OCMClassMock([UIPanGestureRecognizer class]);
OCMStub([gestureRecognizerMock state]).andReturn(UIGestureRecognizerStateBegan);

[photosViewController didPanWithGestureRecognizer:gestureRecognizerMock];

OCMVerify([photosVCMock dismissViewControllerAnimated:YES userInitiated:YES completion:[OCMArg any]]);
}

- (void)testProgrammaticDismissalUserInitiatedFlagIsFalse {
NSArray *photos = [self newTestPhotos];
NYTPhotosViewController *photosViewController = [[NYTPhotosViewController alloc] initWithPhotos:photos];

id photosVCMock = OCMPartialMock(photosViewController);

[photosViewController dismissViewControllerAnimated:YES completion:nil];

OCMVerify([photosVCMock dismissViewControllerAnimated:YES userInitiated:NO completion:[OCMArg any]]);
}

#pragma mark - Helpers

- (NSArray *)newTestPhotos {
Expand Down
23 changes: 20 additions & 3 deletions Pod/Classes/ios/NYTPhotosViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,25 @@

NS_ASSUME_NONNULL_BEGIN

// All notifications will have the `NYTPhotosViewController` instance set as the object.
/**
* Notification name issued when this `NYTPhotosViewController` navigates to a different photo.
*
* Includes the `NYTPhotosViewController` instance, as the notification's object.
*/
extern NSString * const NYTPhotosViewControllerDidNavigateToPhotoNotification;

/**
* Notification name issued when this `NYTPhotosViewController` is about to be dismissed.
*
* Includes the `NYTPhotosViewController` instance, as the notification's object.
*/
extern NSString * const NYTPhotosViewControllerWillDismissNotification;

/**
* Notification name issued when this `NYTPhotosViewController` has been dismissed.
*
* Includes the `NYTPhotosViewController` instance, as the notification's object.
*/
extern NSString * const NYTPhotosViewControllerDidDismissNotification;

@interface NYTPhotosViewController : UIViewController
Expand Down Expand Up @@ -125,14 +141,15 @@ extern NSString * const NYTPhotosViewControllerDidDismissNotification;
- (void)photosViewController:(NYTPhotosViewController *)photosViewController didNavigateToPhoto:(id <NYTPhoto>)photo atIndex:(NSUInteger)photoIndex;

/**
* Called immediately before the photos view controller is about to start dismissal. This will be the beginning of the interactive panning to dismiss, if it is enabled and performed.
* Called immediately before the `NYTPhotosViewController` is about to start a user-initiated dismissal.
* This will be the beginning of the interactive panning to dismiss, if it is enabled and performed.
*
* @param photosViewController The `NYTPhotosViewController` instance that sent the delegate message.
*/
- (void)photosViewControllerWillDismiss:(NYTPhotosViewController *)photosViewController;

/**
* Called immediately after the photos view controller has dismissed.
* Called immediately after the photos view controller has been dismissed by the user.
*
* @param photosViewController The `NYTPhotosViewController` instance that sent the delegate message.
*/
Expand Down
16 changes: 11 additions & 5 deletions Pod/Classes/ios/NYTPhotosViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
return UIStatusBarAnimationFade;
}

- (void)dismissViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion {
[self dismissViewControllerAnimated:animated userInitiated:NO completion:completion];
}

#pragma mark - NYTPhotosViewController

- (instancetype)initWithPhotos:(NSArray *)photos {
Expand Down Expand Up @@ -243,7 +247,7 @@ - (void)updateOverlayInformation {
}

- (void)doneButtonTapped:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
[self dismissViewControllerAnimated:YES userInitiated:YES completion:nil];
}

- (void)actionButtonTapped:(id)sender {
Expand Down Expand Up @@ -336,15 +340,17 @@ - (void)didSingleTapWithGestureRecognizer:(UITapGestureRecognizer *)tapGestureRe
- (void)didPanWithGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer {
if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
self.transitionController.forcesNonInteractiveDismissal = NO;
[self dismissViewControllerAnimated:YES completion:nil];
[self dismissViewControllerAnimated:YES userInitiated:YES completion:nil];
}
else {
self.transitionController.forcesNonInteractiveDismissal = YES;
[self.transitionController didPanWithPanGestureRecognizer:panGestureRecognizer viewToPan:self.pageViewController.view anchorPoint:self.boundsCenterPoint];
}
}

#pragma mark - View Controller Dismissal

- (void)dismissViewControllerAnimated:(BOOL)animated completion:(void (^)(void))completion {
- (void)dismissViewControllerAnimated:(BOOL)animated userInitiated:(BOOL)isUserInitiated completion:(void (^)(void))completion {
UIView *startingView;
if (self.currentlyDisplayedPhoto.image || self.currentlyDisplayedPhoto.placeholderImage || self.currentlyDisplayedPhoto.imageData) {
startingView = self.currentPhotoViewController.scalingImageView.imageView;
Expand All @@ -357,8 +363,8 @@ - (void)dismissViewControllerAnimated:(BOOL)animated completion:(void (^)(void))
[self setOverlayViewHidden:YES animated:animated];

// Cocoa convention is not to call delegate methods when you do something directly in code,
// so we'll not call delegate methods if this is a programmatic, noninteractive dismissal:
BOOL shouldSendDelegateMessages = !self.transitionController.forcesNonInteractiveDismissal;
// so we'll not call delegate methods if this is a programmatic dismissal:
BOOL const shouldSendDelegateMessages = isUserInitiated;

if (shouldSendDelegateMessages && [self.delegate respondsToSelector:@selector(photosViewControllerWillDismiss:)]) {
[self.delegate photosViewControllerWillDismiss:self];
Expand Down

0 comments on commit fd0ed13

Please sign in to comment.