1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-09-28 03:57:43 +02:00
bitwarden-mobile/src/App/Pages/Vault/GroupingsPage/GroupingsPage.xaml.cs

273 lines
10 KiB
C#
Raw Normal View History

2019-05-28 18:01:55 +02:00
using Bit.App.Abstractions;
using Bit.App.Controls;
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;
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;
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;
private readonly ICipherService _cipherService;
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;
private PreviousPageInfo _previousPage;
public GroupingsPage(bool mainPage, CipherType? type = null, string folderId = null,
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();
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");
_cipherService = ServiceContainer.Resolve<ICipherService>("cipherService");
_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;
_previousPage = previousPage;
if (pageTitle != null)
{
2019-05-07 04:35:42 +02:00
_vm.PageTitle = pageTitle;
}
2019-05-08 14:33:17 +02: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
}
public ExtendedListView ListView { get; set; }
2019-03-29 18:24:44 +01:00
protected async override void OnAppearing()
{
base.OnAppearing();
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
{
if (message.Command == "syncStarted")
2019-06-05 15:39:30 +02:00
{
Device.BeginInvokeOnMainThread(() => IsBusy = true);
}
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;
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
{
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();
}
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);
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
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);
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
}
await ShowPreviousPageAsync();
2019-05-08 14:33:17 +02:00
}, _mainContent);
2019-05-28 18:01:55 +02: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);
if (Device.RuntimePlatform == Device.iOS)
2019-05-28 18:01:55 +02:00
{
var pushPromptShow = await _storageService.GetAsync<bool?>(Constants.PushInitialPromptShownKey);
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);
}
if (!pushPromptShow.GetValueOrDefault(false) ||
2019-05-28 18:01:55 +02:00
DateTime.UtcNow - lastPushRegistration > TimeSpan.FromDays(1))
{
await _pushNotificationService.RegisterAsync();
}
}
else if (Device.RuntimePlatform == Device.Android)
2019-05-28 18:01:55 +02:00
{
if (DateTime.UtcNow - lastPushRegistration > TimeSpan.FromDays(1))
{
await _pushNotificationService.RegisterAsync();
}
if (!_deviceActionService.AutofillAccessibilityServiceRunning()
&& !_deviceActionService.AutofillServiceEnabled())
{
if (migratedFromV1.GetValueOrDefault())
{
var migratedFromV1AutofillPromptShown = await _storageService.GetAsync<bool?>(
Constants.MigratedFromV1AutofillPromptShown);
if (!migratedFromV1AutofillPromptShown.GetValueOrDefault())
{
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;
if (!DoOnce())
2019-05-07 04:49:57 +02:00
{
return;
}
if (!(e.SelectedItem is GroupingsPageListItem item))
2019-04-24 17:23:03 +02:00
{
return;
}
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
}
else if (item.Folder != null)
{
2019-05-07 04:35:42 +02:00
await _vm.SelectFolderAsync(item.Folder);
}
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
}
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
}
private async void Search_Clicked(object sender, EventArgs e)
{
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-08 16:42:55 +02:00
private async void Sync_Clicked(object sender, EventArgs e)
2019-05-30 06:29:00 +02:00
{
await _vm.SyncAsync();
}
private async void Lock_Clicked(object sender, EventArgs e)
2019-05-30 06:29:00 +02:00
{
await _lockService.LockAsync(true, true);
2019-05-30 06:29:00 +02:00
}
private async void Exit_Clicked(object sender, EventArgs e)
2019-05-30 06:29:00 +02:00
{
await _vm.ExitAsync();
}
private async void AddButton_Clicked(object sender, EventArgs e)
2019-05-08 16:42:55 +02: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
}
private async Task ShowPreviousPageAsync()
{
if (_previousPage == null)
{
return;
}
if (_previousPage.Page == "view" && !string.IsNullOrWhiteSpace(_previousPage.CipherId))
{
await Navigation.PushModalAsync(new NavigationPage(new ViewPage(_previousPage.CipherId)));
}
else if (_previousPage.Page == "edit" && !string.IsNullOrWhiteSpace(_previousPage.CipherId))
{
await Navigation.PushModalAsync(new NavigationPage(new AddEditPage(_previousPage.CipherId)));
}
_previousPage = null;
}
2019-03-29 18:24:44 +01:00
}
}