-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHSettingsViewController.m
More file actions
244 lines (187 loc) · 7.86 KB
/
SHSettingsViewController.m
File metadata and controls
244 lines (187 loc) · 7.86 KB
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
//
// SHSettingsViewController.m
//
// Copyright (c) 2013 Lodoss. All rights reserved.
//
#import "SHSettingsViewController.h"
#import "SHLoginManager.h"
#import "SHDataManager.h"
#import "SHPurchaseManager.h"
#import "SHUser+Extensions.h"
#import "SHPurchaseViewController.h"
#import "SHSettingsPushNotificationViewController.h"
#import "SHChangePasswordViewController.h"
#import "SHSocialSettingsViewController.h"
#import "SHSettingsCell.h"
#import "SHDesignHelper.h"
#import "SHGAIHelper.h"
#import "SHAppDelegate.h"
#import "SHGlobalAlertsHelper.h"
#import "UIColor+RGBColor.h"
@interface SHSettingsViewController()
{
BOOL _showsPasswordChange;
}
@property (strong, nonatomic, readonly) NSArray *menuItems;
@end
@implementation SHSettingsViewController
static const NSInteger kSHSettingsPushNotificationsItem = 100;
static const NSInteger kSHSettingsChangePasswordItem = 101;
static const NSInteger kSHSettingsSocialItem = 102;
@synthesize menuItems = _menuItems;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self setupNavigationBar];
self.preferredContentSize = CGSizeMake(320, 480);
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setupNavigationBar {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Log out", @"Log out") style:UIBarButtonItemStyleBordered target:self action:@selector(logout)];
[SHDesignHelper setupNavBarButtonItem:self.navigationItem.rightBarButtonItem];
}
+ (BOOL)shouldShowPasswordChange {
return [SHLoginManager sharedInstance].currentUser.isPasswordSet.boolValue;
}
- (NSArray *)menuItems {
if (_menuItems != nil) {
return _menuItems;
}
_showsPasswordChange = NO;
if ([SHLoginManager sharedInstance].isSkippedIn) {
_menuItems = @[ ];
} else {
NSMutableArray *menuItems = [NSMutableArray arrayWithCapacity:3];
[menuItems addObject:@{ @"title" : NSLocalizedString(@"Notifications", @"Notifications"), @"tag" : @(kSHSettingsPushNotificationsItem) }];
if ([self.class shouldShowPasswordChange]) {
[menuItems addObject:@{ @"title" : NSLocalizedString(@"Change Password", nil), @"tag" : @(kSHSettingsChangePasswordItem) }];
_showsPasswordChange = YES;
}
[menuItems addObject:@{ @"title" : NSLocalizedString(@"Social Networks", @"Social Networks"), @"tag" : @(kSHSettingsSocialItem) }];
_menuItems = [menuItems copy];
}
return _menuItems;
}
- (NSString *)title {
return NSLocalizedString(@"Settings", @"Settings");
}
- (void)viewDidLoad {
[super viewDidLoad];
#if DEBUG || ADHOC
NSString *buildNumberString = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
self.versionLabel.text = [NSString stringWithFormat:@"v.%@", buildNumberString];
self.versionLabel.hidden = NO;
#endif
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePaidStatus) name:SHPurchaseManagerPurchaseSuccessfulNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSIndexPath *selectedRow = self.tableView.indexPathForSelectedRow;
if (selectedRow != nil) {
[self.tableView deselectRowAtIndexPath:selectedRow animated:animated];
}
[self updatePaidStatus];
// reset menu items and reload table if password state changes
if ([self.class shouldShowPasswordChange] != _showsPasswordChange) {
_menuItems = nil;
[self.tableView reloadData];
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[SHGAIHelper setDefaultTrackerScreenName:@"Settings"];
}
- (void)updatePaidStatus {
if (![SHPurchaseManager sharedInstance].isAppPurchased) {
self.tableView.tableFooterView = self.buyView;
} else {
self.tableView.tableFooterView = [[UIView alloc] init];
}
}
- (void)logout {
[SHGlobalAlertsHelper showLogoutSequenceWithConfirmationInView:self.view];
}
- (void)requestPurchase {
[SHGAIHelper sendDefaultTrackerEventWithCategory:@"Purchase" action:@"PressedInSettings" label:nil value:0];
[[SHAppDelegate instance] showProgressView];
[[SHPurchaseManager sharedInstance] requestAppPurchaseWithCompletion:^(NSError *error) {
[[SHAppDelegate instance] hideProgressView];
if (error != nil) {
if (error.code != SKErrorPaymentCancelled) {
[SHToolbox showErrorAlertWithTitle:NSLocalizedString(@"Purchase Error", @"IAP purchase error title") andError:error];
[SHGAIHelper sendDefaultTrackerEventWithCategory:@"Purchase" action:@"Failed" label:@(error.code).stringValue value:0];
} else {
[SHGAIHelper sendDefaultTrackerEventWithCategory:@"Purchase" action:@"Cancelled" label:nil value:0];
}
} else {
[SHPurchaseViewController showPurchaseSuccessfulAlert];
[SHGAIHelper sendDefaultTrackerEventWithCategory:@"Purchase" action:@"Successful" label:nil value:0];
}
}];
}
- (void)restorePurchase {
[[SHAppDelegate instance] showProgressView];
[[SHPurchaseManager sharedInstance] restoreAppPurchaseWithCompletion:^(NSError *error) {
[[SHAppDelegate instance] hideProgressView];
if (error != nil) {
if (error.code != SKErrorPaymentCancelled) {
[SHToolbox showErrorAlertWithTitle:NSLocalizedString(@"Purchase Restoration Error", @"IAP purchase restoration error title") andError:error];
}
}
}];
}
#pragma mark - TableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.menuItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SHSettingsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SHSettingsCell"];
if (cell == nil) {
cell = [SHSettingsCell cellFromNib];
}
NSDictionary *item = self.menuItems[indexPath.row];
cell.titleLabel.text = item[@"title"];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *item = self.menuItems[indexPath.row];
switch ([item[@"tag"] integerValue]) {
case kSHSettingsPushNotificationsItem:
{
SHSettingsPushNotificationViewController *vc = [[SHSettingsPushNotificationViewController alloc] initWithNibName:@"SHSettingsPushNotificationViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}
break;
case kSHSettingsChangePasswordItem:
{
SHChangePasswordViewController *vc = [[SHChangePasswordViewController alloc] initWithNibName:@"SHChangePasswordViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}
break;
case kSHSettingsSocialItem:
{
SHSocialSettingsViewController *vc = [[SHSocialSettingsViewController alloc] initWithNibName:@"SHSocialSettingsViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}
break;
default:
break;
}
}
#pragma mark - IBAction
- (IBAction)buyPressed:(id)sender {
if (![SHPurchaseManager canPurchase]) {
[SHPurchaseViewController showPurchasesUnavailableAlert];
return;
}
[self requestPurchase];
}
- (IBAction)restoreButtonPressed:(id)sender {
[self restorePurchase];
}
@end