bitwarden-mobile/src/App/Pages/Vault/CiphersPageViewModel.cs

204 lines
7.8 KiB
C#
Raw Normal View History

2022-04-26 17:21:17 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Bit.App.Abstractions;
2019-05-21 04:18:34 +02:00
using Bit.App.Resources;
2019-06-08 18:18:49 +02:00
using Bit.Core;
using Bit.Core.Abstractions;
2019-05-21 04:18:34 +02:00
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Core.Models.View;
using Bit.Core.Utilities;
using Xamarin.Forms;
namespace Bit.App.Pages
{
public class CiphersPageViewModel : BaseViewModel
{
private readonly IPlatformUtilsService _platformUtilsService;
private readonly ICipherService _cipherService;
2019-05-07 04:35:42 +02:00
private readonly ISearchService _searchService;
2019-05-21 04:18:34 +02:00
private readonly IDeviceActionService _deviceActionService;
2019-06-08 18:18:49 +02:00
private readonly IStateService _stateService;
private readonly IPasswordRepromptService _passwordRepromptService;
2019-05-07 04:35:42 +02:00
private CancellationTokenSource _searchCancellationTokenSource;
private bool _showNoData;
2019-05-07 04:35:42 +02:00
private bool _showList;
2019-06-08 18:18:49 +02:00
private bool _websiteIconsEnabled;
public CiphersPageViewModel()
{
_platformUtilsService = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService");
_cipherService = ServiceContainer.Resolve<ICipherService>("cipherService");
2019-05-07 04:35:42 +02:00
_searchService = ServiceContainer.Resolve<ISearchService>("searchService");
2019-05-21 04:18:34 +02:00
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
2019-06-08 18:18:49 +02:00
_stateService = ServiceContainer.Resolve<IStateService>("stateService");
_passwordRepromptService = ServiceContainer.Resolve<IPasswordRepromptService>("passwordRepromptService");
2019-05-21 04:18:34 +02:00
Ciphers = new ExtendedObservableCollection<CipherView>();
CipherOptionsCommand = new Command<CipherView>(CipherOptionsAsync);
}
public Command CipherOptionsCommand { get; set; }
public ExtendedObservableCollection<CipherView> Ciphers { get; set; }
2019-05-07 04:35:42 +02:00
public Func<CipherView, bool> Filter { get; set; }
2019-05-21 04:18:34 +02:00
public string AutofillUrl { get; set; }
public bool Deleted { get; set; }
public bool ShowNoData
{
get => _showNoData;
2019-05-30 18:04:31 +02:00
set => SetProperty(ref _showNoData, value, additionalPropertyNames: new string[]
{
nameof(ShowSearchDirection)
});
}
2019-05-07 04:35:42 +02:00
public bool ShowList
{
get => _showList;
2019-05-30 18:04:31 +02:00
set => SetProperty(ref _showList, value, additionalPropertyNames: new string[]
{
nameof(ShowSearchDirection)
});
2019-05-07 04:35:42 +02:00
}
2019-05-30 18:04:31 +02:00
public bool ShowSearchDirection => !ShowList && !ShowNoData;
2019-06-08 18:18:49 +02:00
public bool WebsiteIconsEnabled
{
get => _websiteIconsEnabled;
set => SetProperty(ref _websiteIconsEnabled, value);
}
public async Task InitAsync()
{
Account Switching (#1807) * Account Switching (#1720) * Account switching * WIP * wip * wip * updates to send test logic * fixed Send tests * fixes for theme handling on account switching and re-adding existing account * switch fixes * fixes * fixes * cleanup * vault timeout fixes * account list status enhancements * logout fixes and token handling improvements * merge latest (#1727) * remove duplicate dependency * fix for initial login token storage paradox (#1730) * Fix avatar color update toolbar item issue on iOS for account switching (#1735) * Updated account switching menu UI (#1733) * updated account switching menu UI * additional changes * add key suffix to constant * GetFirstLetters method tweaks * Fix crash on account switching when logging out when having more than user at a time (#1740) * single account migration to multi-account on app update (#1741) * Account Switching Tap to dismiss (#1743) * Added tap to dismiss on the Account switching overlay and improved a bit the code * Fix account switching overlay background transparent on the proper place * Fixed transparent background and the shadow on the account switching overlay * Fix iOS top space on Account switching list overlay after modal (#1746) * Fix top space added to Account switching list overlay after closing modal * Fix top space added to Account switching list overlay after closing modal on lock, login and home views just in case we add modals in the future there as well * Usability: dismiss account list on certain events (#1748) * dismiss account list on certain events * use new FireAndForget method for back button logic * Create and use Account Switching overlay control (#1753) * Added Account switching overlay control and its own ViewModel and refactored accordingly * Fix account switching Accounts list binding update * Implemented dismiss account switching overlay when changing tabs and when selecting the same tab. Also updated the deprecated listener on CustomTabbedRenderer on Android (#1755) * Overriden Equals on AvatarImageSource so it doesn't get set multiple times when it's the same image thus producing blinking on tab chaged (#1756) * Usability improvements for logout on vault timeout (#1781) * accountswitching fixes (#1784) * Fix for invalid PIN lock state when switching accounts (#1792) * fix for pin lock flow * named tuple values and updated async * clear send service cache on account switch (#1796) * Global theme and account removal (#1793) * Global theme and account removal * remove redundant call to hide account list overlay * cleanup and additional tweaks * add try/catch to remove account dialog flow Co-authored-by: Federico Maccaroni <fedemkr@gmail.com>
2022-02-23 18:40:17 +01:00
WebsiteIconsEnabled = !(await _stateService.GetDisableFaviconAsync()).GetValueOrDefault();
if (!string.IsNullOrWhiteSpace((Page as CiphersPage).SearchBar.Text))
2019-07-07 04:19:29 +02:00
{
Search((Page as CiphersPage).SearchBar.Text, 200);
2019-07-07 04:19:29 +02:00
}
2019-06-08 18:18:49 +02:00
}
2019-05-07 04:35:42 +02:00
public void Search(string searchText, int? timeout = null)
{
var previousCts = _searchCancellationTokenSource;
var cts = new CancellationTokenSource();
Task.Run(async () =>
{
List<CipherView> ciphers = null;
var searchable = !string.IsNullOrWhiteSpace(searchText) && searchText.Length > 1;
if (searchable)
2019-05-07 04:35:42 +02:00
{
if (timeout != null)
2019-05-07 04:35:42 +02:00
{
await Task.Delay(timeout.Value);
}
if (searchText != (Page as CiphersPage).SearchBar.Text)
2019-05-07 04:35:42 +02:00
{
return;
}
else
{
previousCts?.Cancel();
}
try
{
2022-04-26 17:21:17 +02:00
ciphers = await _searchService.SearchCiphersAsync(searchText,
Filter ?? (c => c.IsDeleted == Deleted), null, cts.Token);
2019-05-07 04:35:42 +02:00
cts.Token.ThrowIfCancellationRequested();
}
catch (OperationCanceledException)
2019-05-07 04:35:42 +02:00
{
return;
2019-05-07 04:35:42 +02:00
}
}
if (ciphers == null)
2019-05-07 04:35:42 +02:00
{
ciphers = new List<CipherView>();
}
Device.BeginInvokeOnMainThread(() =>
{
Ciphers.ResetWithRange(ciphers);
ShowNoData = searchable && Ciphers.Count == 0;
ShowList = searchable && !ShowNoData;
});
2019-05-07 04:35:42 +02:00
}, cts.Token);
_searchCancellationTokenSource = cts;
}
public async Task SelectCipherAsync(CipherView cipher)
{
2019-05-21 04:18:34 +02:00
string selection = null;
if (!string.IsNullOrWhiteSpace(AutofillUrl))
2019-05-21 04:18:34 +02:00
{
var options = new List<string> { AppResources.Autofill };
if (cipher.Type == CipherType.Login &&
Xamarin.Essentials.Connectivity.NetworkAccess != Xamarin.Essentials.NetworkAccess.None)
2019-05-21 04:18:34 +02:00
{
options.Add(AppResources.AutofillAndSave);
}
options.Add(AppResources.View);
selection = await Page.DisplayActionSheet(AppResources.AutofillOrView, AppResources.Cancel, null,
options.ToArray());
}
if (selection == AppResources.View || string.IsNullOrWhiteSpace(AutofillUrl))
2019-05-21 04:18:34 +02:00
{
var page = new ViewPage(cipher.Id);
await Page.Navigation.PushModalAsync(new NavigationPage(page));
}
else if (selection == AppResources.Autofill || selection == AppResources.AutofillAndSave)
2019-05-21 04:18:34 +02:00
{
if (selection == AppResources.AutofillAndSave)
2019-05-21 04:18:34 +02:00
{
var uris = cipher.Login?.Uris?.ToList();
if (uris == null)
2019-05-21 04:18:34 +02:00
{
uris = new List<LoginUriView>();
}
uris.Add(new LoginUriView
{
Uri = AutofillUrl,
Match = null
});
cipher.Login.Uris = uris;
try
{
await _deviceActionService.ShowLoadingAsync(AppResources.Saving);
await _cipherService.SaveWithServerAsync(await _cipherService.EncryptAsync(cipher));
await _deviceActionService.HideLoadingAsync();
}
catch (ApiException e)
2019-05-21 04:18:34 +02:00
{
await _deviceActionService.HideLoadingAsync();
if (e?.Error != null)
2019-10-22 22:37:40 +02:00
{
await _platformUtilsService.ShowDialogAsync(e.Error.GetSingleMessage(),
AppResources.AnErrorHasOccurred);
}
2019-05-21 04:18:34 +02:00
}
}
if (_deviceActionService.SystemMajorVersion() < 21)
2019-05-21 04:18:34 +02:00
{
await Utilities.AppHelpers.CipherListOptions(Page, cipher, _passwordRepromptService);
2019-05-21 04:18:34 +02:00
}
else
{
_deviceActionService.Autofill(cipher);
}
}
}
private async void CipherOptionsAsync(CipherView cipher)
{
if ((Page as BaseContentPage).DoOnce())
2019-05-07 05:07:47 +02:00
{
await Utilities.AppHelpers.CipherListOptions(Page, cipher, _passwordRepromptService);
2019-05-07 05:07:47 +02:00
}
}
}
}