forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDISettingsViewController.m
355 lines (283 loc) · 10.2 KB
/
CDISettingsViewController.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
//
// CDISettingsViewController.m
// Cheddar for iOS
//
// Created by Sam Soffes on 4/20/12.
// Copyright (c) 2012 Nothing Magical. All rights reserved.
//
#import "CDISettingsViewController.h"
#import "CDIUpgradeViewController.h"
#import "CDIGroupedTableViewCell.h"
#import "CDIHUDView.h"
#import "UIColor+CheddariOSAdditions.h"
#import "UIButton+CheddariOSAdditions.h"
#import "UIFont+CheddariOSAdditions.h"
#import "CDIAppDelegate.h"
#import "CDISplitViewController.h"
#import "CDIListsViewController.h"
#import "CDISettingsTextSizePickerViewController.h"
#import "CDISettingsFontPickerViewController.h"
#import "CDISettingsTapPickerViewController.h"
#import "CDIWebViewController.h"
NSString *const kCDIFontDidChangeNotificationName = @"CDIFontDidChangeNotification";
@interface CDISettingsViewController ()
@property (nonatomic, strong) SSLabel *upgradeLabel;
@property (nonatomic, strong) UIButton *upgradeButton;
@end
@implementation CDISettingsViewController
@synthesize upgradeLabel = _upgradeLabel;
- (SSLabel *)upgradeLabel {
if (!_upgradeLabel) {
_upgradeLabel = [[SSLabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 60.0f)];
_upgradeLabel.font = [UIFont cheddarInterfaceFontOfSize:14.0f];
_upgradeLabel.backgroundColor = [UIColor clearColor];
_upgradeLabel.textAlignment = UITextAlignmentCenter;
_upgradeLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_upgradeLabel.numberOfLines = 2;
_upgradeLabel.textEdgeInsets = UIEdgeInsetsMake(4.0f, 10.0f, 0.0f, 10.0f);
_upgradeLabel.verticalTextAlignment = SSLabelVerticalTextAlignmentTop;
_upgradeLabel.textColor = [UIColor cheddarOrangeColor];
_upgradeLabel.userInteractionEnabled = YES;
}
return _upgradeLabel;
}
- (UIButton *)upgradeButton {
if (!_upgradeButton) {
_upgradeButton = [UIButton cheddarBigOrangeButton];
_upgradeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
_upgradeButton.frame = CGRectMake(roundf((self.view.frame.size.width - 300.0f) / 2.0f), 6.0f, 300.0f, 42.0f);
[_upgradeButton setTitle:@"Upgrade to Plus" forState:UIControlStateNormal];
[_upgradeButton addTarget:self action:@selector(upgrade:) forControlEvents:UIControlEventTouchUpInside];
[self.upgradeLabel addSubview:_upgradeButton];
}
return _upgradeButton;
}
#pragma mark - NSObject
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Settings";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(close:)];
SSLabel *footer = [[SSLabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 33.0f)];
footer.backgroundColor = [UIColor clearColor];
footer.autoresizingMask = UIViewAutoresizingFlexibleWidth;
footer.textAlignment = UITextAlignmentCenter;
footer.textColor = [UIColor cheddarLightTextColor];
footer.font = [UIFont cheddarInterfaceFontOfSize:14.0f];
footer.text = [NSString stringWithFormat:@"Version %@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
footer.shadowColor = [UIColor whiteColor];
footer.shadowOffset = CGSizeMake(0.0f, 1.0f);
footer.verticalTextAlignment = SSLabelVerticalTextAlignmentTop;
self.tableView.tableFooterView = footer;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_updateUI) name:kCDKPlusDidChangeNotificationName object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self _updateUI];
[self.tableView reloadData];
}
#pragma mark - Actions
- (void)close:(id)sender {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
- (void)upgrade:(id)sender {
UIViewController *viewController = [[CDIUpgradeViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self.navigationController presentModalViewController:navigationController animated:YES];
}
- (void)signOut:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sign Out" message:@"Are you sure you want to sign out of Cheddar?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Sign Out", nil];
[alert show];
}
#pragma mark - Private
- (void)_updateUI {
CDKUser *user = [CDKUser currentUser];
if (user.hasPlus.boolValue) {
self.upgradeLabel.text = @"You have Cheddar Plus and we really love you for that.";
_upgradeButton.alpha = 0.0f;
} else {
self.upgradeLabel.text = @"";
self.upgradeButton.alpha = 1.0f;
}
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != 1) {
return;
}
// Sign out
[CDKUser setCurrentUser:nil];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults removeObjectForKey:kCDISelectedListKey];
[userDefaults synchronize];
CDIAppDelegate *appDelegate = [CDIAppDelegate sharedAppDelegate];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
appDelegate.window.rootViewController = [[CDISplitViewController alloc] init];
} else {
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[CDIListsViewController alloc] init]];
appDelegate.window.rootViewController = navigationController;
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Account
if (section == 0) {
return 1;
}
// Display
else if (section == 1) {
return 2;
}
// Tasks
else if (section == 2) {
return 1;
}
// About and Support
else if (section == 3) {
return 2;
}
// Sign out
else if (section == 4) {
return 1;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"None";
NSUInteger numberOfRows = [tableView numberOfRowsInSection:indexPath.section];
if (numberOfRows == 1) {
cellIdentifier = @"Both";
} else if (indexPath.row == 0) {
cellIdentifier = @"Top";
} else if (indexPath.row == numberOfRows - 1) {
cellIdentifier = @"Bottom";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[CDIGroupedTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Account
if (indexPath.section == 0) {
cell.textLabel.text = @"Manage Account";
cell.detailTextLabel.text = nil;
}
// Display
else if (indexPath.section == 1) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Text Size";
cell.detailTextLabel.text = [CDISettingsTextSizePickerViewController textForSelectedKey];
} else if (indexPath.row == 1) {
cell.textLabel.text = @"Font";
cell.detailTextLabel.text = [CDISettingsFontPickerViewController textForSelectedKey];
}
}
// Tasks
else if (indexPath.section == 2) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Tap Action";
cell.detailTextLabel.text = [CDISettingsTapPickerViewController textForSelectedKey];
}
}
// About and Support
else if (indexPath.section == 3) {
if (indexPath.row == 0) {
cell.textLabel.text = @"About Cheddar";
cell.detailTextLabel.text = nil;
} else if (indexPath.row == 1) {
cell.textLabel.text = @"Get Help";
cell.detailTextLabel.text = nil;
}
}
// Sign out
else if (indexPath.section == 4) {
cell.textLabel.text = @"Sign Out";
cell.detailTextLabel.text = nil;
}
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if (section != 0) {
return nil;
}
return self.upgradeLabel;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if (section != 0) {
return 0.0f;
}
return 60.0f;
}
#pragma mark - UITableViewDelegate
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return @"Account";
} else if (section == 1) {
return @"Display";
} else if (section == 2) {
return @"Tasks";
}
return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIViewController *viewController = nil;
// Account
if (indexPath.section == 0) {
CDIWebViewController *viewController = [[CDIWebViewController alloc] init];
[viewController loadURL:[NSURL URLWithString:@"https://cheddarapp.com/account"]];
[self.navigationController pushViewController:viewController animated:YES];
return;
}
// Display
else if (indexPath.section == 1) {
// Text Size
if (indexPath.row == 0) {
viewController = [[CDISettingsTextSizePickerViewController alloc] init];
}
// Font
else if (indexPath.row == 1) {
viewController = [[CDISettingsFontPickerViewController alloc] init];
}
}
// Tasks
else if (indexPath.section == 2) {
// Tap Action
if (indexPath.row == 0) {
viewController = [[CDISettingsTapPickerViewController alloc] init];
}
}
// Support and About
else if (indexPath.section == 3) {
// About
if (indexPath.row == 0) {
CDIWebViewController *viewController = [[CDIWebViewController alloc] init];
[viewController loadURL:[[NSBundle mainBundle] URLForResource:@"About" withExtension:@"html"]];
[self.navigationController pushViewController:viewController animated:YES];
return;
}
// Support
else if (indexPath.row == 1) {
CDIWebViewController *viewController = [[CDIWebViewController alloc] init];
[viewController loadURL:[NSURL URLWithString:@"https://cheddarapp.com/support"]];
[self.navigationController pushViewController:viewController animated:YES];
return;
}
}
// Sign out
else if (indexPath.section == 4) {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self signOut:nil];
return;
}
if (viewController) {
[self.navigationController pushViewController:viewController animated:YES];
}
}
@end