-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDisplayBuildsController.m
554 lines (439 loc) · 17 KB
/
DisplayBuildsController.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
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
//
// DisplayBuildsController.m
// iBroke
//
// Created by Mujtaba Hussain on 19/10/11.
// This code is distributed under the terms and conditions of the MIT license.
//
// Copyright (c) 2011 Mujtaba Hussain
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "DisplayBuildsController.h"
#import "ASIHTTPRequest.h"
#import "SBJson.h"
#import "SettingsController.h"
#import "AddressFormatter.h"
#import "BuildDashboard.h"
#import "MeaningfulErrors.h"
#import "DisplayBuildsView.h"
#import "BuildDetailController.h"
#import "Build.h"
#import "NetworkCheck.h"
#import "UIColor+Hex.h"
#import "NSArray+Blocks.h"
#import "UINavigationBar+Utilities.h"
#import "LoadingView.h"
#import "ConnectionFailedView.h"
#import "NotificationView.h"
#define kHiddenBuildsKey @"kHiddenBuildsKey"
@interface DisplayBuildsController ()
@property (nonatomic, retain) DisplayBuildsView *buildsView;
@property (nonatomic, retain) NSMutableArray *hiddenBuilds;
@property (nonatomic, retain) NSArray *filteredBuilds;
@property (nonatomic, retain) UIBarButtonItem *edit;
@property (nonatomic, retain) UIBarButtonItem *done;
@property (nonatomic) BOOL inFullTableEditMode;
- (NSArray*)builds;
- (UIColor *)colorForStatus:(Build *)build;
- (void)saveHiddenBuilds;
- (void)restoreHiddenBuilds;
- (void)editTableView;
@end
@implementation DisplayBuildsController
@synthesize reachability = _reachability;
@synthesize buildsView = _buildsView;
@synthesize address = _address;
@synthesize buildData = _buildData;
@synthesize inFullTableEditMode = _inFullTableEditMode;
@synthesize hiddenBuilds = _hiddenBuilds;
@synthesize filteredBuilds = _filteredBuilds;
@synthesize edit =_edit;
@synthesize done = _done;
- (id)initWithAddress:(NSString *)address;
{
self = [super initWithNibName:nil bundle:nil];
if (self)
{
[[self navigationItem] setHidesBackButton:YES animated:NO];
UIImage *image = [UIImage imageNamed:@"Settings"];
UIButton *settings = [[UIButton alloc] initWithFrame:CGRectMake(0., 0., 35., 35.)];
[settings setImage:image forState:UIControlStateNormal];
[settings addTarget:self action:@selector(settings) forControlEvents:UIControlEventTouchUpInside];
[settings setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithCustomView:settings];
[[self navigationItem] setRightBarButtonItem:settingsButton animated:YES];
UIButton *editButton = [[UIButton alloc] initWithFrame:CGRectMake(0., 0., 35., 35.)];
[editButton setImage:[UIImage imageNamed:@"Edit"] forState:UIControlStateNormal];
[editButton addTarget:self action:@selector(editTableView) forControlEvents:UIControlEventTouchUpInside];
[editButton setShowsTouchWhenHighlighted:YES];
_edit = [[UIBarButtonItem alloc] initWithCustomView:editButton];
UIButton *doneButton = [[UIButton alloc] initWithFrame:CGRectMake(0., 0., 30., 30.)];
[doneButton setImage:[UIImage imageNamed:@"Done"] forState:UIControlStateNormal];
[doneButton addTarget:self action:@selector(editTableView) forControlEvents:UIControlEventTouchUpInside];
[doneButton setShowsTouchWhenHighlighted:YES];
_done = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
[[self navigationItem] setLeftBarButtonItem:_edit animated:YES];
[[[self navigationItem] leftBarButtonItem] setStyle:UIBarButtonItemStyleBordered];
[[[self navigationItem] leftBarButtonItem] setEnabled:NO];
[self setTitle:@"Builds"];
[self setAddress:address];
[self connectToAddress];
[self setHiddenBuilds:[NSMutableArray array]];
_buildsView = [[DisplayBuildsView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
[_buildsView setDelegate:self];
[_buildsView setDataSource:self];
[self setView:_buildsView];
}
return self;
}
- (void)settings;
{
[[self navigationController] pushViewController:[[SettingsController alloc] initWithNibName:nil bundle:nil] animated:YES];
}
- (void)editTableView;
{
BOOL edit = ![self inFullTableEditMode];
if (edit)
{
[[self navigationItem] setLeftBarButtonItem:_done animated:YES];
}
else
{
[[self navigationItem] setLeftBarButtonItem:_edit animated:YES];
}
[self setEditing:edit animated:YES];
}
#pragma mark - ShakeToReload
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
{
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
{
if (event.type == UIEventSubtypeMotionShake) {
[self connectToAddress];
}
}
#pragma mark - ASyncRequest
- (void)connectToAddress;
{
if (![NetworkCheck isNetworkAvailable])
{
NotificationView *noNetwork = [[NotificationView alloc] initWithFrame:CGRectZero
andMessage:[NetworkCheck error]
andType:kErrorNotification];
[noNetwork setNeedsLayout];
[[self view] addSubview:noNetwork];
[UIView animateWithDuration:3.0
animations:^{
[noNetwork setAlpha:0.0];
}
completion:^(BOOL finished) {
[noNetwork removeFromSuperview];
}
];
}
else
{
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[self address]]];
[request setDelegate:self];
[request setCachePolicy:ASIDoNotWriteToCacheCachePolicy];
[request startAsynchronous];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - ASIHTTPRequestDelegate
- (void)requestStarted:(ASIHTTPRequest *)request;
{
LoadingView *loadingView = [[LoadingView alloc] initWithFrame:[[self buildsView] bounds] andMessage:@"Loading build dashboard..."];
[[self buildsView] addSubview:loadingView];
[[self editButtonItem] setEnabled:NO];
[[self buildsView] setNeedsDisplay];
}
- (void)requestFinished:(ASIHTTPRequest *)request;
{
UIView *loadingView = [[self buildsView] viewWithTag:3];
[loadingView removeFromSuperview];
// Set up the animation
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionFade];
[[loadingView layer] addAnimation:animation forKey:@"layerAnimation"];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *buildData = [parser objectWithString:[request responseString] error:nil];
BuildDashboard *dashboard = [[BuildDashboard alloc] initWithBuildData:buildData];
[self setBuildData:[dashboard builds]];
[self restoreHiddenBuilds];
[self setFilteredBuilds:nil];
[[[self navigationItem] leftBarButtonItem] setEnabled:YES];
[[self buildsView] reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSString *message = [MeaningfulErrors messageForErrorCode:[[request error] code]];
NotificationView *noNetwork = [[NotificationView alloc] initWithFrame:CGRectZero
andMessage:message
andType:kErrorNotification];
[noNetwork setNeedsLayout];
[[self view] addSubview:noNetwork];
[UIView animateWithDuration:3.0
animations:^{
[noNetwork setAlpha:0.0];
}
completion:^(BOOL finished) {
[noNetwork removeFromSuperview];
}
];
UIView *loadingView = [[self buildsView] viewWithTag:3];
[loadingView removeFromSuperview];
NSLog(@"Error Fetching Data %@",[error description]);
[[self buildsView] reloadData];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;
{
[super setEditing:editing animated:animated];
[self setFilteredBuilds:nil];
[[self buildsView] setEditing:editing animated:animated];
double delayInSeconds = .4;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSMutableArray *indexPaths = [NSMutableArray array];
if (editing)
{
[[self hiddenBuilds] each:^(id item) {
[indexPaths addObject:[NSIndexPath indexPathForRow:[[self builds] indexOfObject:item] inSection:0]];
}];
[[self buildsView] insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
double delayInSeconds = .4;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[[self buildsView] reloadData];
});
}
else
{
[[self hiddenBuilds] each:^(id item) {
[indexPaths addObject:[NSIndexPath indexPathForRow:[[self buildData] indexOfObject:item] inSection:0]];
}];
[[self buildsView] deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
});
[self setInFullTableEditMode:editing];
}
- (NSArray *)filteredBuilds;
{
if (!_filteredBuilds)
{
_filteredBuilds = [[self buildData] filter:^BOOL(id item) {
Build *build = (Build*)item;
return [[self hiddenBuilds] containsObject:build];
}];
}
return _filteredBuilds;
}
- (NSArray *)builds;
{
return [self inFullTableEditMode] ? [self buildData] : [self filteredBuilds];
}
#pragma mark - View lifecycle
- (void)viewWillAppear:(BOOL)animated;
{
[super viewWillAppear:animated];
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView;
{
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
Build *build = [[self builds] objectAtIndex:[indexPath row]];
if ([build isFailed])
{
BuildDetailController *detailViewController = [[BuildDetailController alloc] initWithBuild:build];
[[self navigationController] pushViewController:detailViewController animated:YES];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath;
{
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL editingMode = [[self hiddenBuilds] containsObject:[[self builds] objectAtIndex:[indexPath row]]];
if (editingMode == YES)
{
// UIImageView *show = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"show"]];
// [[tableView cellForRowAtIndexPath:indexPath] setEditingAccessoryView:show];
return UITableViewCellEditingStyleInsert;
}
else
{
// UIImageView *show = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hide"]];
// [[tableView cellForRowAtIndexPath:indexPath] setEditingAccessoryView:show];
return UITableViewCellEditingStyleDelete;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"Hide";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
Build *build = [[self builds] objectAtIndex:[indexPath row]];
if (![self inFullTableEditMode])
{
[[self hiddenBuilds] addObject:build];
[self saveHiddenBuilds];
[self setFilteredBuilds:nil];
[[self buildsView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
if ([[self hiddenBuilds] containsObject:build])
{
[[self hiddenBuilds] removeObject:build];
}
else
{
[[self hiddenBuilds] addObject:build];
}
[self saveHiddenBuilds];
[self setFilteredBuilds:nil];
[[self buildsView] reloadData];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return [[self builds] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (nil == cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
[[cell textLabel] setFont:[UIFont fontWithName:@"Futura-Medium" size:18.]];
[cell setBackgroundView :[[UIImageView alloc] init]];
[cell setSelectedBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row-middle.png"]]];
}
Build *build = [[self builds] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[build name]];
[[cell textLabel] setTextColor:[self colorForStatus:build]];
[cell setAccessoryView:nil];
[cell setAccessoryType:UITableViewCellAccessoryNone];
[[cell detailTextLabel] setText:@""];
if ([build isFailed])
{
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
UIImageView *disclosure = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"disclosure"]];
[disclosure setFrame:CGRectMake(0., 0., 15., 15.)];
[cell setAccessoryView:disclosure];
}
else if ([build isDisabled])
{
[[cell detailTextLabel] setText:@"Disabled"];
[[cell detailTextLabel] setFont:[UIFont fontWithName:@"Verdana" size:10.]];
UIImageView *disabled = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"disabled"]];
[disabled setFrame:CGRectMake(0., 0., 15., 15.)];
[cell setAccessoryView:disabled];
[[cell detailTextLabel] setTextColor:[self colorForStatus:build]];
}
else if ([build isBuilding])
{
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[spinner startAnimating];
[spinner setHidden:NO];
[cell setAccessoryView:spinner];
}
else if([build isAborted])
{
UIImageView *aborted = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"aborted"]];
[aborted setFrame:CGRectMake(0., 0., 15., 15.)];
[cell setAccessoryView:aborted];
[[cell detailTextLabel] setText:@"Aborted"];
[[cell detailTextLabel] setFont:[UIFont fontWithName:@"Verdana" size:10.]];
[[cell detailTextLabel] setTextColor:[self colorForStatus:build]];
}
else // passing
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
[[cell detailTextLabel] setText:@""];
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return 1;
}
#pragma - Persistence
-(void)saveHiddenBuilds;
{
NSArray *buildIds = [[self hiddenBuilds] map:^id<NSObject>(id<NSObject> item) {
Build *build = (Build*)item;
return [NSString stringWithFormat:@"%@::%@", [build url], [build name]];
}];
[[NSUserDefaults standardUserDefaults] setObject:buildIds forKey:kHiddenBuildsKey];
}
-(void)restoreHiddenBuilds;
{
NSArray *buildIds = [[NSUserDefaults standardUserDefaults] objectForKey:kHiddenBuildsKey];
[self setHiddenBuilds:[NSMutableArray arrayWithArray:[[self buildData] pick:^BOOL(id item) {
Build *build = (Build*)item;
return [buildIds containsObject:[NSString stringWithFormat:@"%@::%@", [build url], [build name]]];
}]]];
}
#pragma mark - Private methods
- (UIColor *)colorForStatus:(Build *)build;
{
if ([build isFailed]) return [UIColor colorWithHexString:@"CC0033"];
if ([build isBuilding]) return [UIColor colorWithHexString:@"0066CC"];
if ([build isDisabled]) return [UIColor colorWithHexString:@"B5B5B5"];
if ([build isAborted]) return [UIColor colorWithHexString:@"42526C"];
return [UIColor colorWithHexString:@"458B00"]; // green
}
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
@end