1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-09-11 01:18:12 +02:00
bitwarden-mobile/src/App/Pages/Settings/SettingsSyncPage.cs

106 lines
3.1 KiB
C#
Raw Normal View History

2016-05-02 23:50:16 +02: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 00:49:01 +02:00
using Plugin.Connectivity.Abstractions;
2016-05-02 23:50:16 +02:00
using Xamarin.Forms;
using XLabs.Ioc;
using Plugin.Settings.Abstractions;
2016-05-02 23:50:16 +02:00
namespace Bit.App.Pages
{
public class SettingsSyncPage : ExtendedContentPage
2016-05-02 23:50:16 +02:00
{
private readonly ISyncService _syncService;
private readonly IUserDialogs _userDialogs;
2016-05-07 00:49:01 +02:00
private readonly IConnectivity _connectivity;
private readonly ISettings _settings;
public SettingsSyncPage()
2016-05-02 23:50:16 +02:00
{
_syncService = Resolver.Resolve<ISyncService>();
_userDialogs = Resolver.Resolve<IUserDialogs>();
2016-05-07 00:49:01 +02:00
_connectivity = Resolver.Resolve<IConnectivity>();
_settings = Resolver.Resolve<ISettings>();
Init();
}
public Label LastSyncLabel { get; set; }
public void Init()
{
var syncButton = new Button
{
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-02 23:50:16 +02:00
Title = "Sync";
Content = stackLayout;
}
2016-05-07 00:49:01 +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();
}
}
private void SetLastSync()
{
var lastSyncDate = _settings.GetValueOrDefault<DateTime?>(Constants.SettingLastSync);
LastSyncLabel.Text = "Last Sync: " + lastSyncDate?.ToLocalTime().ToString() ?? "Never";
}
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();
_userDialogs.HideLoading();
2016-05-11 23:30:09 +02:00
if(succeeded)
{
_userDialogs.Toast("Syncing complete.");
}
else
{
_userDialogs.Toast("Syncing failed.");
}
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
}
}