-
Notifications
You must be signed in to change notification settings - Fork 1
/
BuildsController.m
400 lines (299 loc) · 11.8 KB
/
BuildsController.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
//
// BuildsController.m
// Broken
//
// Created by Mujtaba Hussain on 7/04/11.
// Copyright 2011 REA Group. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "BuildsController.h"
#import "BrokenBuildController.h"
#import "JenkinsInstanceController.h"
#import "OverlayView.h"
#import "BuildsFilterView.h"
@implementation BuildsController
-(id)initWithStyle:(UITableViewStyle)style;
{
return [self initWithStyle:style address:nil];
}
- (id) initWithStyle:(UITableViewStyle)style defaults:(NSUserDefaults *)defaults;
{
NSString *host = [defaults objectForKey:@"host"];
NSString *port = [defaults objectForKey:@"port"];
NSString *address = [NSString stringWithFormat:@"%@:%@/api/json", host, port];
return [self initWithStyle:style address:address];
}
- (id)initWithStyle:(UITableViewStyle)style address:(NSString *)address;
{
self = [super initWithStyle:style];
if (self) {
[[[self navigationController] navigationBar] setBarStyle:UIBarStyleBlackTranslucent];
[[self tableView] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"Background.png"]]];
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"Settings.png"]
style:UIButtonTypeRoundedRect
target:self
action:@selector(settings)] autorelease] animated:YES];
[[self navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"Refresh.png"]
style:UIButtonTypeRoundedRect
target:self
action:@selector(refresh)] autorelease] animated:YES];
[self setTitle:@"Builds"];
[[self navigationController] setNavigationBarHidden:YES];
searchBar_ = [[[UISearchBar alloc] initWithFrame:CGRectMake(2., 5., 12., 15.)] autorelease];
[searchBar_ setDelegate:self];
[searchBar_ setShowsCancelButton:YES animated:YES];
[searchBar_ setBarStyle:UIBarStyleBlackTranslucent];
[searchBar_ setPlaceholder:@"Search builds"];
[searchBar_ setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[searchBar_ respondsToSelector:@selector(searchBarTapped)];
[searchBar_ sizeToFit];
CGRect overlayFrame = CGRectMake(0., [searchBar_ frame].size.height, [[self tableView] bounds].size.width, [[self tableView] bounds].size.height);
OverlayView *overlayView = [[[OverlayView alloc] initWithFrame:overlayFrame] autorelease];
[overlayView addGestureRecognizer:[[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tappedOverlay)] autorelease]];
[self setOverlay:overlayView];
[[self tableView] setTableHeaderView:searchBar_];
BuildsFilterView *filterView = [[[BuildsFilterView alloc] initWithFrame:CGRectMake(0., 0., 320., 40.)] autorelease];
[filterView setDelegate:self];
[[self tableView] setTableFooterView:filterView];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:address]];
[request setDelegate:self];
[request startAsynchronous];
}
return self;
}
- (void)dealloc;
{
[searchBar_ dealloc];
[_builds dealloc];
[allBuilds_ dealloc];
[overlay_ dealloc];
[super dealloc];
}
@synthesize builds = _builds;
@synthesize allBuilds = allBuilds_;
@synthesize overlay = overlay_;
@synthesize searchBar = searchBar_;
#pragma mark - NavBarButtons
- (void)refresh;
{
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
NSString *address = [NSString stringWithFormat:@"%@:%@/api/json",
[settings objectForKey:@"host"],
[settings objectForKey:@"port"]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:address]];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)settings;
{
JenkinsInstanceController *settings = [[[JenkinsInstanceController alloc] initWithNibName:nil bundle:nil] autorelease];
[[self navigationController] pushViewController:settings animated:YES];
}
- (void)tappedOverlay;
{
[[self overlay] removeFromSuperview];
[[self searchBar] resignFirstResponder];
}
#pragma mark - ASIHTTPRequestDelegate
- (void)requestStarted:(ASIHTTPRequest *)request;
{
}
- (void)requestFinished:(ASIHTTPRequest *)request;
{
NSString *json_string = [request responseString];
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
NSDictionary *object = [parser objectWithString:json_string error:nil];
NSArray *builds = [[object objectForKey:@"jobs"] asBuilds];
[self setBuilds:(NSMutableArray *)builds];
[self setAllBuilds:builds];
[[self tableView] reloadData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"Error Fetching Data %@",[error description]);
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc]
initWithTitle:@"Settings"
style:UIButtonTypeRoundedRect
target:self
action:@selector(settings)] autorelease] animated:YES];
[super viewDidLoad];
[[self navigationItem] setHidesBackButton:YES];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
//return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self builds] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
Build *build = [[self builds] objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[build name]];
[[cell textLabel] setTextColor:[build currentState]];
if ([build isBroken]) {
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
else if([build isStable]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else {
UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
[spinner startAnimating];
[spinner setHidden:NO];
[cell setAccessoryView:spinner];
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Build *build = [[self builds] objectAtIndex:[indexPath row]];
if ([build isStable]) {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Stable build"
message:@"Well Done!"
delegate:nil
cancelButtonTitle:@"OK!"
otherButtonTitles:nil, nil] autorelease];
[alert show];
}
else if ([build isBroken]) {
BrokenBuildController *brokenBuildController = [[[BrokenBuildController alloc] initWithNibName:nil
bundle:nil
brokenBuild:build] autorelease];
[[self navigationController] pushViewController:brokenBuildController animated:YES];
}
else {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Building"
message:@"Please let it build!"
delegate:nil
cancelButtonTitle:@"OK!"
otherButtonTitles:nil, nil] autorelease];
[alert show];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - SearchBarDelegate
- (void)searchBarTapped;
{
[self becomeFirstResponder];
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;
{
[searchBar setShowsScopeBar:YES];
[searchBar sizeToFit];
[searchBar setShowsCancelButton:YES animated:YES];
[[self view] addSubview:[self overlay]];
[UIView beginAnimations:@"FadeIn" context:nil];
[UIView setAnimationDuration:0.5];
[UIView commitAnimations];
return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;
{
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar sizeToFit];
[searchBar setShowsScopeBar:NO];
return YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
{
NSString *buildToFind = [searchBar text];
if (buildToFind == nil) {
return;
}
NSMutableArray *searchResults = [self searchForBuild:buildToFind];
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
[[self overlay] removeFromSuperview];
if (searchResults != nil) {
[self setBuilds:searchResults];
[[self tableView] reloadData];
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;
{
[searchBar setText:@""];
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
[[self overlay] removeFromSuperview];
}
#pragma mark - Search
- (NSMutableArray *)searchForBuild:(NSString *)buildName;
{
NSMutableArray *matchedBuilds = [[[NSMutableArray alloc] init] autorelease];
if (nil != buildName) {
for (Build *build in _builds) {
if ([buildName isEqualToString:[build name]]) {
[matchedBuilds insertObject:build atIndex:0];
}
}
}
return matchedBuilds;
}
#pragma mark - BuildsFilterViewDelegate
- (void)filterAllBuilds;
{
[self setBuilds:(NSMutableArray *)allBuilds_];
[[self tableView] reloadData];
}
- (void)filterFailingBuilds;
{
[self setBuilds:(NSMutableArray *)allBuilds_];
NSMutableArray *failingBuilds = [[[NSMutableArray alloc] init] autorelease];
for (Build *build in _builds) {
if ([build isBroken]) {
[failingBuilds insertObject:build atIndex:0];
}
}
[self setBuilds:failingBuilds];
[[self tableView] reloadData];
}
- (void)filterPassingBuilds;
{
[self setBuilds:(NSMutableArray *)allBuilds_];
NSMutableArray *failingBuilds = [[[NSMutableArray alloc] init] autorelease];
for (Build *build in _builds)
{
if ([build isStable])
{
[failingBuilds insertObject:build atIndex:0];
}
}
[self setBuilds:failingBuilds];
[[self tableView] reloadData];
}
@end