-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHKMInviteView.m
executable file
·365 lines (315 loc) · 13.4 KB
/
HKMInviteView.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
#import <AddressBook/AddressBook.h>
#import "HKMInviteView.h"
#import "HKMInviteViewCell.h"
#import "HKMDiscoverer.h"
#import "HKMLead.h"
#import "MBProgressHUD.h"
#import "ODRefreshControl.h"
@interface HKMInviteView (private)
- (void)fadeIn;
- (void)fadeOut;
@end
@implementation HKMInviteView
@synthesize delegate;
#pragma mark - initialization & cleaning up
- (id)initWithKey:(NSString *)apiKey
title:(NSString *)aTitle
sendBtnLabel:(NSString *)sendBtnLabel
{
NSLog(@"initWithKey invoked %@", self);
CGRect rect = [[UIScreen mainScreen] applicationFrame];
if (self = [super initWithFrame:rect])
{
self.backgroundColor = [UIColor clearColor];
_title = [aTitle copy];
// register to receive all AGE notification
[self registerNotification];
// activate AGE
[HKMDiscoverer activate:[apiKey copy]];
_firstUse = [[HKMDiscoverer agent]installCode] == nil;
// start discovering address book and request IOS6 address book permission
[self launchWithPermissionCheck];
float tableViewWidth = rect.size.width - 2 * POPLISTVIEW_SCREENINSET;
float tableViewHeight = rect.size.height - 2 * POPLISTVIEW_SCREENINSET - POPLISTVIEW_HEADER_HEIGHT - POPLISTVIEW_FOOTER_HEIGHT- RADIUS;
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(POPLISTVIEW_SCREENINSET,
POPLISTVIEW_SCREENINSET + POPLISTVIEW_HEADER_HEIGHT,
tableViewWidth,
tableViewHeight)];
_tableView.separatorColor = [UIColor colorWithWhite:0 alpha:.2];
_tableView.backgroundColor = [UIColor clearColor];
_tableView.dataSource = self;
_tableView.delegate = self;
// add pulldown refresh control
ODRefreshControl *refreshControl = [[[ODRefreshControl alloc] initInScrollView:_tableView] autorelease];
[refreshControl addTarget:self action:@selector(dropViewDidBeginRefreshing:) forControlEvents:UIControlEventValueChanged];
[self addSubview:_tableView];
// add invite button
float buttonWidth = (tableViewWidth-POPLISTVIEW_BUTTONINSET*2);
float buttonHeight = POPLISTVIEW_FOOTER_HEIGHT-POPLISTVIEW_BUTTONINSET*2;
_sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[_sendButton addTarget:self
action:@selector(sendInviteAction)
forControlEvents:UIControlEventTouchUpInside];
[_sendButton setTitle:[sendBtnLabel copy] forState:UIControlStateNormal];
_sendButton.frame = CGRectMake(POPLISTVIEW_SCREENINSET+POPLISTVIEW_BUTTONINSET, POPLISTVIEW_SCREENINSET + POPLISTVIEW_HEADER_HEIGHT + tableViewHeight + POPLISTVIEW_BUTTONINSET, buttonWidth, buttonHeight);
[self addSubview:_sendButton];
}
return self;
}
- (void)dropViewDidBeginRefreshing:(ODRefreshControl *)refreshControl
{
double delayInSeconds = 0.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[refreshControl endRefreshing];
});
[MBProgressHUD showHUDAddedTo:self animated:YES];
[[HKMDiscoverer agent] queryLeads];
}
- (void)dealloc
{
[_title release];
[_tableView release];
[[NSNotificationCenter defaultCenter] removeObserver: self];
[super dealloc];
}
#pragma mark - Private Methods
- (void)fadeIn
{
self.transform = CGAffineTransformMakeScale(1.3, 1.3);
self.alpha = 0;
[UIView animateWithDuration:.35 animations:^{
self.alpha = 1;
self.transform = CGAffineTransformMakeScale(1, 1);
}];
}
- (void)fadeOut
{
[UIView animateWithDuration:.35 animations:^{
self.transform = CGAffineTransformMakeScale(1.3, 1.3);
self.alpha = 0.0;
} completion:^(BOOL finished) {
if (finished) {
[self removeFromSuperview];
}
}];
}
#pragma mark - Instance Methods
- (void)showInView:(UIView *)aView animated:(BOOL)animated
{
[aView addSubview:self];
if (animated) {
[self fadeIn];
}
}
#pragma mark - Tableview datasource & delegates
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([HKMDiscoverer agent].leads != nil) {
int rows = [[HKMDiscoverer agent].leads count];
return rows;
} else {
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentity = @"HKINviteViewCell";
HKMInviteViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity];
if (cell == nil) {
cell = [[[HKMInviteViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity] autorelease];
}
HKMLead *lead = (HKMLead *)[[HKMDiscoverer agent].leads objectAtIndex:indexPath.row];
cell.textLabel.text = lead.name;
cell.detailTextLabel.text = lead.osType;
cell.imageView.image = (lead.image) ? lead.image : [UIImage imageNamed:@"contact.png"];
if (lead.selected) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
HKMLead *lead = [[HKMDiscoverer agent].leads objectAtIndex:indexPath.row];
if (lead.selected) {
lead.selected = NO;
} else {
lead.selected = YES;
}
[_tableView reloadData];
}
#pragma mark - TouchTouchTouch
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// tell the delegate the cancellation
if (self.delegate && [self.delegate respondsToSelector:@selector(inviteCancelled)]) {
[self.delegate inviteCancelled];
}
// dismiss self
[self fadeOut];
}
#pragma mark - DrawDrawDraw
- (void)drawRect:(CGRect)rect
{
CGRect bgRect = CGRectInset(rect, POPLISTVIEW_SCREENINSET, POPLISTVIEW_SCREENINSET);
CGRect titleRect = CGRectMake(POPLISTVIEW_SCREENINSET + 10, POPLISTVIEW_SCREENINSET + 10 + 5,
rect.size.width - 2 * (POPLISTVIEW_SCREENINSET + 10), 30);
CGRect separatorRect = CGRectMake(POPLISTVIEW_SCREENINSET, POPLISTVIEW_SCREENINSET + POPLISTVIEW_HEADER_HEIGHT - 2,
rect.size.width - 2 * POPLISTVIEW_SCREENINSET, 2);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Draw the background with shadow
CGContextSetShadowWithColor(ctx, CGSizeZero, 6., [UIColor colorWithWhite:0 alpha:.75].CGColor);
[[UIColor colorWithWhite:0 alpha:.75] setFill];
float x = POPLISTVIEW_SCREENINSET;
float y = POPLISTVIEW_SCREENINSET;
float width = bgRect.size.width;
float height = bgRect.size.height;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, x, y + RADIUS);
CGPathAddArcToPoint(path, NULL, x, y, x + RADIUS, y, RADIUS);
CGPathAddArcToPoint(path, NULL, x + width, y, x + width, y + RADIUS, RADIUS);
CGPathAddArcToPoint(path, NULL, x + width, y + height, x + width - RADIUS, y + height, RADIUS);
CGPathAddArcToPoint(path, NULL, x, y + height, x, y + height - RADIUS, RADIUS);
CGPathCloseSubpath(path);
CGContextAddPath(ctx, path);
CGContextFillPath(ctx);
CGPathRelease(path);
// Draw the title and the separator with shadow
CGContextSetShadowWithColor(ctx, CGSizeMake(0, 1), 0.5f, [UIColor blackColor].CGColor);
[[UIColor colorWithRed:0.020 green:0.549 blue:0.961 alpha:1.] setFill];
[_title drawInRect:titleRect withFont:[UIFont systemFontOfSize:20.]];
CGContextFillRect(ctx, separatorRect);
}
#pragma mark - UI interaction
- (void)sendInviteAction {
_lastInviteCount = 0;
NSMutableArray *phones = [[NSMutableArray arrayWithCapacity:16] retain];
for (HKMLead *lead in [HKMDiscoverer agent].leads) {
if (lead.selected) {
[phones addObject:lead.phone];
_lastInviteCount++;
}
}
if ([phones count] > 0) {
[[HKMDiscoverer agent] newReferral:phones withName:nil useVirtualNumber:YES];
} else {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = INVITE_SELECT_SOME_FRIENDS_MSG;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[MBProgressHUD hideHUDForView:self animated:YES];
});
}
}
#pragma mark - AGE address book handling functions
- (void) launchWithPermissionCheck
{
ABAddressBookRef ab = ABAddressBookCreate();
if (ABAddressBookRequestAccessWithCompletion != NULL) {
ABAddressBookRequestAccessWithCompletion(ab, ^(bool granted, CFErrorRef error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD showHUDAddedTo:self animated:YES];
[[HKMDiscoverer agent] discover:0];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView* alert = [[UIAlertView alloc] init];
alert.title = ADDRESSBOOK_DENIED_ACCESS_ALERT_TITLE;
alert.message = ADDRESSBOOK_DENIED_ACCESS_ALERT_MSG;
[alert addButtonWithTitle:@"Dismiss"];
alert.cancelButtonIndex = 0;
[alert show];
[alert release];
});
}
});
} else {
// iOS 5
[MBProgressHUD showHUDAddedTo:self animated:YES];
[[HKMDiscoverer agent] discover:0];
}
}
// register for notification of AGE related callback events
- (void)registerNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(discoverCompleted) name:NOTIF_HOOK_DISCOVER_COMPLETE object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(discoverCompleted) name:NOTIF_HOOK_DISCOVER_NO_CHANGE object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(discoverFailed) name:NOTIF_HOOK_DISCOVER_FAILED object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryLeadsCompleted) name:NOTIF_HOOK_QUERY_ORDER_COMPLETE object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryLeadsFailed) name:NOTIF_HOOK_QUERY_ORDER_FAILED object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkError) name:NOTIF_HOOK_NETWORK_ERROR object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendReferralCompleted) name:NOTIF_HOOK_NEW_REFERRAL_COMPLETE object:nil];
}
- (void) discoverCompleted
{
if (_firstUse) {
_aTimer = [NSTimer scheduledTimerWithTimeInterval:FIRST_USE_WAIT_TIME
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:NO];
} else {
[[HKMDiscoverer agent] queryLeads];
}
}
- (void) discoverFailed {
[MBProgressHUD hideHUDForView:self animated:YES];
[self fadeOut];
}
-(void)timerFired:(NSTimer *) theTimer
{
NSLog(@"timerFired @ %@", [theTimer fireDate]);
BOOL status = [[HKMDiscoverer agent] queryLeads];
NSLog(@"timerFired - queryLeads status=%d", status);
}
- (void) queryLeadsCompleted
{
[_tableView reloadData];
[MBProgressHUD hideHUDForView:self animated:YES];
int rowsOfData = [_tableView numberOfRowsInSection:0];
if (rowsOfData == 0) {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"Pull down to refresh";
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1.5 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[MBProgressHUD hideHUDForView:self animated:YES];
});
}
}
- (void) queryLeadsFailed
{
[MBProgressHUD hideHUDForView:self animated:YES];
[self fadeOut];
}
- (void) sendReferralCompleted
{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"Invitation Sent!";
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[MBProgressHUD hideHUDForView:self animated:YES];
// tell the delegate the selection
if (self.delegate && [self.delegate respondsToSelector:@selector(invitedCount:)]) {
[self.delegate invitedCount:_lastInviteCount];
}
[self fadeOut];
});
}
- (void) networkError
{
NSLog(@"networkError invoked");
[MBProgressHUD hideHUDForView:self animated:YES];
UIAlertView* alert = [[UIAlertView alloc] init];
alert.title = NO_NETWORK_COVERAGE_ERROR_ALERT_TITLE;
alert.message = NO_NETWORK_COVERAGE_ERROR_ALERT_MSG;
[alert addButtonWithTitle:@"OK"];
alert.cancelButtonIndex = 0;
[alert show];
[alert release];
[self fadeOut];
}
@end