├── BlueBadge.h ├── BlueBadge.m ├── MIT-LICENSE ├── MultipleDownload.h ├── MultipleDownload.m └── README.textile /BlueBadge.h: -------------------------------------------------------------------------------- 1 | // 2 | // BlueBadge.m 3 | // 4 | // Copyright 2008 Stepcase Limited. 5 | // 6 | 7 | #import 8 | 9 | 10 | @interface BlueBadge : UIView { 11 | NSInteger count; 12 | } 13 | 14 | @property (nonatomic) NSInteger count; 15 | 16 | - (void)drawWithCount:(NSInteger)i; 17 | 18 | @end 19 | -------------------------------------------------------------------------------- /BlueBadge.m: -------------------------------------------------------------------------------- 1 | // 2 | // BlueBadge.m 3 | // 4 | // Copyright 2008 Stepcase Limited. 5 | // 6 | 7 | #import "BlueBadge.h" 8 | 9 | 10 | @implementation BlueBadge 11 | 12 | @synthesize count; 13 | 14 | - (id)initWithFrame:(CGRect)frame { 15 | if (self = [super initWithFrame:frame]) { 16 | // Initialization code 17 | [self setBackgroundColor: [UIColor clearColor]]; 18 | [self setCount: 0]; 19 | } 20 | return self; 21 | } 22 | 23 | 24 | - (void)drawRect:(CGRect)rect { 25 | if (count == 0) return; 26 | NSString *countString = [NSString stringWithFormat: @"%d", count]; 27 | UIFont *font = [UIFont boldSystemFontOfSize: 16]; 28 | CGSize numberSize = [countString sizeWithFont: font]; 29 | 30 | CGContextRef context = UIGraphicsGetCurrentContext(); 31 | float radius = numberSize.height / 2.0; 32 | float startPoint = (rect.size.width - (numberSize.width + numberSize.height))/2.0; 33 | 34 | CGContextSetRGBFillColor(context, 0.55, 0.6, 0.70, 1.0); 35 | CGContextBeginPath(context); 36 | CGContextAddArc(context, startPoint + radius, radius, radius, M_PI / 2 , 3 * M_PI / 2, NO); 37 | CGContextAddArc(context, startPoint + radius + numberSize.width, radius, radius, 3 * M_PI / 2, M_PI / 2, NO); 38 | CGContextClosePath(context); 39 | CGContextFillPath(context); 40 | 41 | [[UIColor whiteColor] set]; 42 | [countString drawInRect: CGRectMake(startPoint + radius, rect.origin.y, rect.size.width, rect.size.height) withFont: font]; 43 | } 44 | 45 | - (void)drawWithCount:(NSInteger)i { 46 | self.count = i; 47 | [self setNeedsDisplay]; 48 | } 49 | 50 | - (void)dealloc { 51 | [super dealloc]; 52 | } 53 | 54 | 55 | @end 56 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2008 Stepcase Limited 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /MultipleDownload.h: -------------------------------------------------------------------------------- 1 | // 2 | // MultipleDownload.h 3 | // 4 | // Copyright 2008 Stepcase Limited. 5 | // 6 | 7 | #import 8 | 9 | 10 | @interface MultipleDownload : NSObject { 11 | NSMutableArray *urls; 12 | NSMutableDictionary *requests; 13 | NSMutableArray *receivedDatas; 14 | NSInteger finishCount; 15 | id delegate; 16 | } 17 | 18 | @property (nonatomic,retain) NSMutableArray *urls; 19 | @property (nonatomic,retain) NSMutableDictionary *requests; 20 | @property (nonatomic,retain) NSMutableArray *receivedDatas; 21 | @property NSInteger finishCount; 22 | @property (retain) id delegate; 23 | 24 | - (id)initWithUrls:(NSArray *)aUrls; 25 | - (void)requestWithUrls:(NSArray *)aUrls; 26 | - (NSData *)dataAtIndex:(NSInteger)idx; 27 | - (NSString *)dataAsStringAtIndex:(NSInteger)idx; 28 | - (void)setDelegate:(id)val; 29 | - (id)delegate; 30 | 31 | @end 32 | 33 | @interface NSObject (MultipleDownloadDelegateMethods) 34 | 35 | - (void)didFinishDownload:(NSNumber*)idx; 36 | - (void)didFinishAllDownload; 37 | 38 | @end 39 | -------------------------------------------------------------------------------- /MultipleDownload.m: -------------------------------------------------------------------------------- 1 | // 2 | // MultipleDownload.m 3 | // 4 | // Copyright 2008 Stepcase Limited. 5 | // 6 | 7 | #import "MultipleDownload.h" 8 | 9 | 10 | @implementation MultipleDownload 11 | 12 | @synthesize urls, requests, receivedDatas, finishCount; 13 | 14 | - init { 15 | if ((self = [super init])) { 16 | self.finishCount = 0; 17 | NSMutableArray *array = [[NSMutableArray alloc] init]; 18 | self.receivedDatas = array; 19 | [array release]; 20 | 21 | NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 22 | self.requests = dict; 23 | [dict release]; 24 | 25 | } 26 | return self; 27 | } 28 | 29 | - (void)dealloc { 30 | [urls release]; 31 | [requests release]; 32 | [receivedDatas autorelease]; 33 | [super dealloc]; 34 | } 35 | 36 | - (void)setDelegate:(id)val 37 | { 38 | delegate = val; 39 | } 40 | 41 | - (id)delegate 42 | { 43 | return delegate; 44 | } 45 | 46 | 47 | #pragma mark Methods 48 | - (id)initWithUrls:(NSArray *)aUrls { 49 | if ((self = [self init])) 50 | [self requestWithUrls:aUrls]; 51 | return self; 52 | } 53 | 54 | - (void)requestWithUrls:(NSArray *)aUrls { 55 | 56 | [receivedDatas removeAllObjects]; 57 | [requests removeAllObjects]; 58 | [urls autorelease]; 59 | urls = [aUrls copy]; 60 | 61 | for(NSInteger i=0; i< [urls count]; i++){ 62 | NSMutableData *aData = [[NSMutableData alloc] init]; 63 | [receivedDatas addObject: aData]; 64 | [aData release]; 65 | 66 | NSURLRequest *request = [[NSURLRequest alloc] 67 | initWithURL: [NSURL URLWithString: [urls objectAtIndex:i]] 68 | cachePolicy: NSURLRequestReloadIgnoringLocalCacheData 69 | timeoutInterval: 10 70 | ]; 71 | NSURLConnection *connection = [[NSURLConnection alloc] 72 | initWithRequest:request 73 | delegate:self]; 74 | 75 | [requests setObject: [NSNumber numberWithInt: i] forKey: [NSValue valueWithNonretainedObject:connection]]; 76 | [connection release]; 77 | [request release]; 78 | } 79 | } 80 | 81 | - (NSData *)dataAtIndex:(NSInteger)idx { 82 | return [receivedDatas objectAtIndex:idx]; 83 | } 84 | 85 | - (NSString *)dataAsStringAtIndex:(NSInteger)idx { 86 | return [[[NSString alloc] initWithData:[receivedDatas objectAtIndex:idx] encoding:NSUTF8StringEncoding] autorelease]; 87 | } 88 | 89 | #pragma mark NSURLConnection Delegates 90 | - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 91 | { 92 | NSInteger i = [[requests objectForKey: [NSValue valueWithNonretainedObject:connection]] intValue]; 93 | [[receivedDatas objectAtIndex:i] setLength:0]; 94 | } 95 | 96 | - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 97 | { 98 | NSInteger i = [[requests objectForKey: [NSValue valueWithNonretainedObject:connection]] intValue]; 99 | [[receivedDatas objectAtIndex:i] appendData:data]; 100 | } 101 | 102 | - (void)connectionDidFinishLoading:(NSURLConnection *)connection 103 | { 104 | NSInteger i = [[requests objectForKey: [NSValue valueWithNonretainedObject:connection]] intValue]; 105 | finishCount++; 106 | 107 | if ([delegate respondsToSelector:@selector(didFinishDownload:)]) 108 | [delegate performSelector:@selector(didFinishDownload:) withObject: [NSNumber numberWithInt: i]]; 109 | 110 | if(finishCount >= [urls count]){ 111 | if ([delegate respondsToSelector:@selector(didFinishAllDownload)]) 112 | [delegate didFinishAllDownload]; 113 | } 114 | } 115 | 116 | - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 117 | if ([delegate respondsToSelector:@selector(didFailDownloadWithError:)]) 118 | [delegate performSelector:@selector(didFailDownloadWithError:) withObject: error]; 119 | } 120 | 121 | @end 122 | -------------------------------------------------------------------------------- /README.textile: -------------------------------------------------------------------------------- 1 | h2. MultiDownload class 2 | 3 | To initialize and start the download: 4 | 5 |

 6 | 	self.urls = [NSMutableArray arrayWithObjects:
 7 | 					  @"http://maps.google.com/maps/geo?output=json&q=Lai+Chi+Kok,Hong+Kong",
 8 | 					  @"http://maps.google.com/maps/geo?output=json&q=Central,Hong+Kong",
 9 | 					  @"http://maps.google.com/maps/geo?output=json&q=Wan+Chai,Hong+Kong",
10 | 					  nil];
11 | 
12 | 	self.downloads = [[MultipleDownload alloc] initWithUrls: urls];
13 | 	self.downloads.delegate = self;
14 | 
15 | 16 | For processing the download, use MultiDownload delegates: 17 | 18 |

19 |  - (void) didFinishDownload:(NSNumber*)idx {
20 | 	NSLog(@"%d download: %@", [idx intValue], [downloads dataAsStringAtIndex: [idx intValue]]);
21 |  }
22 | 
23 |  - (void) didFinishAllDownload {
24 | 	NSLog(@"Finished all download!");
25 | 	[downloads release];
26 |  }
27 | 
28 | 29 | h2. BlueBadge class 30 | 31 | It uses CoreGraphic to draw a blue badge similar to the mail count in Mail.app. To initialize: 32 | 33 |

34 | BlueBadge *blueBadge = [[BlueBadge alloc] initWithFrame: rect];
35 | [cell addSubview:blueBadge];
36 | [blueBadge release];
37 | 
38 | 39 | When you need to update the count: 40 |

41 | [blueBadge drawWithCount: numOfMail];
42 | 
43 | --------------------------------------------------------------------------------