Skip to content

Commit

Permalink
Add mutable version for a batch request
Browse files Browse the repository at this point in the history
  • Loading branch information
grigaci committed Nov 18, 2015
1 parent 531055c commit d3bd9f8
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 3 deletions.
48 changes: 46 additions & 2 deletions BIObjCHelpers/Batch/BIBatchRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
FOUNDATION_EXPORT const NSInteger kDefaultBatchRequestSize;

@class BIBatchResponse;
@class BIMutableBatchRequest;

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

Expand All @@ -32,7 +33,7 @@ typedef NS_ENUM(NSUInteger, BIBatchInsertPosition) {
* @brief Defines a set of data request values.
* Mostly used in table and collection views for inserting sets of data.
*/
@interface BIBatchRequest : NSObject
@interface BIBatchRequest : NSObject <NSCopying, NSMutableCopying>

- (nonnull instancetype)init NS_UNAVAILABLE;
+ (nonnull instancetype)new NS_UNAVAILABLE;
Expand Down Expand Up @@ -73,6 +74,49 @@ typedef NS_ENUM(NSUInteger, BIBatchInsertPosition) {
* @brief Specify an index from where to start inserting the new elements that will be fetched.
* Defaults to BIBatchInsertPositionBottom;
*/
@property (nonatomic, assign) NSUInteger insertPosition;
@property (nonatomic, assign, readwrite) NSUInteger insertPosition;

/*!
* @brief Additional flags that can be set for a batch request.
*/
@property (nonatomic, assign, readonly) NSUInteger options;

/*!
* @brief Overriden method for returning the exact class type for a copied object.
*/
- (nonnull BIBatchRequest *)copy;

/*!
* @brief Overriden method for returning the exact class type for a mutable copy.
*/
- (nonnull BIMutableBatchRequest *)mutableCopy;

@end


/*!
* Mutable version of a batch request.
*/
@interface BIMutableBatchRequest : BIBatchRequest

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

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

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

/*!
* @brief Additional flags that can be set for a batch request.
*/
@property (nonatomic, assign, readwrite) NSUInteger options;

@end
51 changes: 51 additions & 0 deletions BIObjCHelpers/Batch/BIBatchRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @interface BIBatchRequest ()
@property (nonatomic, assign, readwrite) NSUInteger batchSize;
@property (nonatomic, assign, readwrite) NSUInteger sectionIndex;
@property (nonatomic, copy, nullable, readwrite) BIBatchRequestCompletionBlock completionBlock;
@property (nonatomic, assign, readwrite) NSUInteger options;

@end

Expand Down Expand Up @@ -42,4 +43,54 @@ - (instancetype)initWithCompletionBlock:(BIBatchRequestCompletionBlock)completio
completionBlock:completionBlock];
}

#pragma mark - NSCopying methods

- (id)copyWithZone:(nullable NSZone *)zone {
BIBatchRequest *copy = [[BIBatchRequest allocWithZone:zone] initWithCompletionBlock:self.completionBlock];
copy.batchSize = self.batchSize;
copy.sectionIndex = self.sectionIndex;
copy.insertPosition = self.insertPosition;
copy.options = self.options;
return copy;
}

#pragma mark - NSMutableCopying methods

- (id)mutableCopyWithZone:(nullable NSZone *)zone {
BIMutableBatchRequest *mutableCopy = [[BIMutableBatchRequest allocWithZone:zone] initWithCompletionBlock:self.completionBlock];
mutableCopy.batchSize = self.batchSize;
mutableCopy.sectionIndex = self.sectionIndex;
mutableCopy.insertPosition = self.insertPosition;
mutableCopy.options = self.options;
return mutableCopy;
}

#pragma mark - NSObject methods

- (nonnull BIBatchRequest *)copy {
return [super copy];
}

- (nonnull BIMutableBatchRequest *)mutableCopy {
return [super mutableCopy];
}

@end

@implementation BIMutableBatchRequest

@dynamic batchSize;
@dynamic sectionIndex;
@dynamic completionBlock;
@dynamic options;

#pragma mark - Init methods

- (nonnull instancetype)initWithSection:(NSUInteger)sectionIndex
batchSize:(NSUInteger)batchSize
completionBlock:(nullable BIBatchRequestCompletionBlock)completionBlock {
self = [super initWithSection:sectionIndex batchSize:batchSize completionBlock:completionBlock];
return self;
}

@end
29 changes: 29 additions & 0 deletions BIObjCHelpersTests/Tests/Batch/BIBatchRequestTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,33 @@ - (void)testProperties {
XCTAssertTrue(wasCalled);
}

#pragma mark - Test copy

- (void)test_copy {
BIBatchRequest *request1 = [[BIBatchRequest alloc] initWithSection:1
batchSize:23
completionBlock:nil];
BIBatchRequest *request2 = [request1 copy];
XCTAssertEqual(request1.sectionIndex, request2.sectionIndex);
XCTAssertEqual(request1.batchSize, request2.batchSize);
XCTAssertEqual(request1.insertPosition, request2.insertPosition);
XCTAssertEqual(request1.options, request2.options);
XCTAssertTrue([request2 isKindOfClass:[BIBatchRequest class]]);
}


#pragma mark - Test copy

- (void)test_mutableCopy {
BIBatchRequest *request1 = [[BIBatchRequest alloc] initWithSection:0
batchSize:19
completionBlock:nil];
BIMutableBatchRequest *request2 = [request1 mutableCopy];
XCTAssertEqual(request1.sectionIndex, request2.sectionIndex);
XCTAssertEqual(request1.batchSize, request2.batchSize);
XCTAssertEqual(request1.insertPosition, request2.insertPosition);
XCTAssertEqual(request1.options, request2.options);
XCTAssertTrue([request2 isKindOfClass:[BIMutableBatchRequest class]]);
}

@end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d3bd9f8

Please sign in to comment.