Skip to content

Commit

Permalink
cleaning code
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Rafaj <[email protected]>
  • Loading branch information
Ondrej Rafaj committed Oct 15, 2013
1 parent cd1e953 commit ed43930
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 51 deletions.
8 changes: 4 additions & 4 deletions iJenkins/FTAPIConnector.m
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ - (NSData *)processDictionaryIntoDataPayload:(NSDictionary *)dictionary {

- (NSURLRequest *)requestForDataObject:(id <FTAPIDataAbstractObject>)data {
NSDictionary *payload = [data payloadData];
NSString *url = [NSString stringWithFormat:@"%@%@%@", [dAccountsManager selectedAccount].baseUrl, [data methodName], [data suffix]];
NSString *url = [NSString stringWithFormat:@"%@%@%@", [[FTAccountsManager sharedManager] selectedAccount].baseUrl, [data methodName], [data suffix]];
if (payload && [data httpMethod] == FTHttpMethodGet) {
BOOL isQM = !([url rangeOfString:@"?"].location == NSNotFound);
NSString *par = [NSString stringWithFormat:@"%@%@", (isQM ? @"&" : @"?"), [NSString serializeParams:payload]];
Expand All @@ -198,15 +198,15 @@ - (NSURLRequest *)requestForDataObject:(id <FTAPIDataAbstractObject>)data {
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
dFTAPIConnectorDebugFull NSLog(@"Request URL: %@", url);

BOOL authenticate = (dAccountsManager.selectedAccount.username && dAccountsManager.selectedAccount.username.length > 1);
BOOL authenticate = ([FTAccountsManager sharedManager].selectedAccount.username && [FTAccountsManager sharedManager].selectedAccount.username.length > 1);
if (authenticate) {
[[FTAPIConnector sharedClient] clearAuthorizationHeader];
[[FTAPIConnector sharedClient] setAuthorizationHeaderWithUsername:dAccountsManager.selectedAccount.username password:dAccountsManager.selectedAccount.passwordOrToken];
[[FTAPIConnector sharedClient] setAuthorizationHeaderWithUsername:[FTAccountsManager sharedManager].selectedAccount.username password:[FTAccountsManager sharedManager].selectedAccount.passwordOrToken];
}

NSMutableURLRequest *request = [[[FTAPIConnector sharedClient] requestWithMethod:@"" path:@"" parameters:nil] mutableCopy];
[request setURL:[NSURL URLWithString:url]];
NSTimeInterval timeout = dAccountsManager.selectedAccount.timeout;
NSTimeInterval timeout = [FTAccountsManager sharedManager].selectedAccount.timeout;
if (timeout < 1.5) timeout = 8;
[request setTimeoutInterval:timeout];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
Expand Down
3 changes: 0 additions & 3 deletions iJenkins/FTAccountsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
#import "FTAccount.h"


#define dAccountsManager [FTAccountsManager sharedManager]


@interface FTAccountsManager : NSObject

@property (nonatomic, strong) FTAccount *selectedAccount;
Expand Down
14 changes: 7 additions & 7 deletions iJenkins/FTAccountsViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ - (FTAccount *)accountForIndexPath:(NSIndexPath *)indexPath {
#pragma mark Creating elements

- (void)createTableView {
_data = [dAccountsManager accounts];
_demoAccounts = [dAccountsManager demoAccounts];
_data = [[FTAccountsManager sharedManager] accounts];
_demoAccounts = [[FTAccountsManager sharedManager] demoAccounts];

[super createTableView];
}
Expand Down Expand Up @@ -296,7 +296,7 @@ - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
FTAccount *acc = [_data objectAtIndex:indexPath.row];
[dAccountsManager removeAccount:acc];
[[FTAccountsManager sharedManager] removeAccount:acc];
[tableView reloadData];
}
}
Expand Down Expand Up @@ -357,7 +357,7 @@ - (UITableViewCell *)accountCellForIndexPath:(NSIndexPath *)indexPath {

// TODO: Finish the API request to check server API, not just reachability
/*
[dAccountsManager setSelectedAccount:acc];
[[FTAccountsManager sharedManager] setSelectedAccount:acc];
FTAPIOverallLoadDataObject *loadObject = [[FTAPIOverallLoadDataObject alloc] init];
[FTAPIConnector connectWithObject:loadObject andOnCompleteBlock:^(id<FTAPIDataAbstractObject> dataObject, NSError *error) {
if (error) {
Expand Down Expand Up @@ -431,7 +431,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
if (indexPath.section != 3) {
if ([self datasourceForIndexPath:indexPath].count > 0) {
FTAccount *acc = [self accountForIndexPath:indexPath];
[dAccountsManager setSelectedAccount:acc];
[[FTAccountsManager sharedManager] setSelectedAccount:acc];
[FTAPIConnector resetForAccount:acc];

FTServerHomeViewController *c = [[FTServerHomeViewController alloc] init];
Expand Down Expand Up @@ -476,7 +476,7 @@ - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtInd
#pragma mark Add account view controller delegate methods

- (void)addAccountViewController:(FTAddAccountViewController *)controller didAddAccount:(FTAccount *)account {
[dAccountsManager addAccount:account];
[[FTAccountsManager sharedManager] addAccount:account];
[self reloadData];
[self scrollToAccount:account];
[self dismissViewControllerAnimated:YES completion:^{
Expand All @@ -485,7 +485,7 @@ - (void)addAccountViewController:(FTAddAccountViewController *)controller didAdd
}

- (void)addAccountViewController:(FTAddAccountViewController *)controller didModifyAccount:(FTAccount *)account {
[dAccountsManager updateAccount:account];
[[FTAccountsManager sharedManager] updateAccount:account];
[self reloadData];
[self scrollToAccount:account];
[self dismissViewControllerAnimated:YES completion:^{
Expand Down
8 changes: 4 additions & 4 deletions iJenkins/FTLoginAlert.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ - (void)showLoginDialogWithLoginBlock:(FTLoginAlertLoginBlock)loginBlock andCanc

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:FTLangGet(@"Please login") message:nil delegate:self cancelButtonTitle:FTLangGet(@"Cancel") otherButtonTitles:FTLangGet(@"Login"), nil];
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[alert textFieldAtIndex:0] setText:dAccountsManager.selectedAccount.username];
[[alert textFieldAtIndex:0] setText:[FTAccountsManager sharedManager].selectedAccount.username];
[[alert textFieldAtIndex:0] setPlaceholder:FTLangGet(@"Username")];
[alert show];
}
Expand All @@ -55,10 +55,10 @@ - (void)showLoginDialogWithLoginBlock:(FTLoginAlertLoginBlock)loginBlock andCanc

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
if (alertView.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput && (buttonIndex == 1)) {
[dAccountsManager.selectedAccount setUsername:[alertView textFieldAtIndex:0].text];
[dAccountsManager.selectedAccount setPasswordOrToken:[alertView textFieldAtIndex:1].text];
[[FTAccountsManager sharedManager].selectedAccount setUsername:[alertView textFieldAtIndex:0].text];
[[FTAccountsManager sharedManager].selectedAccount setPasswordOrToken:[alertView textFieldAtIndex:1].text];
if (currentLoginBlock) {
currentLoginBlock(dAccountsManager.selectedAccount.username, dAccountsManager.selectedAccount.passwordOrToken);
currentLoginBlock([FTAccountsManager sharedManager].selectedAccount.username, [FTAccountsManager sharedManager].selectedAccount.passwordOrToken);
}
}
else {
Expand Down
2 changes: 1 addition & 1 deletion iJenkins/FTManageViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
if (!cell) {
cell = [[FTIconCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
BOOL ok = (dAccountsManager.selectedAccount.username && dAccountsManager.selectedAccount.username.length > 0);
BOOL ok = ([FTAccountsManager sharedManager].selectedAccount.username && [FTAccountsManager sharedManager].selectedAccount.username.length > 0);
NSDictionary *d = _data[indexPath.row];
if (![d[@"loginRequired"] boolValue]) {
ok = YES;
Expand Down
61 changes: 30 additions & 31 deletions iJenkins/FTServerHomeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ @implementation FTServerHomeViewController
- (void)loadData {
if (!_serverObject) {
_isDataAvailable = NO;
self.searchBar.text = @"";
_searchBar.text = @"";

_serverObject = [[FTAPIServerDataObject alloc] init];
if (_selectedView) {
Expand All @@ -70,8 +70,8 @@ - (void)loadData {
}
}
else {
if (dAccountsManager.selectedAccount.accountType == FTAccountTypeKeychain) {
[dAccountsManager updateAccount:dAccountsManager.selectedAccount];
if ([FTAccountsManager sharedManager].selectedAccount.accountType == FTAccountTypeKeychain) {
[[FTAccountsManager sharedManager] updateAccount:[FTAccountsManager sharedManager].selectedAccount];
}
[_overviewCell setJobsStats:_serverObject.jobsStats];
if (_serverObject.jobs.count > 0) {
Expand All @@ -84,9 +84,9 @@ - (void)loadData {
_views = _serverObject.views;
}

self.jobs = [NSArray arrayWithArray:_serverObject.jobs];
_jobs = [NSArray arrayWithArray:_serverObject.jobs];
[super.tableView reloadData];
[self setTitle:dAccountsManager.selectedAccount.name];
[self setTitle:[FTAccountsManager sharedManager].selectedAccount.name];

if (_serverObject.views.count > 1) {
if (!_selectedView) {
Expand All @@ -105,34 +105,19 @@ - (void)loadData {
}
}

#pragma mark Search bar delegate

- (void)filterSearchResultsWithSearchString:(NSString *)searchString
{
NSMutableArray *arr = [NSMutableArray array];

for (FTAPIJobDataObject *job in _serverObject.jobs) {
NSRange isRange = [job.name rangeOfString:searchString options:NSCaseInsensitiveSearch];
if (isRange.location != NSNotFound) {
[arr addObject:job];
}
}
self.searchResults = arr;
}

#pragma mark Creating elements

- (void)createTableView {
[super createTableView];

self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, super.tableView.width, 44)];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.width, 44)];

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;
_searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
[_searchController setSearchResultsDataSource:self];
[_searchController setSearchResultsDelegate:self];
[_searchController setDelegate:self];

[self.tableView setTableHeaderView:self.searchBar];
[self.tableView setTableHeaderView:_searchBar];

_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:@selector(refreshActionCalled:) forControlEvents:UIControlEventValueChanged];
Expand Down Expand Up @@ -182,6 +167,20 @@ - (void)showViewSelector:(UIBarButtonItem *)sender {
[self presentViewController:nc animated:YES completion:NULL];
}

#pragma mark Search bar delegate

- (void)filterSearchResultsWithSearchString:(NSString *)searchString {
NSMutableArray *arr = [NSMutableArray array];

for (FTAPIJobDataObject *job in _serverObject.jobs) {
NSRange isRange = [job.name rangeOfString:searchString options:NSCaseInsensitiveSearch];
if (isRange.location != NSNotFound) {
[arr addObject:job];
}
}
_searchResults = arr;
}

#pragma mark Table view delegate and data source methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
Expand All @@ -194,12 +193,12 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
if ([self isJobsSection:section]) {
return [self.jobs count];
return [_jobs count];
}
else return 3;
}
else {
return [self.searchResults count];
return [_searchResults count];
}
}

Expand Down Expand Up @@ -325,7 +324,7 @@ - (FTIconCell *)iconCellForRowAtIndexPath:(NSIndexPath *)indexPath {
[cell.iconView setDefaultIconIdentifier:@"icon-cogs"];
[cell.textLabel setText:FTLangGet(@"Manage Jenkins")];
// TODO: Decide if the manage section is only for logged in users!
if ((dAccountsManager.selectedAccount.username && dAccountsManager.selectedAccount.username.length > 0) || YES) {
if (([FTAccountsManager sharedManager].selectedAccount.username && [FTAccountsManager sharedManager].selectedAccount.username.length > 0) || YES) {
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
[cell setSelectionStyle:UITableViewCellSelectionStyleDefault];
[cell.iconView setAlpha:1];
Expand Down Expand Up @@ -355,7 +354,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
return [self iconCellForRowAtIndexPath:indexPath];
}
}
else if ([self.jobs count] == 0) {
else if ([_jobs count] == 0) {
return [self cellForNoJob];
}
else {
Expand Down Expand Up @@ -463,7 +462,7 @@ - (FTAPIJobDataObject *)jobAtIndexPath:(NSIndexPath *)indexPath inTableView:(UIT
return nil;
}

NSArray *dataSource = (tableView == self.tableView ? self.jobs : self.searchResults);
NSArray *dataSource = (tableView == self.tableView ? _jobs : _searchResults);
NSUInteger dataCount = [dataSource count];

if (dataCount > 0 && indexPath.row < dataCount) {
Expand Down
2 changes: 1 addition & 1 deletion iJenkins/FTUserDetailViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ - (void)setupView {
#pragma mark Tableview delegate & datasource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return (_isLoading || [_nickName isEqualToString:dAccountsManager.selectedAccount.username]) ? 1 : 2;
return (_isLoading || [_nickName isEqualToString:[FTAccountsManager sharedManager].selectedAccount.username]) ? 1 : 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
Expand Down

0 comments on commit ed43930

Please sign in to comment.