bitwarden-android/src/App/Pages/Vault/AutofillCiphersPageViewModel.cs

183 lines
6.8 KiB
C#
Raw Normal View History

2019-05-17 21:46:31 +03:00
using Bit.App.Abstractions;
using Bit.App.Models;
2019-05-17 20:14:26 +03:00
using Bit.App.Resources;
2019-05-30 06:35:34 +03:00
using Bit.App.Utilities;
2019-05-17 20:14:26 +03:00
using Bit.Core;
using Bit.Core.Abstractions;
2019-05-17 21:46:31 +03:00
using Bit.Core.Enums;
using Bit.Core.Exceptions;
2019-05-17 20:14:26 +03:00
using Bit.Core.Models.View;
using Bit.Core.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Bit.App.Pages
{
public class AutofillCiphersPageViewModel : BaseViewModel
{
private readonly IPlatformUtilsService _platformUtilsService;
2019-05-17 21:46:31 +03:00
private readonly IDeviceActionService _deviceActionService;
2019-05-17 20:14:26 +03:00
private readonly ICipherService _cipherService;
2019-06-08 19:18:49 +03:00
private readonly IStateService _stateService;
2019-05-17 20:14:26 +03:00
private AppOptions _appOptions;
private bool _showList;
2019-05-17 21:58:42 +03:00
private string _noDataText;
2019-06-08 19:18:49 +03:00
private bool _websiteIconsEnabled;
2019-05-17 20:14:26 +03:00
public AutofillCiphersPageViewModel()
{
_platformUtilsService = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService");
_cipherService = ServiceContainer.Resolve<ICipherService>("cipherService");
2019-05-17 21:46:31 +03:00
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
2019-06-08 19:18:49 +03:00
_stateService = ServiceContainer.Resolve<IStateService>("stateService");
2019-05-17 20:14:26 +03:00
GroupedItems = new ExtendedObservableCollection<GroupingsPageListGroup>();
CipherOptionsCommand = new Command<CipherView>(CipherOptionsAsync);
}
2019-05-17 22:24:15 +03:00
public string Name { get; set; }
public string Uri { get; set; }
2019-05-17 20:14:26 +03:00
public Command CipherOptionsCommand { get; set; }
public ExtendedObservableCollection<GroupingsPageListGroup> GroupedItems { get; set; }
public bool ShowList
{
get => _showList;
set => SetProperty(ref _showList, value);
}
2019-05-17 21:58:42 +03:00
public string NoDataText
{
get => _noDataText;
set => SetProperty(ref _noDataText, value);
}
2019-06-08 19:18:49 +03:00
public bool WebsiteIconsEnabled
{
get => _websiteIconsEnabled;
set => SetProperty(ref _websiteIconsEnabled, value);
}
2019-05-17 21:58:42 +03:00
2019-05-17 20:14:26 +03:00
public void Init(AppOptions appOptions)
{
_appOptions = appOptions;
2019-05-17 22:24:15 +03:00
Uri = appOptions.Uri;
string name = null;
if(Uri.StartsWith(Constants.AndroidAppProtocol))
2019-05-17 20:14:26 +03:00
{
2019-05-17 22:24:15 +03:00
name = Uri.Substring(Constants.AndroidAppProtocol.Length);
2019-05-17 20:14:26 +03:00
}
2019-05-31 15:30:18 +03:00
else
{
name = CoreHelpers.GetDomain(Uri);
}
if(string.IsNullOrWhiteSpace(name))
2019-05-17 20:14:26 +03:00
{
2019-05-17 22:24:15 +03:00
name = "--";
2019-05-17 20:14:26 +03:00
}
2019-05-17 22:24:15 +03:00
Name = name;
PageTitle = string.Format(AppResources.ItemsForUri, Name ?? "--");
NoDataText = string.Format(AppResources.NoItemsForUri, Name ?? "--");
2019-05-17 20:14:26 +03:00
}
public async Task LoadAsync()
{
2019-06-08 19:18:49 +03:00
WebsiteIconsEnabled = !(await _stateService.GetAsync<bool?>(Constants.DisableFaviconKey))
.GetValueOrDefault();
2019-05-17 20:14:26 +03:00
ShowList = false;
2019-05-17 21:58:42 +03:00
var groupedItems = new List<GroupingsPageListGroup>();
2019-05-17 22:24:15 +03:00
var ciphers = await _cipherService.GetAllDecryptedByUrlAsync(Uri, null);
2019-05-17 20:14:26 +03:00
var matching = ciphers.Item1?.Select(c => new GroupingsPageListItem { Cipher = c }).ToList();
2019-05-31 18:36:44 +03:00
var hasMatching = matching?.Any() ?? false;
2019-05-17 21:58:42 +03:00
if(matching?.Any() ?? false)
{
groupedItems.Add(
2019-05-31 18:36:44 +03:00
new GroupingsPageListGroup(matching, AppResources.MatchingItems, matching.Count, false, true));
2019-05-17 21:58:42 +03:00
}
var fuzzy = ciphers.Item2?.Select(c =>
new GroupingsPageListItem { Cipher = c, FuzzyAutofill = true }).ToList();
if(fuzzy?.Any() ?? false)
{
groupedItems.Add(
2019-05-31 18:36:44 +03:00
new GroupingsPageListGroup(fuzzy, AppResources.PossibleMatchingItems, fuzzy.Count, false,
!hasMatching));
2019-05-17 21:58:42 +03:00
}
GroupedItems.ResetWithRange(groupedItems);
ShowList = groupedItems.Any();
2019-05-17 20:14:26 +03:00
}
2019-05-17 21:46:31 +03:00
public async Task SelectCipherAsync(CipherView cipher, bool fuzzy)
2019-05-17 20:14:26 +03:00
{
2019-06-14 15:05:28 +03:00
if(cipher == null)
{
return;
}
2019-05-17 21:46:31 +03:00
if(_deviceActionService.SystemMajorVersion() < 21)
{
2019-05-30 06:35:34 +03:00
await AppHelpers.CipherListOptions(Page, cipher);
2019-05-17 21:46:31 +03:00
}
else
{
var autofillResponse = AppResources.Yes;
if(fuzzy)
{
var options = new List<string> { AppResources.Yes };
if(cipher.Type == CipherType.Login &&
Xamarin.Essentials.Connectivity.NetworkAccess != Xamarin.Essentials.NetworkAccess.None)
2019-05-17 21:46:31 +03:00
{
options.Add(AppResources.YesAndSave);
}
autofillResponse = await _deviceActionService.DisplayAlertAsync(null,
2019-05-17 22:24:15 +03:00
string.Format(AppResources.BitwardenAutofillServiceMatchConfirm, Name), AppResources.No,
2019-05-17 21:46:31 +03:00
options.ToArray());
}
if(autofillResponse == AppResources.YesAndSave && cipher.Type == CipherType.Login)
{
var uris = cipher.Login?.Uris?.ToList();
if(uris == null)
{
uris = new List<LoginUriView>();
}
uris.Add(new LoginUriView
{
2019-05-17 22:24:15 +03:00
Uri = Uri,
2019-05-17 21:46:31 +03:00
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)
{
await _deviceActionService.HideLoadingAsync();
2019-10-22 23:37:40 +03:00
if(e?.Error != null)
{
await _platformUtilsService.ShowDialogAsync(e.Error.GetSingleMessage(),
AppResources.AnErrorHasOccurred);
}
2019-05-17 21:46:31 +03:00
}
}
if(autofillResponse == AppResources.Yes || autofillResponse == AppResources.YesAndSave)
{
_deviceActionService.Autofill(cipher);
}
}
2019-05-17 20:14:26 +03:00
}
private async void CipherOptionsAsync(CipherView cipher)
{
2019-05-31 00:24:41 +03:00
if((Page as BaseContentPage).DoOnce())
2019-05-17 20:14:26 +03:00
{
2019-05-31 00:24:41 +03:00
await AppHelpers.CipherListOptions(Page, cipher);
2019-05-17 20:14:26 +03:00
}
}
}
}