Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MWMAuthorizationLoginViewController.mm « Login « CustomViews « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9e0f711ea9fa191d9e0da84cd38774e5663d737c (plain)
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
#import "MWMCommon.h"
#import "MapsAppDelegate.h"
#import "MWMAlertViewController.h"
#import "MWMAuthorizationCommon.h"
#import "MWMAuthorizationLoginViewController.h"
#import "MWMAuthorizationWebViewLoginViewController.h"
#import "Statistics.h"

#include "Framework.h"

#include "editor/server_api.hpp"

#include "indexer/osm_editor.hpp"

#include "platform/platform.hpp"

#include "base/logging.hpp"

namespace
{
NSString * const kWebViewAuthSegue = @"Authorization2WebViewAuthorizationSegue";
NSString * const kOSMAuthSegue = @"Authorization2OSMAuthorizationSegue";

NSString * const kCancel = L(@"cancel");
NSString * const kLogout = L(@"logout");
NSString * const kRefresh = L(@"refresh");
} // namespace

using namespace osm;
using namespace osm_auth_ios;

@interface MWMAuthorizationLoginViewController ()

@property (weak, nonatomic) IBOutlet UIView * authView;
@property (weak, nonatomic) IBOutlet UIView * accountView;

@property (weak, nonatomic) IBOutlet UIButton * loginGoogleButton;
@property (weak, nonatomic) IBOutlet UIButton * loginFacebookButton;
@property (weak, nonatomic) IBOutlet UIButton * loginOSMButton;
@property (weak, nonatomic) IBOutlet UIButton * signupButton;

@property (weak, nonatomic) IBOutlet UILabel * changesCountLabel;
@property (weak, nonatomic) IBOutlet UILabel * lastUpdateLabel;
@property (weak, nonatomic) IBOutlet UILabel * rankLabel;
@property (weak, nonatomic) IBOutlet UILabel * changesToNextPlaceLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint * yourPlaceLabelCenterYAlignment;

@end

@implementation MWMAuthorizationLoginViewController

- (void)viewDidLoad
{
  [super viewDidLoad];
  [Statistics logEvent:kStatEventName(kStatAuthorization, kStatOpen)];
}

- (void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  [self checkConnection];
  if (AuthorizationHaveCredentials())
    [self configHaveAuth];
  else
    [self configNoAuth];

  AuthorizationSetNeedCheck(NO);
}

- (void)checkConnection
{
  BOOL const isConnected = Platform::IsConnected();
  self.loginGoogleButton.enabled = isConnected;
  self.loginFacebookButton.enabled = isConnected;
  self.signupButton.enabled = isConnected;

  AuthorizationConfigButton(self.loginGoogleButton, AuthorizationButtonType::AuthorizationButtonTypeGoogle);
  AuthorizationConfigButton(self.loginFacebookButton, AuthorizationButtonType::AuthorizationButtonTypeFacebook);
  AuthorizationConfigButton(self.loginOSMButton, AuthorizationButtonType::AuthorizationButtonTypeOSM);

  if (!isConnected)
  {
    self.loginGoogleButton.layer.borderColor = [UIColor clearColor].CGColor;
    self.loginFacebookButton.layer.borderColor = [UIColor clearColor].CGColor;
  }
}

- (void)configHaveAuth
{
  NSString * osmUserName = OSMUserName();
  self.title = osmUserName.length > 0 ? osmUserName : L(@"osm_account").capitalizedString;
  self.authView.hidden = YES;
  self.accountView.hidden = NO;

  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"•••" style:UIBarButtonItemStylePlain target:self action:@selector(showActionSheet)];
  [self refresh:NO];
}

- (void)configNoAuth
{
  self.title = L(@"profile").capitalizedString;
  self.authView.hidden = NO;
  self.accountView.hidden = YES;
}

#pragma mark - Actions

- (void)performOnlineAction:(MWMVoidBlock)block
{
  if (Platform::IsConnected())
    block();
  else
    [self.alertController presentNoConnectionAlert];
}

- (IBAction)loginGoogle
{
  [self performOnlineAction:^
  {
    [Statistics logEvent:kStatEditorAuthRequets withParameters:@{kStatValue : kStatGoogle, kStatFrom : kStatProfile}];
    [self performSegueWithIdentifier:kWebViewAuthSegue sender:self.loginGoogleButton];
  }];
}

- (IBAction)loginFacebook
{
  [self performOnlineAction:^
  {
    [Statistics logEvent:kStatEditorAuthRequets withParameters:@{kStatValue : kStatFacebook, kStatFrom : kStatProfile}];
    [self performSegueWithIdentifier:kWebViewAuthSegue sender:self.loginFacebookButton];
  }];
}

- (IBAction)loginOSM
{
  [self performOnlineAction:^
  {
    [Statistics logEvent:kStatEditorAuthRequets withParameters:@{kStatValue : kStatOSM, kStatFrom : kStatProfile}];
    [self performSegueWithIdentifier:kOSMAuthSegue sender:self.loginOSMButton];
  }];
}

- (IBAction)signup
{
  [self performOnlineAction:^
  {
    [Statistics logEvent:kStatEditorRegRequest withParameters:@{kStatFrom : kStatProfile}];
    [self openUrl:[NSURL URLWithString:@(OsmOAuth::ServerAuth().GetRegistrationURL().c_str())]];
  }];
}

- (IBAction)osmTap
{
  [self openUrl:[NSURL URLWithString:@"https://wiki.openstreetmap.org/wiki/Main_Page"]];
}

- (void)logout
{
  [Statistics logEvent:kStatEventName(kStatAuthorization, kStatLogout)];
  NSString * osmUserName = OSMUserName();
  if (osmUserName.length > 0)
    GetFramework().DropUserStats(osmUserName.UTF8String);
  AuthorizationStoreCredentials({});
  [self.navigationController popViewControllerAnimated:YES];
}

- (void)refresh:(BOOL)force
{
  [self updateUI];
  __weak auto weakSelf = self;
  auto const policy = force ? editor::UserStatsLoader::UpdatePolicy::Force
                            : editor::UserStatsLoader::UpdatePolicy::Lazy;
  NSString * osmUserName = OSMUserName();
  if (osmUserName.length > 0)
    GetFramework().UpdateUserStats(osmUserName.UTF8String, policy, ^{ [weakSelf updateUI]; });
}

- (void)updateUI
{
  NSString * osmUserName = OSMUserName();
  if (osmUserName.length == 0)
    return;
  editor::UserStats stats = GetFramework().GetUserStats(osmUserName.UTF8String);
  if (!stats)
    return;
  int32_t changesCount;
  if (stats.GetChangesCount(changesCount))
    self.changesCountLabel.text = @(changesCount).stringValue;
  int32_t rank;
  if (stats.GetRank(rank))
    self.rankLabel.text = @(rank).stringValue;
  string levelUpFeat;
  if (stats.GetLevelUpRequiredFeat(levelUpFeat))
  {
    self.yourPlaceLabelCenterYAlignment.priority = UILayoutPriorityDefaultLow;
    self.changesToNextPlaceLabel.hidden = NO;
    self.changesToNextPlaceLabel.text =
        [NSString stringWithFormat:@"%@ %@", L(@"editor_profile_changes_for_next_place"),
                                   @(levelUpFeat.c_str())];
  }
  else
  {
    self.yourPlaceLabelCenterYAlignment.priority = UILayoutPriorityDefaultHigh;
    self.changesToNextPlaceLabel.hidden = YES;
  }

  NSString * lastUploadDate = [NSDateFormatter
      localizedStringFromDate:[NSDate dateWithTimeIntervalSince1970:stats.GetLastUpdate()]
                    dateStyle:NSDateFormatterShortStyle
                    timeStyle:NSDateFormatterShortStyle];
  self.lastUpdateLabel.text =
      [NSString stringWithFormat:L(@"last_update"), lastUploadDate.UTF8String];
}

#pragma mark - ActionSheet

- (void)showActionSheet
{
  UIAlertController * alertController =
      [UIAlertController alertControllerWithTitle:nil
                                          message:nil
                                   preferredStyle:UIAlertControllerStyleActionSheet];
  [alertController addAction:[UIAlertAction actionWithTitle:kRefresh
                                                      style:UIAlertActionStyleDefault
                                                    handler:^(UIAlertAction * action) {
                                                      [self refresh:YES];
                                                    }]];
  [alertController addAction:[UIAlertAction actionWithTitle:kLogout
                                                      style:UIAlertActionStyleDestructive
                                                    handler:^(UIAlertAction * action) {
                                                      [self logout];
                                                    }]];
  [alertController
      addAction:[UIAlertAction actionWithTitle:kCancel style:UIAlertActionStyleCancel handler:nil]];

  if (IPAD)
  {
    UIPopoverPresentationController * popPresenter =
        [alertController popoverPresentationController];
    popPresenter.barButtonItem = self.navigationItem.rightBarButtonItem;
  }
  [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - Segue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  MWMAuthorizationWebViewLoginViewController * dvc = segue.destinationViewController;
  if ([self.loginGoogleButton isEqual:sender])
    dvc.authType = MWMWebViewAuthorizationTypeGoogle;
  else if ([self.loginFacebookButton isEqual:sender])
    dvc.authType = MWMWebViewAuthorizationTypeFacebook;
}

@end