2022-02-23 18:40:17 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Bit.App.Controls;
|
2019-05-28 18:01:55 +02:00
|
|
|
|
using Bit.App.Resources;
|
2019-05-08 16:42:55 +02:00
|
|
|
|
using Bit.Core.Abstractions;
|
2019-04-24 22:50:34 +02:00
|
|
|
|
using Bit.Core.Enums;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
using Bit.Core.Models.Data;
|
2019-04-19 22:45:16 +02:00
|
|
|
|
using Bit.Core.Utilities;
|
2019-03-29 18:24:44 +01:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Bit.App.Pages
|
|
|
|
|
{
|
2019-05-01 16:20:05 +02:00
|
|
|
|
public partial class GroupingsPage : BaseContentPage
|
2019-03-29 18:24:44 +01:00
|
|
|
|
{
|
2019-04-19 22:45:16 +02:00
|
|
|
|
private readonly IBroadcasterService _broadcasterService;
|
|
|
|
|
private readonly ISyncService _syncService;
|
2019-05-28 18:01:55 +02:00
|
|
|
|
private readonly IPushNotificationService _pushNotificationService;
|
2022-02-23 18:40:17 +01:00
|
|
|
|
private readonly IStateService _stateService;
|
2020-05-29 18:26:36 +02:00
|
|
|
|
private readonly IVaultTimeoutService _vaultTimeoutService;
|
2019-11-22 15:51:30 +01:00
|
|
|
|
private readonly ICipherService _cipherService;
|
2019-06-04 16:51:10 +02:00
|
|
|
|
private readonly IDeviceActionService _deviceActionService;
|
2019-05-07 04:35:42 +02:00
|
|
|
|
private readonly GroupingsPageViewModel _vm;
|
2019-05-14 15:48:40 +02:00
|
|
|
|
private readonly string _pageName;
|
2019-04-24 22:50:34 +02:00
|
|
|
|
|
2019-07-31 22:50:16 +02:00
|
|
|
|
private PreviousPageInfo _previousPage;
|
|
|
|
|
|
2019-04-24 22:50:34 +02:00
|
|
|
|
public GroupingsPage(bool mainPage, CipherType? type = null, string folderId = null,
|
2022-04-26 17:21:17 +02:00
|
|
|
|
string collectionId = null, string pageTitle = null, PreviousPageInfo previousPage = null,
|
2020-05-20 19:35:20 +02:00
|
|
|
|
bool deleted = false)
|
2019-03-29 18:24:44 +01:00
|
|
|
|
{
|
2019-05-14 15:48:40 +02:00
|
|
|
|
_pageName = string.Concat(nameof(GroupingsPage), "_", DateTime.UtcNow.Ticks);
|
2019-03-29 18:24:44 +01:00
|
|
|
|
InitializeComponent();
|
2019-05-08 14:33:17 +02:00
|
|
|
|
SetActivityIndicator(_mainContent);
|
2019-04-19 22:45:16 +02:00
|
|
|
|
_broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
|
|
|
|
|
_syncService = ServiceContainer.Resolve<ISyncService>("syncService");
|
2019-05-28 18:01:55 +02:00
|
|
|
|
_pushNotificationService = ServiceContainer.Resolve<IPushNotificationService>("pushNotificationService");
|
2022-02-23 18:40:17 +01:00
|
|
|
|
_stateService = ServiceContainer.Resolve<IStateService>("stateService");
|
2020-05-29 18:26:36 +02:00
|
|
|
|
_vaultTimeoutService = ServiceContainer.Resolve<IVaultTimeoutService>("vaultTimeoutService");
|
2019-11-22 15:51:30 +01:00
|
|
|
|
_cipherService = ServiceContainer.Resolve<ICipherService>("cipherService");
|
2019-06-04 16:51:10 +02:00
|
|
|
|
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
|
2019-05-07 04:35:42 +02:00
|
|
|
|
_vm = BindingContext as GroupingsPageViewModel;
|
|
|
|
|
_vm.Page = this;
|
|
|
|
|
_vm.MainPage = mainPage;
|
|
|
|
|
_vm.Type = type;
|
|
|
|
|
_vm.FolderId = folderId;
|
|
|
|
|
_vm.CollectionId = collectionId;
|
2020-05-20 19:35:20 +02:00
|
|
|
|
_vm.Deleted = deleted;
|
2019-07-31 22:50:16 +02:00
|
|
|
|
_previousPage = previousPage;
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (pageTitle != null)
|
2019-04-24 22:50:34 +02:00
|
|
|
|
{
|
2019-05-07 04:35:42 +02:00
|
|
|
|
_vm.PageTitle = pageTitle;
|
2019-04-24 22:50:34 +02:00
|
|
|
|
}
|
2019-05-08 14:33:17 +02:00
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (Device.RuntimePlatform == Device.iOS)
|
2019-05-08 14:33:17 +02:00
|
|
|
|
{
|
|
|
|
|
_absLayout.Children.Remove(_fab);
|
2019-06-14 23:31:06 +02:00
|
|
|
|
ToolbarItems.Add(_addItem);
|
2019-05-08 14:33:17 +02:00
|
|
|
|
}
|
2019-05-08 16:42:55 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
2019-05-30 06:29:00 +02:00
|
|
|
|
ToolbarItems.Add(_syncItem);
|
|
|
|
|
ToolbarItems.Add(_lockItem);
|
|
|
|
|
ToolbarItems.Add(_exitItem);
|
2019-05-08 16:42:55 +02:00
|
|
|
|
}
|
2020-05-20 19:35:20 +02:00
|
|
|
|
if (deleted)
|
|
|
|
|
{
|
|
|
|
|
_absLayout.Children.Remove(_fab);
|
|
|
|
|
ToolbarItems.Remove(_addItem);
|
|
|
|
|
}
|
2022-02-23 18:40:17 +01:00
|
|
|
|
if (!mainPage)
|
|
|
|
|
{
|
|
|
|
|
ToolbarItems.Remove(_accountAvatar);
|
|
|
|
|
}
|
2019-03-29 18:24:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (_syncService.SyncInProgress)
|
2019-06-05 15:39:30 +02:00
|
|
|
|
{
|
|
|
|
|
IsBusy = true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
_accountAvatar?.OnAppearing();
|
|
|
|
|
if (_vm.MainPage)
|
|
|
|
|
{
|
|
|
|
|
_vm.AvatarImageSource = await GetAvatarImageSourceAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 15:48:40 +02:00
|
|
|
|
_broadcasterService.Subscribe(_pageName, async (message) =>
|
2019-04-19 22:45:16 +02:00
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (message.Command == "syncStarted")
|
2019-06-05 15:39:30 +02:00
|
|
|
|
{
|
|
|
|
|
Device.BeginInvokeOnMainThread(() => IsBusy = true);
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
else if (message.Command == "syncCompleted")
|
2019-04-19 22:45:16 +02:00
|
|
|
|
{
|
|
|
|
|
await Task.Delay(500);
|
2019-05-30 17:40:33 +02:00
|
|
|
|
Device.BeginInvokeOnMainThread(() =>
|
|
|
|
|
{
|
2019-06-05 15:39:30 +02:00
|
|
|
|
IsBusy = false;
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (_vm.LoadedOnce)
|
2019-06-21 22:53:17 +02:00
|
|
|
|
{
|
|
|
|
|
var task = _vm.LoadAsync();
|
|
|
|
|
}
|
2019-05-30 17:40:33 +02:00
|
|
|
|
});
|
2019-04-19 22:45:16 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-05-01 16:20:05 +02:00
|
|
|
|
await LoadOnAppearedAsync(_mainLayout, false, async () =>
|
2019-04-19 22:45:16 +02:00
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!_syncService.SyncInProgress || (await _cipherService.GetAllAsync()).Any())
|
2019-04-19 22:45:16 +02:00
|
|
|
|
{
|
2019-06-13 20:08:21 +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-13 20:08:21 +02:00
|
|
|
|
{
|
2019-10-06 03:31:55 +02:00
|
|
|
|
await Task.Delay(1000);
|
2019-06-13 20:08:21 +02:00
|
|
|
|
await _vm.LoadAsync();
|
|
|
|
|
}
|
2019-04-19 22:45:16 +02:00
|
|
|
|
}
|
2019-05-01 16:20:05 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(5000);
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!_vm.Loaded)
|
2019-05-01 16:20:05 +02:00
|
|
|
|
{
|
2019-05-07 04:35:42 +02:00
|
|
|
|
await _vm.LoadAsync();
|
2019-05-01 16:20:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-31 22:50:16 +02:00
|
|
|
|
await ShowPreviousPageAsync();
|
2020-05-20 19:35:20 +02:00
|
|
|
|
AdjustToolbar();
|
2019-05-08 14:33:17 +02:00
|
|
|
|
}, _mainContent);
|
2019-05-28 18:01:55 +02:00
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!_vm.MainPage)
|
2019-06-05 05:25:09 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 18:01:55 +02:00
|
|
|
|
// Push registration
|
2022-02-23 18:40:17 +01:00
|
|
|
|
var lastPushRegistration = await _stateService.GetPushLastRegistrationDateAsync();
|
2019-05-28 18:01:55 +02:00
|
|
|
|
lastPushRegistration = lastPushRegistration.GetValueOrDefault(DateTime.MinValue);
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (Device.RuntimePlatform == Device.iOS)
|
2019-05-28 18:01:55 +02:00
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
var pushPromptShow = await _stateService.GetPushInitialPromptShownAsync();
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!pushPromptShow.GetValueOrDefault(false))
|
2019-05-28 18:01:55 +02:00
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
await _stateService.SetPushInitialPromptShownAsync(true);
|
2019-05-28 18:01:55 +02:00
|
|
|
|
await DisplayAlert(AppResources.EnableAutomaticSyncing, AppResources.PushNotificationAlert,
|
|
|
|
|
AppResources.OkGotIt);
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!pushPromptShow.GetValueOrDefault(false) ||
|
2019-05-28 18:01:55 +02:00
|
|
|
|
DateTime.UtcNow - lastPushRegistration > TimeSpan.FromDays(1))
|
|
|
|
|
{
|
|
|
|
|
await _pushNotificationService.RegisterAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
else if (Device.RuntimePlatform == Device.Android)
|
2019-05-28 18:01:55 +02:00
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (DateTime.UtcNow - lastPushRegistration > TimeSpan.FromDays(1))
|
2019-06-04 16:51:10 +02:00
|
|
|
|
{
|
|
|
|
|
await _pushNotificationService.RegisterAsync();
|
|
|
|
|
}
|
2019-05-28 18:01:55 +02:00
|
|
|
|
}
|
2019-04-19 22:45:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 18:40:17 +01:00
|
|
|
|
protected override bool OnBackButtonPressed()
|
|
|
|
|
{
|
|
|
|
|
if (_accountListOverlay.IsVisible)
|
|
|
|
|
{
|
|
|
|
|
_accountListOverlay.HideAsync().FireAndForget();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-19 22:45:16 +02:00
|
|
|
|
protected override void OnDisappearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnDisappearing();
|
2019-06-05 15:39:30 +02:00
|
|
|
|
IsBusy = false;
|
2019-05-14 15:48:40 +02:00
|
|
|
|
_broadcasterService.Unsubscribe(_pageName);
|
2020-08-05 19:19:27 +02:00
|
|
|
|
_vm.DisableRefreshing();
|
2022-02-23 18:40:17 +01:00
|
|
|
|
_accountAvatar?.OnDisappearing();
|
2019-03-29 18:24:44 +01:00
|
|
|
|
}
|
2019-04-24 17:23:03 +02:00
|
|
|
|
|
2021-05-13 20:36:20 +02:00
|
|
|
|
private async void RowSelected(object sender, SelectionChangedEventArgs e)
|
2019-04-24 17:23:03 +02:00
|
|
|
|
{
|
2021-05-13 20:36:20 +02:00
|
|
|
|
((ExtendedCollectionView)sender).SelectedItem = null;
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!DoOnce())
|
2019-05-07 04:49:57 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-05-13 20:36:20 +02:00
|
|
|
|
if (!(e.CurrentSelection?.FirstOrDefault() is GroupingsPageListItem item))
|
2019-04-24 17:23:03 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 19:35:20 +02:00
|
|
|
|
if (item.IsTrash)
|
|
|
|
|
{
|
|
|
|
|
await _vm.SelectTrashAsync();
|
|
|
|
|
}
|
|
|
|
|
else if (item.Cipher != null)
|
2019-04-24 17:23:03 +02:00
|
|
|
|
{
|
2019-05-07 04:35:42 +02:00
|
|
|
|
await _vm.SelectCipherAsync(item.Cipher);
|
2019-04-24 17:23:03 +02:00
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
else if (item.Folder != null)
|
2019-04-24 22:50:34 +02:00
|
|
|
|
{
|
2019-05-07 04:35:42 +02:00
|
|
|
|
await _vm.SelectFolderAsync(item.Folder);
|
2019-04-24 22:50:34 +02:00
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
else if (item.Collection != null)
|
2019-04-24 17:23:03 +02:00
|
|
|
|
{
|
2019-05-07 04:35:42 +02:00
|
|
|
|
await _vm.SelectCollectionAsync(item.Collection);
|
2019-04-24 17:23:03 +02:00
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
else if (item.Type != null)
|
2019-05-01 17:31:00 +02:00
|
|
|
|
{
|
2019-05-07 04:35:42 +02:00
|
|
|
|
await _vm.SelectTypeAsync(item.Type.Value);
|
2019-05-01 17:31:00 +02:00
|
|
|
|
}
|
2019-04-24 17:23:03 +02:00
|
|
|
|
}
|
2019-05-03 03:25:26 +02:00
|
|
|
|
|
2019-06-01 07:07:02 +02:00
|
|
|
|
private async void Search_Clicked(object sender, EventArgs e)
|
2019-05-03 03:25:26 +02:00
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
await _accountListOverlay.HideAsync();
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (DoOnce())
|
2019-05-07 05:07:47 +02:00
|
|
|
|
{
|
2022-04-10 19:00:52 +02:00
|
|
|
|
var page = new CiphersPage(_vm.Filter, _vm.MainPage ? null : _vm.PageTitle, deleted: _vm.Deleted);
|
2021-10-18 15:56:20 +02:00
|
|
|
|
await Navigation.PushModalAsync(new NavigationPage(page));
|
2019-05-07 05:07:47 +02:00
|
|
|
|
}
|
2019-05-03 03:25:26 +02:00
|
|
|
|
}
|
2019-05-08 16:42:55 +02:00
|
|
|
|
|
2019-06-01 07:07:02 +02:00
|
|
|
|
private async void Sync_Clicked(object sender, EventArgs e)
|
2019-05-30 06:29:00 +02:00
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
await _accountListOverlay.HideAsync();
|
2019-05-30 06:29:00 +02:00
|
|
|
|
await _vm.SyncAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-01 07:07:02 +02:00
|
|
|
|
private async void Lock_Clicked(object sender, EventArgs e)
|
2019-05-30 06:29:00 +02:00
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
await _accountListOverlay.HideAsync();
|
2020-05-29 18:26:36 +02:00
|
|
|
|
await _vaultTimeoutService.LockAsync(true, true);
|
2019-05-30 06:29:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-01 07:07:02 +02:00
|
|
|
|
private async void Exit_Clicked(object sender, EventArgs e)
|
2019-05-30 06:29:00 +02:00
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
await _accountListOverlay.HideAsync();
|
2019-05-30 06:29:00 +02:00
|
|
|
|
await _vm.ExitAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-01 07:07:02 +02:00
|
|
|
|
private async void AddButton_Clicked(object sender, EventArgs e)
|
2019-05-08 16:42:55 +02:00
|
|
|
|
{
|
2022-02-23 18:40:17 +01:00
|
|
|
|
var skipAction = _accountListOverlay.IsVisible && Device.RuntimePlatform == Device.Android;
|
|
|
|
|
await _accountListOverlay.HideAsync();
|
|
|
|
|
if (skipAction)
|
|
|
|
|
{
|
|
|
|
|
// Account list in the process of closing via tapping on invisible FAB, skip this attempt
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-20 19:35:20 +02:00
|
|
|
|
if (!_vm.Deleted && DoOnce())
|
2019-05-31 17:55:11 +02:00
|
|
|
|
{
|
|
|
|
|
var page = new AddEditPage(null, _vm.Type, _vm.FolderId, _vm.CollectionId);
|
|
|
|
|
await Navigation.PushModalAsync(new NavigationPage(page));
|
|
|
|
|
}
|
2019-05-08 16:42:55 +02:00
|
|
|
|
}
|
2019-07-31 22:50:16 +02:00
|
|
|
|
|
|
|
|
|
private async Task ShowPreviousPageAsync()
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (_previousPage == null)
|
2019-07-31 22:50:16 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-02-23 18:40:17 +01:00
|
|
|
|
await _accountListOverlay.HideAsync();
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (_previousPage.Page == "view" && !string.IsNullOrWhiteSpace(_previousPage.CipherId))
|
2019-07-31 22:50:16 +02:00
|
|
|
|
{
|
|
|
|
|
await Navigation.PushModalAsync(new NavigationPage(new ViewPage(_previousPage.CipherId)));
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
else if (_previousPage.Page == "edit" && !string.IsNullOrWhiteSpace(_previousPage.CipherId))
|
2019-07-31 22:50:16 +02:00
|
|
|
|
{
|
|
|
|
|
await Navigation.PushModalAsync(new NavigationPage(new AddEditPage(_previousPage.CipherId)));
|
|
|
|
|
}
|
|
|
|
|
_previousPage = null;
|
|
|
|
|
}
|
2020-05-20 19:35:20 +02:00
|
|
|
|
|
|
|
|
|
private void AdjustToolbar()
|
|
|
|
|
{
|
|
|
|
|
_addItem.IsEnabled = !_vm.Deleted;
|
|
|
|
|
_addItem.IconImageSource = _vm.Deleted ? null : "plus.png";
|
|
|
|
|
}
|
2022-02-23 18:40:17 +01:00
|
|
|
|
|
|
|
|
|
public async Task HideAccountSwitchingOverlayAsync()
|
|
|
|
|
{
|
|
|
|
|
await _accountListOverlay.HideAsync();
|
|
|
|
|
}
|
2019-03-29 18:24:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|