diff --git a/BIObjCHelpers/Batch/BIBatchRequest.h b/BIObjCHelpers/Batch/BIBatchRequest.h index e5738e1..ad54ef9 100644 --- a/BIObjCHelpers/Batch/BIBatchRequest.h +++ b/BIObjCHelpers/Batch/BIBatchRequest.h @@ -11,6 +11,7 @@ FOUNDATION_EXPORT const NSInteger kDefaultBatchRequestSize; @class BIBatchResponse; +@class BIMutableBatchRequest; typedef void(^BIBatchRequestCompletionBlock)(BIBatchResponse * __nonnull response); @@ -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 - (nonnull instancetype)init NS_UNAVAILABLE; + (nonnull instancetype)new NS_UNAVAILABLE; @@ -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 diff --git a/BIObjCHelpers/Batch/BIBatchRequest.m b/BIObjCHelpers/Batch/BIBatchRequest.m index 9128cab..6a2f4ab 100644 --- a/BIObjCHelpers/Batch/BIBatchRequest.m +++ b/BIObjCHelpers/Batch/BIBatchRequest.m @@ -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 @@ -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 \ No newline at end of file diff --git a/BIObjCHelpersTests/Tests/Batch/BIBatchRequestTestCase.m b/BIObjCHelpersTests/Tests/Batch/BIBatchRequestTestCase.m index a0a9f42..2fe8a07 100644 --- a/BIObjCHelpersTests/Tests/Batch/BIBatchRequestTestCase.m +++ b/BIObjCHelpersTests/Tests/Batch/BIBatchRequestTestCase.m @@ -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 diff --git a/Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/BIObjCHelpers.xcscheme b/Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/BIObjCHelpers.xcscheme index 38d1dbe..af6c9ef 100644 --- a/Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/BIObjCHelpers.xcscheme +++ b/Example/Pods/Pods.xcodeproj/xcshareddata/xcschemes/BIObjCHelpers.xcscheme @@ -14,7 +14,7 @@ buildForArchiving = "YES">