diff --git a/editor/user_stats.cpp b/editor/user_stats.cpp index 629f4543a9..4b0a9290e7 100644 --- a/editor/user_stats.cpp +++ b/editor/user_stats.cpp @@ -127,12 +127,14 @@ bool UserStatsLoader::Update(string const & userName) return true; } -void UserStatsLoader::Update(string const & userName, TOnUpdateCallback fn) +void UserStatsLoader::Update(string const & userName, UpdatePolicy const policy, + TOnUpdateCallback fn) { auto nothingToUpdate = false; + if (policy == UpdatePolicy::Lazy) { lock_guard g(m_mutex); - nothingToUpdate = m_userStats && m_userName == userName && m_userStats && + nothingToUpdate = m_userStats && m_userName == userName && difftime(m_lastUpdate, time(nullptr)) < kSecondsInHour; } @@ -148,6 +150,11 @@ void UserStatsLoader::Update(string const & userName, TOnUpdateCallback fn) }).detach(); } +void UserStatsLoader::Update(string const & userName, TOnUpdateCallback fn) +{ + Update(userName, UpdatePolicy::Lazy, fn); +} + void UserStatsLoader::DropStats(string const & userName) { lock_guard g(m_mutex); diff --git a/editor/user_stats.hpp b/editor/user_stats.hpp index 200330ec23..4b71882736 100644 --- a/editor/user_stats.hpp +++ b/editor/user_stats.hpp @@ -39,13 +39,17 @@ class UserStatsLoader public: using TOnUpdateCallback = function; + enum class UpdatePolicy { Lazy, Force }; + UserStatsLoader(); /// Synchronously sends request to the server. Updates stats and returns true on success. bool Update(string const & userName); - /// Launch the update process if stats are too old. + /// Launches the update process if stats are too old or if policy is UpdatePolicy::Force. /// The process posts fn to a gui thread on success. + void Update(string const & userName, UpdatePolicy policy, TOnUpdateCallback fn); + /// Calls Update with UpdatePolicy::Lazy. void Update(string const & userName, TOnUpdateCallback fn); /// Resets internal state and removes records from settings. diff --git a/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationLoginViewController.mm b/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationLoginViewController.mm index ffc282bf75..e09b0cbbf1 100644 --- a/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationLoginViewController.mm +++ b/iphone/Maps/Classes/CustomViews/Login/MWMAuthorizationLoginViewController.mm @@ -94,7 +94,7 @@ using namespace osm_auth_ios; self.accountView.hidden = NO; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"•••" style:UIBarButtonItemStylePlain target:self action:@selector(showActionSheet)]; - [self refresh]; + [self refresh:NO]; } - (void)configNoAuth @@ -163,11 +163,13 @@ using namespace osm_auth_ios; [self.navigationController popViewControllerAnimated:YES]; } -- (void)refresh +- (void)refresh:(BOOL)force { [self updateUI]; __weak auto weakSelf = self; - GetFramework().UpdateUserStats(OSMUserName().UTF8String, ^ + auto const policy = force ? editor::UserStatsLoader::UpdatePolicy::Force + : editor::UserStatsLoader::UpdatePolicy::Lazy; + GetFramework().UpdateUserStats(OSMUserName().UTF8String, policy, ^ { [weakSelf updateUI]; }); @@ -221,7 +223,7 @@ using namespace osm_auth_ios; UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction:[UIAlertAction actionWithTitle:kRefresh style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { - [self refresh]; + [self refresh:YES]; }]]; [alertController addAction:[UIAlertAction actionWithTitle:kLogout style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { @@ -245,7 +247,7 @@ using namespace osm_auth_ios; if (actionSheet.destructiveButtonIndex == buttonIndex) [self logout]; else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kRefresh]) - [self refresh]; + [self refresh:YES]; } #pragma mark - Segue diff --git a/map/framework.hpp b/map/framework.hpp index 7c87293bd5..5fde90dcb4 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -695,10 +695,10 @@ public: } // Reads user stats from server or gets it from cache calls |fn| on success. - void UpdateUserStats(string const & userName, + void UpdateUserStats(string const & userName, editor::UserStatsLoader::UpdatePolicy policy, editor::UserStatsLoader::TOnUpdateCallback fn) { - m_userStatsLoader.Update(userName, fn); + m_userStatsLoader.Update(userName, policy, fn); } void DropUserStats(string const & userName) { m_userStatsLoader.DropStats(userName); }