forked from pokeb/asi-http-request
/
QueueViewController.m
276 lines (227 loc) · 9.6 KB
/
QueueViewController.m
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// QueueViewController.m
// Part of the ASIHTTPRequest sample project - see http://allseeing-i.com/ASIHTTPRequest for details
//
// Created by Ben Copsey on 07/11/2008.
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
#import "QueueViewController.h"
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
#import "InfoCell.h"
#import "ToggleCell.h"
@implementation QueueViewController
- (IBAction)fetchThreeImages:(id)sender
{
[imageView1 setImage:nil];
[imageView2 setImage:nil];
[imageView3 setImage:nil];
if (!networkQueue) {
networkQueue = [[ASINetworkQueue alloc] init];
}
failed = NO;
[networkQueue reset];
[networkQueue setDownloadProgressDelegate:progressIndicator];
[networkQueue setRequestDidFinishSelector:@selector(imageFetchComplete:)];
[networkQueue setRequestDidFailSelector:@selector(imageFetchFailed:)];
[networkQueue setShowAccurateProgress:[accurateProgress isOn]];
[networkQueue setDelegate:self];
ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/small-image.jpg"]];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]];
[request setDownloadProgressDelegate:imageProgressIndicator1];
[networkQueue addOperation:request];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/medium-image.jpg"]] autorelease];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"2.png"]];
[request setDownloadProgressDelegate:imageProgressIndicator2];
[networkQueue addOperation:request];
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/images/large-image.jpg"]] autorelease];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"3.png"]];
[request setDownloadProgressDelegate:imageProgressIndicator3];
[networkQueue addOperation:request];
[networkQueue go];
}
- (void)imageFetchComplete:(ASIHTTPRequest *)request
{
UIImage *img = [UIImage imageWithContentsOfFile:[request downloadDestinationPath]];
if (img) {
if ([imageView1 image]) {
if ([imageView2 image]) {
[imageView3 setImage:img];
} else {
[imageView2 setImage:img];
}
} else {
[imageView1 setImage:img];
}
}
}
- (void)imageFetchFailed:(ASIHTTPRequest *)request
{
if (!failed) {
if ([[request error] domain] != NetworkRequestErrorDomain || [[request error] code] != ASIRequestCancelledErrorType) {
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Download failed" message:@"Failed to download images" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alertView show];
}
failed = YES;
}
}
- (void)dealloc {
[progressIndicator release];
[networkQueue reset];
[networkQueue release];
[super dealloc];
}
/*
Most of the code below here relates to the table view, and isn't that interesting
*/
- (void)viewDidLoad
{
[[[self navigationBar] topItem] setTitle:@"Using a Queue"];
}
static NSString *intro = @"Demonstrates a fetching 3 items at once, using an ASINetworkQueue to track progress.\r\nEach request has its own downloadProgressDelegate, and the queue has an additional downloadProgressDelegate to track overall progress.";
- (UIView *)tableView:(UITableView *)theTableView viewForHeaderInSection:(NSInteger)section
{
if (section == 1) {
int tablePadding = 40;
int tableWidth = [tableView frame].size.width;
if (tableWidth > 480) { // iPad
tablePadding = 110;
}
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableWidth-(tablePadding/2),30)] autorelease];
UIButton *goButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[goButton setTitle:@"Go!" forState:UIControlStateNormal];
[goButton sizeToFit];
[goButton setFrame:CGRectMake([view frame].size.width-[goButton frame].size.width+10,7,[goButton frame].size.width,[goButton frame].size.height)];
[goButton addTarget:self action:@selector(fetchThreeImages:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:goButton];
if (!progressIndicator) {
progressIndicator = [[UIProgressView alloc] initWithFrame:CGRectMake((tablePadding/2)-10,20,200,10)];
} else {
[progressIndicator setFrame:CGRectMake((tablePadding/2)-10,20,200,10)];
}
[view addSubview:progressIndicator];
return view;
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int tablePadding = 40;
int tableWidth = [tableView frame].size.width;
if (tableWidth > 480) { // iPad
tablePadding = 110;
}
UITableViewCell *cell;
if ([indexPath section] == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"InfoCell"];
if (!cell) {
cell = [InfoCell cell];
}
[[cell textLabel] setText:intro];
[cell layoutSubviews];
} else if ([indexPath section] == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:@"ToggleCell"];
if (!cell) {
cell = [ToggleCell cell];
}
[[cell textLabel] setText:@"Show Accurate Progress"];
accurateProgress = [(ToggleCell *)cell toggle];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"ImagesCell"];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ImagesCell"] autorelease];
imageView1 = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
[imageView1 setBackgroundColor:[UIColor grayColor]];
[cell addSubview:imageView1];
imageProgressIndicator1 = [[[UIProgressView alloc] initWithFrame:CGRectZero] autorelease];
[cell addSubview:imageProgressIndicator1];
imageLabel1 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
if (tableWidth > 480) {
[imageLabel1 setText:@"This image is 15KB in size"];
} else {
[imageLabel1 setText:@"Img size: 15KB"];
}
[imageLabel1 setTextAlignment:UITextAlignmentCenter];
[imageLabel1 setFont:[UIFont systemFontOfSize:11]];
[imageLabel1 setBackgroundColor:[UIColor clearColor]];
[cell addSubview:imageLabel1];
imageView2 = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
[imageView2 setBackgroundColor:[UIColor grayColor]];
[cell addSubview:imageView2];
imageProgressIndicator2 = [[[UIProgressView alloc] initWithFrame:CGRectZero] autorelease];
[cell addSubview:imageProgressIndicator2];
imageLabel2 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
if (tableWidth > 480) {
[imageLabel2 setText:@"This image is 176KB in size"];
} else {
[imageLabel2 setText:@"Img size: 176KB"];
}
[imageLabel2 setTextAlignment:UITextAlignmentCenter];
[imageLabel2 setFont:[UIFont systemFontOfSize:11]];
[imageLabel2 setBackgroundColor:[UIColor clearColor]];
[cell addSubview:imageLabel2];
imageView3 = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
[imageView3 setBackgroundColor:[UIColor grayColor]];
[cell addSubview:imageView3];
imageProgressIndicator3 = [[[UIProgressView alloc] initWithFrame:CGRectZero] autorelease];
[cell addSubview:imageProgressIndicator3];
imageLabel3 = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
if (tableWidth > 480) {
[imageLabel3 setText:@"This image is 1.4MB in size"];
} else {
[imageLabel3 setText:@"Img size: 1.4MB"];
}
[imageLabel3 setTextAlignment:UITextAlignmentCenter];
[imageLabel3 setFont:[UIFont systemFontOfSize:11]];
[imageLabel3 setBackgroundColor:[UIColor clearColor]];
[cell addSubview:imageLabel3];
}
NSUInteger imageWidth = (tableWidth-tablePadding-20)/3;
NSUInteger imageHeight = imageWidth*0.66;
[imageView1 setFrame:CGRectMake(tablePadding/2,10,imageWidth,imageHeight)];
[imageProgressIndicator1 setFrame:CGRectMake(tablePadding/2,15+imageHeight,imageWidth,20)];
[imageLabel1 setFrame:CGRectMake(tablePadding/2,25+imageHeight,imageWidth,20)];
[imageView2 setFrame:CGRectMake((tablePadding/2)+imageWidth+10,10,imageWidth,imageHeight)];
[imageProgressIndicator2 setFrame:CGRectMake((tablePadding/2)+imageWidth+10,15+imageHeight,imageWidth,20)];
[imageLabel2 setFrame:CGRectMake(tablePadding/2+imageWidth+10,25+imageHeight,imageWidth,20)];
[imageView3 setFrame:CGRectMake((tablePadding/2)+(imageWidth*2)+20,10,imageWidth,imageHeight)];
[imageProgressIndicator3 setFrame:CGRectMake((tablePadding/2)+(imageWidth*2)+20,15+imageHeight,imageWidth,20)];
[imageLabel3 setFrame:CGRectMake(tablePadding/2+(imageWidth*2)+20,25+imageHeight,imageWidth,20)];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (CGFloat)tableView:(UITableView *)theTableView heightForHeaderInSection:(NSInteger)section
{
if (section == 1) {
return 50;
}
return 34;
}
- (CGFloat)tableView:(UITableView *)theTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([indexPath section] == 0) {
return [InfoCell neededHeightForDescription:intro withTableWidth:[tableView frame].size.width]+20;
} else if ([indexPath section] == 2) {
int tablePadding = 40;
int tableWidth = [tableView frame].size.width;
if (tableWidth > 480) { // iPad
tablePadding = 110;
}
NSUInteger imageWidth = (tableWidth-tablePadding-20)/3;
NSUInteger imageHeight = imageWidth*0.66;
return imageHeight+50;
} else {
return 42;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
@end