Skip to content

Commit

Permalink
Refactor table and collection views pull to refresh and infinite scro…
Browse files Browse the repository at this point in the history
…lling
  • Loading branch information
grigaci committed Sep 9, 2015
1 parent bddc2c7 commit 8204355
Show file tree
Hide file tree
Showing 103 changed files with 3,007 additions and 3,207 deletions.
90 changes: 45 additions & 45 deletions BIObjCHelpers.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
BlueprintName = "BIObjCHelpersTests"
ReferencedContainer = "container:BIObjCHelpers.xcodeproj">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "BIActivityIndicatorContainerViewTestCase">
</Test>
<Test
Identifier = "BITableViewCellTestCase">
</Test>
</SkippedTests>
</TestableReference>
</Testables>
<MacroExpansion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ FOUNDATION_EXPORT const NSInteger kDefaultBatchSize;

typedef void(^BIBatchCompletionBlock)(NSError * __nullable error, NSArray * __nullable newIndexPaths);

@interface BIBatch : NSObject
@interface BIBatch : NSObject// __deprecated_msg("Use BIBatchRequest")

/*!
* @brief Designated initializer for a table view batch.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// BIBatchHelper.h
// BIBatchHelpers.h
// BIObjCHelpers
//
// Created by Mihai Chifor on 8/31/15.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//
// BIBatchHelper.m
// BIBatchHelpers.m
// BIObjCHelpers
//
// Created by Mihai Chifor on 8/31/15.
// Copyright (c) 2015 iGama Apps. All rights reserved.
//

#import "BIBatchHelper.h"
#import "BIBatchHelpers.h"

BOOL BIDisplayShouldFetchBatch(BIScrollDirection scrollDirection,
CGRect bounds,
Expand Down
75 changes: 75 additions & 0 deletions BIObjCHelpers/Batch/BIBatchRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// BIBatchRequest.h
// BIObjCHelpers
//
// Created by Bogdan Iusco on 08/09/15.
// Copyright (c) 2015 iGama Apps. All rights reserved.
//

#import <Foundation/Foundation.h>

FOUNDATION_EXPORT const NSInteger kDefaultBatchRequestSize;

@class BIBatchResponse;

typedef void(^BIBatchRequestCompletionBlock)(BIBatchResponse * __nonnull response);

/*!
@brief Helper values for batch insert position.
*/
typedef NS_ENUM(NSUInteger, BIBatchInsertPosition) {
/*!
* @brief insert new elements at the beggining.
*/
BIBatchInsertPositionTop = 0,
/*!
* @brief insert new elements at the end.
*/
BIBatchInsertPositionBottom = UINT_MAX
};

/*!
* @brief Defines a set of data request values.
* Mostly used in table and collection views for inserting sets of data.
*/
@interface BIBatchRequest : NSObject

/*!
* @brief Designated initializer for a table view batch.
* @param sectionIndex The index of the section where to fetch data.
* @param batchSize The size of the batch to fetch.
* @param completionBlock Code block to be called when fetching is done or in case of error.
*/
- (nonnull instancetype)initWithSection:(NSUInteger)sectionIndex
batchSize:(NSUInteger)batchSize
completionBlock:(nullable BIBatchRequestCompletionBlock)completionBlock NS_DESIGNATED_INITIALIZER;

/*!
* @brief Convenience kDefaultTableViewBatchSize for a table view batch.
* @discussion Fetches kCardDefaultBatchSize elements in section index 0.
* @param completionBlock Code block to be called when fetching is done or in case of error.
*/
- (nonnull instancetype)initWithCompletionBlock:(nullable BIBatchRequestCompletionBlock)completionBlock;

/*!
* @brief Size of the batch that is fetching.
*/
@property (nonatomic, assign, readonly) NSUInteger batchSize;

/*!
* @brief Section index for which data is fetching.
*/
@property (nonatomic, assign, readonly) NSUInteger sectionIndex;

/*!
* @brief Code block to be called when fetching is done or in case of error.
*/
@property (nonatomic, copy, nullable, readonly) BIBatchRequestCompletionBlock completionBlock;

/*!
* @brief Specify an index from where to start inserting the new elements that will be fetched.
* Defaults to BIBatchInsertPositionBottom;
*/
@property (nonatomic, assign) NSUInteger insertPosition;

@end
45 changes: 45 additions & 0 deletions BIObjCHelpers/Batch/BIBatchRequest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// BIBatchRequest.m
// BIObjCHelpers
//
// Created by Bogdan Iusco on 08/09/15.
// Copyright (c) 2015 iGama Apps. All rights reserved.
//

#import "BIBatchRequest.h"

const NSInteger kDefaultBatchRequestSize = 3;

@interface BIBatchRequest ()

@property (nonatomic, assign, readwrite) NSUInteger batchSize;
@property (nonatomic, assign, readwrite) NSUInteger sectionIndex;
@property (nonatomic, copy, nullable, readwrite) BIBatchRequestCompletionBlock completionBlock;

@end


@implementation BIBatchRequest

#pragma mark - Init methods

- (instancetype)initWithSection:(NSUInteger)sectionIndex
batchSize:(NSUInteger)batchSize
completionBlock:(BIBatchRequestCompletionBlock)completionBlock {
self = [super init];
if (self) {
self.sectionIndex = sectionIndex;
self.batchSize = batchSize;
self.completionBlock = completionBlock;
self.insertPosition = BIBatchInsertPositionBottom;
}
return self;
}

- (instancetype)initWithCompletionBlock:(BIBatchRequestCompletionBlock)completionBlock {
return [self initWithSection:0
batchSize:kDefaultBatchRequestSize
completionBlock:completionBlock];
}

@end
46 changes: 46 additions & 0 deletions BIObjCHelpers/Batch/BIBatchResponse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// BIBatchResponse.h
// BIObjCHelpers
//
// Created by Bogdan Iusco on 08/09/15.
// Copyright (c) 2015 iGama Apps. All rights reserved.
//

#import <UIKit/UIKit.h>

@class BIBatchRequest;

@interface BIBatchResponse : NSObject

+ (nonnull instancetype)batchResponseForRequest:(nonnull BIBatchRequest *)batch
error:(nullable NSError *)error
newIndexPaths:(nullable NSArray *)newIndexPaths;

- (nonnull instancetype)initWithBatchRequest:(nonnull BIBatchRequest *)batch
error:(nullable NSError *)error
newIndexPaths:(nullable NSArray *)newIndexPaths;

- (nonnull instancetype)init NS_UNAVAILABLE;
+ (nonnull instancetype)new NS_UNAVAILABLE;

@property (nonatomic, strong, nullable, readonly) NSError *error;
@property (nonatomic, copy, nullable, readonly) NSArray *indexPaths;
@property (nonatomic, strong, nullable, readonly) BIBatchRequest *batchRequest;

@end

@interface BIBatchResponse (UITableView)

- (nonnull instancetype)initWithBatchRequest:(nonnull BIBatchRequest *)batch
tableView:(nonnull UITableView *)tableView
countNewItems:(NSUInteger)countNewItems;

@end

@interface BIBatchResponse (UICollectionView)

- (nonnull instancetype)initWithBatchRequest:(nonnull BIBatchRequest *)batch
collectionView:(nonnull UICollectionView *)collectionView
countNewItems:(NSUInteger)countNewItems;

@end
100 changes: 100 additions & 0 deletions BIObjCHelpers/Batch/BIBatchResponse.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//
// BIBatchResponse.m
// BIObjCHelpers
//
// Created by Bogdan Iusco on 08/09/15.
// Copyright (c) 2015 iGama Apps. All rights reserved.
//

#import "BIBatchResponse.h"
#import "BIBatchRequest.h"

@interface BIBatchResponse ()

@property (nonatomic, strong, nullable, readwrite) NSError *error;
@property (nonatomic, copy, nullable, readwrite) NSArray *indexPaths;
@property (nonatomic, strong, nullable, readwrite) BIBatchRequest *batchRequest;

@end

@implementation BIBatchResponse

#pragma mark - Factory methods

+ (nonnull instancetype)batchResponseForRequest:(nonnull BIBatchRequest *)batch
error:(nullable NSError *)error
newIndexPaths:(nullable NSArray *)newIndexPaths {
return [[self alloc] initWithBatchRequest:batch
error:error
newIndexPaths:newIndexPaths];
}

#pragma mark - Init methods

- (nonnull instancetype)initWithBatchRequest:(nonnull BIBatchRequest *)batch
error:(nullable NSError *)error
newIndexPaths:(nullable NSArray *)newIndexPaths {
self = [super init];
if (self) {
self.batchRequest = batch;
self.error = error;
self.indexPaths = newIndexPaths;
}
return self;
}

- (nonnull instancetype)initWithBatchRequest:(nonnull BIBatchRequest *)batchRequest
tableView:(nonnull UITableView *)tableView
countNewItems:(NSUInteger)countNewItems {
NSUInteger sectionIndex = batchRequest.sectionIndex;
NSUInteger countSectionItems = [tableView numberOfRowsInSection:sectionIndex];
self = [self initWithBatchRequest:batchRequest
countSectionItems:countSectionItems
countNewItems:countNewItems];
return self;
}

- (nonnull instancetype)initWithBatchRequest:(nonnull BIBatchRequest *)batchRequest
collectionView:(nonnull UICollectionView *)collectionView
countNewItems:(NSUInteger)countNewItems {
NSUInteger sectionIndex = batchRequest.sectionIndex;
NSUInteger countSectionItems = [collectionView numberOfItemsInSection:sectionIndex];
self = [self initWithBatchRequest:batchRequest
countSectionItems:countSectionItems
countNewItems:countNewItems];
return self;
}

- (nonnull instancetype)initWithBatchRequest:(nonnull BIBatchRequest *)batchRequest
countSectionItems:(NSUInteger)countSectionItems
countNewItems:(NSUInteger)countNewItems {
NSUInteger sectionIndex = batchRequest.sectionIndex;
NSUInteger newRowIndex = 0;
switch (batchRequest.insertPosition) {
case BIBatchInsertPositionTop:
newRowIndex = 0;
break;
case BIBatchInsertPositionBottom:
newRowIndex = countSectionItems;
break;
default:
newRowIndex = batchRequest.insertPosition;
break;
}

NSUInteger lastRowIndex = newRowIndex + countNewItems;
NSMutableArray *mutableArray = [NSMutableArray new];
for (NSUInteger index = newRowIndex; index < lastRowIndex; index++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:sectionIndex];
[mutableArray addObject:indexPath];
}

self = [self initWithBatchRequest:batchRequest
error:nil
newIndexPaths:[mutableArray copy]];

return self;
}

@end

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "BIDatasourceCollectionView.h"
#import "_BICollectionView+Internal.h"

@interface BIDatasourceCollectionView ()

Expand All @@ -28,6 +29,10 @@ - (instancetype)initWithCollectionView:(UICollectionView *)collectionView {
if (self) {
self.collectionView = collectionView;
self.collectionView.dataSource = self;
if ([self.collectionView isKindOfClass:[BICollectionView class]]) {
BICollectionView *biCollectionView = (BICollectionView *)self.collectionView;
biCollectionView.datasource = self;
}
}
return self;
}
Expand Down
Loading

0 comments on commit 8204355

Please sign in to comment.