1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-07-01 11:24:59 +02:00
bitwarden-mobile/src/App/Pages/Vault/ViewPage.xaml.cs

225 lines
7.3 KiB
C#
Raw Normal View History

2019-06-14 14:45:28 +02:00
using Bit.App.Resources;
using Bit.Core.Abstractions;
2019-04-24 17:23:03 +02:00
using Bit.Core.Utilities;
using System.Collections.Generic;
2019-06-05 15:39:30 +02:00
using System.Threading.Tasks;
2019-05-02 20:53:45 +02:00
using Xamarin.Forms;
2019-04-24 17:23:03 +02:00
namespace Bit.App.Pages
{
public partial class ViewPage : BaseContentPage
2019-04-24 17:23:03 +02:00
{
private readonly IBroadcasterService _broadcasterService;
2019-06-05 15:39:30 +02:00
private readonly ISyncService _syncService;
2019-04-24 17:23:03 +02:00
private ViewPageViewModel _vm;
public ViewPage(string cipherId)
{
InitializeComponent();
_broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
2019-06-05 15:39:30 +02:00
_syncService = ServiceContainer.Resolve<ISyncService>("syncService");
2019-04-24 17:23:03 +02:00
_vm = BindingContext as ViewPageViewModel;
_vm.Page = this;
_vm.CipherId = cipherId;
2019-05-09 20:12:30 +02:00
SetActivityIndicator(_mainContent);
if(Device.RuntimePlatform == Device.iOS)
{
_absLayout.Children.Remove(_fab);
2019-06-14 14:45:28 +02:00
ToolbarItems.Add(_closeItem);
ToolbarItems.Add(_editItem);
ToolbarItems.Add(_moreItem);
2019-05-09 20:12:30 +02:00
}
else
{
_fab.Clicked = EditButton_Clicked;
_mainLayout.Padding = new Thickness(0, 0, 0, 75);
2019-06-14 14:45:28 +02:00
ToolbarItems.Add(_attachmentsItem);
ToolbarItems.Add(_deleteItem);
2019-05-09 20:12:30 +02:00
}
2019-04-24 17:23:03 +02:00
}
public ViewPageViewModel ViewModel => _vm;
protected override async void OnAppearing()
2019-04-24 17:23:03 +02:00
{
base.OnAppearing();
2019-06-05 15:39:30 +02:00
if(_syncService.SyncInProgress)
2019-04-24 17:23:03 +02:00
{
2019-06-05 15:39:30 +02:00
IsBusy = true;
}
_broadcasterService.Subscribe(nameof(ViewPage), async (message) =>
{
if(message.Command == "syncStarted")
{
Device.BeginInvokeOnMainThread(() => IsBusy = true);
}
else if(message.Command == "syncCompleted")
2019-04-24 17:23:03 +02:00
{
2019-06-05 15:39:30 +02:00
await Task.Delay(500);
2019-05-30 17:40:33 +02:00
Device.BeginInvokeOnMainThread(() =>
2019-04-24 17:23:03 +02:00
{
2019-06-05 15:39:30 +02:00
IsBusy = false;
2019-06-05 23:25:12 +02:00
if(message.Data is Dictionary<string, object> data && data.ContainsKey("successfully"))
2019-04-24 17:23:03 +02:00
{
2019-05-30 17:40:33 +02:00
var success = data["successfully"] as bool?;
2019-06-05 23:25:12 +02:00
if(success.GetValueOrDefault())
2019-05-30 17:40:33 +02:00
{
2019-06-05 23:25:12 +02:00
var task = _vm.LoadAsync(() => AdjustToolbar());
2019-05-30 17:40:33 +02:00
}
2019-04-24 17:23:03 +02:00
}
2019-05-30 17:40:33 +02:00
});
2019-04-24 17:23:03 +02:00
}
});
2019-05-30 14:40:10 +02:00
await LoadOnAppearedAsync(_scrollView, true, async () =>
{
2019-06-05 23:25:12 +02:00
var success = await _vm.LoadAsync(() => AdjustToolbar());
2019-05-30 14:40:10 +02:00
if(!success)
{
await Navigation.PopModalAsync();
}
}, _mainContent);
2019-04-24 17:23:03 +02:00
}
protected override void OnDisappearing()
{
base.OnDisappearing();
2019-06-05 15:39:30 +02:00
IsBusy = false;
2019-04-24 17:23:03 +02:00
_broadcasterService.Unsubscribe(nameof(ViewPage));
2019-04-26 06:26:09 +02:00
_vm.CleanUp();
2019-04-24 17:23:03 +02:00
}
2019-05-02 20:53:45 +02:00
private async void PasswordHistory_Tapped(object sender, System.EventArgs e)
{
2019-05-07 05:22:48 +02:00
if(DoOnce())
{
await Navigation.PushModalAsync(new NavigationPage(new PasswordHistoryPage(_vm.CipherId)));
}
2019-05-02 20:53:45 +02:00
}
2019-05-07 17:25:21 +02:00
private async void EditToolbarItem_Clicked(object sender, System.EventArgs e)
{
if(DoOnce())
{
await Navigation.PushModalAsync(new NavigationPage(new AddEditPage(_vm.CipherId)));
}
}
2019-05-09 20:12:30 +02:00
private void EditButton_Clicked(object sender, System.EventArgs e)
{
EditToolbarItem_Clicked(sender, e);
}
2019-05-10 19:47:59 +02:00
2019-05-11 05:43:35 +02:00
private async void Attachments_Clicked(object sender, System.EventArgs e)
2019-05-10 19:47:59 +02:00
{
if(DoOnce())
{
2019-05-11 05:43:35 +02:00
var page = new AttachmentsPage(_vm.CipherId);
await Navigation.PushModalAsync(new NavigationPage(page));
2019-05-10 19:47:59 +02:00
}
}
private async void Share_Clicked(object sender, System.EventArgs e)
{
if(DoOnce())
{
var page = new SharePage(_vm.CipherId);
await Navigation.PushModalAsync(new NavigationPage(page));
}
}
private async void Delete_Clicked(object sender, System.EventArgs e)
{
if(DoOnce())
{
2019-05-30 14:35:50 +02:00
if(await _vm.DeleteAsync())
{
await Navigation.PopModalAsync();
}
2019-05-10 19:47:59 +02:00
}
}
2019-05-10 20:28:17 +02:00
private async void Collections_Clicked(object sender, System.EventArgs e)
2019-05-10 19:47:59 +02:00
{
if(DoOnce())
{
2019-05-10 20:28:17 +02:00
var page = new CollectionsPage(_vm.CipherId);
await Navigation.PushModalAsync(new NavigationPage(page));
2019-05-10 19:47:59 +02:00
}
}
2019-06-05 23:25:12 +02:00
2019-06-14 14:45:28 +02:00
private async void More_Clicked(object sender, System.EventArgs e)
{
if(!DoOnce())
{
return;
}
var options = new List<string> { AppResources.Attachments };
options.Add(_vm.Cipher.OrganizationId == null ? AppResources.Share : AppResources.Collections);
2019-06-14 14:45:28 +02:00
var selection = await DisplayActionSheet(AppResources.Options, AppResources.Cancel,
AppResources.Delete, options.ToArray());
if(selection == AppResources.Delete)
{
if(await _vm.DeleteAsync())
{
await Navigation.PopModalAsync();
}
}
else if(selection == AppResources.Attachments)
{
var page = new AttachmentsPage(_vm.CipherId);
await Navigation.PushModalAsync(new NavigationPage(page));
}
else if(selection == AppResources.Collections)
{
var page = new CollectionsPage(_vm.CipherId);
await Navigation.PushModalAsync(new NavigationPage(page));
}
else if(selection == AppResources.Share)
{
var page = new SharePage(_vm.CipherId);
await Navigation.PushModalAsync(new NavigationPage(page));
}
}
2019-06-12 03:31:51 +02:00
private async void Close_Clicked(object sender, System.EventArgs e)
{
if(DoOnce())
{
await Navigation.PopModalAsync();
}
}
2019-06-05 23:25:12 +02:00
private void AdjustToolbar()
{
2019-06-12 03:31:51 +02:00
if(Device.RuntimePlatform != Device.Android || _vm.Cipher == null)
{
return;
}
if(_vm.Cipher.OrganizationId == null)
2019-06-05 23:25:12 +02:00
{
2019-06-12 03:31:51 +02:00
if(ToolbarItems.Contains(_collectionsItem))
2019-06-05 23:25:12 +02:00
{
2019-06-12 03:31:51 +02:00
ToolbarItems.Remove(_collectionsItem);
2019-06-05 23:25:12 +02:00
}
2019-06-12 03:31:51 +02:00
if(!ToolbarItems.Contains(_shareItem))
2019-06-05 23:25:12 +02:00
{
2019-06-12 03:31:51 +02:00
ToolbarItems.Insert(1, _shareItem);
2019-06-05 23:25:12 +02:00
}
2019-06-12 03:31:51 +02:00
}
else
{
if(ToolbarItems.Contains(_shareItem))
2019-06-05 23:25:12 +02:00
{
2019-06-12 03:31:51 +02:00
ToolbarItems.Remove(_shareItem);
}
if(!ToolbarItems.Contains(_collectionsItem))
{
ToolbarItems.Insert(1, _collectionsItem);
2019-06-05 23:25:12 +02:00
}
}
}
2019-04-24 17:23:03 +02:00
}
}