forked from peterhajas/MobileNotifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MNAlertViewController.m
359 lines (291 loc) · 12.1 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
/*
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, alertBackgroundShadow;
@synthesize chevronButton;
@synthesize alertHeaderLabel, alertTextLabel;
@synthesize laterButton, openButton;
@synthesize dataObj;
@synthesize alertIsShowingPopOver;
@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,60);
[UIView setAnimationDidStopSelector:@selector(animationDidStop:didFinish:inContext:)];
[UIView setAnimationDelegate:self];
alertBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 60.0)];
alertBackgroundImageView.frame = CGRectMake(0.0, 0.0, 320.0, 60.0);
alertBackgroundImageView.image = [UIImage imageWithContentsOfFile:@"/Library/Application Support/MobileNotifier/alert_bg.png"];
[alertBackgroundImageView setAlpha:0.0];
alertBackgroundShadow = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 60.0, 320, 17)];
alertBackgroundShadow.image = [UIImage imageWithContentsOfFile:@"/Library/Application Support/MobileNotifier/alert_bg_shadow.png"];
[alertBackgroundShadow setAlpha:0.0];
iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15.0, 15.0, 30.0, 30.0)];
iconImageView.frame = CGRectMake(12.0, 10.0, 38.0, 38.0);
iconImageView.image = [_delegate iconForBundleID:dataObj.bundleID];
[iconImageView setAlpha:0.0];
iconImageView.layer.cornerRadius = 5.5;
iconImageView.layer.masksToBounds = YES;
chevronButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
chevronButton.contentMode = UIViewContentModeCenter;
chevronButton.frame = CGRectMake(285.0, 22, 10.0, 15.0);
[chevronButton setImage:[UIImage imageWithContentsOfFile: @"/Library/Application Support/MobileNotifier/alert_chevron.png"]
forState:UIControlStateNormal];
[chevronButton setAlpha:0.0];
alertExpandButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
alertExpandButton.frame = CGRectMake(0.0, 0.0, 320.0, 60.0);
[alertExpandButton setAlpha:0.0];
alertHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0, 11.0, 216.0, 22.0)];
alertHeaderLabel.frame = CGRectMake(60.0, 11.0, 216.0, 22.0);
alertHeaderLabel.adjustsFontSizeToFitWidth = NO;
alertHeaderLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:17.000];
alertHeaderLabel.text = dataObj.header;
alertHeaderLabel.textAlignment = UITextAlignmentLeft;
alertHeaderLabel.textColor = [UIColor colorWithRed:0.000 green:0.000 blue:0.000 alpha:1.000];
alertHeaderLabel.backgroundColor = [UIColor clearColor];
alertHeaderLabel.shadowColor = [UIColor whiteColor];
alertHeaderLabel.shadowOffset = CGSizeMake(0,1);
[alertHeaderLabel setAlpha:0.0];
alertTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(60.0, 27.0, 216.0, 22.0)];
alertTextLabel.frame = CGRectMake(60.0, 27.0, 216.0, 22.0);
alertTextLabel.adjustsFontSizeToFitWidth = NO;
alertTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:13.000];
alertTextLabel.text = dataObj.text;
alertTextLabel.textAlignment = UITextAlignmentLeft;
alertTextLabel.textColor = [UIColor colorWithRed:0.000 green:0.000 blue:0.000 alpha:1.000];
alertTextLabel.backgroundColor = [UIColor clearColor];
alertTextLabel.shadowColor = [UIColor whiteColor];
alertTextLabel.shadowOffset = CGSizeMake(0,1);
[alertTextLabel setAlpha:0.0];
//Popdown alert actions
alertActionBackgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(1.0, 47.0, 319.0, 93.0)];
alertActionBackgroundImageView.image = [UIImage imageWithContentsOfFile:@"/Library/Application Support/MobileNotifier/popup_bg.png"];
alertActionBackgroundImageView.opaque = NO;
//alertActionBackgroundImageView.opaque = NO;
alertActionBackgroundImageView.backgroundColor = [UIColor clearColor];
alertActionBackgroundImageView.alpha = 0.0;
//We also need the shadow for the Popdown
alertActionBackgroundImageViewShadow = [[UIImageView alloc] initWithFrame:CGRectMake(1.0, 47.0, 319.0, 93.0)];
alertActionBackgroundImageViewShadow.image = [UIImage imageWithContentsOfFile:@"/Library/Application Support/MobileNotifier/popup_bg_shadow.png"];
alertActionBackgroundImageViewShadow.opaque = NO;
openButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
openButton.frame = CGRectMake(163.0, 76.0, 139.0, 47.0);
[openButton setAlpha:0.0];
if(dataObj.type == kSMSAlert)
{
//Change this to quick reply once that feature is functional
[openButton setBackgroundImage:[UIImage imageWithContentsOfFile: @"/Library/Application Support/MobileNotifier/open_btn.png"]
forState:UIControlStateNormal];
}
else
{
[openButton setBackgroundImage:[UIImage imageWithContentsOfFile: @"/Library/Application Support/MobileNotifier/open_btn.png"]
forState:UIControlStateNormal];
}
laterButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
laterButton.frame = CGRectMake(16.0, 76.0, 139.0, 47.0);
[laterButton setBackgroundImage:[UIImage imageWithContentsOfFile: @"/Library/Application Support/MobileNotifier/close_btn.png"]
forState:UIControlStateNormal];
[laterButton setAlpha:0.0];
//Wire up buttons
[chevronButton addTarget:self action:@selector(chevronPushed:)
forControlEvents:UIControlEventTouchUpInside];
[openButton addTarget:self action:@selector(openPushed:)
forControlEvents:UIControlEventTouchUpInside];
[laterButton addTarget:self action:@selector(laterPushed:)
forControlEvents:UIControlEventTouchUpInside];
[alertExpandButton addTarget:self action:@selector(chevronPushed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:alertBackgroundImageView];
[self.view addSubview:iconImageView];
[self.view addSubview:alertHeaderLabel];
[self.view addSubview:alertTextLabel];
[self.view addSubview:chevronButton];
[self.view addSubview:alertBackgroundShadow];
[self.view addSubview:alertExpandButton];
alertIsShowingPopOver = NO;
[self fadeInView];
}
-(void)viewDidLoad
{
[super viewDidLoad];
// Single finger, double tap
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(didSwipeRight)];
swipeRight.numberOfTouchesRequired = 1;
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
[swipeRight release];
}
-(void)didSwipeRight
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
CGRect frame = self.view.frame;
frame.origin.x += 320;
self.view.frame = frame;
[UIView commitAnimations];
//Notify the delegate
[_delegate alertViewController:self hadActionTaken:kAlertSentAway];
}
-(void)chevronPushed:(id)sender
{
if(alertIsShowingPopOver)
{
CGRect frame = self.view.frame;
frame.size.height -= 45;
self.view.frame = frame;
[UIButton beginAnimations:nil context:NULL];
[UIButton setAnimationDuration:0.3];
CGAffineTransform transform = CGAffineTransformMakeRotation(0);
chevronButton.transform = transform;
[UIButton commitAnimations];
[self fadeBottomAway:YES];
}
else
{
CGRect frame = self.view.frame;
frame.size.height += 93;
self.view.frame = frame;
[UIButton beginAnimations:nil context:NULL];
[UIButton setAnimationDuration:0.3];
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14/2);
chevronButton.transform = transform;
[UIButton commitAnimations];
[self fadeBottomAway:NO];
[self.view addSubview:openButton];
[self.view addSubview:laterButton];
}
alertIsShowingPopOver = !alertIsShowingPopOver;
[_delegate alertShowingPopOver:alertIsShowingPopOver];
}
-(void)fadeBottomAway:(bool)fadeBottom
{
if(fadeBottom)
{
[self.view addSubview:alertBackgroundShadow];
[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];
[UIView commitAnimations];
}
else
{
[self.view addSubview:alertActionBackgroundImageViewShadow];
[self.view addSubview:alertActionBackgroundImageView];
[UIView beginAnimations:@"showLower" context:NULL];
[UIView setAnimationDuration:0.3];
[alertActionBackgroundImageView setAlpha:0.9];
[alertActionBackgroundImageViewShadow setAlpha:1.0];
[openButton setAlpha:1.0];
[laterButton 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];
[chevronButton setAlpha:0.0];
[alertBackgroundShadow setAlpha:0.0];
[alertExpandButton setAlpha:0.0];
[alertActionBackgroundImageView setAlpha:0.0];
[alertActionBackgroundImageViewShadow setAlpha:0.0];
[openButton setAlpha:0.0];
[laterButton setAlpha:0.0];
[UIView commitAnimations];
alertIsShowingPopOver = NO;
}
-(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];
[chevronButton setAlpha:1.0];
[alertBackgroundShadow 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];
}
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];
}
@end