2016-05-02 23:50:16 +02:00
|
|
|
|
using System;
|
2016-05-06 06:17:38 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Acr.UserDialogs;
|
|
|
|
|
using Bit.App.Abstractions;
|
2016-05-11 04:53:34 +02:00
|
|
|
|
using Bit.App.Controls;
|
2016-05-07 19:42:09 +02:00
|
|
|
|
using Bit.App.Resources;
|
2016-05-07 00:49:01 +02:00
|
|
|
|
using Plugin.Connectivity.Abstractions;
|
2016-05-02 23:50:16 +02:00
|
|
|
|
using Xamarin.Forms;
|
2016-05-06 06:17:38 +02:00
|
|
|
|
using XLabs.Ioc;
|
2016-07-30 23:29:04 +02:00
|
|
|
|
using Plugin.Settings.Abstractions;
|
2016-05-02 23:50:16 +02:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
2016-07-02 01:21:12 +02:00
|
|
|
|
public class SettingsSyncPage : ExtendedContentPage
|
2016-05-02 23:50:16 +02:00
|
|
|
|
{
|
2016-05-06 06:17:38 +02:00
|
|
|
|
private readonly ISyncService _syncService;
|
|
|
|
|
private readonly IUserDialogs _userDialogs;
|
2016-05-07 00:49:01 +02:00
|
|
|
|
private readonly IConnectivity _connectivity;
|
2016-07-30 23:29:04 +02:00
|
|
|
|
private readonly ISettings _settings;
|
2016-05-06 06:17:38 +02:00
|
|
|
|
|
2016-07-02 01:21:12 +02:00
|
|
|
|
public SettingsSyncPage()
|
2016-05-02 23:50:16 +02:00
|
|
|
|
{
|
2016-05-06 06:17:38 +02:00
|
|
|
|
_syncService = Resolver.Resolve<ISyncService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
2016-05-07 00:49:01 +02:00
|
|
|
|
_connectivity = Resolver.Resolve<IConnectivity>();
|
2016-07-30 23:29:04 +02:00
|
|
|
|
_settings = Resolver.Resolve<ISettings>();
|
2016-05-06 06:17:38 +02:00
|
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-30 23:29:04 +02:00
|
|
|
|
public Label LastSyncLabel { get; set; }
|
|
|
|
|
|
2016-05-06 06:17:38 +02:00
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
var syncButton = new Button
|
|
|
|
|
{
|
2016-07-30 23:29:04 +02:00
|
|
|
|
Text = "Sync Vault Now",
|
|
|
|
|
Command = new Command(async () => await SyncAsync()),
|
|
|
|
|
Style = (Style)Application.Current.Resources["btn-primaryAccent"]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LastSyncLabel = new Label
|
|
|
|
|
{
|
|
|
|
|
Style = (Style)Application.Current.Resources["text-muted"],
|
|
|
|
|
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
|
|
|
|
|
HorizontalTextAlignment = TextAlignment.Center
|
2016-05-06 06:17:38 +02:00
|
|
|
|
};
|
|
|
|
|
|
2016-07-30 23:29:04 +02:00
|
|
|
|
SetLastSync();
|
|
|
|
|
|
|
|
|
|
var stackLayout = new StackLayout
|
|
|
|
|
{
|
|
|
|
|
VerticalOptions = LayoutOptions.CenterAndExpand,
|
|
|
|
|
Children = { syncButton, LastSyncLabel },
|
|
|
|
|
Padding = new Thickness(15, 0)
|
|
|
|
|
};
|
2016-05-06 06:17:38 +02:00
|
|
|
|
|
2016-05-02 23:50:16 +02:00
|
|
|
|
Title = "Sync";
|
2016-05-06 06:17:38 +02:00
|
|
|
|
Content = stackLayout;
|
2016-07-30 23:29:04 +02:00
|
|
|
|
}
|
2016-05-07 00:49:01 +02:00
|
|
|
|
|
2016-07-30 23:29:04 +02:00
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2016-05-11 23:30:09 +02:00
|
|
|
|
if(!_connectivity.IsConnected)
|
2016-05-07 00:49:01 +02:00
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
}
|
2016-05-06 06:17:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-30 23:29:04 +02:00
|
|
|
|
private void SetLastSync()
|
|
|
|
|
{
|
|
|
|
|
var lastSyncDate = _settings.GetValueOrDefault<DateTime?>(Constants.SettingLastSync);
|
2016-08-02 06:32:57 +02:00
|
|
|
|
LastSyncLabel.Text = "Last Sync: " + lastSyncDate?.ToLocalTime().ToString() ?? "Never";
|
2016-07-30 23:29:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 06:17:38 +02:00
|
|
|
|
public async Task SyncAsync()
|
|
|
|
|
{
|
2016-05-11 23:30:09 +02:00
|
|
|
|
if(!_connectivity.IsConnected)
|
2016-05-07 00:49:01 +02:00
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-11 23:30:09 +02:00
|
|
|
|
_userDialogs.ShowLoading("Syncing...", MaskType.Black);
|
2016-06-30 04:04:09 +02:00
|
|
|
|
var succeeded = await _syncService.FullSyncAsync();
|
2016-05-06 06:17:38 +02:00
|
|
|
|
_userDialogs.HideLoading();
|
2016-05-11 23:30:09 +02:00
|
|
|
|
if(succeeded)
|
2016-05-06 06:17:38 +02:00
|
|
|
|
{
|
2016-07-26 06:38:41 +02:00
|
|
|
|
_userDialogs.Toast("Syncing complete.");
|
2016-05-06 06:17:38 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-07-26 06:38:41 +02:00
|
|
|
|
_userDialogs.Toast("Syncing failed.");
|
2016-05-06 06:17:38 +02:00
|
|
|
|
}
|
2016-07-30 23:29:04 +02:00
|
|
|
|
|
|
|
|
|
SetLastSync();
|
2016-05-02 23:50:16 +02:00
|
|
|
|
}
|
2016-05-07 00:49:01 +02:00
|
|
|
|
|
|
|
|
|
public void AlertNoConnection()
|
|
|
|
|
{
|
2016-05-11 23:30:09 +02:00
|
|
|
|
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage, AppResources.Ok);
|
2016-05-07 00:49:01 +02:00
|
|
|
|
}
|
2016-05-02 23:50:16 +02:00
|
|
|
|
}
|
|
|
|
|
}
|