forked from peterhajas/MobileNotifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMNAlertViewController.m
509 lines (422 loc) · 17.7 KB
/
MNAlertViewController.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
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
/*
Copyright (c) 2010-2011, Peter Hajas
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Peter Hajas nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL PETER HAJAS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "MNAlertViewController.h"
@interface SBIconModel (peterhajas)
+(id)sharedInstance;
-(id)applicationIconForDisplayIdentifier:(id)displayIdentifier;
@end
@interface SBIcon (peterhajas)
-(id)iconImageView;
@end
@implementation MNAlertViewController
@synthesize alertBackgroundImageView, alertActionBackgroundImageView, iconImageView;
@synthesize alertHeaderLabel, alertTextLabel;
@synthesize laterButton, openButton;
@synthesize dataObj;
@synthesize alertIsShowingPopOver;
@synthesize useBlackAlertStyle;
@synthesize delegate = _delegate;
-(id)init
{
self = [super init];
if (self != nil)
{
self.view.clipsToBounds = YES;
}
return self;
}
-(id)initWithMNData:(MNAlertData*) data
{
self = [super init];
dataObj = data;
return self;
}
-(void)loadView
{
[super loadView];
self.view.frame = CGRectMake(0,0,320,40);
[UIView setAnimationDidStopSelector:@selector(animationDidStop:didFinish:inContext:)];
[UIView setAnimationDelegate:self];
alertBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 40.0)];
[alertBackgroundImageView setAlpha:0.0];
if (useBlackAlertStyle)
{
// Black alert style
alertBackgroundImageView.image = [UIImage imageWithContentsOfFile:@"/Library/Application Support/MobileNotifier/alert_bg_small.png"];
}
else
{
// Gray alert style
alertBackgroundImageView.image = [UIImage imageWithContentsOfFile:@"/Library/Application Support/MobileNotifier/statusbar_alert_bg.png"];
}
iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(17.0, 9.0, 22.5, 22.5)];
iconImageView.image = [_delegate iconForBundleID:dataObj.bundleID];
[iconImageView setAlpha:0.0];
iconImageView.layer.cornerRadius = 5.5;
iconImageView.layer.masksToBounds = YES;
alertExpandButton = [UIButton buttonWithType:UIButtonTypeCustom];
alertExpandButton.frame = CGRectMake(0.0, 0.0, 320.0, 40.0);
[alertExpandButton setAlpha:0.0];
alertHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(49.0, 3.0, 216.0, 22.0)];
alertHeaderLabel.adjustsFontSizeToFitWidth = NO;
alertHeaderLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:14.000];
alertHeaderLabel.text = dataObj.header;
alertHeaderLabel.textAlignment = UITextAlignmentLeft;
alertHeaderLabel.backgroundColor = [UIColor clearColor];
if (useBlackAlertStyle)
{
// Black alert style
alertHeaderLabel.textColor = [UIColor colorWithRed:1.000 green:1.000 blue:1.000 alpha:1.0];
alertHeaderLabel.shadowColor = [UIColor grayColor];
}
else
{
// Gray alert style
alertHeaderLabel.textColor = [UIColor colorWithRed:0.000 green:0.000 blue:0.000 alpha:0.9];
alertHeaderLabel.shadowColor = [UIColor whiteColor];
}
alertHeaderLabel.shadowOffset = CGSizeMake(0,1);
[alertHeaderLabel setAlpha:0.0];
alertTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(49.0, 17.0, 216.0, 22.0)];
alertTextLabel.adjustsFontSizeToFitWidth = NO;
alertTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:11.000];
alertTextLabel.text = dataObj.text;
alertTextLabel.textAlignment = UITextAlignmentLeft;
alertTextLabel.backgroundColor = [UIColor clearColor];
if (useBlackAlertStyle)
{
// Black alert style
alertTextLabel.textColor = [UIColor colorWithRed:1.000 green:1.000 blue:1.000 alpha:1.000];
alertTextLabel.shadowColor = [UIColor blackColor];
}
else
{
// Gray alert style
alertTextLabel.textColor = [UIColor colorWithRed:0.000 green:0.000 blue:0.000 alpha:0.9];
alertTextLabel.shadowColor = [UIColor whiteColor];
}
alertTextLabel.shadowOffset = CGSizeMake(0,1);
[alertTextLabel setAlpha:0.0];
detailText = [[UITextView alloc] initWithFrame:CGRectMake(8.0, 50.0, 303.0, 75.0)];
detailText.delegate = self;
detailText.font = [UIFont fontWithName:@"HelveticaNeue" size:16.000];
detailText.text = dataObj.text;
detailText.textAlignment = UITextAlignmentLeft;
detailText.textColor = [UIColor colorWithRed:1.000 green:1.000 blue:1.000 alpha:0.8];
detailText.backgroundColor = [UIColor clearColor];
detailText.editable = NO;
[detailText setAlpha:0.0];
dateText = [[UILabel alloc] initWithFrame:CGRectMake(16.0, 127.0, 65.0, 15.0)];
dateText.font = [UIFont fontWithName:@"HelveticaNeue" size:10.500];
dateText.text = [dataObj.time descriptionWithCalendarFormat:@"%H:%M" timeZone: nil locale: nil];
dateText.textAlignment = UITextAlignmentLeft;
dateText.textColor = [UIColor colorWithRed:1.000 green:1.000 blue:1.000 alpha:0.5];
dateText.backgroundColor = [UIColor clearColor];
[dateText setAlpha:0.0];
// Popdown alert actions
alertActionBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(3.5, 30.0, 313.0, 229.0)];
alertActionBackgroundImageView.image = [UIImage imageWithContentsOfFile:@"/Library/Application Support/MobileNotifier/popout_bg.png"];
alertActionBackgroundImageView.opaque = NO;
alertActionBackgroundImageView.backgroundColor = [UIColor clearColor];
alertActionBackgroundImageView.alpha = 0.0;
openButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
openButton.frame = CGRectMake(72.0, 205.0, 46.0, 40.0);
[openButton setBackgroundImage:[UIImage imageWithContentsOfFile: @"/Library/Application Support/MobileNotifier/btn_open.png"]
forState:UIControlStateNormal];
[openButton setAlpha:0.0];
laterButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
laterButton.frame = CGRectMake(16.0, 205.0, 46.0, 40.0);
[laterButton setBackgroundImage:[UIImage imageWithContentsOfFile: @"/Library/Application Support/MobileNotifier/btn_archive.png"]
forState:UIControlStateNormal];
[laterButton setAlpha:0.0];
closeButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
closeButton.frame = CGRectMake(274, 7.0, 25.0, 26.0);
if (useBlackAlertStyle)
{
// Black alert style
[closeButton setBackgroundImage:[UIImage imageWithContentsOfFile: @"/Library/Application Support/MobileNotifier/statusbar_alert_dismiss_black.png"]
forState:UIControlStateNormal]; }
else
{
// Gray alert style
[closeButton setBackgroundImage:[UIImage imageWithContentsOfFile: @"/Library/Application Support/MobileNotifier/statusbar_alert_dismiss.png"]
forState:UIControlStateNormal]; }
// Wire up buttons
[openButton addTarget:self action:@selector(openPushed:)
forControlEvents:UIControlEventTouchUpInside];
[laterButton addTarget:self action:@selector(laterPushed:)
forControlEvents:UIControlEventTouchUpInside];
[closeButton addTarget:self action:@selector(closePushed:)
forControlEvents:UIControlEventTouchUpInside];
[alertExpandButton addTarget:self action:@selector(chevronPushed:)
forControlEvents:UIControlEventTouchUpInside];
// If we're an SMS alert, we have some more setup to do!
if(dataObj.type == kSMSAlert)
{
// Set up the SMS elements
sendButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
sendButton.frame = CGRectMake(226.0, 205.0, 76.0, 40.0);
[sendButton setBackgroundImage:[UIImage imageWithContentsOfFile: @"/Library/Application Support/MobileNotifier/btn_send.png"]
forState:UIControlStateNormal];
[sendButton setAlpha:0.0];
[sendButton setTitle:@"Send" forState:UIControlStateNormal];
sendButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20.00];
sendButton.titleLabel.textAlignment = UITextAlignmentCenter;
sendButton.titleLabel.textColor = [UIColor colorWithRed:1.000 green:1.000 blue:1.000 alpha:1.0];
sendButton.titleLabel.backgroundColor = [UIColor clearColor];
sendButton.titleLabel.shadowColor = [UIColor blackColor];
sendButton.titleLabel.shadowOffset = CGSizeMake(0,-1);
[sendButton addTarget:self action:@selector(sendPushed:)
forControlEvents:UIControlEventTouchUpInside];
charactersTyped = [[UILabel alloc] initWithFrame:CGRectMake(258.0, 127.0, 45.0, 15.0)];
charactersTyped.font = [UIFont fontWithName:@"HelveticaNeue" size:10.500];
charactersTyped.text = @"0/160";
charactersTyped.textAlignment = UITextAlignmentRight;
charactersTyped.textColor = [UIColor colorWithRed:1.000 green:1.000 blue:1.000 alpha:0.5];
charactersTyped.backgroundColor = [UIColor clearColor];
[charactersTyped setAlpha:0.0];
textBox = [[BCZeroEdgeTextView alloc] initWithFrame:CGRectMake(17.0, 145.0, 285.0, 50.0)];
[textBox setDelegate:self];
textBox.keyboardAppearance = UIKeyboardAppearanceAlert;
textBox.keyboardType = UIKeyboardTypeDefault;
textBox.returnKeyType = UIReturnKeyDefault;
textBox.font = [UIFont fontWithName:@"HelveticaNeue" size:12.500];
textBox.textColor = [UIColor colorWithRed:0.000 green:0.000 blue:0.000 alpha:0.8];
[textBox setAlpha:0.0];
}
// Add everything to our view
[self.view addSubview:alertBackgroundImageView];
[self.view addSubview:iconImageView];
[self.view addSubview:alertHeaderLabel];
[self.view addSubview:alertTextLabel];
[self.view addSubview:alertExpandButton];
// Release the stuff we don't want to hang on to
[alertBackgroundImageView release];
[alertHeaderLabel release];
[alertTextLabel release];
alertIsShowingPopOver = NO;
[self fadeInView];
}
-(void)viewDidLoad
{
[super viewDidLoad];
// Detect single finger, double tap
/*UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(laterPushed:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:doubleTap];
[doubleTap release];*/
}
-(void)chevronPushed:(id)sender
{
[_delegate alertShowingPopOver:!alertIsShowingPopOver];
if (alertIsShowingPopOver)
{
CGRect frame = self.view.frame;
frame.size.height -= 229;
self.view.frame = frame;
[self fadeBottomAway:YES];
}
else
{
CGRect frame = self.view.frame;
frame.size.height += 229;
self.view.frame = frame;
[self fadeBottomAway:NO];
[self.view addSubview:openButton];
[self.view addSubview:laterButton];
[self.view addSubview:closeButton];
[self.view addSubview:detailText];
[self.view addSubview:dateText];
if (dataObj.type == kSMSAlert)
{
[self.view addSubview:sendButton];
[self.view addSubview:charactersTyped];
[self.view addSubview:textBox];
UIKeyboardEnableAutomaticAppearance();
[textBox becomeFirstResponder];
}
}
alertIsShowingPopOver = !alertIsShowingPopOver;
}
-(void)fadeBottomAway:(bool)fadeBottom
{
if (fadeBottom)
{
[UIView beginAnimations:@"hideLower" context:NULL];
[UIView setAnimationDuration:0.3];
[alertActionBackgroundImageView setAlpha:0.0];
[alertActionBackgroundImageViewShadow setAlpha:0.0];
[openButton setAlpha:0.0];
[laterButton setAlpha:0.0];
[detailText setAlpha:0.0];
[dateText setAlpha:0.0];
[closeButton setHidden:YES];
[textBox resignFirstResponder];
if (dataObj.type == kSMSAlert)
{
[sendButton setAlpha:0.0];
[charactersTyped setAlpha:0.0];
[textBox setAlpha:0.0];
}
[UIView commitAnimations];
}
else
{
[self.view addSubview:alertActionBackgroundImageView];
[UIView beginAnimations:@"showLower" context:NULL];
[UIView setAnimationDuration:0.3];
[alertActionBackgroundImageView setAlpha:1.0];
[openButton setAlpha:1.0];
[laterButton setAlpha:1.0];
[detailText setAlpha:1.0];
[dateText setAlpha:1.0];
[closeButton setHidden:NO];
[textBox becomeFirstResponder];
if (dataObj.type == kSMSAlert)
{
[sendButton setAlpha:1.0];
[charactersTyped setAlpha:1.0];
[textBox setAlpha:1.0];
}
[UIView commitAnimations];
}
}
-(void)fadeOutWholeView
{
[UIView beginAnimations:@"fadeOutWholeView" context:NULL];
[UIView setAnimationDuration:0.2];
[alertBackgroundImageView setAlpha:0.0];
[iconImageView setAlpha:0.0];
[alertHeaderLabel setAlpha:0.0];
[alertTextLabel setAlpha:0.0];
[alertExpandButton setAlpha:0.0];
[alertActionBackgroundImageView setAlpha:0.0];
[alertActionBackgroundImageViewShadow setAlpha:0.0];
[openButton setAlpha:0.0];
[laterButton setAlpha:0.0];
[detailText setAlpha:0.0];
[dateText setAlpha:0.0];
[closeButton setAlpha:0.0];
if (dataObj.type == kSMSAlert)
{
[sendButton setAlpha:0.0];
[charactersTyped setAlpha:0.0];
[textBox setAlpha:0.0];
[textBox resignFirstResponder];
}
[UIView commitAnimations];
alertIsShowingPopOver = NO;
UIKeyboardDisableAutomaticAppearance();
}
-(void)fadeInView
{
[UIView beginAnimations:@"fadeInView" context:NULL];
[UIView setAnimationDuration:0.2];
[alertBackgroundImageView setAlpha:1.0];
[iconImageView setAlpha:1.0];
[alertHeaderLabel setAlpha:1.0];
[alertTextLabel setAlpha:1.0];
[alertExpandButton setAlpha:1.0];
[UIView commitAnimations];
}
-(void)animationDidStop:(NSString*)animationID didFinish:(NSNumber*)finished inContext:(id)context
{
if ([animationID isEqualToString:@"fadeInView"])
{
[alertActionBackgroundImageView removeFromSuperview];
[alertActionBackgroundImageViewShadow removeFromSuperview];
[openButton removeFromSuperview];
[laterButton removeFromSuperview];
[detailText removeFromSuperview];
[dateText removeFromSuperview];
if (dataObj.type == kSMSAlert)
{
[sendButton removeFromSuperview];
[charactersTyped removeFromSuperview];
[textBox removeFromSuperview];
}
}
if ([animationID isEqualToString:@"fadeOutWholeView"])
{
[self.view removeFromSuperview];
}
}
-(void)openPushed:(id)sender
{
[self fadeOutWholeView];
// Notify the delegate
[_delegate alertViewController:self hadActionTaken:kAlertTakeAction];
}
-(void)laterPushed:(id)sender
{
[self fadeOutWholeView];
// Notify the delegate
[_delegate alertViewController:self hadActionTaken:kAlertSentAway];
}
-(void)closePushed:(id)sender
{
[self fadeOutWholeView];
// Notify the delegate
[_delegate alertViewController:self hadActionTaken:kAlertClosed];
}
-(void)sendPushed:(id)sender
{
// Take the text in the textbox, and send it!
[MNSMSSender sendMessage:textBox.text toNumber:dataObj.senderAddress];
// Dismiss this alert entirely (as if it were deleted)
[self closePushed:nil];
}
// UITextViewDelegate methods
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
charactersTyped.text = [NSString stringWithFormat:@"%d/160", [textView.text length] + [text length] - range.length];
return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
return YES;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x != 0.0)
{
[scrollView setContentOffset: CGPointMake(0.0, scrollView.contentOffset.y)];
}
}
@end