-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a method to track progress using a well defined protocol
- Loading branch information
Showing
9 changed files
with
282 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// DKAPIProgress.h | ||
// DKAPIRequest | ||
// | ||
// Created by Keith Pitt on 23/08/11. | ||
// Copyright 2011 Mostly Disco. All rights reserved. | ||
// | ||
|
||
#import "DKAPIProgressProtocol.h" | ||
|
||
typedef enum { | ||
|
||
DKAPIProgressDownload, | ||
|
||
DKAPIProgressUpload | ||
|
||
} DKAPIProgressMethod; | ||
|
||
@interface DKAPIProgress : NSObject | ||
|
||
@property (nonatomic, retain) id <DKAPIProgressProtocol> delegate; | ||
|
||
@property (nonatomic, retain) DKAPIRequest * apiRequest; | ||
|
||
@property (nonatomic) DKAPIProgressMethod progressMethod; | ||
|
||
- (id)initWithDelegate:(id <DKAPIProgressProtocol>)progressDelegate progressMethod:(DKAPIProgressMethod)method apiRequest:(DKAPIRequest *)request; | ||
|
||
- (void)setProgress:(float)progress; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// DKAPIProgress.m | ||
// DKAPIRequest | ||
// | ||
// Created by Keith Pitt on 23/08/11. | ||
// Copyright 2011 Mostly Disco. All rights reserved. | ||
// | ||
|
||
#import "DKAPIProgress.h" | ||
|
||
@implementation DKAPIProgress | ||
|
||
@synthesize delegate, progressMethod, apiRequest; | ||
|
||
- (id)initWithDelegate:(id <DKAPIProgressProtocol>)progressDelegate progressMethod:(DKAPIProgressMethod)method apiRequest:(DKAPIRequest *)request { | ||
|
||
if ((self = [super init])) { | ||
self.delegate = progressDelegate; | ||
self.progressMethod = method; | ||
self.apiRequest = request; | ||
} | ||
|
||
return self; | ||
|
||
} | ||
|
||
- (void)setProgress:(float)progress { | ||
|
||
// Forward the progress float to the correct method on the delegate (if it responds | ||
// to the selector) | ||
|
||
if (progressMethod == DKAPIProgressUpload && [self.delegate respondsToSelector:@selector(apiRequest:uploadProgress:)]) { | ||
|
||
[self.delegate apiRequest:self.apiRequest uploadProgress:progress]; | ||
|
||
} else if (progressMethod == DKAPIProgressDownload && [self.delegate respondsToSelector:@selector(apiRequest:downloadProgress:)]) { | ||
|
||
[self.delegate apiRequest:self.apiRequest downloadProgress:progress]; | ||
|
||
} | ||
|
||
} | ||
|
||
- (void)dealloc { | ||
|
||
self.delegate = nil; | ||
self.apiRequest = nil; | ||
|
||
[super dealloc]; | ||
|
||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// DKAPIProgressProtocol.h | ||
// DKAPIRequest | ||
// | ||
// Created by Keith Pitt on 23/08/11. | ||
// Copyright 2011 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
@class DKAPIRequest; | ||
|
||
@protocol DKAPIProgressProtocol <NSObject> | ||
|
||
@optional | ||
|
||
- (void)apiRequest:(DKAPIRequest *)apiRequest downloadProgress:(float)progress; | ||
- (void)apiRequest:(DKAPIRequest *)apiRequest uploadProgress:(float)progress; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// DKAPIProgressSpec.m | ||
// DKAPIRequest | ||
// | ||
// Created by Keith Pitt on 22/08/11. | ||
// Copyright 2011 Mostly Disco. All rights reserved. | ||
// | ||
|
||
#import "SpecHelper.h" | ||
|
||
#import "DKAPIProgress.h" | ||
#import "DKAPIRequest.h" | ||
|
||
#import "MockProgressDelegate.h" | ||
|
||
SPEC_BEGIN(DKAPIProgressSpec) | ||
|
||
__block DKAPIRequest * request; | ||
__block DKAPIProgress * progresss; | ||
__block id progressDelegate; | ||
|
||
beforeEach(^{ | ||
|
||
request = [DKAPIRequest new]; | ||
progressDelegate = [OCMockObject niceMockForClass:[MockProgressDelegate class]]; | ||
progresss = [[DKAPIProgress alloc] initWithDelegate:progressDelegate progressMethod:DKAPIProgressUpload apiRequest:request]; | ||
|
||
}); | ||
|
||
describe(@"- (id)initWithDelegate:progressMethod:apiRequest:", ^{ | ||
|
||
it(@"should set the appropriate properties", ^{ | ||
|
||
expect(progresss.delegate).toEqual(progressDelegate); | ||
expect(progresss.apiRequest).toEqual(request); | ||
expect(progresss.progressMethod).toEqual(DKAPIProgressUpload); | ||
|
||
}); | ||
|
||
}); | ||
|
||
describe(@"- (void)setProgress:(float)progress;", ^{ | ||
|
||
it(@"should forward to the upload method correctly", ^{ | ||
|
||
progresss.progressMethod = DKAPIProgressUpload; | ||
|
||
[[progressDelegate expect] apiRequest:request uploadProgress:0.75]; | ||
|
||
[progresss setProgress:0.75]; | ||
|
||
}); | ||
|
||
it(@"should forward to the download method correctly", ^{ | ||
|
||
progresss.progressMethod = DKAPIProgressDownload; | ||
|
||
[[progressDelegate expect] apiRequest:request downloadProgress:0.75]; | ||
|
||
[progresss setProgress:0.75]; | ||
|
||
}); | ||
|
||
}); | ||
|
||
SPEC_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// MockProgressDelegate.h | ||
// DKAPIRequest | ||
// | ||
// Created by Keith Pitt on 23/08/11. | ||
// Copyright 2011 __MyCompanyName__. All rights reserved. | ||
// | ||
|
||
#import "DKAPIProgressProtocol.h" | ||
|
||
@interface MockProgressDelegate : NSObject <DKAPIProgressProtocol> | ||
|
||
@end |
Oops, something went wrong.