forked from soffes/cheddar-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDISettingsViewController.m
327 lines (264 loc) · 11.3 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
//
// 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 "CDISettingsTableViewCell.h"
#import "TTTAttributedLabel.h"
#import "UIColor+CheddariOSAdditions.h"
#import "UIButton+CheddariOSAdditions.h"
#import "UIFont+CheddariOSAdditions.h"
#import <MessageUI/MessageUI.h>
#import "CDIAppDelegate.h"
#import "CDISplitViewController.h"
#import "CDIListsViewController.h"
@interface CDISettingsViewController () <MFMailComposeViewControllerDelegate>
@end
@implementation CDISettingsViewController {
TTTAttributedLabel *_label;
UIButton *_upgradeButton;
UIButton *_supportButton;
UIButton *_signOutButton;
NSCache *_headerCache;
}
#pragma mark - NSObject
- (id)init {
return (self = [super initWithStyle:UITableViewStyleGrouped]);
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Settings";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(close:)];
UIView *background = [[UIView alloc] initWithFrame:CGRectZero];
background.backgroundColor = [UIColor cheddarArchesColor];
self.tableView.backgroundView = background;
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 cheddarFontOfSize: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;
// CGFloat width = self.view.bounds.size.width;
// _label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, width - 40.0f, 240.0f)];
// _label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// _label.backgroundColor = [UIColor clearColor];
// _label.font = [UIFont cheddarFontOfSize:18.0f];
// _label.numberOfLines = 0;
// _label.textColor = [UIColor cheddarTextColor];
// _label.verticalAlignment = TTTAttributedLabelVerticalAlignmentTop;
// _label.userInteractionEnabled = YES;
// [self.view addSubview:_label];
//
// _upgradeButton = [UIButton cheddarBigOrangeButton];
// _upgradeButton.frame = CGRectMake(roundf((width - 280.0f) / 2.0f), 238.0f, 280.0f, 45.0f);
// _upgradeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
// [_upgradeButton setTitle:@"Upgrade to Cheddar Plus" forState:UIControlStateNormal];
// [_upgradeButton addTarget:self action:@selector(upgrade:) forControlEvents:UIControlEventTouchUpInside];
// _upgradeButton.alpha = 0.0f;
// [self.view addSubview:_upgradeButton];
//
// _supportButton = [UIButton cheddarBigGrayButton];
// _supportButton.frame = CGRectMake(roundf((width - 280.0f) / 2.0f), 292.0f, 280.0f, 45.0f);
// _supportButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
// [_supportButton setTitle:@"Support" forState:UIControlStateNormal];
// [_supportButton addTarget:self action:@selector(support:) forControlEvents:UIControlEventTouchUpInside];
// [self.view addSubview:_supportButton];
//
// _signOutButton = [UIButton cheddarBigGrayButton];
// _signOutButton.frame = CGRectMake(roundf((width - 280.0f) / 2.0f), 346.0f, 280.0f, 45.0f);
// _signOutButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
// [_signOutButton setTitle:@"Sign Out" forState:UIControlStateNormal];
// [_signOutButton addTarget:self action:@selector(signOut:) forControlEvents:UIControlEventTouchUpInside];
// [self.view addSubview:_signOutButton];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_updateUI) name:kCDKPlusDidChangeNotificationName object:nil];
[self _updateUI];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
}
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
#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)support:(id)sender {
if ([MFMailComposeViewController canSendMail] == NO) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://cheddarapp.com/support"]];
return;
}
MFMailComposeViewController *viewController = [[MFMailComposeViewController alloc] init];
viewController.toRecipients = [NSArray arrayWithObject:@"support@cheddarapp.com"];
viewController.subject = @"Cheddar for iOS Help";
viewController.mailComposeDelegate = self;
[self.navigationController presentModalViewController:viewController 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) {
NSString *text = @"You currently have a Plus account. You're awesome! You can create unlimited lists!";
[_label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
CTFontRef boldFont = CTFontCreateWithName((__bridge CFStringRef)kCDIBoldFontName, 20.0f, NULL);
if (boldFont) {
[mutableAttributedString addAttribute:(__bridge NSString *)kCTFontAttributeName value:(__bridge id)boldFont range:NSMakeRange(51, 31)];
CFRelease(boldFont);
}
return mutableAttributedString;
}];
_upgradeButton.enabled = NO;
_upgradeButton.alpha = 0.0f;
} else {
NSString *text = @"You currently have a free account.\n\nWith a plus account, you can create unlimited lists. You only get two lists with a free account.";
[_label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
CTFontRef boldFont = CTFontCreateWithName((__bridge CFStringRef)kCDIBoldFontName, 20.0f, NULL);
if (boldFont) {
[mutableAttributedString addAttribute:(__bridge NSString *)kCTFontAttributeName value:(__bridge id)boldFont range:NSMakeRange(36, 52)];
CFRelease(boldFont);
}
return mutableAttributedString;
}];
_upgradeButton.enabled = YES;
_upgradeButton.alpha = 1.0f;
}
}
#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[controller dismissModalViewControllerAnimated:YES];
if (result == MFMailComposeResultSent) {
SSHUDView *hud = [[SSHUDView alloc] init];
[hud completeQuicklyWithTitle:@"Sent!"];
}
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != 1) {
return;
}
// Sign out
[CDKUser setCurrentUser:nil];
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 - NSKeyValueObserving
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context != (__bridge void *)self) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
return;
}
if ([keyPath isEqualToString:@"hasPlus"]) {
[self _updateUI];
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 2;
} else if (section == 1) {
return 1;
} else if (section == 2) {
return 3;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[CDISettingsTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
}
// Display
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Text Size";
cell.detailTextLabel.text = @"Large";
} else if (indexPath.row == 1) {
cell.textLabel.text = @"Font";
cell.detailTextLabel.text = @"Gotham";
}
}
// Tasks
if (indexPath.section == 1) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Tap Action";
cell.detailTextLabel.text = @"Complete";
}
}
// Other
if (indexPath.section == 2) {
if (indexPath.row == 0) {
cell.textLabel.text = @"About";
cell.detailTextLabel.text = nil;
} else if (indexPath.row == 1) {
cell.textLabel.text = @"Support";
cell.detailTextLabel.text = nil;
} else if (indexPath.row == 2) {
cell.textLabel.text = @"Sign Out";
cell.detailTextLabel.text = nil;
}
}
return cell;
}
#pragma mark - UITableViewDelegate
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return @"Display";
} else if (section == 1) {
return @"Tasks";
}
return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 2) {
return nil;
}
if (!_headerCache) {
_headerCache = [[NSCache alloc] init];
}
NSNumber *key = [NSNumber numberWithInteger:section];
SSLabel *label = [_headerCache objectForKey:key];
if (!label) {
CGFloat x = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 40.0f : 20.0f;
label = [[SSLabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
label.textEdgeInsets = UIEdgeInsetsMake(10.0f, x, 0.0f, 0.0f);
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor cheddarTextColor];
label.font = [UIFont boldCheddarFontOfSize:17.0f];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0f, 1.0f);
[_headerCache setObject:label forKey:key];
}
label.text = [self tableView:tableView titleForHeaderInSection:section];
return label;
}
@end