2019-05-28 18:01:55 +02:00
|
|
|
|
using Bit.App.Abstractions;
|
2019-06-05 06:15:54 +02:00
|
|
|
|
using Bit.App.Controls;
|
2019-07-31 22:50:16 +02:00
|
|
|
|
using Bit.App.Models;
|
2019-05-28 18:01:55 +02:00
|
|
|
|
using Bit.App.Resources;
|
|
|
|
|
using Bit.Core;
|
2019-05-08 16:42:55 +02:00
|
|
|
|
using Bit.Core.Abstractions;
|
2019-04-24 22:50:34 +02:00
|
|
|
|
using Bit.Core.Enums;
|
2019-04-19 22:45:16 +02:00
|
|
|
|
using Bit.Core.Utilities;
|
2019-05-14 15:48:40 +02:00
|
|
|
|
using System;
|
2019-11-22 15:51:30 +01:00
|
|
|
|
using System.Linq;
|
2019-03-29 18:24:44 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
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;
|
|
|
|
|
private readonly IStorageService _storageService;
|
2019-05-30 06:29:00 +02:00
|
|
|
|
private readonly ILockService _lockService;
|
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,
|
2019-07-31 22:50:16 +02:00
|
|
|
|
string collectionId = null, string pageTitle = null, PreviousPageInfo previousPage = null)
|
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-06-05 06:15:54 +02:00
|
|
|
|
ListView = _listView;
|
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");
|
|
|
|
|
_storageService = ServiceContainer.Resolve<IStorageService>("storageService");
|
2019-05-30 06:29:00 +02:00
|
|
|
|
_lockService = ServiceContainer.Resolve<ILockService>("lockService");
|
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;
|
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
|
|
|
|
}
|
2019-03-29 18:24:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-05 06:15:54 +02:00
|
|
|
|
public ExtendedListView ListView { get; set; }
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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-06-05 05:25:09 +02:00
|
|
|
|
var migratedFromV1 = await _storageService.GetAsync<bool?>(Constants.MigratedFromV1);
|
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();
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01: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-06-05 05:26:56 +02:00
|
|
|
|
// Forced sync if for some reason we have no data after a v1 migration
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (_vm.MainPage && !_syncService.SyncInProgress && migratedFromV1.GetValueOrDefault() &&
|
2019-06-16 02:44:36 +02:00
|
|
|
|
!_vm.HasCiphers &&
|
2019-06-05 05:26:56 +02:00
|
|
|
|
Xamarin.Essentials.Connectivity.NetworkAccess != Xamarin.Essentials.NetworkAccess.None)
|
2019-06-05 05:25:09 +02:00
|
|
|
|
{
|
2019-06-05 05:39:58 +02:00
|
|
|
|
var triedV1ReSync = await _storageService.GetAsync<bool?>(Constants.TriedV1Resync);
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!triedV1ReSync.GetValueOrDefault())
|
2019-06-05 05:39:58 +02:00
|
|
|
|
{
|
|
|
|
|
await _storageService.SaveAsync(Constants.TriedV1Resync, true);
|
|
|
|
|
await _syncService.FullSyncAsync(true);
|
|
|
|
|
}
|
2019-06-05 05:25:09 +02:00
|
|
|
|
}
|
2019-07-31 22:50:16 +02:00
|
|
|
|
await ShowPreviousPageAsync();
|
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
|
|
|
|
|
var lastPushRegistration = await _storageService.GetAsync<DateTime?>(Constants.PushLastRegistrationDateKey);
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
var pushPromptShow = await _storageService.GetAsync<bool?>(Constants.PushInitialPromptShownKey);
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!pushPromptShow.GetValueOrDefault(false))
|
2019-05-28 18:01:55 +02:00
|
|
|
|
{
|
|
|
|
|
await _storageService.SaveAsync(Constants.PushInitialPromptShownKey, true);
|
|
|
|
|
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();
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!_deviceActionService.AutofillAccessibilityServiceRunning()
|
2019-06-04 16:51:10 +02:00
|
|
|
|
&& !_deviceActionService.AutofillServiceEnabled())
|
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (migratedFromV1.GetValueOrDefault())
|
2019-06-04 16:51:10 +02:00
|
|
|
|
{
|
|
|
|
|
var migratedFromV1AutofillPromptShown = await _storageService.GetAsync<bool?>(
|
|
|
|
|
Constants.MigratedFromV1AutofillPromptShown);
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!migratedFromV1AutofillPromptShown.GetValueOrDefault())
|
2019-06-04 16:51:10 +02:00
|
|
|
|
{
|
|
|
|
|
await DisplayAlert(AppResources.Autofill,
|
|
|
|
|
AppResources.AutofillServiceNotEnabled, AppResources.Ok);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await _storageService.SaveAsync(Constants.MigratedFromV1AutofillPromptShown, true);
|
2019-05-28 18:01:55 +02:00
|
|
|
|
}
|
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);
|
2019-03-29 18:24:44 +01:00
|
|
|
|
}
|
2019-04-24 17:23:03 +02:00
|
|
|
|
|
|
|
|
|
private async void RowSelected(object sender, SelectedItemChangedEventArgs e)
|
|
|
|
|
{
|
2019-05-07 04:35:42 +02:00
|
|
|
|
((ListView)sender).SelectedItem = null;
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!DoOnce())
|
2019-05-07 04:49:57 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (!(e.SelectedItem is GroupingsPageListItem item))
|
2019-04-24 17:23:03 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 14:16:28 +01:00
|
|
|
|
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
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (DoOnce())
|
2019-05-07 05:07:47 +02:00
|
|
|
|
{
|
|
|
|
|
var page = new CiphersPage(_vm.Filter, _vm.FolderId != null, _vm.CollectionId != null,
|
|
|
|
|
_vm.Type != null);
|
|
|
|
|
await Navigation.PushModalAsync(new NavigationPage(page), false);
|
|
|
|
|
}
|
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
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
{
|
2019-06-01 07:07:02 +02:00
|
|
|
|
await _lockService.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
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
{
|
2020-03-28 14:16:28 +01:00
|
|
|
|
if (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;
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
2019-03-29 18:24:44 +01:00
|
|
|
|
}
|
|
|
|
|
}
|