-
Notifications
You must be signed in to change notification settings - Fork 93
/
HUServerListViewController.m
129 lines (100 loc) · 3.8 KB
/
HUServerListViewController.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
//
// HUServerListViewController.m
// Spika
//
// Created by Josip Marković on 27.03.2014..
//
//
#import "HUServerListViewController.h"
#import "HUServerListCell.h"
#import "DatabaseManager.h"
#import "ModelServer.h"
#import "AlertViewManager.h"
#import "HUHTTPClient.h"
@interface HUServerListViewController ()
@end
@implementation HUServerListViewController {
NSMutableArray *serverList;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIColor *color = [UIColor grayColor];
self.addServerText.attributedPlaceholder = [[NSAttributedString alloc]
initWithString: NSLocalizedString(@"AddServerHint", @"")
attributes:@{NSForegroundColorAttributeName: color}];
serverList = [NSMutableArray array];
DMArrayBlock successBlock = ^(NSArray *servers) {
for (NSDictionary *dictionary in servers) {
ModelServer *server = [[ModelServer alloc] initWithDictionary:dictionary];
[serverList addObject:server];
}
[self.tableViewServerList reloadData];
[[AlertViewManager defaultManager] dismiss];
};
[[AlertViewManager defaultManager] showWaiting:@"" message:@""];
[[DatabaseManager defaultManager] getServerListWithSuccess:successBlock andError:nil];}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = NO;
[self addBackButton];
self.addServerText.placeholder = NSLocalizedString(@"AddServerHint", @"");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ModelServer *server = [serverList objectAtIndex:indexPath.row];
static NSString *serverListCellIdentifier = @"serverListCell";
HUServerListCell *cell = (HUServerListCell *)[tableView dequeueReusableCellWithIdentifier:serverListCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HUServerListCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.serverLabel.text = server.name;
cell.serverUrl.text = server.url;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ModelServer *server = [serverList objectAtIndex:indexPath.row];
[[NSUserDefaults standardUserDefaults] setObject:server.url forKey:serverBaseURLprefered];
[[NSUserDefaults standardUserDefaults] setObject:server.name forKey:serverBaseNamePrefered];
[HUHTTPClient refreshClient];
NSString *selected = server.name;
[self.delegate addItemViewController:self didFinishEnteringItem:selected];
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return serverList.count;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 82;
}
#pragma mark - Override
- (NSString *) title {
return NSLocalizedString(@"ServerList-Title", @"");
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *selected = textField.text;
[[NSUserDefaults standardUserDefaults] setObject:selected forKey:serverBaseURLprefered];
[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:serverBaseNamePrefered];
[HUHTTPClient refreshClient];
[self.delegate addItemViewController:self didFinishEnteringItem:selected];
return NO;
}
@end