From 3247380347cad7bbfd8cb4d5f85ef6a54f576334 Mon Sep 17 00:00:00 2001 From: VladiMihaylenko Date: Thu, 4 Feb 2016 14:18:29 +0300 Subject: [PATCH] [ios] Caching OSM user name. --- .../Login/MWMAuthorizationCommon.h | 1 + .../Login/MWMAuthorizationCommon.mm | 38 +++++++++++++++++++ .../MWMAuthorizationLoginViewController.mm | 26 ++----------- .../MWMAuthorizationSignupViewController.mm | 4 +- iphone/Maps/Classes/MapsAppDelegate.mm | 4 +- iphone/Maps/Mapsme.storyboard | 2 +- iphone/Maps/SettingsAndMoreVC.mm | 10 ++++- 7 files changed, 56 insertions(+), 29 deletions(-) diff --git a/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationCommon.h b/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationCommon.h index 0259073879..8500a25157 100644 --- a/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationCommon.h +++ b/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationCommon.h @@ -23,5 +23,6 @@ void AuthorizationSetUserSkip(); BOOL AuthorizationIsUserSkip(); void AuthorizationSetNeedCheck(BOOL needCheck); BOOL AuthorizationIsNeedCheck(); +NSString * OSMUserName(); } // namespace osm_auth_ios diff --git a/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationCommon.mm b/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationCommon.mm index 9e517214bb..e611d61d6b 100644 --- a/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationCommon.mm +++ b/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationCommon.mm @@ -1,7 +1,11 @@ +#import "Common.h" #import "MWMAuthorizationCommon.h" #import "UIButton+RuntimeAttributes.h" #import "UIColor+MapsMeColor.h" +#include "base/logging.hpp" +#include "editor/server_api.hpp" + namespace osm_auth_ios { @@ -9,6 +13,33 @@ NSString * const kOSMRequestToken = @"OSMRequestToken"; NSString * const kOSMRequestSecret = @"OSMRequestSecret"; NSString * const kAuthUserSkip = @"AuthUserSkip"; NSString * const kAuthNeedCheck = @"AuthNeedCheck"; +NSString * const kOSMUserName = @"UDOsmUserName"; + +void SetOSMUserNameWithCredentials(osm::TKeySecret const & keySecret) +{ + using namespace osm; + dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ + { + ServerApi06 const api {OsmOAuth::ServerAuth(keySecret)}; + try + { + NSUserDefaults * ud = [NSUserDefaults standardUserDefaults]; + [ud setObject:@(api.GetUserPreferences().m_displayName.c_str()) forKey:kOSMUserName]; + [ud synchronize]; + } + catch (exception const & ex) + { + LOG(LWARNING, ("Can't load user preferences from OSM server:", ex.what())); + } + }); +} + +void SetEmptyOSMUserName() +{ + NSUserDefaults * ud = [NSUserDefaults standardUserDefaults]; + [ud setObject:nil forKey:kOSMUserName]; + [ud synchronize]; +} UIColor * AuthorizationButtonTextColor(AuthorizationButtonType type) { @@ -58,11 +89,13 @@ void AuthorizationStoreCredentials(osm::TKeySecret const & keySecret) { [ud removeObjectForKey:kOSMRequestToken]; [ud removeObjectForKey:kOSMRequestSecret]; + SetEmptyOSMUserName(); } else { [ud setObject:@(keySecret.first.c_str()) forKey:kOSMRequestToken]; [ud setObject:@(keySecret.second.c_str()) forKey:kOSMRequestSecret]; + SetOSMUserNameWithCredentials(keySecret); } [ud synchronize]; } @@ -109,4 +142,9 @@ BOOL AuthorizationIsNeedCheck() return [[NSUserDefaults standardUserDefaults] boolForKey:kAuthNeedCheck]; } +NSString * OSMUserName() +{ + return [[NSUserDefaults standardUserDefaults] stringForKey:kOSMUserName]; +} + } // namespace osm_auth_ios diff --git a/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationLoginViewController.mm b/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationLoginViewController.mm index b82fc1a99e..4fa9fa6ca7 100644 --- a/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationLoginViewController.mm +++ b/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationLoginViewController.mm @@ -1,13 +1,11 @@ #import "Common.h" #import "MapsAppDelegate.h" #import "MWMAuthorizationCommon.h" -#import "MWMAuthorizationCommon.h" #import "MWMAuthorizationLoginViewController.h" #import "MWMAuthorizationWebViewLoginViewController.h" #import "Statistics.h" #import "UIColor+MapsMeColor.h" -#include "editor/osm_auth.hpp" #include "editor/server_api.hpp" namespace @@ -17,6 +15,7 @@ NSString * const kOSMAuthSegue = @"Authorization2OSMAuthorizationSegue"; } // namespace using namespace osm; +using namespace osm_auth_ios; @interface MWMAuthorizationLoginViewController () @@ -50,8 +49,6 @@ using namespace osm; @implementation MWMAuthorizationLoginViewController -using namespace osm_auth_ios; - - (void)viewDidLoad { [[Statistics instance] logEvent:kStatEventName(kStatAuthorization, kStatOpen)]; @@ -111,25 +108,8 @@ using namespace osm_auth_ios; - (void)configHaveAuth { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ - { - ServerApi06 const api(OsmOAuth::ServerAuth(AuthorizationGetCredentials())); - try - { - UserPreferences const prefs = api.GetUserPreferences(); - dispatch_async(dispatch_get_main_queue(), ^ - { - self.title = @(prefs.m_displayName.c_str()); - }); - } - catch (exception const & ex) - { - LOG(LWARNING, ("Can't load user preferences from OSM server:", ex.what())); - } - }); - // TODO(@igrechuhin): Cache user name and other info to display while offline. - // Note that this cache should be reset if user logs out. - self.title = L(@"osm_account").capitalizedString; + NSString * osmUserName = OSMUserName(); + self.title = osmUserName.length > 0 ? osmUserName : L(@"osm_account").capitalizedString; self.message.hidden = YES; self.loginGoogleButton.hidden = YES; self.loginFacebookButton.hidden = YES; diff --git a/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationSignupViewController.mm b/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationSignupViewController.mm index ba5c07f526..8469f60e55 100644 --- a/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationSignupViewController.mm +++ b/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationSignupViewController.mm @@ -12,6 +12,8 @@ typedef NS_OPTIONS(NSUInteger, MWMFieldCorrect) MWMFieldCorrectAll = MWMFieldCorrectEmail | MWMFieldCorrectLogin | MWMFieldCorrectPassword }; +using namespace osm_auth_ios; + @interface MWMAuthorizationSignupViewController () @property (weak, nonatomic) IBOutlet UIButton * signupGoogleButton; @@ -29,8 +31,6 @@ typedef NS_OPTIONS(NSUInteger, MWMFieldCorrect) @implementation MWMAuthorizationSignupViewController -using namespace osm_auth_ios; - - (void)viewDidLoad { [super viewDidLoad]; diff --git a/iphone/Maps/Classes/MapsAppDelegate.mm b/iphone/Maps/Classes/MapsAppDelegate.mm index 6918ad9de6..e9d19a0732 100644 --- a/iphone/Maps/Classes/MapsAppDelegate.mm +++ b/iphone/Maps/Classes/MapsAppDelegate.mm @@ -86,6 +86,8 @@ void InitLocalizedStrings() f.AddString("routing_failed_internal_error", [L(@"routing_failed_internal_error") UTF8String]); } +using namespace osm_auth_ios; + @interface MapsAppDelegate () @property (nonatomic) NSInteger standbyCounter; @@ -106,8 +108,6 @@ void InitLocalizedStrings() ActiveMapsObserver * m_mapsObserver; } -using namespace osm_auth_ios; - + (MapsAppDelegate *)theApp { return (MapsAppDelegate *)[UIApplication sharedApplication].delegate; diff --git a/iphone/Maps/Mapsme.storyboard b/iphone/Maps/Mapsme.storyboard index cc86568d9a..77cbf3b9d3 100644 --- a/iphone/Maps/Mapsme.storyboard +++ b/iphone/Maps/Mapsme.storyboard @@ -1769,7 +1769,7 @@ - + diff --git a/iphone/Maps/SettingsAndMoreVC.mm b/iphone/Maps/SettingsAndMoreVC.mm index 832e8f086a..d78549f0c6 100644 --- a/iphone/Maps/SettingsAndMoreVC.mm +++ b/iphone/Maps/SettingsAndMoreVC.mm @@ -1,4 +1,5 @@ #import "CommunityVC.h" +#import "MWMAuthorizationCommon.h" #import "MWMAuthorizationLoginViewController.h" #import "RichTextVC.h" #import "SettingsAndMoreVC.h" @@ -86,6 +87,12 @@ extern NSDictionary * const deviceNames = @{@"x86_64" : @"Simulator", @{@"Id" : @"Copyright", @"Title" : L(@"copyright"), @"Icon" : @"IconCopyright"}]}]; } +- (void)viewWillAppear:(BOOL)animated +{ + [super viewWillAppear:animated]; + [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic]; +} + #pragma mark - TableView - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView @@ -122,7 +129,8 @@ extern NSDictionary * const deviceNames = @{@"x86_64" : @"Simulator", cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[UITableViewCell className]]; cell.backgroundColor = [UIColor white]; - cell.textLabel.text = item[@"Title"]; + NSString * osmUserName = osm_auth_ios::OSMUserName(); + cell.textLabel.text = [item[@"Id"] isEqualToString:@"Authorization"] && osmUserName ? osmUserName : item[@"Title"]; cell.imageView.image = [UIImage imageNamed:item[@"Icon"]]; cell.imageView.mwm_coloring = MWMImageColoringBlack; cell.textLabel.textColor = [UIColor blackPrimaryText];