bitwarden-mobile/src/App/Pages/Vault/AutofillCiphersPage.xaml.cs

148 lines
4.9 KiB
C#
Raw Normal View History

2022-04-26 17:21:17 +02:00
using System;
using System.Linq;
2019-06-14 02:41:24 +02:00
using System.Threading.Tasks;
using Bit.App.Controls;
2022-04-26 17:21:17 +02:00
using Bit.App.Models;
using Bit.App.Utilities;
2022-04-26 17:21:17 +02:00
using Bit.Core.Abstractions;
using Bit.Core.Enums;
using Bit.Core.Utilities;
2019-05-17 19:14:26 +02:00
using Xamarin.Forms;
namespace Bit.App.Pages
{
public partial class AutofillCiphersPage : BaseContentPage
{
private readonly AppOptions _appOptions;
private readonly IBroadcasterService _broadcasterService;
private readonly ISyncService _syncService;
private readonly IVaultTimeoutService _vaultTimeoutService;
2019-05-21 04:18:34 +02:00
private AutofillCiphersPageViewModel _vm;
2019-05-17 19:14:26 +02:00
public AutofillCiphersPage(AppOptions appOptions)
{
_appOptions = appOptions;
InitializeComponent();
SetActivityIndicator(_mainContent);
2019-05-17 19:14:26 +02:00
_vm = BindingContext as AutofillCiphersPageViewModel;
_vm.Page = this;
_vm.Init(appOptions);
2019-05-21 04:18:34 +02:00
_broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
_syncService = ServiceContainer.Resolve<ISyncService>("syncService");
_vaultTimeoutService = ServiceContainer.Resolve<IVaultTimeoutService>("vaultTimeoutService");
2019-05-17 19:14:26 +02:00
}
protected async override void OnAppearing()
{
base.OnAppearing();
if (_syncService.SyncInProgress)
{
IsBusy = true;
}
if (!await AppHelpers.IsVaultTimeoutImmediateAsync())
{
await _vaultTimeoutService.CheckVaultTimeoutAsync();
}
if (await _vaultTimeoutService.IsLockedAsync())
{
return;
}
_accountAvatar?.OnAppearing();
_vm.AvatarImageSource = await GetAvatarImageSourceAsync();
_broadcasterService.Subscribe(nameof(AutofillCiphersPage), async (message) =>
{
if (message.Command == "syncStarted")
{
Device.BeginInvokeOnMainThread(() => IsBusy = true);
}
else if (message.Command == "syncCompleted")
{
await Task.Delay(500);
Device.BeginInvokeOnMainThread(() =>
{
IsBusy = false;
if (_vm.LoadedOnce)
{
var task = _vm.LoadAsync();
}
});
}
});
2022-04-26 17:21:17 +02:00
2019-05-17 19:14:26 +02:00
await LoadOnAppearedAsync(_mainLayout, false, async () =>
{
2019-06-14 02:41:24 +02:00
try
{
await _vm.LoadAsync();
}
2022-04-26 17:21:17 +02:00
catch (Exception e) when (e.Message.Contains("No key."))
2019-06-14 02:41:24 +02:00
{
2019-10-06 03:31:55 +02:00
await Task.Delay(1000);
2019-06-14 02:41:24 +02:00
await _vm.LoadAsync();
}
2019-05-17 19:14:26 +02:00
}, _mainContent);
}
protected override bool OnBackButtonPressed()
{
if (_accountListOverlay.IsVisible)
{
_accountListOverlay.HideAsync().FireAndForget();
return true;
}
if (Device.RuntimePlatform == Device.Android)
{
_appOptions.Uri = null;
}
return base.OnBackButtonPressed();
}
protected override void OnDisappearing()
{
base.OnDisappearing();
IsBusy = false;
_accountAvatar?.OnDisappearing();
}
private async void RowSelected(object sender, SelectionChangedEventArgs e)
2019-05-17 19:14:26 +02:00
{
((ExtendedCollectionView)sender).SelectedItem = null;
if (!DoOnce())
2019-05-17 19:14:26 +02:00
{
return;
}
if (e.CurrentSelection?.FirstOrDefault() is GroupingsPageListItem item && item.Cipher != null)
2019-05-17 19:14:26 +02:00
{
2019-05-17 20:46:31 +02:00
await _vm.SelectCipherAsync(item.Cipher, item.FuzzyAutofill);
2019-05-17 19:14:26 +02:00
}
}
2019-05-17 20:46:31 +02:00
private async void AddButton_Clicked(object sender, System.EventArgs e)
2019-05-17 19:14:26 +02:00
{
if (!DoOnce())
2019-05-17 20:46:31 +02:00
{
return;
}
if (_appOptions.FillType.HasValue && _appOptions.FillType != CipherType.Login)
2019-05-17 20:46:31 +02:00
{
2019-05-17 21:24:15 +02:00
var pageForOther = new AddEditPage(type: _appOptions.FillType, fromAutofill: true);
2019-05-17 20:46:31 +02:00
await Navigation.PushModalAsync(new NavigationPage(pageForOther));
return;
}
2019-05-17 21:24:15 +02:00
var pageForLogin = new AddEditPage(null, CipherType.Login, uri: _vm.Uri, name: _vm.Name,
fromAutofill: true);
2019-05-17 20:46:31 +02:00
await Navigation.PushModalAsync(new NavigationPage(pageForLogin));
2019-05-17 19:14:26 +02:00
}
private void Search_Clicked(object sender, System.EventArgs e)
{
2019-05-21 04:18:34 +02:00
var page = new CiphersPage(null, autofillUrl: _vm.Uri);
Application.Current.MainPage = new NavigationPage(page);
2019-05-17 19:14:26 +02:00
}
}
}