1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-06-28 10:54:59 +02:00
bitwarden-mobile/src/App/Pages/Vault/GroupingsPage/GroupingsPage.xaml.cs

185 lines
6.4 KiB
C#
Raw Normal View History

2019-05-28 18:01:55 +02:00
using Bit.App.Abstractions;
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;
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-05-07 04:35:42 +02:00
private readonly GroupingsPageViewModel _vm;
2019-05-14 15:48:40 +02:00
private readonly string _pageName;
public GroupingsPage(bool mainPage, CipherType? type = null, string folderId = null,
string collectionId = null, string pageTitle = 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-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-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;
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)
{
_absLayout.Children.Remove(_fab);
}
2019-05-08 16:42:55 +02:00
else
{
_fab.Clicked = AddButton_Clicked;
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
}
protected async override void OnAppearing()
{
base.OnAppearing();
2019-05-14 15:48:40 +02:00
_broadcasterService.Subscribe(_pageName, async (message) =>
2019-04-19 22:45:16 +02:00
{
if(message.Command == "syncCompleted")
{
await Task.Delay(500);
2019-05-30 17:40:33 +02:00
Device.BeginInvokeOnMainThread(() =>
{
var task = _vm.LoadAsync();
});
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
{
2019-05-01 16:20:05 +02:00
if(!_syncService.SyncInProgress)
2019-04-19 22:45:16 +02:00
{
2019-05-07 04:35:42 +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);
2019-05-07 04:35:42 +02: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-05-08 14:33:17 +02:00
}, _mainContent);
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)
{
var pushPromptShow = await _storageService.GetAsync<bool?>(Constants.PushInitialPromptShownKey);
if(!pushPromptShow.GetValueOrDefault(false))
{
await _storageService.SaveAsync(Constants.PushInitialPromptShownKey, true);
await DisplayAlert(AppResources.EnableAutomaticSyncing, AppResources.PushNotificationAlert,
AppResources.OkGotIt);
}
if(!pushPromptShow.GetValueOrDefault(false) ||
DateTime.UtcNow - lastPushRegistration > TimeSpan.FromDays(1))
{
await _pushNotificationService.RegisterAsync();
}
}
else if(Device.RuntimePlatform == Device.Android &&
DateTime.UtcNow - lastPushRegistration > TimeSpan.FromDays(1))
{
await _pushNotificationService.RegisterAsync();
}
2019-04-19 22:45:16 +02:00
}
protected override void OnDisappearing()
{
base.OnDisappearing();
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;
2019-05-07 04:49:57 +02:00
if(!DoOnce())
{
return;
}
2019-04-24 17:23:03 +02:00
if(!(e.SelectedItem is GroupingsPageListItem item))
{
return;
}
if(item.Cipher != null)
{
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
}
2019-05-01 17:31:00 +02:00
else if(item.Type != null)
{
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)
{
2019-05-07 05:07:47 +02:00
if(DoOnce())
{
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
{
2019-05-31 17:55:11 +02:00
if(DoOnce())
{
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-03-29 18:24:44 +01:00
}
}