2022-04-26 17:21:17 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Bit.App.Abstractions;
|
2019-05-14 18:05:30 +02:00
|
|
|
|
using Bit.App.Resources;
|
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
|
|
|
|
public class SyncPageViewModel : BaseViewModel
|
|
|
|
|
{
|
|
|
|
|
private readonly IDeviceActionService _deviceActionService;
|
|
|
|
|
private readonly IPlatformUtilsService _platformUtilsService;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
private readonly IStateService _stateService;
|
2019-05-14 18:05:30 +02:00
|
|
|
|
private readonly ISyncService _syncService;
|
2021-10-28 18:52:41 +02:00
|
|
|
|
private readonly ILocalizeService _localizeService;
|
2019-05-14 18:05:30 +02:00
|
|
|
|
|
2019-05-14 22:36:54 +02:00
|
|
|
|
private string _lastSync = "--";
|
2020-08-05 19:19:27 +02:00
|
|
|
|
private bool _inited;
|
|
|
|
|
private bool _syncOnRefresh;
|
2019-05-14 22:36:54 +02:00
|
|
|
|
|
2019-05-14 18:05:30 +02:00
|
|
|
|
public SyncPageViewModel()
|
|
|
|
|
{
|
|
|
|
|
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
|
|
|
|
|
_platformUtilsService = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService");
|
2022-02-23 18:40:17 +01:00
|
|
|
|
_stateService = ServiceContainer.Resolve<IStateService>("stateService");
|
2019-05-14 18:05:30 +02:00
|
|
|
|
_syncService = ServiceContainer.Resolve<ISyncService>("syncService");
|
2021-10-28 18:52:41 +02:00
|
|
|
|
_localizeService = ServiceContainer.Resolve<ILocalizeService>("localizeService");
|
2019-05-14 18:05:30 +02:00
|
|
|
|
|
|
|
|
|
PageTitle = AppResources.Sync;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 19:19:27 +02:00
|
|
|
|
public bool EnableSyncOnRefresh
|
|
|
|
|
{
|
|
|
|
|
get => _syncOnRefresh;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (SetProperty(ref _syncOnRefresh, value))
|
|
|
|
|
{
|
|
|
|
|
var task = UpdateSyncOnRefreshAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 22:36:54 +02:00
|
|
|
|
public string LastSync
|
|
|
|
|
{
|
|
|
|
|
get => _lastSync;
|
|
|
|
|
set => SetProperty(ref _lastSync, value);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 19:19:27 +02:00
|
|
|
|
public async Task InitAsync()
|
|
|
|
|
{
|
|
|
|
|
await SetLastSyncAsync();
|
2022-02-23 18:40:17 +01:00
|
|
|
|
EnableSyncOnRefresh = await _stateService.GetSyncOnRefreshAsync();
|
2020-08-05 19:19:27 +02:00
|
|
|
|
_inited = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task UpdateSyncOnRefreshAsync()
|
|
|
|
|
{
|
|
|
|
|
if (_inited)
|
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
await _stateService.SetSyncOnRefreshAsync(_syncOnRefresh);
|
2020-08-05 19:19:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 22:36:54 +02:00
|
|
|
|
public async Task SetLastSyncAsync()
|
|
|
|
|
{
|
|
|
|
|
var last = await _syncService.GetLastSyncAsync();
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (last != null)
|
2019-05-14 22:36:54 +02:00
|
|
|
|
{
|
|
|
|
|
var localDate = last.Value.ToLocalTime();
|
2021-10-28 18:52:41 +02:00
|
|
|
|
LastSync = string.Format("{0} {1}",
|
|
|
|
|
_localizeService.GetLocaleShortDate(localDate),
|
|
|
|
|
_localizeService.GetLocaleShortTime(localDate));
|
2019-05-14 22:36:54 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LastSync = AppResources.Never;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 18:05:30 +02:00
|
|
|
|
public async Task SyncAsync()
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (Xamarin.Essentials.Connectivity.NetworkAccess == Xamarin.Essentials.NetworkAccess.None)
|
2019-06-04 04:43:52 +02:00
|
|
|
|
{
|
|
|
|
|
await _platformUtilsService.ShowDialogAsync(AppResources.InternetConnectionRequiredMessage,
|
|
|
|
|
AppResources.InternetConnectionRequiredTitle);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-05-14 18:05:30 +02:00
|
|
|
|
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();
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (success)
|
2019-05-14 22:36:54 +02:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
catch (ApiException e)
|
2019-05-14 18:05:30 +02:00
|
|
|
|
{
|
|
|
|
|
await _deviceActionService.HideLoadingAsync();
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (e?.Error != null)
|
2019-10-22 22:37:40 +02:00
|
|
|
|
{
|
|
|
|
|
await _platformUtilsService.ShowDialogAsync(e.Error.GetSingleMessage(),
|
|
|
|
|
AppResources.AnErrorHasOccurred);
|
|
|
|
|
}
|
2019-05-14 18:05:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|