Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MUFavouriteServerEditViewController.m « Classes « Source - github.com/mumble-voip/mumble-iphoneos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8b036b3ce5e2b1f65c403dbcb93fa0ef2a192047 (plain)
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
// Copyright 2009-2010 The 'Mumble for iOS' Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#import "MUFavouriteServerEditViewController.h"

#import "MUColor.h"
#import "MUDatabase.h"
#import "MUFavouriteServer.h"
#import "MUTableViewHeaderLabel.h"
#import "MUImage.h"
#import "MUOperatingSystem.h"
#import "MUBackgroundView.h"

@interface MUFavouriteServerEditViewController () {
    BOOL               _editMode;
    MUFavouriteServer  *_favourite;
    id                 _target;
    SEL                _doneAction;

    UITableViewCell    *_descriptionCell;
    UITextField        *_descriptionField;
    UITableViewCell    *_addressCell;
    UITextField        *_addressField;
    UITableViewCell    *_portCell;
    UITextField        *_portField;
    UITableViewCell    *_usernameCell;
    UITextField        *_usernameField;
    UITableViewCell    *_passwordCell;
    UITextField        *_passwordField;

    UITextField        *_activeTextField;
    UITableViewCell    *_activeCell;
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView;
@end

@implementation MUFavouriteServerEditViewController

#pragma mark -
#pragma mark Initialization

- (id) initInEditMode:(BOOL)editMode withContentOfFavouriteServer:(MUFavouriteServer *)favServ {
    if ((self = [super initWithStyle:UITableViewStyleGrouped])) {
        _editMode = editMode;
        if (favServ) {
            _favourite = [favServ copy];
        } else {
            _favourite = [[MUFavouriteServer alloc] init];
        }
        
        _descriptionCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MUFavouriteServerDescription"];
        [_descriptionCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [[_descriptionCell textLabel] setText:NSLocalizedString(@"Description", nil)];
        _descriptionField = [[UITextField alloc] initWithFrame:CGRectMake(110.0, 10.0, 185.0, 30.0)];
        [_descriptionField setTextColor:[MUColor selectedTextColor]];
        [_descriptionField addTarget:self action:@selector(textFieldBeganEditing:) forControlEvents:UIControlEventEditingDidBegin];
        [_descriptionField addTarget:self action:@selector(textFieldEndedEditing:) forControlEvents:UIControlEventEditingDidEnd];
        [_descriptionField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [_descriptionField addTarget:self action:@selector(textFieldDidEndOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
        [_descriptionField setReturnKeyType:UIReturnKeyNext];
        [_descriptionField setAdjustsFontSizeToFitWidth:NO];
        [_descriptionField setTextAlignment:UITextAlignmentLeft];
        [_descriptionField setPlaceholder:NSLocalizedString(@"Mumble Server", nil)];
        [_descriptionField setAutocapitalizationType:UITextAutocapitalizationTypeWords];
        [_descriptionField setText:[_favourite displayName]];
        [_descriptionField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [[_descriptionCell contentView] addSubview:_descriptionField];

        _addressCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MUFavouriteServerAddress"];
        [_addressCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [[_addressCell textLabel] setText:NSLocalizedString(@"Address", nil)];
        _addressField = [[UITextField alloc] initWithFrame:CGRectMake(110.0, 10.0, 185.0, 30.0)];
        [_addressField setTextColor:[MUColor selectedTextColor]];
        [_addressField addTarget:self action:@selector(textFieldBeganEditing:) forControlEvents:UIControlEventEditingDidBegin];
        [_addressField addTarget:self action:@selector(textFieldEndedEditing:) forControlEvents:UIControlEventEditingDidEnd];
        [_addressField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [_addressField addTarget:self action:@selector(textFieldDidEndOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
        [_addressField setReturnKeyType:UIReturnKeyNext];
        [_addressField setAdjustsFontSizeToFitWidth:NO];
        [_addressField setTextAlignment:UITextAlignmentLeft];
        [_addressField setPlaceholder:NSLocalizedString(@"Hostname or IP address", nil)];
        [_addressField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [_addressField setAutocorrectionType:UITextAutocorrectionTypeNo];
        [_addressField setKeyboardType:UIKeyboardTypeURL];
        [_addressField setText:[_favourite hostName]];
        [_addressField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [[_addressCell contentView] addSubview:_addressField];

        _portCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MUFavouriteServerPort"];
        [_portCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [[_portCell textLabel] setText:NSLocalizedString(@"Port", nil)];
        _portField = [[UITextField alloc] initWithFrame:CGRectMake(110.0, 10.0, 185.0, 30.0)];
        [_portField setTextColor:[MUColor selectedTextColor]];
        [_portField addTarget:self action:@selector(textFieldBeganEditing:) forControlEvents:UIControlEventEditingDidBegin];
        [_portField addTarget:self action:@selector(textFieldEndedEditing:) forControlEvents:UIControlEventEditingDidEnd];
        [_portField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [_portField addTarget:self action:@selector(textFieldDidEndOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
        [_portField setReturnKeyType:UIReturnKeyNext];
        [_portField setAdjustsFontSizeToFitWidth:YES];
        [_portField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [_portField setAutocorrectionType:UITextAutocorrectionTypeNo];
        [_portField setTextAlignment:UITextAlignmentLeft];
        [_portField setPlaceholder:@"64738"];
        [_portField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
        if ([_favourite port] != 0)
            [_portField setText:[NSString stringWithFormat:@"%lu", (unsigned long)[_favourite port]]];
        else
            [_portField setText:@""];
        [_portField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [[_portCell contentView] addSubview:_portField];

        _usernameCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MUFavouriteServerUsername"];
        [_usernameCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [[_usernameCell textLabel] setText:NSLocalizedString(@"Username", nil)];
        _usernameField = [[UITextField alloc] initWithFrame:CGRectMake(110.0, 10.0, 185.0, 30.0)];
        [_usernameField setTextColor:[MUColor selectedTextColor]];
        [_usernameField addTarget:self action:@selector(textFieldBeganEditing:) forControlEvents:UIControlEventEditingDidBegin];
        [_usernameField addTarget:self action:@selector(textFieldEndedEditing:) forControlEvents:UIControlEventEditingDidEnd];
        [_usernameField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [_usernameField addTarget:self action:@selector(textFieldDidEndOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
        [_usernameField setReturnKeyType:UIReturnKeyNext];
        [_usernameField setAdjustsFontSizeToFitWidth:NO];
        [_usernameField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [_usernameField setAutocorrectionType:UITextAutocorrectionTypeNo];
        [_usernameField setTextAlignment:UITextAlignmentLeft];
        [_usernameField setPlaceholder:[[NSUserDefaults standardUserDefaults] objectForKey:@"DefaultUserName"]];
        [_usernameField setSecureTextEntry:NO];
        [_usernameField setText:[_favourite userName]];
        [_usernameField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [[_usernameCell contentView] addSubview:_usernameField];
        
        _passwordCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MUFavouriteServerPassword"];
        [_passwordCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [[_passwordCell textLabel] setText:NSLocalizedString(@"Password", nil)];
        _passwordField = [[UITextField alloc] initWithFrame:CGRectMake(110.0, 10.0, 185.0, 30.0)];
        [_passwordField setTextColor:[MUColor selectedTextColor]];
        [_passwordField addTarget:self action:@selector(textFieldBeganEditing:) forControlEvents:UIControlEventEditingDidBegin];
        [_passwordField addTarget:self action:@selector(textFieldEndedEditing:) forControlEvents:UIControlEventEditingDidEnd];
        [_passwordField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [_passwordField addTarget:self action:@selector(textFieldDidEndOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
        [_passwordField setReturnKeyType:UIReturnKeyDefault];
        [_passwordField setAdjustsFontSizeToFitWidth:NO];
        [_passwordField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [_passwordField setAutocorrectionType:UITextAutocorrectionTypeNo];
        [_passwordField setPlaceholder:NSLocalizedString(@"Optional", nil)];
        [_passwordField setSecureTextEntry:YES];
        [_passwordField setTextAlignment:UITextAlignmentLeft];
        [_passwordField setText:[_favourite password]];
        [_passwordField setClearButtonMode:UITextFieldViewModeWhileEditing];
        [[_passwordCell contentView] addSubview:_passwordField];
        
    }
    return self;
}

- (id) init {
    return [self initInEditMode:NO withContentOfFavouriteServer:nil];
}

- (void) dealloc {
    [_favourite release];

    [_descriptionCell release];
    [_descriptionField release];
    [_addressCell release];
    [_addressField release];
    [_portCell release];
    [_portField release];
    [_usernameCell release];
    [_usernameField release];
    [_passwordCell release];
    [_passwordField release];

    [super dealloc];
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    // On iPad, we support all interface orientations.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        return YES;
    }
    
    return toInterfaceOrientation == UIInterfaceOrientationPortrait;
}

#pragma mark -
#pragma mark View lifecycle

- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    UINavigationBar *navBar = self.navigationController.navigationBar;
    if (MUGetOperatingSystemVersion() >= MUMBLE_OS_IOS_7) {
        navBar.tintColor = [UIColor whiteColor];
        navBar.translucent = NO;
        navBar.backgroundColor = [UIColor blackColor];
    }
    navBar.barStyle = UIBarStyleBlackOpaque;
    
    self.tableView.backgroundView = [MUBackgroundView backgroundView];
    
    if (MUGetOperatingSystemVersion() >= MUMBLE_OS_IOS_7) {
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableView.separatorInset = UIEdgeInsetsZero;
    } else {
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

    // View title
    if (!_editMode) {
        [[self navigationItem] setTitle:NSLocalizedString(@"New Favourite", nil)];
    } else {
        [[self navigationItem] setTitle:NSLocalizedString(@"Edit Favourite", nil)];
    }

    // Cancel button
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", nil)
                                                                     style:UIBarButtonItemStylePlain
                                                                    target:self
                                                                    action:@selector(cancelClicked:)];
    [[self navigationItem] setLeftBarButtonItem:cancelButton];
    [cancelButton release];

    // Done
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", nil)
                                                                   style:UIBarButtonItemStyleDone
                                                                  target:self
                                                                  action:@selector(doneClicked:)];
    [[self navigationItem] setRightBarButtonItem:doneButton];
    [doneButton release];
}

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
     return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 5;
    }
    return 0;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([indexPath section] == 0) {
        if ([indexPath row] == 0) {
            return _descriptionCell;
        } else if ([indexPath row] == 1) {
            return _addressCell;
        } else if ([indexPath row] == 2) {
            return _portCell;
        } else if ([indexPath row] == 3) {
            return _usernameCell;
        } else if ([indexPath row] == 4) {
            return _passwordCell;
        }
    }
    return nil;
}

#pragma mark -
#pragma mark UIBarButton actions

- (void) cancelClicked:(id)sender {
    [[self navigationController] dismissModalViewControllerAnimated:YES];
}

- (void) doneClicked:(id)sender {
    // Perform some basic tidying up. For example, for the port field, we
    // want the default port number to be used if it wasn't filled out.
    if ([_favourite displayName] == nil) {
        [_favourite setDisplayName:NSLocalizedString(@"Mumble Server", nil)];
    }
    if ([_favourite port] == 0) {
        [_favourite setPort:64738];
    }

    // Get rid of oureslves and call back to our target to tell it that
    // we're done.
    [[self navigationController] dismissModalViewControllerAnimated:YES];
    if ([_target respondsToSelector:_doneAction]) {
        [_target performSelector:_doneAction withObject:self];
    }
}

#pragma mark -
#pragma mark Data accessors

- (MUFavouriteServer *) copyFavouriteFromContent {
    return [_favourite copy];
}

#pragma mark -
#pragma mark Target/actions

- (void) setTarget:(id)target {
    _target = target;
}

- (id) target {
    return _target;
}

- (void) setDoneAction:(SEL)action {
    _doneAction = action;
}

- (SEL) doneAction {
    return _doneAction;
}

#pragma mark -
#pragma mark Text field actions

- (void) textFieldBeganEditing:(UITextField *)sender {
    _activeTextField = sender;
    if (sender == _descriptionField) {
        _activeCell = _descriptionCell;
    } else if (sender == _addressField) {
        _activeCell = _addressCell;
    } else if (sender == _portField) {
        _activeCell = _portCell;
    } else if (sender == _usernameField) {
        _activeCell = _usernameCell;
    } else if (sender == _passwordField) {
        _activeCell = _passwordCell;
    }
}

- (void) textFieldEndedEditing:(UITextField *)sender {
    _activeTextField = nil;
}

- (void) textFieldDidChange:(UITextField *)sender {
    if (sender == _descriptionField) {
        [_favourite setDisplayName:[sender text]];
    } else if (sender == _addressField) {
        [_favourite setHostName:[sender text]];
    } else if (sender == _portField) {
        [_favourite setPort:[[sender text] integerValue]];
    } else if (sender == _usernameField) {
        [_favourite setUserName:[sender text]];
    } else if (sender == _passwordField) {
        [_favourite setPassword:[sender text]];
    }
}

- (void) textFieldDidEndOnExit:(UITextField *)sender {
    if (sender == _descriptionField) {
        [_addressField becomeFirstResponder];
        _activeTextField = _addressField;
        _activeCell = _addressCell;
    } else if (sender == _addressField) {
        [_portField becomeFirstResponder];
        _activeTextField = _portField;
        _activeCell = _portCell;
    } else if (sender == _portField) {
        [_usernameField becomeFirstResponder];
        _activeTextField = _usernameField;
        _activeCell = _usernameCell;
    } else if (sender == _usernameField) {
        [_passwordField becomeFirstResponder];
        _activeTextField = _passwordField;
        _activeCell = _passwordCell;
    } else if (sender == _passwordField) {
        [_passwordField resignFirstResponder];
        _activeTextField = nil;
        _activeCell = nil;
    }
    if (_activeCell) {
        [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:_activeCell]
                              atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
}

- (void) keyboardWasShown:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    [UIView animateWithDuration:0.2f animations:^{
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
        self.tableView.contentInset = contentInsets;
        self.tableView.scrollIndicatorInsets = contentInsets;
    } completion:^(BOOL finished) {
        if (!finished)
            return;

        [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:_activeCell]
                              atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        
    }];
}

- (void) keyboardWillBeHidden:(NSNotification*)aNotification {
    [UIView animateWithDuration:0.2f animations:^{
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        self.tableView.contentInset = contentInsets;
        self.tableView.scrollIndicatorInsets = contentInsets;
    } completion:^(BOOL finished) {
        // ...
    }];
}


@end