This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
SettingsViewController.m
executable file
·397 lines (347 loc) · 11 KB
/
SettingsViewController.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
//
// SettingsView.m
// PassDrop
//
// Created by Rudis Muiznieks on 1/30/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "SettingsViewController.h"
#import "Global.h"
#import <DropboxSDK/DropboxSDK.h>
@implementation SettingsViewController
@synthesize settingsTable;
@synthesize autoClearSwitch;
@synthesize ignoreBackupSwitch;
@synthesize aboutView;
int pickerViewMode;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
#pragma mark -
#pragma mark view lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
PassDropAppDelegate *app = (PassDropAppDelegate*)[[UIApplication sharedApplication] delegate];
autoClearSwitch = [[UISwitch alloc] init];
ignoreBackupSwitch = [[UISwitch alloc] init];
[autoClearSwitch setOn:app.prefs.autoClearClipboard animated:NO];
[autoClearSwitch addTarget:self action:@selector(openLastSwitched) forControlEvents:UIControlEventValueChanged];
[ignoreBackupSwitch setOn:app.prefs.ignoreBackup animated:NO];
[ignoreBackupSwitch addTarget:self action:@selector(ignoreBackupSwitched) forControlEvents:UIControlEventValueChanged];
aboutView = [[AboutViewController alloc] init];
aboutView.title = @"About";
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated {
PassDropAppDelegate *app = (PassDropAppDelegate*)[[UIApplication sharedApplication] delegate];
app.settingsView = self;
[super viewDidAppear:animated];
[self updateSettingsUI];
}
- (void) viewWillDisappear:(BOOL)animated {
PassDropAppDelegate *app = (PassDropAppDelegate*)[[UIApplication sharedApplication] delegate];
app.settingsView = nil;
}
#pragma mark -
#pragma mark ui actions
- (IBAction)dbButtonClicked {
if([[DBSession sharedSession] isLinked]){
UIAlertView *unlinkConfirm = [[UIAlertView alloc] initWithTitle:@"Unlink DropBox" message:@"Are you sure you want to unlink your DropBox account? This will also remove all databases from your device." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Unlink",nil];
[unlinkConfirm show];
[unlinkConfirm release];
} else {
//DBLoginController *dbLoginController = [[DBLoginController new] autorelease];
//[dbLoginController presentFromController:self];
[[DBSession sharedSession] linkFromController:self];
}
}
- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)index {
if(index == 1){
PassDropAppDelegate *app = (PassDropAppDelegate*)[[UIApplication sharedApplication] delegate];
[[DBSession sharedSession] unlinkAll];
[app dropboxWasReset];
[self updateSettingsUI];
}
}
- (void)updateSettingsUI {
[settingsTable reloadData];
}
- (void) openLastSwitched {
PassDropAppDelegate *app = (PassDropAppDelegate*)[[UIApplication sharedApplication] delegate];
app.prefs.autoClearClipboard = autoClearSwitch.on;
[app.prefs savePrefs];
}
- (void) ignoreBackupSwitched {
PassDropAppDelegate *app = (PassDropAppDelegate*)[[UIApplication sharedApplication] delegate];
app.prefs.ignoreBackup = ignoreBackupSwitch.on;
[app.prefs savePrefs];
}
#pragma mark -
#pragma mark dropbox delegate
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
-(BOOL)shouldAutorotate{
return YES;
}
#pragma mark -
#pragma mark table delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
return 3;
}
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case 0:
return @"Database Sources";
break;
case 1:
return @"PassDrop Settings";
break;
case 2:
return nil;
break;
default:
break;
}
return nil;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
return 1;
break;
case 1:
return 4;
break;
case 2:
return 1;
break;
default:
break;
}
return 0;
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
UIActionSheet *actionSheet;
switch ([indexPath section]) {
case 0:
[self dbButtonClicked];
break;
case 1:
switch ([indexPath row]) {
case 2:
// clear clipboard
break;
case 0:
// lock in background
pickerViewMode = 1;
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Immediately", @"10 secs", @"30 secs", @"1 min", @"5 mins", @"10 mins", @"30 mins", @"1 hour", @"2 hours", @"Never", nil];
//[actionSheet showInView:self.view];
[actionSheet showFromRect:[tableView cellForRowAtIndexPath:indexPath].frame inView:self.view animated:YES];
[actionSheet release];
break;
case 1:
pickerViewMode = 2;
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Writable", @"Read Only", @"Always Ask", nil];
//[actionSheet showInView:self.view];
[actionSheet showFromRect:[tableView cellForRowAtIndexPath:indexPath].frame inView:self.view animated:YES];
[actionSheet release];
break;
case 4:
// ignore backups
break;
default:
break;
}
break;
case 2:
// show about screen
[[self navigationController] pushViewController:aboutView animated:YES];
break;
default:
break;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark -
#pragma mark table data source
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString *cellIdentifier = @"Cell";
static NSString *switchCellIdentifier = @"SwitchCell";
static NSString *valueCellIdentifier = @"ValueCell";
UITableViewCell *cell;
PassDropAppDelegate *app = (PassDropAppDelegate*)[[UIApplication sharedApplication] delegate];
if(indexPath.section == 0 || (indexPath.section == 1 && indexPath.row < 2)){
cell = [tableView dequeueReusableCellWithIdentifier:valueCellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:valueCellIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryNone;
if(indexPath.section == 0){
cell.textLabel.text = @"DropBox";
if([[DBSession sharedSession] isLinked]){
cell.detailTextLabel.text = @"Linked";
} else {
cell.detailTextLabel.text = @"Not Linked";
}
} else {
switch ([indexPath row]) {
case 0:
cell.textLabel.text = @"Lock In Background";
cell.detailTextLabel.text = [self convertSecondsToString:app.prefs.lockInBackgroundSeconds];
break;
case 1:
cell.textLabel.text = @"Open Databases";
cell.detailTextLabel.text = [self openModeStringForMode:app.prefs.databaseOpenMode];
break;
}
}
} else if(indexPath.section == 1 && (indexPath.row == 2 || indexPath.row == 3)){
cell = [tableView dequeueReusableCellWithIdentifier:switchCellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:switchCellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if(indexPath.row == 2){
cell.textLabel.text = @"Auto-Clear Clipboard";
cell.accessoryView = autoClearSwitch;
} else {
cell.textLabel.text = @"Search Ignores Backup";
cell.accessoryView = ignoreBackupSwitch;
}
} else {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
cell.textLabel.text = @"About PassDrop";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
#pragma mark -
#pragma mark pref data conversion helpers
- (NSString*)openModeStringForMode:(int)openMode {
switch (openMode) {
case kOpenModeWritable:
return @"Writable";
break;
case kOpenModeReadOnly:
return @"Read Only";
break;
case kOpenModeAlwaysAsk:
return @"Always Ask";
break;
}
return nil;
}
- (int)convertArrayTimesIndexToSeconds:(int)index {
switch(index){
case 0:
return 0;
break;
case 1:
return 10;
break;
case 2:
return 30;
break;
case 3:
return 60;
break;
case 4:
return 300;
break;
case 5:
return 600;
break;
case 6:
return 1800;
break;
case 7:
return 3600;
break;
case 8:
return 7200;
break;
}
return -1;
}
- (NSString*)convertSecondsToString:(int)seconds {
switch(seconds){
case -1:
return @"Never";
break;
case 0:
return @"Immediately";
break;
case 10:
return @"10 secs";
break;
case 30:
return @"30 secs";
break;
case 60:
return @"1 min";
break;
case 300:
return @"5 mins";
break;
case 600:
return @"10 mins";
break;
case 1800:
return @"30 mins";
break;
case 3600:
return @"1 hour";
break;
case 7200:
return @"2 hours";
break;
}
return [NSString stringWithFormat:@"%d secs", seconds];
}
#pragma mark -
#pragma mark picker view stuff
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
PassDropAppDelegate *app = (PassDropAppDelegate*)[[UIApplication sharedApplication] delegate];
if(pickerViewMode == 1 && buttonIndex < 10){
app.prefs.lockInBackgroundSeconds = [self convertArrayTimesIndexToSeconds:buttonIndex];
} else {
if(buttonIndex < 3){
app.prefs.databaseOpenMode = buttonIndex;
}
}
[app.prefs savePrefs];
[self updateSettingsUI];
}
#pragma mark -
#pragma mark memory management
- (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.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[aboutView release];
[autoClearSwitch release];
[settingsTable release];
[super dealloc];
}
@end