Commit c4ffccce by Dima Bart

Convert BUYStatusOperation to return a BUYStatus.

parent fe599067
......@@ -155,7 +155,9 @@
- (NSOperation *)pollCompletionStatusAndGetCheckoutWithToken:(NSString *)token start:(BOOL)start completion:(BUYDataCheckoutBlock)block
{
BUYStatusOperation *operation = [BUYStatusOperation operationWithClient:self checkoutToken:token completion:block];
BUYStatusOperation *operation = [BUYStatusOperation operationWithClient:self checkoutToken:token completion:^(BUYStatus status, BUYCheckout *checkout, NSError *error) {
block(checkout, error);
}];
if (start) {
[self startOperation:operation];
}
......
......@@ -33,6 +33,8 @@ NS_ASSUME_NONNULL_BEGIN
@protocol BUYPaymentToken;
typedef void (^BUYCheckoutOperationCompletion)(BUYCheckout * _Nullable checkout, NSError * _Nullable error);
@interface BUYCheckoutOperation : BUYGroupOperation
+ (instancetype)operationWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken token:(id<BUYPaymentToken>)token completion:(BUYCheckoutOperationCompletion)completion;
......
......@@ -25,17 +25,17 @@
//
#import "BUYGroupOperation.h"
#import "BUYClient.h"
NS_ASSUME_NONNULL_BEGIN
@class BUYCheckout;
@class BUYClient;
typedef void (^BUYCheckoutOperationCompletion)(BUYCheckout * _Nullable checkout, NSError * _Nullable error);
typedef void (^BUYCheckoutStatusOperationCompletion)(BUYStatus status, BUYCheckout * _Nullable checkout, NSError * _Nullable error);
@interface BUYStatusOperation : BUYGroupOperation
+ (instancetype)operationWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken completion:(BUYCheckoutOperationCompletion)completion;
- (instancetype)initWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken completion:(BUYCheckoutOperationCompletion)completion;
+ (instancetype)operationWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken completion:(BUYCheckoutStatusOperationCompletion)completion;
- (instancetype)initWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken completion:(BUYCheckoutStatusOperationCompletion)completion;
@end
......
......@@ -33,7 +33,9 @@
@property (strong, nonatomic, readonly) BUYClient *client;
@property (strong, nonatomic, readonly) NSString *checkoutToken;
@property (strong, nonatomic, readonly) BUYCheckoutOperationCompletion completion;
@property (strong, nonatomic, readonly) BUYCheckoutStatusOperationCompletion completion;
- (void)finishWithError:(NSError *)error NS_UNAVAILABLE;
@end
......@@ -41,12 +43,12 @@
#pragma mark - Init -
+ (instancetype)operationWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken completion:(BUYCheckoutOperationCompletion)completion
+ (instancetype)operationWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken completion:(BUYCheckoutStatusOperationCompletion)completion
{
return [[[self class] alloc] initWithClient:client checkoutToken:checkoutToken completion:completion];
}
- (instancetype)initWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken completion:(BUYCheckoutOperationCompletion)completion
- (instancetype)initWithClient:(BUYClient *)client checkoutToken:(NSString *)checkoutToken completion:(BUYCheckoutStatusOperationCompletion)completion
{
self = [super initWithRequestQueue:client.requestQueue operations:nil];
if (self) {
......@@ -82,17 +84,17 @@
if (self.cancelled) {
return;
}
self.completion(object, nil);
self.completion(BUYStatusComplete, object, nil);
}
- (void)finishWithError:(NSError *)error
- (void)finishWithStatus:(BUYStatus)status error:(NSError *)error
{
[super finishWithError:error];
if (self.cancelled) {
return;
}
self.completion(nil, error);
self.completion(status, nil, error);
}
#pragma mark - Operations -
......@@ -101,7 +103,7 @@
{
BUYRequestOperation *operation = (BUYRequestOperation *)[self.client getCompletionStatusOfCheckoutWithToken:self.checkoutToken start:NO completion:^(BUYStatus status, NSError *error) {
if (status != BUYStatusComplete) {
[self finishWithError:error];
[self finishWithStatus:status error:error];
}
}];
operation.pollingHandler = ^BOOL (NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
......@@ -117,7 +119,7 @@
if (checkout) {
[self finishWithObject:checkout];
} else {
[self finishWithError:error];
[self finishWithStatus:BUYStatusComplete error:error]; // Assumed because the polling operation has to success for us to get here
}
}];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment