2016-05-03 00:50:16 +03:00
|
|
|
|
using System;
|
2016-05-06 07:17:38 +03:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Bit.App.Abstractions;
|
2016-05-11 05:53:34 +03:00
|
|
|
|
using Bit.App.Controls;
|
2016-05-07 20:42:09 +03:00
|
|
|
|
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;
|
2016-05-06 07:17:38 +03:00
|
|
|
|
using XLabs.Ioc;
|
2016-07-31 00:29:04 +03:00
|
|
|
|
using Plugin.Settings.Abstractions;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
2016-07-02 02:21:12 +03:00
|
|
|
|
public class SettingsSyncPage : ExtendedContentPage
|
2016-05-03 00:50:16 +03:00
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
private readonly ISyncService _syncService;
|
2017-12-22 23:00:11 +03:00
|
|
|
|
private readonly IDeviceActionService _deviceActionService;
|
2016-05-07 01:49:01 +03:00
|
|
|
|
private readonly IConnectivity _connectivity;
|
2016-07-31 00:29:04 +03:00
|
|
|
|
private readonly ISettings _settings;
|
2016-08-05 02:35:56 +03:00
|
|
|
|
private readonly IGoogleAnalyticsService _googleAnalyticsService;
|
2016-05-06 07:17:38 +03:00
|
|
|
|
|
2016-07-02 02:21:12 +03:00
|
|
|
|
public SettingsSyncPage()
|
2016-05-03 00:50:16 +03:00
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
_syncService = Resolver.Resolve<ISyncService>();
|
2017-12-22 23:00:11 +03:00
|
|
|
|
_deviceActionService = Resolver.Resolve<IDeviceActionService>();
|
2016-05-07 01:49:01 +03:00
|
|
|
|
_connectivity = Resolver.Resolve<IConnectivity>();
|
2016-07-31 00:29:04 +03:00
|
|
|
|
_settings = Resolver.Resolve<ISettings>();
|
2016-08-05 02:35:56 +03:00
|
|
|
|
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
|
2016-05-06 07:17:38 +03:00
|
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-31 00:29:04 +03:00
|
|
|
|
public Label LastSyncLabel { get; set; }
|
|
|
|
|
|
2016-05-06 07:17:38 +03:00
|
|
|
|
public void Init()
|
|
|
|
|
{
|
2016-08-24 07:07:46 +03:00
|
|
|
|
var syncButton = new ExtendedButton
|
2016-05-06 07:17:38 +03:00
|
|
|
|
{
|
2016-11-25 21:22:11 +03:00
|
|
|
|
Text = AppResources.SyncVaultNow,
|
2016-07-31 00:29:04 +03:00
|
|
|
|
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 07:17:38 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-07-31 00:29:04 +03:00
|
|
|
|
SetLastSync();
|
|
|
|
|
|
|
|
|
|
var stackLayout = new StackLayout
|
|
|
|
|
{
|
|
|
|
|
VerticalOptions = LayoutOptions.CenterAndExpand,
|
|
|
|
|
Children = { syncButton, LastSyncLabel },
|
|
|
|
|
Padding = new Thickness(15, 0)
|
|
|
|
|
};
|
2016-05-06 07:17:38 +03:00
|
|
|
|
|
2017-12-14 17:26:32 +03:00
|
|
|
|
if(Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.UWP)
|
2017-02-09 03:18:34 +03:00
|
|
|
|
{
|
2017-10-13 16:11:37 +03:00
|
|
|
|
ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Close));
|
2017-02-09 03:18:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-25 21:22:11 +03:00
|
|
|
|
Title = AppResources.Sync;
|
2016-05-06 07:17:38 +03:00
|
|
|
|
Content = stackLayout;
|
2016-07-31 00:29:04 +03:00
|
|
|
|
}
|
2016-05-07 01:49:01 +03:00
|
|
|
|
|
2016-07-31 00:29:04 +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();
|
|
|
|
|
}
|
2016-05-06 07:17:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-31 00:29:04 +03:00
|
|
|
|
private void SetLastSync()
|
|
|
|
|
{
|
2017-08-29 23:03:26 +03:00
|
|
|
|
DateTime? lastSyncDate = null;
|
|
|
|
|
if(_settings.Contains(Constants.LastSync))
|
|
|
|
|
{
|
|
|
|
|
lastSyncDate = _settings.GetValueOrDefault(Constants.LastSync, DateTime.UtcNow);
|
|
|
|
|
}
|
2016-08-27 02:23:59 +03:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-11-25 21:22:11 +03:00
|
|
|
|
LastSyncLabel.Text = AppResources.LastSync + " " + lastSyncDate?.ToLocalTime().ToString() ?? AppResources.Never;
|
2016-08-27 02:23:59 +03:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// some users with different calendars have issues with ToString()ing a date
|
|
|
|
|
// it seems the linker is at fault. just catch for now since this isn't that important.
|
|
|
|
|
// ref http://bit.ly/2c2JU7b
|
|
|
|
|
}
|
2016-07-31 00:29:04 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 07:17:38 +03:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-23 07:56:45 +03:00
|
|
|
|
_deviceActionService.ShowLoading(AppResources.Syncing);
|
2017-02-07 07:40:24 +03:00
|
|
|
|
var succeeded = await _syncService.FullSyncAsync(true);
|
2017-12-23 07:56:45 +03:00
|
|
|
|
_deviceActionService.HideLoading();
|
2016-05-12 00:30:09 +03:00
|
|
|
|
if(succeeded)
|
2016-05-06 07:17:38 +03:00
|
|
|
|
{
|
2017-12-22 23:00:11 +03:00
|
|
|
|
_deviceActionService.Toast(AppResources.SyncingComplete);
|
2016-08-05 02:35:56 +03:00
|
|
|
|
_googleAnalyticsService.TrackAppEvent("Synced");
|
2016-05-06 07:17:38 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-12-22 23:00:11 +03:00
|
|
|
|
_deviceActionService.Toast(AppResources.SyncingFailed);
|
2016-05-06 07:17:38 +03:00
|
|
|
|
}
|
2016-07-31 00:29:04 +03:00
|
|
|
|
|
|
|
|
|
SetLastSync();
|
2016-05-03 00:50:16 +03:00
|
|
|
|
}
|
2016-05-07 01:49:01 +03:00
|
|
|
|
|
|
|
|
|
public void AlertNoConnection()
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage,
|
|
|
|
|
AppResources.Ok);
|
2016-05-07 01:49:01 +03:00
|
|
|
|
}
|
2016-05-03 00:50:16 +03:00
|
|
|
|
}
|
|
|
|
|
}
|