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

Add delegate to the designated initializer #155

Merged
merged 3 commits into from
Feb 2, 2016
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
33 changes: 29 additions & 4 deletions Example/Tests/NYTPhotosViewControllerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,47 @@ - (void)testRightBarButtonItemsAreNilAfterSettingToNil {
XCTAssertNil(photosViewController.rightBarButtonItems);
}

- (void)testConvenienceInitializerAcceptsNil {
- (void)testOneArgConvenienceInitializerAcceptsNil {
XCTAssertNoThrow([[NYTPhotosViewController alloc] initWithPhotos:nil]);
}

- (void)testDesignatedInitializerAcceptsNilForPhotosParameter {
- (void)testTwoArgConvenienceInitializerAcceptsNilForPhotosParameter {
XCTAssertNoThrow([[NYTPhotosViewController alloc] initWithPhotos:nil initialPhoto:[[NYTExamplePhoto alloc] init]]);
}

- (void)testDesignatedInitializerAcceptsNilForInitialPhotoParameter {
- (void)testTwoArgConvenienceInitializerAcceptsNilForInitialPhotoParameter {
XCTAssertNoThrow([[NYTPhotosViewController alloc] initWithPhotos:[self newTestPhotos] initialPhoto:nil]);
}

- (void)testDesignatedInitializerAcceptsNilForBothParameters {
- (void)testTwoArgConvenienceInitializerAcceptsNilForBothParameters {
XCTAssertNoThrow([[NYTPhotosViewController alloc] initWithPhotos:nil initialPhoto:nil]);
}

- (void)testDesignatedInitializerAcceptsNilForPhotosParameter {
id delegateMock = OCMProtocolMock(@protocol(NYTPhotosViewControllerDelegate));
XCTAssertNoThrow([[NYTPhotosViewController alloc] initWithPhotos:nil initialPhoto:[NYTExamplePhoto new] delegate:delegateMock]);
}

- (void)testDesignatedInitializerAcceptsNilForInitialPhotoParameter {
id delegateMock = OCMProtocolMock(@protocol(NYTPhotosViewControllerDelegate));
XCTAssertNoThrow([[NYTPhotosViewController alloc] initWithPhotos:[self newTestPhotos] initialPhoto:nil delegate:delegateMock]);
}

- (void)testDesignatedInitializerAcceptsNilForDelegateParameter {
XCTAssertNoThrow([[NYTPhotosViewController alloc] initWithPhotos:[self newTestPhotos] initialPhoto:[NYTExamplePhoto new] delegate:nil]);
}

- (void)testDesignatedInitializerAcceptsNilForAllParameters {
XCTAssertNoThrow([[NYTPhotosViewController alloc] initWithPhotos:nil initialPhoto:nil delegate:nil]);
}

- (void)testDesignatedInitializerSetsDelegate {
id delegateMock = OCMProtocolMock(@protocol(NYTPhotosViewControllerDelegate));
NYTPhotosViewController *sut = [[NYTPhotosViewController alloc] initWithPhotos:[self newTestPhotos] initialPhoto:nil delegate:delegateMock];

XCTAssertEqual(sut.delegate, delegateMock);
}

- (void)testDisplayPhotoAcceptsNil {
NYTPhotosViewController *photosViewController = [[NYTPhotosViewController alloc] initWithPhotos:[self newTestPhotos]];
XCTAssertNoThrow([photosViewController displayPhoto:nil animated:NO]);
Expand Down
15 changes: 13 additions & 2 deletions Pod/Classes/ios/NYTPhotosViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,34 @@ extern NSString * const NYTPhotosViewControllerDidDismissNotification;
@property (nonatomic, weak, nullable) id <NYTPhotosViewControllerDelegate> delegate;

/**
* A convenience initializer that calls `initWithPhotos:initialPhoto:`, passing the first photo as the `initialPhoto` argument.
* A convenience initializer that calls `initWithPhotos:initialPhoto:delegate:`, passing the first photo as the `initialPhoto` argument, and `nil` as the `delegate` argument.
*
* @param photos An array of objects conforming to the `NYTPhoto` protocol.
*
* @return A fully initialized object.
*/
- (instancetype)initWithPhotos:(NSArray <id <NYTPhoto>> * _Nullable)photos;

/**
* A convenience initializer that calls `initWithPhotos:initialPhoto:delegate:`, passing `nil` as the `delegate` argument.
*
* @param photos An array of objects conforming to the `NYTPhoto` protocol.
* @param initialPhoto The photo to display initially. Must be contained within the `photos` array. If `nil` or not within the `photos` array, the first photo within the `photos` array will be displayed.
*
* @return A fully initialized object.
*/
- (instancetype)initWithPhotos:(NSArray <id <NYTPhoto>> * _Nullable)photos initialPhoto:(id <NYTPhoto> _Nullable)initialPhoto;

/**
* The designated initializer that stores the array of objects conforming to the `NYTPhoto` protocol for display, along with specifying an initial photo for display.
*
* @param photos An array of objects conforming to the `NYTPhoto` protocol.
* @param initialPhoto The photo to display initially. Must be contained within the `photos` array. If `nil` or not within the `photos` array, the first photo within the `photos` array will be displayed.
* @param delegate The delegate for this `NYTPhotosViewController`.
*
* @return A fully initialized object.
*/
- (instancetype)initWithPhotos:(NSArray <id <NYTPhoto>> * _Nullable)photos initialPhoto:(id <NYTPhoto> _Nullable)initialPhoto NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithPhotos:(NSArray <id <NYTPhoto>> * _Nullable)photos initialPhoto:(id <NYTPhoto> _Nullable)initialPhoto delegate:(nullable id <NYTPhotosViewControllerDelegate>)delegate NS_DESIGNATED_INITIALIZER;

/**
* Displays the specified photo. Can be called before the view controller is displayed. Calling with a photo not contained within the data source has no effect.
Expand Down
14 changes: 10 additions & 4 deletions Pod/Classes/ios/NYTPhotosViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];

if (self) {
[self commonInitWithPhotos:nil initialPhoto:nil];
[self commonInitWithPhotos:nil initialPhoto:nil delegate:nil];
}

return self;
Expand Down Expand Up @@ -156,21 +156,27 @@ - (void)dismissViewControllerAnimated:(BOOL)animated completion:(void (^)(void))
#pragma mark - NYTPhotosViewController

- (instancetype)initWithPhotos:(NSArray *)photos {
return [self initWithPhotos:photos initialPhoto:photos.firstObject];
return [self initWithPhotos:photos initialPhoto:photos.firstObject delegate:nil];
}

- (instancetype)initWithPhotos:(NSArray *)photos initialPhoto:(id <NYTPhoto>)initialPhoto {
return [self initWithPhotos:photos initialPhoto:initialPhoto delegate:nil];
}

- (instancetype)initWithPhotos:(NSArray *)photos initialPhoto:(id <NYTPhoto>)initialPhoto delegate:(id<NYTPhotosViewControllerDelegate>)delegate {
self = [super initWithNibName:nil bundle:nil];

if (self) {
[self commonInitWithPhotos:photos initialPhoto:initialPhoto];
[self commonInitWithPhotos:photos initialPhoto:initialPhoto delegate:delegate];
}

return self;
}

- (void)commonInitWithPhotos:(NSArray *)photos initialPhoto:(id <NYTPhoto>)initialPhoto {
- (void)commonInitWithPhotos:(NSArray *)photos initialPhoto:(id <NYTPhoto>)initialPhoto delegate:(id<NYTPhotosViewControllerDelegate>)delegate {
_dataSource = [[NYTPhotosDataSource alloc] initWithPhotos:photos];
_delegate = delegate;

_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPanWithGestureRecognizer:)];
_singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSingleTapWithGestureRecognizer:)];

Expand Down