Skip to content

Commit

Permalink
fixed periodical sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Willem B committed Aug 5, 2015
1 parent a001809 commit 1e22338
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
7 changes: 3 additions & 4 deletions EmaXamarin/EmaXamarin/App.cs
@@ -1,5 +1,4 @@
using System.IO;
using EmaXamarin.Api;
using EmaXamarin.Api;
using EmaXamarin.CloudStorage;
using EmaXamarin.Pages;
using Xamarin.Forms;
Expand All @@ -19,15 +18,15 @@ public App(IFileRepository fileRepository, IExternalBrowserService externalBrows
{
fileRepository.StorageDirectory = PersistedState.CustomStorageDirectory;
}
catch
catch
{
//can't do much about it now.
}

PageFactory.Initialize(service, fileRepository, externalBrowserService, _applicationEvents);

SyncBootstrapper.RefreshDropboxSync(fileRepository);
SyncBootstrapper.RefreshFromSyncInterval();
SyncBootstrapper.RefreshForDropbox(fileRepository);

MainPage = new NavigationPage(PageFactory.Current.CreateEmaWikiPage());
}
Expand Down
39 changes: 24 additions & 15 deletions EmaXamarin/EmaXamarin/CloudStorage/SyncBootstrapper.cs
Expand Up @@ -12,12 +12,13 @@ public class SyncBootstrapper
private static readonly Dictionary<string, Synchronization> Synchronizations = new Dictionary<string, Synchronization>();
private static readonly object LockObject = new object();
private static bool _isSyncing;
private static ISyncProgress _syncProgress;
private static Exception _syncException;
private static bool _stop;
private static bool _taskIsActive;
private static readonly Logging Logger = Logging.For<SyncBootstrapper>();
private static ISyncProgress _syncProgress;
private static Exception _syncException;
private static int _intervalMinutes;
private static DateTime _lastSyncDateTime = DateTime.MinValue;

public static bool CanSync
{
Expand Down Expand Up @@ -63,11 +64,18 @@ private static void SyncPeriodically(int intervalMinutes)
Task.Run(async () =>
{
_taskIsActive = true;
//always wait a few seconds: allow the UI to appear, so we have a progressbar available to show
await Task.Delay(TimeSpan.FromSeconds(5));
while (!_stop)
{
Logger.Info("Sync triggered by interval");
await StartSync();
await Task.Delay(_intervalMinutes*60*1000);
if (_lastSyncDateTime.AddMinutes(_intervalMinutes) < DateTime.Now)
{
Logger.Info("Sync triggered by interval");
await StartSync();
}
await Task.Delay(TimeSpan.FromSeconds(1));
}
_taskIsActive = false;
});
Expand All @@ -83,18 +91,18 @@ private static async Task StartSync()
if (_isSyncing)
return;

Synchronization[] synchronizations;
lock (LockObject)
try
{
if (_isSyncing)
return;
Synchronization[] synchronizations;
lock (LockObject)
{
if (_isSyncing)
return;

_isSyncing = true;
synchronizations = Synchronizations.Values.ToArray();
}
_isSyncing = true;
synchronizations = Synchronizations.Values.ToArray();
}

try
{
if (_syncProgress != null)
_syncProgress.OnSyncStart();

Expand All @@ -115,6 +123,7 @@ private static async Task StartSync()
finally
{
_isSyncing = false;
_lastSyncDateTime = DateTime.Now;
}
}

Expand Down Expand Up @@ -143,7 +152,7 @@ public static void RefreshFromSyncInterval()
}
}

public static void RefreshForDropbox(IFileRepository fileRepository)
public static void RefreshDropboxSync(IFileRepository fileRepository)
{
var userPermission = PersistedState.UserLogin;
if (string.IsNullOrEmpty(userPermission.Secret) || string.IsNullOrEmpty(userPermission.Token))
Expand Down
4 changes: 2 additions & 2 deletions EmaXamarin/EmaXamarin/Pages/SettingsPage.cs
Expand Up @@ -103,7 +103,7 @@ private void InitializeDropboxSettings(IExternalBrowserService externalBrowserSe
{
PersistedState.UserLogin = null;
}
SyncBootstrapper.RefreshForDropbox(_fileRepository);
SyncBootstrapper.RefreshDropboxSync(_fileRepository);
};
}

Expand All @@ -123,7 +123,7 @@ private async void ApplicationEventsOnResumed(object sender, EventArgs eventArgs
PersistedState.UserLogin = userPermission;
}
}
SyncBootstrapper.RefreshForDropbox(_fileRepository);
SyncBootstrapper.RefreshDropboxSync(_fileRepository);
}

private void InitializeStorageSettings(IFileRepository fileRepository)
Expand Down
25 changes: 17 additions & 8 deletions EmaXamarin/EmaXamarin/Pages/SyncProgressContentView.cs
Expand Up @@ -26,22 +26,31 @@ public SyncProgressContentView()

public void OnSyncStart()
{
_progressbar.Progress = 0;
IsVisible = true;
Device.BeginInvokeOnMainThread(() =>
{
_progressbar.Progress = 0;
IsVisible = true;
});
}

public async void ReportProgress(int totalSteps, int currentStep, string label)
public void ReportProgress(int totalSteps, int currentStep, string label)
{
var fraction = (double) currentStep/Math.Max(1, totalSteps);
double progress = Math.Min(1, Math.Max(0, fraction));
Device.BeginInvokeOnMainThread(async () =>
{
var fraction = (double) currentStep/Math.Max(1, totalSteps);
double progress = Math.Min(1, Math.Max(0, fraction));
_progressLabel.Text = label;
await _progressbar.ProgressTo(progress, 100, Easing.Linear);
_progressLabel.Text = label;
await _progressbar.ProgressTo(progress, 100, Easing.Linear);
});
}

public void OnSyncFinished()
{
IsVisible = false;
Device.BeginInvokeOnMainThread(() =>
{
IsVisible = false;
});
}
}
}

0 comments on commit 1e22338

Please sign in to comment.