Skip to content

Commit

Permalink
Merge pull request #108 from Shopify/feature/collection-pagination
Browse files Browse the repository at this point in the history
Pagination Support for Collections
  • Loading branch information
gabrieloc committed Mar 16, 2016
2 parents b668e82 + 63e49cc commit 75a407d
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 520 deletions.
51 changes: 51 additions & 0 deletions Mobile Buy SDK/Mobile Buy SDK Tests/BUYClientTest_Storefront.m
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,57 @@ - (void)testCollections
}];
}

- (void)testCollectionsFromFirstPage
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetCollection_0"];
}];

XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getCollectionsPage:1 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) {

XCTAssertEqual(1, page);

XCTAssertNotNil((collections));
XCTAssertNil(error);

XCTAssertNotNil([collections.firstObject title]);
XCTAssertNotNil([collections.firstObject handle]);
XCTAssertNotNil([collections.firstObject collectionId]);

[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) {
XCTAssertNil(error);
}];
}

- (void)testCollectionsFromEmptyPage
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetOutOfIndexCollectionPage_0"];
}];

XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getCollectionsPage:999 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) {

XCTAssertEqual(999, page);

XCTAssertNotNil((collections));
XCTAssert(collections.count == 0);
XCTAssertNil(error);

[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:^(NSError *error) {
XCTAssertNil(error);
}];
}

- (void)testProductsInCollection
{
if (self.collection == nil) {
Expand Down
624 changes: 112 additions & 512 deletions Mobile Buy SDK/Mobile Buy SDK Tests/mocked_responses.json

Large diffs are not rendered by default.

30 changes: 25 additions & 5 deletions Mobile Buy SDK/Mobile Buy SDK/Data/BUYClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ typedef void (^BUYDataShippingRatesBlock)(NSArray *shippingRates, BUYStatus stat
*/
typedef void (^BUYDataShopBlock)(BUYShop *shop, NSError *error);

/**
* Return block containing a list of BUYCollection objects and/or an NSError
*
* @param collections An array of BUYCollection objects
* @param error Optional NSError
*/
typedef void (^BUYDataCollectionsBlock)(NSArray<BUYCollection *> *collections, NSError *error);

/**
* Return block containing a BUYProduct and/or an NSError
*
Expand All @@ -165,15 +173,17 @@ typedef void (^BUYDataProductBlock)(BUYProduct *product, NSError *error);
* @param products An array of BUYProduct objects
* @param error Optional NSError
*/
typedef void (^BUYDataProductsBlock)(NSArray *products, NSError *error);
typedef void (^BUYDataProductsBlock)(NSArray<BUYProduct *> *products, NSError *error);

/**
* Return block containing list of collections
* Return block containing a list of BUYCollection objects
*
* @param collections An array of BUYCollection objects
* @param error Optional NSError
* @param page Index of the page requested
* @param reachedEnd Boolean indicating whether additional pages exist
* @param error An optional NSError
*/
typedef void (^BUYDataCollectionsBlock)(NSArray *collections, NSError *error);
typedef void (^BUYDataCollectionsListBlock)(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error);

/**
* Return block containing a list of BUYProduct objects, the page requested, a boolean to determine whether the end of the list has been reach and/or an optional NSError
Expand All @@ -183,7 +193,7 @@ typedef void (^BUYDataCollectionsBlock)(NSArray *collections, NSError *error);
* @param reachedEnd Boolean indicating whether additional pages exist
* @param error An optional NSError
*/
typedef void (^BUYDataProductListBlock)(NSArray *products, NSUInteger page, BOOL reachedEnd, NSError *error);
typedef void (^BUYDataProductListBlock)(NSArray<BUYProduct *> *products, NSUInteger page, BOOL reachedEnd, NSError *error);

/**
* Return block containing a list of BUYProductImage objects and/or an NSError
Expand Down Expand Up @@ -313,6 +323,16 @@ typedef void (^BUYDataGiftCardBlock)(BUYGiftCard *giftCard, NSError *error);
*/
- (NSURLSessionDataTask *)getCollections:(BUYDataCollectionsBlock)block;

/**
* Fetches collections based off page
*
* @param page Index of the page requested
* @param block (^BUYDataCollectionsBlock)(NSArray *collections, NSError *error)
*
* @return The associated NSURLSessionDataTask
*/
- (NSURLSessionDataTask *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block;

/**
* Fetches the products in the given collection with the collection's
* default sort order set in the shop's admin
Expand Down
14 changes: 11 additions & 3 deletions Mobile Buy SDK/Mobile Buy SDK/Data/BUYClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,23 @@ - (NSURLSessionDataTask *)getProductsByIds:(NSArray *)productIds completion:(BUY

- (NSURLSessionDataTask *)getCollections:(BUYDataCollectionsBlock)block
{
NSURLComponents *components = [self URLComponentsForChannelsAppendingPath:kBUYClientPathCollectionPublications queryItems:nil];

return [self getCollectionsPage:1 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) {
block(collections, error);
}];
}

- (NSURLSessionDataTask *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block
{
NSURLComponents *components = [self URLComponentsForChannelsAppendingPath:kBUYClientPathCollectionPublications
queryItems:@{@"limit" : [NSString stringWithFormat:@"%lu", (unsigned long)self.pageSize],
@"page" : [NSString stringWithFormat:@"%lu", (unsigned long)page]}];
return [self getRequestForURL:components.URL completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {

NSArray *collections = nil;
if (json && error == nil) {
collections = [BUYCollection convertJSONArray:json[kBUYClientPathCollectionPublications]];
}
block(collections, error);
block(collections, page, [self hasReachedEndOfPage:collections], error);
}];
}

Expand Down

0 comments on commit 75a407d

Please sign in to comment.