forked from mbogh/bemyeyes-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMEMainViewController.m
238 lines (197 loc) · 8.97 KB
/
BMEMainViewController.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
//
// BMEMainViewController.m
// BeMyEyes
//
// Created by Simon Støvring on 09/03/14.
// Copyright (c) 2014 Be My Eyes. All rights reserved.
//
#import "BMEMainViewController.h"
#import <PSAlertView/PSPDFAlertView.h>
#import "BMEClient.h"
#import "BMEUser.h"
#import "BMEAccessControlHandler.h"
#import "BMEAccessViewController.h"
#import "BMECallViewController.h"
#define BMEMainKnownLanguagesSegue @"KnownLanguages"
static NSString *const BMEAccessViewSegue = @"AccessView";
@interface BMEMainViewController ()
@property (weak, nonatomic) IBOutlet UIButton *settingsButton;
@property (assign, nonatomic, getter = isLoggedOut) BOOL loggedOut;
@property (weak, nonatomic) UIViewController *currentViewController;
@end
@implementation BMEMainViewController
#pragma mark -
#pragma mark Lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[MKLocalization registerForLocalization:self];
BMERole role = [BMEClient sharedClient].currentUser.role;
switch (role) {
case BMERoleHelper:
[self displayHelperView];
break;
case BMERoleBlind:
[self displayBlindView];
break;
default:
break;
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleAppBecameActive)
name:UIApplicationDidBecomeActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initiateCall) name:BMEInitiateCallIfPossibleNotification object:nil];
[[BMEClient sharedClient] verifyTokenAuthOnServerWithCompletion:^(BOOL valid) {
if (!valid) { // Force user to log out
NSString *title = MKLocalizedFromTable(BME_MAIN_ALERT_FORCED_LOGOUT_TITLE, BMEMainLocalizationTable);
NSString *message = MKLocalizedFromTable(BME_MAIN_ALERT_FORCED_LOGOUT_MESSAGE, BMEMainLocalizationTable);
NSString *confirmButton = MKLocalizedFromTable(BME_MAIN_ALERT_FORCED_LOGOUT_CONFIRM, BMEMainLocalizationTable);
PSPDFAlertView *alertView = [[PSPDFAlertView alloc] initWithTitle:title message:message];
[alertView addButtonWithTitle:confirmButton block:^{
[[BMEClient sharedClient] logoutWithCompletion:^(BOOL success, NSError *error) {
[[NSNotificationCenter defaultCenter] postNotificationName:BMEDidLogOutNotification object:nil];
}];
}];
[alertView show];
}
}];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self checkAccess];
[self askForMoreLanguagesIfNecessary];
}
- (void)handleAppBecameActive
{
[self checkAccess];
}
- (void)checkAccess
{
if ([BMEClient sharedClient].currentUser.role == BMERoleHelper) {
[BMEAccessControlHandler hasNotificationsEnabled:^(BOOL isUserEnabled, BOOL validToken) {
if (isUserEnabled) {
// If user is helper and has notifications enabled, to a request to register for a possibly new device token
[BMEAccessControlHandler requireNotificationsEnabled:^(BOOL isUserEnabled, BOOL validToken) {
[self askForAccessIfNecessary];
}];
} else {
[self askForAccessIfNecessary];
}
}];
} else {
[self askForAccessIfNecessary];
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:BMEAccessViewSegue]) {
BMERole role = [BMEClient sharedClient].currentUser.role;
((BMEAccessViewController *)segue.destinationViewController).role = role;
}
}
- (void)shouldLocalize {
self.settingsButton.accessibilityLabel = MKLocalizedFromTable(BME_MAIN_SETTINGS_BUTTON_ACCESSIBILITY_LABEL, BMEMainLocalizationTable);
}
- (UIViewController *)childViewControllerForStatusBarStyle {
return _currentViewController ? self.currentViewController : nil;
}
- (UIViewController *)childViewControllerForStatusBarHidden {
return _currentViewController ? self.currentViewController : nil;
}
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
if (_currentViewController) {
return self.currentViewController.preferredStatusBarUpdateAnimation;
}
return super.preferredStatusBarUpdateAnimation;
}
#pragma mark -
#pragma mark Private Methods
- (IBAction)popController:(UIStoryboardSegue *)segue { }
- (void)displayHelperView {
[self displayMainControllerWithIdentifier:BMEMainHelperControllerIdentifier];
}
- (void)displayBlindView {
[self displayMainControllerWithIdentifier:BMEMainBlindControllerIdentifier];
}
- (void)displayMainControllerWithIdentifier:(NSString *)identifier {
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
[self addChildViewController:controller];
[self.view insertSubview:controller.view belowSubview:self.settingsButton];
[controller.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
self.currentViewController = controller;
}
- (void)askForMoreLanguagesIfNecessary {
if ([BMEClient sharedClient].currentUser.peopleHelped.integerValue >= BMEPeopleHelpedBeforeAskingForMoreLanguages &&
![GVUserDefaults standardUserDefaults].hasAskedForMoreLanguages) {
NSString *title, *message, *cancelButton, *confirmButton;
if ([[BMEClient sharedClient].currentUser isHelper]) {
title = MKLocalizedFromTable(BME_MAIN_ALERT_MORE_LANGAUGES_HELPER_TITLE, BMEMainLocalizationTable);
message = MKLocalizedFromTable(BME_MAIN_ALERT_MORE_LANGAUGES_HELPER_MESSAGE, BMEMainLocalizationTable);
cancelButton = MKLocalizedFromTable(BME_MAIN_ALERT_MORE_LANGAUGES_HELPER_CANCEL, BMEMainLocalizationTable);
confirmButton = MKLocalizedFromTable(BME_MAIN_ALERT_MORE_LANGAUGES_HELPER_CONFIRM, BMEMainLocalizationTable);
} else {
title = MKLocalizedFromTable(BME_MAIN_ALERT_MORE_LANGAUGES_BLIND_TITLE, BMEMainLocalizationTable);
message = MKLocalizedFromTable(BME_MAIN_ALERT_MORE_LANGAUGES_BLIND_MESSAGE, BMEMainLocalizationTable);
cancelButton = MKLocalizedFromTable(BME_MAIN_ALERT_MORE_LANGAUGES_BLIND_CANCEL, BMEMainLocalizationTable);
confirmButton = MKLocalizedFromTable(BME_MAIN_ALERT_MORE_LANGAUGES_BLIND_CONFIRM, BMEMainLocalizationTable);
}
PSPDFAlertView *alertView = [[PSPDFAlertView alloc] initWithTitle:title message:message];
[alertView setCancelButtonWithTitle:cancelButton block:nil];
[alertView addButtonWithTitle:confirmButton block:^{
[self performSegueWithIdentifier:BMEMainKnownLanguagesSegue sender:self];
}];
[alertView show];
[GVUserDefaults standardUserDefaults].hasAskedForMoreLanguages = YES;
}
}
- (void)askForAccessIfNecessary
{
[BMEAccessControlHandler enabledForRole:[BMEClient sharedClient].currentUser.role completion:^(BOOL isEnabled, BOOL validToken) {
if (isEnabled) {
if (!validToken) {
// User has enable push, but something else went wrong
NSString *title = MKLocalizedFromTable(BME_MAIN_ALERT_NOTIFICATIONS_ERROR_TITLE, BMEMainLocalizationTable);
NSString *message = MKLocalizedFromTable(BME_MAIN_ALERT_NOTIFICATIONS_ERROR_MESSAGE, BMEMainLocalizationTable);
NSString *cancelButton = MKLocalizedFromTable(BME_MAIN_ALERT_CANCEL, BMEMainLocalizationTable);
PSPDFAlertView *alertView = [[PSPDFAlertView alloc] initWithTitle:title message:message];
[alertView setCancelButtonWithTitle:cancelButton block:nil];
[alertView show];
return;
}
return;
}
[self performSegueWithIdentifier:BMEAccessViewSegue sender:self];
}];
}
#pragma mark - Call
- (BOOL)inCall {
return [self.navigationController.visibleViewController isKindOfClass:[BMECallViewController class]];
}
- (BOOL)ableToCall {
BOOL inCall = [self inCall];
return !inCall; // Not in call
}
- (BOOL)accessibilityPerformMagicTap {
if ([self inCall]) {
// Do nothing
} else if([self ableToCall]) {
return [self initiateCall];
}
return NO;
}
- (BOOL)initiateCall {
if (![BMEClient sharedClient].currentUser.isBlind) {
return NO;
}
BMECallViewController *callController = [self.storyboard instantiateViewControllerWithIdentifier:BMECallControllerIdentifier];
callController.callMode = BMECallModeCreate;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:callController];
navigationController.navigationBarHidden = YES;
[self presentViewController:navigationController animated:YES completion:nil];
return YES;
}
@end