-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHSocialSettingsViewController.m
More file actions
189 lines (155 loc) · 6.91 KB
/
SHSocialSettingsViewController.m
File metadata and controls
189 lines (155 loc) · 6.91 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
//
// SHSocialSettingsViewController.m
//
// Copyright (c) 2013 Lodoss. All rights reserved.
//
#import "SHSocialSettingsViewController.h"
#import "SHLoginManager.h"
#import "SHDataManager.h"
#import "SHFacebookManager.h"
#import "SHDesignHelper.h"
#import "SHGAIHelper.h"
#import "MBProgressHUD+Conveniency.h"
#import "SHGlobalAlertsHelper.h"
#import <UIActionSheet+Blocks.h>
#import "UIColor+RGBColor.h"
#import "UIColor+SHPalette.h"
@implementation SHSocialSettingsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.preferredContentSize = CGSizeMake(320, 480);
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (NSString *)title {
return NSLocalizedString(@"Social Networks", @"Social Networks");
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setupFacebookUI];
[self setupSeparators];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupFacebookUI) name:SHFacebookUserDidChangeNotification object:nil];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[SHGAIHelper sendDefaultTrackerScreenViewWithName:@"SocialSettings"];
}
- (void)setupFacebookUI {
if ([SHFacebookManager sharedInstance].isSessionOpen) {
[self setupFacebookUIForOpenSession];
} else {
[self setupFacebookUIForNotOpenedSession];
}
}
- (void)setupFacebookUIForOpenSession {
self.facebookMainLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Connected", @"Connected")];
self.facebookMainLabel.textColor = [UIColor orangeColor_SH];
self.accessoryImageView.image = [UIImage imageNamed:@"ic_ok"];
}
- (void)setupFacebookUIForNotOpenedSession {
self.facebookMainLabel.text = [NSString stringWithFormat:NSLocalizedString(@"Connected", @"Connected")];
self.facebookMainLabel.textColor = [UIColor orangeColor_SH];
self.accessoryImageView.image = [UIImage imageNamed:@"ic_ok"];
}
- (void)setupSeparators {
CGFloat separatorHeight = 1.0f / [[UIScreen mainScreen] scale];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.facebookView.bounds.size.height - separatorHeight, 320, separatorHeight)];
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
view.backgroundColor = [UIColor colorWithHexString:@"dfdfe7"];
} else {
view.backgroundColor = [UIColor colorWithHexString:@"c8c7cc"];
}
[self.facebookView addSubview:view];
}
- (void)connectFacebook {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view withLabelText:NSLocalizedString(@"Connecting Facebook...", @"Connecting Facebook spinner") animated:YES];
hud.dimBackground = YES;
[[SHFacebookManager sharedInstance] authorizeFacebookAndLoadProfileFromController:self withCompletion:^(BOOL completed, NSError *error) {
if (!completed && error == nil) {
[hud hide:YES];
return;
}
if (!completed || error != nil) {
[hud hide:NO];
[SHToolbox showErrorAlertWithTitle:NSLocalizedString(@"Facebook Error", @"Facebook Error") andError:error];
return;
}
[[SHFacebookManager sharedInstance] sendFacebookConnectIfNecessaryWithCompletion:^(NSError *connectError) {
[hud hide:(connectError == nil)];
if (connectError != nil) {
[SHToolbox showErrorAlertWithTitle:NSLocalizedString(@"Error", @"Error") andError:connectError];
}
// view will be refreshed due to notification if authentication is successful
}];
}];
}
- (void)disconnectFacebook {
UIActionSheet *actionSheet = nil;
if ([SHLoginManager sharedInstance].isLoggedInViaFacebook) {
actionSheet = [self disconnectFacebookUserActionSheet];
[SHGAIHelper sendDefaultTrackerEventWithCategory:@"Login" action:@"Logout" label:nil value:0];
} else {
actionSheet = [self disconnectOrdinaryUserActionSheet];
[SHGAIHelper sendDefaultTrackerEventWithCategory:@"Social" action:@"DisconnectFacebook" label:nil value:0];
}
[self show:actionSheet];
}
- (UIActionSheet *)disconnectFacebookUserActionSheet {
// if user is logged in using Facebook, offer logging out
UIActionSheet *actionSheet = nil;
actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"$Facebook_Login_Disconnect$", @"Facebook login disconnect prompt") delegate:nil cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel") destructiveButtonTitle:NSLocalizedString(@"Log out", @"Log out") otherButtonTitles:nil];
actionSheet.tapBlock = ^(UIActionSheet *anActionSheet, NSInteger buttonIndex) {
if (buttonIndex == anActionSheet.cancelButtonIndex) {
return;
}
[SHGlobalAlertsHelper showLogoutSequence];
};
return actionSheet;
}
- (UIActionSheet *)disconnectOrdinaryUserActionSheet {
// just show disconnect prompt
UIActionSheet *actionSheet = nil;
__weak typeof(self) weakSelf = self;
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel") destructiveButtonTitle:NSLocalizedString(@"Disconnect", @"Facebook disconnect") otherButtonTitles:nil];
actionSheet.tapBlock = ^(UIActionSheet *anActionSheet, NSInteger buttonIndex) {
if (buttonIndex == anActionSheet.cancelButtonIndex) {
return;
}
[weakSelf disconnectFacebookAccount];
};
return actionSheet;
}
- (void)disconnectFacebookAccount {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view withLabelText:NSLocalizedString(@"Disconnecting...", @"Facebook disconnect spinner") animated:YES];
hud.dimBackground = YES;
[[SHDataManager sharedInstance] disconnectFacebookAccountWithCompletion:^(NSError *error) {
[hud hide:(error == nil)];
if (error == nil) {
// actually logout from Facebook only if disconnect was successful
[[SHFacebookManager sharedInstance] logoutFacebook];
// view will be refreshed due to notification
} else {
[SHToolbox showErrorAlertWithTitle:NSLocalizedString(@"Error", @"Error") andError:error];
}
}];
}
- (void)show: (UIActionSheet *)actionSheet {
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
[actionSheet showInView:self.view];
} else {
[actionSheet showInView:self.tabBarController.view];
}
}
- (IBAction)facebookButtonPressed: (id)sender {
if (![SHFacebookManager sharedInstance].isSessionOpen) {
[self connectFacebook];
[SHGAIHelper sendDefaultTrackerEventWithCategory:@"Social" action:@"ConnectFacebookFromSettings" label:nil value:0];
} else {
[self disconnectFacebook];
}
}
@end