-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathNotificationManager.m
More file actions
613 lines (538 loc) · 21.6 KB
/
NotificationManager.m
File metadata and controls
613 lines (538 loc) · 21.6 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//
// NotificationManager.m
//
// Created by Nihal Ahmed on 12-03-16.
// Copyright (c) 2012 NABZ Software. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import "NotificationManager.h"
#pragma mark - Notification Window
@interface NotificationWindow : UIWindow
@end
@implementation NotificationWindow
- (id)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
id hitView = [super hitTest:point withEvent:event];
if (hitView == [self.subviews objectAtIndex:0]) {
return nil;
}
else {
return hitView;
}
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
return YES;
}
@end
#pragma mark - Notification View Controller
@interface NotificationViewController : UIViewController
@end
@implementation NotificationViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor clearColor]];
CGRect frame;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(UIInterfaceOrientationIsLandscape(orientation)) {
frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
}
else {
frame = [[UIScreen mainScreen] bounds];
}
[self.view setFrame:frame];
[self setWantsFullScreenLayout:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == [[UIApplication sharedApplication] statusBarOrientation]);
}
@end
#pragma mark - Notification
@interface Notification : UIView
- (id)initWithText:(NSString *)text icon:(NSString *)icon style:(NotificationStyle)style duration:(NSTimeInterval)duration target:(id)target selector:(SEL)selector;
- (void)show;
- (NSMutableArray *)splitString:(NSString*)str maxWidth:(CGFloat)width forFont:(UIFont *)font;
@property (nonatomic, assign) id delegate;
@property (nonatomic, retain) UIWindow *oldWindow;
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UIViewController *modalViewController;
@property (nonatomic, assign) UILabel *textLabel;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, assign) NotificationStyle style;
@property (nonatomic, assign) NSTimeInterval duration;
@property (nonatomic, assign) id target;
@property (nonatomic, assign) SEL selector;
@property (nonatomic, retain) NSMutableArray *lines;
@end
@implementation Notification
@synthesize delegate, oldWindow, window, modalViewController;
@synthesize text = _text;
@synthesize icon = _icon;
@synthesize style = _style;
@synthesize duration = _duration;
@synthesize target = _targer;
@synthesize selector = _selector;
@synthesize textLabel = _textLabel;
@synthesize lines = _lines;
//Initialize notification object
- (id)initWithText:(NSString *)text icon:(NSString *)icon style:(NotificationStyle)style duration:(NSTimeInterval)duration target:(id)target selector:(SEL)selector
{
self = [super initWithFrame:CGRectZero];
if (self) {
[self setText:text];
[self setIcon:icon];
[self setStyle:style];
[self setDuration:duration];
[self setTarget:target];
[self setSelector:selector];
}
return self;
}
//Prepare notification object with given parameters
- (void)prepareNotification
{
CGSize size = [[self performSelector:@selector(calculateSize)] CGSizeValue];
[self setFrame:CGRectMake(0, 0, size.width, size.height)];
[self setUserInteractionEnabled:NO];
[self setBackgroundColor:[UIColor clearColor]];
[self setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
if(self.style == NotificationStyleStatusBar) {
[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
}
[self.layer setShadowColor:[[UIColor blackColor] CGColor]];
[self.layer setShadowRadius:1];
[self.layer setShadowOpacity:1];
[self.layer setMasksToBounds:NO];
[self.layer setShadowOffset:CGSizeMake(0, 1)];
//Notification background
UIView *background = [[UIView alloc] initWithFrame:self.bounds];
[background setBackgroundColor:kNotificationBackgroundColor];
if(self.style != NotificationStyleStatusBar) {
[background.layer setCornerRadius:kNotificationCornerRadius];
[background.layer setBorderWidth:kNotificationBorderWidth];
[background.layer setBorderColor:[kNotificationBorderColor CGColor]];
}
[background setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self addSubview:background];
[background release];
//If icon specified, add it to the notification
if(self.icon != nil) {
UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(kNotificationPadding, self.bounds.size.height/2 - kNotificationIconSize/2, kNotificationIconSize, kNotificationIconSize)];
if(self.style == NotificationStyleStatusBar) {
[iconView setFrame:CGRectMake(2, 2, 16, 16)];
}
[iconView setContentMode:UIViewContentModeScaleAspectFit];
[iconView setImage:[UIImage imageNamed:self.icon]];
[iconView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin];
[self addSubview:iconView];
[iconView release];
}
//Setup notification text
UILabel *textLabel = [[UILabel alloc] init];
if(self.icon != nil) {
[textLabel setFrame:CGRectMake(kNotificationIconSize + (kNotificationPadding * 1.5), 0.0f, self.bounds.size.width - kNotificationIconSize - (kNotificationPadding * 2.5), self.bounds.size.height)];
if(self.style == NotificationStyleStatusBar) {
[textLabel setFrame:CGRectMake(20, 0.0f, self.bounds.size.width - 22, self.bounds.size.height)];
}
}
else {
[textLabel setFrame:CGRectMake(kNotificationPadding, 0.0f, self.bounds.size.width - (kNotificationPadding * 2), self.bounds.size.height)];
if(self.style == NotificationStyleStatusBar) {
[textLabel setFrame:CGRectMake(2, 0.0f, self.bounds.size.width - 4, self.bounds.size.height)];
}
}
[textLabel setBackgroundColor:[UIColor clearColor]];
[textLabel setTextColor:kNotificationTextColor];
[textLabel setText:self.text];
[textLabel setTextAlignment:UITextAlignmentCenter];
[textLabel setFont:[UIFont fontWithName:kNotificationFontName size:kNotificationFontSize]];
if(self.style == NotificationStyleStatusBar) {
[textLabel setTextAlignment:UITextAlignmentLeft];
[textLabel setFont:[UIFont fontWithName:kNotificationFontName size:12]];
}
[textLabel setNumberOfLines:0];
[textLabel setLineBreakMode:UILineBreakModeWordWrap];
[textLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self addSubview:textLabel];
[textLabel release];
[self setTextLabel:textLabel];
if(self.target != nil && self.selector != nil) {
[self.window setUserInteractionEnabled:YES];
[self setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(notificationTapped)];
[tapGesture setNumberOfTapsRequired:1];
[self addGestureRecognizer:tapGesture];
[tapGesture release];
}
switch (_style) {
case NotificationStyleFadeInBottom: {
[self setCenter:CGPointMake(self.modalViewController.view.bounds.size.width/2, self.modalViewController.view.bounds.size.height - self.bounds.size.height/2 - kNotificationOffset)];
[self setAlpha:0];
break;
}
case NotificationStyleFadeInCenter: {
[self setCenter:CGPointMake(self.modalViewController.view.bounds.size.width/2, self.modalViewController.view.bounds.size.height/2)];
[self setAlpha:0];
break;
}
case NotificationStyleFadeInTop: {
[self setCenter:CGPointMake(self.modalViewController.view.bounds.size.width/2, self.bounds.size.height/2 + kNotificationOffset)];
[self setAlpha:0];
break;
}
case NotificationStyleSlideInBottom: {
[self setCenter:CGPointMake(self.modalViewController.view.bounds.size.width/2, self.modalViewController.view.bounds.size.height + self.bounds.size.height/2)];
break;
}
case NotificationStyleSlideInTop: {
[self setCenter:CGPointMake(self.modalViewController.view.bounds.size.width/2, -self.bounds.size.height/2)];
break;
}
case NotificationStyleZoomInBottom: {
[self setCenter:CGPointMake(self.modalViewController.view.bounds.size.width/2, self.modalViewController.view.bounds.size.height - self.bounds.size.height/2 - kNotificationOffset)];
[self setAlpha:0];
[self setTransform:CGAffineTransformMakeScale(0.1, 0.1)];
break;
}
case NotificationStyleZoomInCenter: {
[self setCenter:CGPointMake(self.modalViewController.view.bounds.size.width/2, self.modalViewController.view.bounds.size.height/2)];
[self setAlpha:0];
[self setTransform:CGAffineTransformMakeScale(0.1, 0.1)];
break;
}
case NotificationStyleZoomInTop: {
[self setCenter:CGPointMake(self.modalViewController.view.bounds.size.width/2, self.bounds.size.height/2 + kNotificationOffset)];
[self setAlpha:0];
[self setTransform:CGAffineTransformMakeScale(0.1, 0.1)];
break;
}
case NotificationStyleStatusBar: {
[self setCenter:CGPointMake(self.modalViewController.view.bounds.size.width/2, -self.bounds.size.height/2)];
if(self.icon != nil) {
[self setLines:[self splitString:self.text maxWidth:self.frame.size.width - 20 forFont:self.textLabel.font]];
}
else {
[self setLines:[self splitString:self.text maxWidth:self.frame.size.width - 2 forFont:self.textLabel.font]];
}
[self.textLabel setText:[self.lines objectAtIndex:0]];
[self.lines removeObjectAtIndex:0];
break;
}
}
}
//Show notification
- (void)show
{
[self setModalViewController:[[[NotificationViewController alloc] init] autorelease]];
NotificationWindow *aWindow = [[NotificationWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[aWindow setWindowLevel:UIWindowLevelStatusBar];
[aWindow setBackgroundColor:[UIColor clearColor]];
[aWindow setUserInteractionEnabled:NO];
[aWindow addSubview:self.modalViewController.view];
[self setWindow:aWindow];
[aWindow release];
[self setOldWindow:[[UIApplication sharedApplication] keyWindow]];
[self.modalViewController.view addSubview:self];
[self.window makeKeyAndVisible];
[self performSelector:@selector(prepareNotification)];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(notificationDidShow)];
switch (_style) {
case NotificationStyleFadeInBottom: {
[self setAlpha:1];
break;
}
case NotificationStyleFadeInCenter: {
[self setAlpha:1];
break;
}
case NotificationStyleFadeInTop: {
[self setAlpha:1];
break;
}
case NotificationStyleSlideInBottom: {
[self setTransform:CGAffineTransformMakeTranslation(0, -(self.bounds.size.height + kNotificationOffset))];
break;
}
case NotificationStyleSlideInTop: {
[self setTransform:CGAffineTransformMakeTranslation(0, self.bounds.size.height + kNotificationOffset)];
break;
}
case NotificationStyleZoomInBottom: {
[self setAlpha:1];
[self setTransform:CGAffineTransformIdentity];
break;
}
case NotificationStyleZoomInCenter: {
[self setAlpha:1];
[self setTransform:CGAffineTransformIdentity];
break;
}
case NotificationStyleZoomInTop: {
[self setAlpha:1];
[self setTransform:CGAffineTransformIdentity];
break;
}
case NotificationStyleStatusBar: {
[self setTransform:CGAffineTransformMakeTranslation(0, self.bounds.size.height)];
break;
}
}
[UIView commitAnimations];
}
//Callback when notification shows
- (void)notificationDidShow
{
if(self.style == NotificationStyleStatusBar && self.lines.count > 0) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:self.duration];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(statusBarNotificationShowNextLine)];
[self.textLabel setAlpha:0];
[UIView commitAnimations];
}
else {
[self performSelector:@selector(hide) withObject:nil afterDelay:self.duration];
}
}
//Hide notification
- (void)hide
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(notificationDidHide)];
switch (_style) {
case NotificationStyleFadeInBottom: {
[self setAlpha:0];
break;
}
case NotificationStyleFadeInCenter: {
[self setAlpha:0];
break;
}
case NotificationStyleFadeInTop: {
[self setAlpha:0];
break;
}
case NotificationStyleSlideInBottom: {
[self setTransform:CGAffineTransformIdentity];
break;
}
case NotificationStyleSlideInTop: {
[self setTransform:CGAffineTransformIdentity];
break;
}
case NotificationStyleZoomInBottom: {
[self setAlpha:0];
break;
}
case NotificationStyleZoomInCenter: {
[self setAlpha:0];
break;
}
case NotificationStyleZoomInTop: {
[self setAlpha:0];
break;
}
case NotificationStyleStatusBar: {
[self setTransform:CGAffineTransformIdentity];
break;
}
}
[UIView commitAnimations];
}
//Callback when notification hides
- (void)notificationDidHide
{
[self.oldWindow makeKeyWindow];
if([self.delegate respondsToSelector:@selector(notificationDidHide:)]) {
[self.delegate performSelector:@selector(notificationDidHide:) withObject:self];
}
[self removeFromSuperview];
}
//Called when notification is tapped
- (void)notificationTapped
{
if([self.target respondsToSelector:self.selector]) {
[self.target performSelector:self.selector];
}
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hide) object:nil];
[self notificationDidHide];
}
//Show the next line for NotificationStyleStatusBar
- (void)statusBarNotificationShowNextLine
{
[self.textLabel setText:[self.lines objectAtIndex:0]];
[self.lines removeObjectAtIndex:0];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(notificationDidShow)];
[self.textLabel setAlpha:1];
[UIView commitAnimations];
}
//Get notification size for current orientation
- (NSValue *)calculateSize
{
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
float reserved = (kNotificationOffset * 2) + (kNotificationPadding * 2);
if(self.icon) {
reserved += kNotificationIconSize + kNotificationPadding/2;
}
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(UIInterfaceOrientationIsLandscape(orientation)) {
if(self.style == NotificationStyleStatusBar) {
return [NSValue valueWithCGSize:CGSizeMake(screenSize.height, 20)];
}
float landscapeWidth = fmin(screenSize.height, kNotificationMaxWidth);
CGSize landscapeSize = [self.text sizeWithFont:[UIFont fontWithName:kNotificationFontName size:kNotificationFontSize]
constrainedToSize:CGSizeMake(landscapeWidth - reserved, CGFLOAT_MAX)];
return [NSValue valueWithCGSize:CGSizeMake(landscapeSize.width - (kNotificationOffset * 2) + reserved, landscapeSize.height + kNotificationPadding * 2)];
}
else {
if(self.style == NotificationStyleStatusBar) {
return [NSValue valueWithCGSize:CGSizeMake(screenSize.width, 20)];
}
float portraitWidth = fmin(screenSize.width, kNotificationMaxWidth);
CGSize portraitSize = [self.text sizeWithFont:[UIFont fontWithName:kNotificationFontName size:kNotificationFontSize]
constrainedToSize:CGSizeMake(portraitWidth - reserved, CGFLOAT_MAX)];
return [NSValue valueWithCGSize:CGSizeMake(portraitSize.width - (kNotificationOffset * 2) + reserved, portraitSize.height + kNotificationPadding * 2)];
}
}
//Get array of lines for NotificationStyleStatusBar
- (NSMutableArray *)splitString:(NSString *)str maxWidth:(CGFloat)width forFont:(UIFont *)font
{
NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:1];
NSArray *lineArray = [str componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for(NSString *string in lineArray) {
NSArray *wordArray = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *line = @"";
NSString *lineWithNext = @"";
for (int i = 0; i < [wordArray count]; i++) {
if (line.length == 0) { line = [wordArray objectAtIndex:i]; }
if (i+1 < [wordArray count]) {
lineWithNext = [[NSArray arrayWithObjects:line, [wordArray objectAtIndex:i+1], nil] componentsJoinedByString:@" "];
if (((int)[lineWithNext sizeWithFont:font].width) <= width) {
line = lineWithNext;
} else {
[tempArray addObject:line];
line = @"";
}
} else {
[tempArray addObject:line];
}
}
}
return tempArray;
}
- (void)dealloc
{
[oldWindow release];
[window release];
[modalViewController release];
[_text release];
[_icon release];
[_lines release];
[super dealloc];
}
@end
#pragma mark - Notification Delegate Protocol
@protocol NotificationManagerDelegate <NSObject>
- (void)notificationDidHide:(UIView *)notification;
@end
#pragma mark - Notification Manager
@interface NotificationManager () {
NSMutableArray *_queue;
}
+ (NotificationManager *)sharedManager;
- (void)notifyText:(NSString *)text icon:(NSString *)icon style:(NotificationStyle)style duration:(NSTimeInterval)duration target:(id)target selector:(SEL)selector showImmediately:(BOOL)showImmediately;
@end
@implementation NotificationManager
static NotificationManager *sharedManager = nil;
+ (NotificationManager *)sharedManager
{
if(sharedManager == nil) {
sharedManager = [[super allocWithZone:NULL] init];
}
return sharedManager;
}
- (id)init
{
if (self = [super init]) {
_queue = [[NSMutableArray alloc] init];
}
return self;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [[self sharedManager] retain];
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (id)retain
{
return self;
}
- (NSUInteger)retainCount
{
return NSUIntegerMax;
}
- (oneway void)release
{
}
- (id)autorelease
{
return self;
}
- (void)dealloc
{
[_queue release];
[super dealloc];
}
//Class method to create new notification object with given parameters
+ (void)notifyText:(NSString *)text icon:(NSString *)icon style:(NotificationStyle)style duration:(NSTimeInterval)duration target:(id)target selector:(SEL)selector showImmediately:(BOOL)showImmediately
{
[[NotificationManager sharedManager] notifyText:text icon:icon style:style duration:duration target:target selector:selector showImmediately:showImmediately];
}
//Create new notification object with given parameters
- (void)notifyText:(NSString *)text icon:(NSString *)icon style:(NotificationStyle)style duration:(NSTimeInterval)duration target:(id)target selector:(SEL)selector showImmediately:(BOOL)showImmediately
{
if(text == nil || text.length == 0) {
return;
}
//Create notification object and add it to the queue
Notification *notification = [[Notification alloc] initWithText:text icon:icon style:style duration:duration target:target selector:selector];
if(showImmediately) {
[notification show];
}
else {
[notification setDelegate:self];
[_queue addObject:notification];
//If queue empty, then show notification immediately
if(_queue.count == 1) {
[notification show];
}
}
[notification release];
}
//Callback recieved when a notification hides
- (void)notificationDidHide:(Notification *)notification
{
//Remove notification from queue
[_queue removeObject:notification];
//If queue not empty, show next notification
if(_queue.count > 0) {
[(Notification *)[_queue objectAtIndex:0] performSelector:@selector(show) withObject:nil afterDelay:0.5];
}
}
@end