Bitwarden-app-android-iphon.../src/App/Pages/Vault/CipherSelectionPage.xaml.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

184 lines
6.1 KiB
C#
Raw Normal View History

2019-06-14 02:41:24 +02:00
using System;
using System.Threading.Tasks;
using Bit.App.Abstractions;
2019-05-17 19:14:26 +02:00
using Bit.App.Models;
using Bit.App.Utilities;
2019-05-21 04:18:34 +02:00
using Bit.Core.Abstractions;
2019-05-17 20:46:31 +02:00
using Bit.Core.Enums;
using Bit.Core.Services;
2019-05-21 04:18:34 +02:00
using Bit.Core.Utilities;
2019-05-17 19:14:26 +02:00
using Xamarin.Forms;
namespace Bit.App.Pages
{
public partial class CipherSelectionPage : BaseContentPage
2019-05-17 19:14:26 +02:00
{
private readonly AppOptions _appOptions;
private readonly IBroadcasterService _broadcasterService;
private readonly ISyncService _syncService;
private readonly IVaultTimeoutService _vaultTimeoutService;
private readonly IAccountsManager _accountsManager;
2019-05-21 04:18:34 +02:00
private readonly CipherSelectionPageViewModel _vm;
2019-05-17 19:14:26 +02:00
public CipherSelectionPage(AppOptions appOptions)
2019-05-17 19:14:26 +02:00
{
_appOptions = appOptions;
if (appOptions?.OtpData is null)
{
BindingContext = new AutofillCiphersPageViewModel();
}
else
{
BindingContext = new OTPCipherSelectionPageViewModel();
}
2019-05-17 19:14:26 +02:00
InitializeComponent();
if (Device.RuntimePlatform == Device.iOS)
{
ToolbarItems.Add(_closeItem);
ToolbarItems.Add(_addItem);
}
SetActivityIndicator(_mainContent);
_vm = BindingContext as CipherSelectionPageViewModel;
2019-05-17 19:14:26 +02:00
_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");
_accountsManager = ServiceContainer.Resolve<IAccountsManager>();
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;
}
// TODO: There's currently an issue on iOS where the toolbar item is not getting updated
// as the others somehow. Removing this so at least we get the circle with ".." instead
// of a white circle
if (Device.RuntimePlatform != Device.iOS)
{
_accountAvatar?.OnAppearing();
_vm.AvatarImageSource = await GetAvatarImageSourceAsync();
}
_broadcasterService.Subscribe(nameof(CipherSelectionPage), async (message) =>
{
try
{
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();
}
});
}
}
catch (Exception ex)
{
LoggerHelper.LogEvenIfCantBeResolved(ex);
}
});
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();
}
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 void AddButton_Clicked(object sender, System.EventArgs e)
2019-05-17 19:14:26 +02:00
{
if (!DoOnce())
2019-05-17 19:14:26 +02:00
{
return;
}
if (_vm is AutofillCiphersPageViewModel autofillVM)
2019-05-17 19:14:26 +02:00
{
AddFromAutofill(autofillVM).FireAndForget();
2019-05-17 19:14:26 +02:00
}
}
private async Task AddFromAutofill(AutofillCiphersPageViewModel autofillVM)
2019-05-17 19:14:26 +02:00
{
if (_appOptions.FillType.HasValue && _appOptions.FillType != CipherType.Login)
2019-05-17 20:46:31 +02:00
{
var pageForOther = new CipherAddEditPage(type: _appOptions.FillType, fromAutofill: true);
2019-05-17 20:46:31 +02:00
await Navigation.PushModalAsync(new NavigationPage(pageForOther));
return;
}
var pageForLogin = new CipherAddEditPage(null, CipherType.Login, uri: autofillVM.Uri, name: _vm.Name,
2019-05-17 21:24:15 +02:00
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, EventArgs e)
2019-05-17 19:14:26 +02:00
{
var page = new CiphersPage(null, appOptions: _appOptions);
Navigation.PushModalAsync(new NavigationPage(page)).FireAndForget();
}
void CloseItem_Clicked(object sender, EventArgs e)
{
if (DoOnce())
{
_accountsManager.StartDefaultNavigationFlowAsync(op => op.OtpData = null).FireAndForget();
}
2019-05-17 19:14:26 +02:00
}
}
}