ASIProgressQueue.m
3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// ASIProgressQueue.m
// asi-http-request
//
// Created by Ben Copsey on 07/11/2008.
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
#import "ASIProgressQueue.h"
#import "ASIHTTPRequest.h"
@implementation ASIProgressQueue
- (id)init
{
self = [super init];
uploadProgressDelegate = nil;
uploadProgressBytes = 0;
uploadProgressTotalBytes = 0;
downloadProgressDelegate = nil;
downloadProgressBytes = 0;
downloadProgressTotalBytes = 0;
return self;
}
- (void)cancelAllOperations
{
uploadProgressBytes = 0;
uploadProgressTotalBytes = 0;
downloadProgressBytes = 0;
downloadProgressTotalBytes = 0;
[super cancelAllOperations];
}
- (void)setUploadProgressDelegate:(id)newDelegate
{
uploadProgressDelegate = newDelegate;
SEL selector = @selector(setMaxValue:);
if ([uploadProgressDelegate respondsToSelector:selector]) {
double max = 1.0;
NSMethodSignature *signature = [[uploadProgressDelegate class] instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
[invocation setArgument:&max atIndex:2];
[invocation invokeWithTarget:uploadProgressDelegate];
}
}
- (void)setDownloadProgressDelegate:(id)newDelegate
{
downloadProgressDelegate = newDelegate;
SEL selector = @selector(setMaxValue:);
if ([downloadProgressDelegate respondsToSelector:selector]) {
double max = 1.0;
NSMethodSignature *signature = [[downloadProgressDelegate class] instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:@selector(setMaxValue:)];
[invocation setArgument:&max atIndex:2];
[invocation invokeWithTarget:downloadProgressDelegate];
}
}
- (void)addOperation:(NSOperation *)operation
{
if ([operation isKindOfClass:[ASIHTTPRequest class]]) {
[(ASIHTTPRequest *)operation setUploadProgressDelegate:self];
[(ASIHTTPRequest *)operation setDownloadProgressDelegate:self];
}
[super addOperation:operation];
}
- (void)incrementUploadSizeBy:(int)bytes
{
if (!uploadProgressDelegate) {
return;
}
uploadProgressTotalBytes += bytes;
[self incrementUploadProgressBy:0];
}
- (void)incrementUploadProgressBy:(int)bytes
{
if (!uploadProgressDelegate || uploadProgressTotalBytes == 0) {
return;
}
uploadProgressBytes += bytes;
double progress = (uploadProgressBytes*1.0)/(uploadProgressTotalBytes*1.0);
[ASIHTTPRequest setProgress:progress forProgressIndicator:uploadProgressDelegate];
}
- (void)incrementDownloadSizeBy:(int)bytes
{
if (!downloadProgressDelegate) {
return;
}
downloadProgressTotalBytes += bytes;
[self incrementDownloadProgressBy:0];
}
- (void)incrementDownloadProgressBy:(int)bytes
{
if (!downloadProgressDelegate || downloadProgressTotalBytes == 0) {
return;
}
downloadProgressBytes += bytes;
double progress = (downloadProgressBytes*1.0)/(downloadProgressTotalBytes*1.0);
[ASIHTTPRequest setProgress:progress forProgressIndicator:downloadProgressDelegate];
}
@synthesize uploadProgressDelegate;
@synthesize downloadProgressDelegate;
@end