Bitwarden-app-android-iphon.../src/App/Pages/Settings/SyncPageViewModel.cs

72 lines
2.3 KiB
C#
Raw Normal View History

2019-05-14 18:05:30 +02:00
using Bit.App.Abstractions;
using Bit.App.Resources;
using Bit.Core.Abstractions;
using Bit.Core.Exceptions;
using Bit.Core.Utilities;
using System.Threading.Tasks;
namespace Bit.App.Pages
{
public class SyncPageViewModel : BaseViewModel
{
private readonly IDeviceActionService _deviceActionService;
private readonly IPlatformUtilsService _platformUtilsService;
private readonly ISyncService _syncService;
2019-05-14 22:36:54 +02:00
private string _lastSync = "--";
2019-05-14 18:05:30 +02:00
public SyncPageViewModel()
{
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
_platformUtilsService = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService");
_syncService = ServiceContainer.Resolve<ISyncService>("syncService");
PageTitle = AppResources.Sync;
}
2019-05-14 22:36:54 +02:00
public string LastSync
{
get => _lastSync;
set => SetProperty(ref _lastSync, value);
}
public async Task SetLastSyncAsync()
{
var last = await _syncService.GetLastSyncAsync();
if(last != null)
{
var localDate = last.Value.ToLocalTime();
LastSync = string.Format("{0} {1}", localDate.ToShortDateString(), localDate.ToShortTimeString());
}
else
{
LastSync = AppResources.Never;
}
}
2019-05-14 18:05:30 +02:00
public async Task SyncAsync()
{
try
{
await _deviceActionService.ShowLoadingAsync(AppResources.Syncing);
2019-05-14 22:36:54 +02:00
var success = await _syncService.FullSyncAsync(true);
2019-05-14 18:05:30 +02:00
await _deviceActionService.HideLoadingAsync();
2019-05-14 22:36:54 +02:00
if(success)
{
await SetLastSyncAsync();
_platformUtilsService.ShowToast("success", null, AppResources.SyncingComplete);
}
else
{
await Page.DisplayAlert(null, AppResources.SyncingFailed, AppResources.Ok);
}
2019-05-14 18:05:30 +02:00
}
catch(ApiException e)
{
await _deviceActionService.HideLoadingAsync();
await Page.DisplayAlert(AppResources.AnErrorHasOccurred, e.Error.GetSingleMessage(), AppResources.Ok);
}
}
}
}