2022-01-24 23:20:48 +03:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Bit.App.Resources;
|
2019-05-14 16:01:07 +03:00
|
|
|
|
using Bit.Core.Abstractions;
|
|
|
|
|
using Bit.Core.Models.Domain;
|
|
|
|
|
using Bit.Core.Utilities;
|
2022-01-24 23:20:48 +03:00
|
|
|
|
#if !FDROID
|
|
|
|
|
using Microsoft.AppCenter.Crashes;
|
|
|
|
|
#endif
|
2019-05-14 16:01:07 +03:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
|
|
|
|
public class GeneratorHistoryPageViewModel : BaseViewModel
|
|
|
|
|
{
|
|
|
|
|
private readonly IPlatformUtilsService _platformUtilsService;
|
|
|
|
|
private readonly IPasswordGenerationService _passwordGenerationService;
|
2021-12-10 23:41:36 +03:00
|
|
|
|
private readonly IClipboardService _clipboardService;
|
2019-05-14 16:01:07 +03:00
|
|
|
|
|
|
|
|
|
private bool _showNoData;
|
|
|
|
|
|
|
|
|
|
public GeneratorHistoryPageViewModel()
|
|
|
|
|
{
|
|
|
|
|
_platformUtilsService = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService");
|
2022-01-24 23:20:48 +03:00
|
|
|
|
_passwordGenerationService = ServiceContainer.Resolve<IPasswordGenerationService>("passwordGenerationService");
|
2021-12-10 23:41:36 +03:00
|
|
|
|
_clipboardService = ServiceContainer.Resolve<IClipboardService>("clipboardService");
|
2019-05-14 16:01:07 +03:00
|
|
|
|
|
|
|
|
|
PageTitle = AppResources.PasswordHistory;
|
|
|
|
|
History = new ExtendedObservableCollection<GeneratedPasswordHistory>();
|
|
|
|
|
CopyCommand = new Command<GeneratedPasswordHistory>(CopyAsync);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Command CopyCommand { get; set; }
|
|
|
|
|
public ExtendedObservableCollection<GeneratedPasswordHistory> History { get; set; }
|
|
|
|
|
|
|
|
|
|
public bool ShowNoData
|
|
|
|
|
{
|
|
|
|
|
get => _showNoData;
|
|
|
|
|
set => SetProperty(ref _showNoData, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task InitAsync()
|
|
|
|
|
{
|
|
|
|
|
var history = await _passwordGenerationService.GetHistoryAsync();
|
|
|
|
|
History.ResetWithRange(history ?? new List<GeneratedPasswordHistory>());
|
|
|
|
|
ShowNoData = History.Count == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task ClearAsync()
|
|
|
|
|
{
|
|
|
|
|
History.ResetWithRange(new List<GeneratedPasswordHistory>());
|
2019-05-31 18:13:46 +03:00
|
|
|
|
ShowNoData = true;
|
2019-05-14 16:01:07 +03:00
|
|
|
|
await _passwordGenerationService.ClearAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void CopyAsync(GeneratedPasswordHistory ph)
|
|
|
|
|
{
|
2021-12-10 23:41:36 +03:00
|
|
|
|
await _clipboardService.CopyTextAsync(ph.Password);
|
2019-05-14 16:01:07 +03:00
|
|
|
|
_platformUtilsService.ShowToast("info", null,
|
|
|
|
|
string.Format(AppResources.ValueHasBeenCopied, AppResources.Password));
|
|
|
|
|
}
|
2022-01-24 23:20:48 +03:00
|
|
|
|
|
|
|
|
|
public async Task UpdateOnThemeChanged()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Device.InvokeOnMainThreadAsync(() => History.ResetWithRange(new List<GeneratedPasswordHistory>()));
|
|
|
|
|
|
|
|
|
|
await InitAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
{
|
|
|
|
|
#if !FDROID
|
|
|
|
|
Crashes.TrackError(ex);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-14 16:01:07 +03:00
|
|
|
|
}
|
|
|
|
|
}
|