bitwarden-android/src/App/Pages/Settings/SettingsSyncPage.cs

110 lines
3.3 KiB
C#
Raw Normal View History

2016-05-03 00:50:16 +03:00
using System;
using System.Threading.Tasks;
using Acr.UserDialogs;
using Bit.App.Abstractions;
using Bit.App.Controls;
using Bit.App.Resources;
2016-05-07 01:49:01 +03:00
using Plugin.Connectivity.Abstractions;
2016-05-03 00:50:16 +03:00
using Xamarin.Forms;
using XLabs.Ioc;
using Plugin.Settings.Abstractions;
2016-05-03 00:50:16 +03:00
namespace Bit.App.Pages
{
public class SettingsSyncPage : ExtendedContentPage
2016-05-03 00:50:16 +03:00
{
private readonly ISyncService _syncService;
private readonly IUserDialogs _userDialogs;
2016-05-07 01:49:01 +03:00
private readonly IConnectivity _connectivity;
private readonly ISettings _settings;
private readonly IGoogleAnalyticsService _googleAnalyticsService;
public SettingsSyncPage()
2016-05-03 00:50:16 +03:00
{
_syncService = Resolver.Resolve<ISyncService>();
_userDialogs = Resolver.Resolve<IUserDialogs>();
2016-05-07 01:49:01 +03:00
_connectivity = Resolver.Resolve<IConnectivity>();
_settings = Resolver.Resolve<ISettings>();
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
Init();
}
public Label LastSyncLabel { get; set; }
public void Init()
{
2016-08-24 07:07:46 +03:00
var syncButton = new ExtendedButton
{
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
};
SetLastSync();
var stackLayout = new StackLayout
{
VerticalOptions = LayoutOptions.CenterAndExpand,
Children = { syncButton, LastSyncLabel },
Padding = new Thickness(15, 0)
};
2016-05-03 00:50:16 +03:00
Title = "Sync";
Content = stackLayout;
}
2016-05-07 01:49:01 +03:00
protected override void OnAppearing()
{
base.OnAppearing();
2016-05-12 00:30:09 +03:00
if(!_connectivity.IsConnected)
2016-05-07 01:49:01 +03:00
{
AlertNoConnection();
}
}
private void SetLastSync()
{
var lastSyncDate = _settings.GetValueOrDefault<DateTime?>(Constants.LastSync);
LastSyncLabel.Text = "Last Sync: " + lastSyncDate?.ToLocalTime().ToString() ?? "Never";
}
public async Task SyncAsync()
{
2016-05-12 00:30:09 +03:00
if(!_connectivity.IsConnected)
2016-05-07 01:49:01 +03:00
{
AlertNoConnection();
return;
}
2016-05-12 00:30:09 +03:00
_userDialogs.ShowLoading("Syncing...", MaskType.Black);
2016-06-30 05:04:09 +03:00
var succeeded = await _syncService.FullSyncAsync();
_userDialogs.HideLoading();
2016-05-12 00:30:09 +03:00
if(succeeded)
{
_userDialogs.Toast("Syncing complete.");
_googleAnalyticsService.TrackAppEvent("Synced");
}
else
{
_userDialogs.Toast("Syncing failed.");
}
SetLastSync();
2016-05-03 00:50:16 +03:00
}
2016-05-07 01:49:01 +03:00
public void AlertNoConnection()
{
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage,
AppResources.Ok);
2016-05-07 01:49:01 +03:00
}
2016-05-03 00:50:16 +03:00
}
}