-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRMLAlertView.m
388 lines (311 loc) · 10.6 KB
/
RMLAlertView.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
#import "RMLAlertView.h"
#import "RMLFrameworkHeaders.h"
#include "RMLDebug.h"
#import "UIKit/UILongPressGestureRecognizer.h"
#define VERTICAL_GAP 10
#define HORIZONTAL_GAP 0
#pragma mark Private interface
@interface RMLAlertView (private)
-(void)startNagTimer;
-(void)alertNag;
-(void)startSnoozing:(int)snoozeTimeMinutes payload:(id)payload;
@end
#pragma mark Button snoozer
@interface ButtonSnoozeDelegate : NSObject <RMLSnoozerDelegate> {
RMLAlertView *_parent;
}
@end
@implementation ButtonSnoozeDelegate
-(id)initWithParent:(RMLAlertView *)parent {
if ((self = [super init])) {
_parent = parent;
[_parent retain];
}
return self;
}
-(void)dealloc {
[_parent release];
[super dealloc];
}
-(void)snoozeCallback:(id)payload {
#if DEBUG_LOG
NSLog(@"ButtonSnoozeDelegate.snoozeCallback: in with %@", payload);
#endif
[_parent.controller.jailbreakFacade showAlert:payload];
}
@end
#pragma mark Nag snoozer
@interface NagSnoozeDelegate : NSObject <RMLSnoozerDelegate> {
RMLAlertView *_parent;
}
@end
@implementation NagSnoozeDelegate
-(id)initWithParent:(RMLAlertView *)parent {
if ((self = [super init])) {
_parent = parent;
[_parent retain];
}
return self;
}
-(void)dealloc {
[_parent release];
[super dealloc];
}
-(void)snoozeCallback:(id)payload {
#if DEBUG_LOG
NSLog(@"NagSnoozeDelegate.snoozeCallback: in with %@", payload);
#endif
if (![_parent.controller.jailbreakFacade isAlertShowing:_parent.payload]) {
#if DEBUG_LOG
NSLog(@"NagSnoozeDelegate.snoozeCallback: alert is no longer showing, aborting");
#endif
return;
}
[_parent.controller.jailbreakFacade wakeup];
[payload alertNag];
[payload startNagTimer];
}
@end
#pragma mark Implementation
@implementation RMLAlertView
@synthesize alertView = _alertView;
@synthesize controller = _controller;
@synthesize payload = _payload;
@synthesize minutesSlider = _minutesSlider;
@synthesize minutesLabel = _minutesLabel;
@synthesize nagCountdown = _nagCountdown;
@synthesize nagSnoozer = _nagSnoozer;
#pragma mark Helpers
-(BOOL)showViewEventButton {
return [[_alertView buttons] count] > 0;
}
#pragma mark Object lifecycle
-(id)initWithAlertView:(UIAlertView *)view controller:(RMLController *)controller payload:(id <NSObject>)payload {
NSString *closeStr = [controller.resourceBundle localizedStringForKey:@"CLOSE" value:@"Close" table:nil];
NSString *viewStr = [controller.resourceBundle localizedStringForKey:@"VIEW_EVENT" value:@"View Event" table:nil];
NSString *snoozeStr = [controller.resourceBundle localizedStringForKey:@"SNOOZE" value:@"Snooze" table:nil];
if ((self = [super initWithTitle:view.title message:view.message delegate:view.delegate
cancelButtonTitle:closeStr otherButtonTitles:nil]))
{
self.controller = controller;
self.payload = payload;
self.alertView = view;
if ([self showViewEventButton]) {
[self addButtonWithTitle:viewStr];
}
int snoozeIndex = [self addButtonWithTitle:snoozeStr];
NSArray *buttons = [self buttons];
UIControl *snoozeButton = [buttons objectAtIndex:snoozeIndex];
#if DEBUG_LOG
NSLog(@"RMLAlertView.initWithAlertView: snoozeButton is %@", snoozeButton);
#endif
UILongPressGestureRecognizer *longPressGesture = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(snoozeLongPress:)] autorelease];
[snoozeButton addGestureRecognizer:longPressGesture];
// Note that starting this here leads to slightly weird behaviour during debugging because the timer alerts play before the official alert sometimes.
// This is not a big deal, and can be ignored.
self.nagCountdown = controller.nagLimit;
[self startNagTimer];
_frameOffset = 0;
}
#if DEBUG_LOG
NSLog(@"RMLAlertView.init: created %@, %@", view.title, self);
#endif
return self;
}
-(void)dealloc {
#if DEBUG_LOG
NSLog(@"RMLAlertView.dealloc: in");
#endif
[self.nagSnoozer release];
[self.alertView release];
[self.controller release];
[self.payload release];
[self.minutesSlider release];
[self.minutesLabel release];
[super dealloc];
}
#pragma mark Snooze button handling
-(CGFloat)getMinX:(NSArray *)buttons {
CGFloat min = CGFLOAT_MAX;
for (id item in buttons) {
UIView *button = (UIView *)item;
CGFloat x = button.frame.origin.x;
if (x < min) {
min = x;
}
}
return min;
}
-(CGFloat)getMaxX:(NSArray *)buttons {
CGFloat max = CGFLOAT_MIN;
for (id item in buttons) {
UIView *button = item;
CGFloat x = button.frame.origin.x + button.frame.size.width;
if (x > max) {
max = x;
}
}
return max;
}
-(CGFloat)getMaxY:(NSArray *)buttons {
CGFloat max = CGFLOAT_MIN;
for (id item in buttons) {
UIView *button = item;
CGFloat x = button.frame.origin.y + button.frame.size.height;
if (x > max) {
max = x;
}
}
return max;
}
-(CGFloat)getHeight:(NSArray *)buttons {
UIView *button = [buttons objectAtIndex:0];
return button.frame.size.height;
}
-(void)snoozeLongPress:(UILongPressGestureRecognizer *)gesture {
#if DEBUG_LOG
NSLog(@"RMLAlertView.snoozeLongPress: in with %@", gesture);
#endif
UIView *button = gesture.view;
UIView *popup = button.superview;
#if DEBUG_LOG
NSLog(@"RMLAlertView.snoozeLongPress: button is %@", button);
#endif
if (!_frameOffset) {
NSArray *buttons = [super buttons];
int snoozeIndex = [self showViewEventButton] ? 2 : 1;
UIView *snoozeButton = [buttons objectAtIndex:snoozeIndex];
CGFloat leftX = [self getMinX:buttons];
CGFloat height = [self getHeight:buttons];
CGFloat rightX = [self getMaxX:buttons];
CGFloat yPos = snoozeButton.frame.origin.y + snoozeButton.frame.size.height + VERTICAL_GAP;
CGFloat labelWidth = (rightX - leftX) / 3;
CGFloat sliderWidth = rightX - leftX - HORIZONTAL_GAP - labelWidth;
CGRect sliderFrame = CGRectMake(leftX, yPos, sliderWidth, height);
OBSlider *slider = [[OBSlider alloc] initWithFrame:sliderFrame];
slider.continuous = NO;
slider.minimumValue = 1;
slider.maximumValue = 60;
slider.value = self.controller.snoozeTime;
[slider addTarget:self action:@selector(sliderUpdated) forControlEvents:UIControlEventValueChanged|UIControlEventTouchDragInside];
[popup addSubview:slider];
self.minutesSlider = slider;
CGRect labelFrame = CGRectMake(leftX + sliderWidth + HORIZONTAL_GAP, yPos, labelWidth, height);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[popup addSubview:label];
self.minutesLabel = label;
_frameOffset = height + VERTICAL_GAP;
[self _layoutIfNeeded];
// Don't respond to this gesture if pressed twice
[button removeGestureRecognizer:gesture];
}
}
-(void)snoozeTimePress:(UIButton *)button {
#if DEBUG_LOG
NSLog(@"RMLAlertView.snoozeTimePress: in with %@", button);
#endif
int snoozeTimeMinutes = [button.currentTitle intValue];
#if DEBUG_LOG
NSLog(@"RMLAlertView.snoozeTimePress: snoozeTime is %d", snoozeTimeMinutes);
#endif
[self startSnoozing:snoozeTimeMinutes payload:self.payload];
NSArray *buttons = [super buttons];
UIView *closeButton = [buttons objectAtIndex:self.cancelButtonIndex];
[self _buttonClicked:closeButton];
}
-(void)updateLabelText {
int minutes = self.minutesSlider.value;
NSString *minutesStr = [self.controller.resourceBundle localizedStringForKey:@"SNOOZE_TIME" value:@"%d minutes" table:nil];
self.minutesLabel.text = [NSString stringWithFormat:minutesStr, minutes];
}
-(void)sliderUpdated {
[self updateLabelText];
}
-(void)_buttonClicked:(id)clicked {
#if DEBUG_LOG
NSLog(@"RMLAlertView._buttonClicked: in for %@", clicked);
#endif
NSArray *buttons = [super buttons];
UIView *snoozeButton = [buttons objectAtIndex:[buttons count] - 1];
if (clicked == snoozeButton) {
int snoozeTimeMinutes = -1;
if (nil != self.minutesSlider) {
snoozeTimeMinutes = self.minutesSlider.value;
}
[self startSnoozing:snoozeTimeMinutes payload:self.payload];
clicked = [buttons objectAtIndex:0];
}
[self.nagSnoozer invalidate];
[super _buttonClicked:clicked];
}
-(void)startSnoozing:(int)snoozeTimeMinutes payload:(id)payload {
#if DEBUG_LOG
NSLog(@"RMLController.startSnoozing: in with payload %@, snoozeTime %d", payload, snoozeTimeMinutes);
#endif
if (-1 == snoozeTimeMinutes) {
snoozeTimeMinutes = self.controller.snoozeTime;
}
ButtonSnoozeDelegate *snoozeDelegate = [[[ButtonSnoozeDelegate alloc] initWithParent:self] autorelease];
RMLSnoozer *snoozer = [[[RMLSnoozer alloc] initWithTime:snoozeTimeMinutes delegate:snoozeDelegate] autorelease];
[snoozer snooze:payload];
}
#pragma mark UIAlertView overrides
-(void)_layoutIfNeeded {
[super _layoutIfNeeded];
if (_frameOffset != 0) {
NSArray *buttons = [super buttons];
int sleepIndex = [self showViewEventButton] ? 2 : 1;
UIView *sleepButton = [buttons objectAtIndex:sleepIndex];
// Fix position of slider and label
CGRect sliderFrame = self.minutesSlider.frame;
sliderFrame.origin.y = sleepButton.frame.origin.y + sleepButton.frame.size.height + VERTICAL_GAP;
self.minutesSlider.frame = sliderFrame;
CGRect labelFrame = self.minutesLabel.frame;
labelFrame.origin.y = sliderFrame.origin.y;
self.minutesLabel.frame = labelFrame;
if ([self showViewEventButton]) {
// Move the close button down
UIView *button0 = [buttons objectAtIndex:0];
CGRect b0frame = button0.frame;
b0frame.origin.y += b0frame.size.height;
button0.frame = b0frame;
}
// Fix dialog's height and re-centre it
CGRect popupFrame = self.frame;
popupFrame.size.height += _frameOffset;
popupFrame.origin.y -= _frameOffset / 2;
self.frame = popupFrame;
[self updateLabelText];
}
#if DEBUG_LOG
NSLog(@"RMLAlertView._layoutIfNeeded: my frame height is now %f", self.frame.size.height);
#endif
}
-(void)layout {
[super layout];
if (_frameOffset) {
[self _layoutIfNeeded];
}
}
#pragma mark Alert nagging
-(void)startNagTimer {
#if DEBUG_LOG
NSLog(@"RMLAlertView.startNagTimer: in");
#endif
if (!self.controller.keepNagging || self.nagCountdown-- == 0) {
#if DEBUG_LOG
NSLog(@"RMLAlertView.startNagTimer: will not nag anymore");
#endif
return;
}
NagSnoozeDelegate *snoozeDelegate = [[[NagSnoozeDelegate alloc] initWithParent:self] autorelease];
self.nagSnoozer = [[[RMLSnoozer alloc] initWithTime:self.controller.nagInterval delegate:snoozeDelegate] autorelease];
[self.nagSnoozer snooze:self];
}
-(void)alertNag {
[self.controller playSound:NO];
}
@end