Add force update of UserStats.

This commit is contained in:
Sergey Magidovich 2016-06-14 11:41:45 +03:00 committed by Vladimir Byko-Ianko
parent 18802c4c9e
commit 3b8eca0a26
4 changed files with 23 additions and 10 deletions

View file

@ -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<mutex> 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<mutex> g(m_mutex);

View file

@ -39,13 +39,17 @@ class UserStatsLoader
public:
using TOnUpdateCallback = function<void()>;
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.

View file

@ -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

View file

@ -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); }