bitwarden-mobile/src/App/Pages/Vault/ViewPage.xaml.cs

328 lines
10 KiB
C#
Raw Normal View History

using System;
2022-04-26 17:21:17 +02:00
using System.Collections.Generic;
using System.Threading.Tasks;
using Bit.App.Resources;
2019-06-14 14:45:28 +02:00
using Bit.Core.Abstractions;
2019-04-24 17:23:03 +02:00
using Bit.Core.Utilities;
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)
2019-05-09 20:12:30 +02:00
{
_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
{
_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;
public void UpdateCipherId(string cipherId)
{
_vm.CipherId = cipherId;
}
protected override async void OnAppearing()
2019-04-24 17:23:03 +02:00
{
base.OnAppearing();
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")
2019-06-05 15:39:30 +02:00
{
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;
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?;
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
}
else if (message.Command == "selectSaveFileResult")
{
Device.BeginInvokeOnMainThread(() =>
{
var data = message.Data as Tuple<string, string>;
if (data == null)
{
return;
}
_vm.SaveFileSelected(data.Item1, data.Item2);
});
}
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());
if (!success)
2019-05-30 14:40:10 +02:00
{
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)
{
if (DoOnce())
2019-05-07 05:22:48 +02:00
{
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())
2019-05-07 17:25:21 +02:00
{
if (_vm.IsDeleted)
{
if (await _vm.RestoreAsync())
{
await Navigation.PopModalAsync();
}
}
else
{
if (!await _vm.PromptPasswordAsync())
{
return;
}
await Navigation.PushModalAsync(new NavigationPage(new AddEditPage(_vm.CipherId)));
}
2019-05-07 17:25:21 +02:00
}
}
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-10 19:47:59 +02:00
{
if (!await _vm.PromptPasswordAsync())
{
return;
}
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())
2019-05-10 19:47:59 +02:00
{
if (!await _vm.PromptPasswordAsync())
{
return;
}
2019-05-10 19:47:59 +02:00
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-10 19:47:59 +02:00
{
if (!await _vm.PromptPasswordAsync())
{
return;
}
if (await _vm.DeleteAsync())
2019-05-30 14:35:50 +02:00
{
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 19:47:59 +02:00
{
if (!await _vm.PromptPasswordAsync())
{
return;
}
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
private async void Clone_Clicked(object sender, System.EventArgs e)
{
if (DoOnce())
{
if (!await _vm.PromptPasswordAsync())
{
return;
}
var page = new AddEditPage(_vm.CipherId, cloneMode: true, viewPage: this);
await Navigation.PushModalAsync(new NavigationPage(page));
}
}
2019-06-14 14:45:28 +02:00
private async void More_Clicked(object sender, System.EventArgs e)
{
if (!DoOnce())
2019-06-14 14:45:28 +02:00
{
return;
}
2022-04-26 17:21:17 +02:00
var options = new List<string> { AppResources.Attachments };
if (_vm.Cipher.OrganizationId == null)
{
options.Add(AppResources.Clone);
options.Add(AppResources.MoveToOrganization);
}
else
{
options.Add(AppResources.Collections);
}
2019-06-14 14:45:28 +02:00
var selection = await DisplayActionSheet(AppResources.Options, AppResources.Cancel,
AppResources.Delete, options.ToArray());
2021-06-10 17:57:18 +02:00
if (!await _vm.PromptPasswordAsync())
{
return;
}
if (selection == AppResources.Delete)
2019-06-14 14:45:28 +02:00
{
if (await _vm.DeleteAsync())
2019-06-14 14:45:28 +02:00
{
await Navigation.PopModalAsync();
}
}
else if (selection == AppResources.Attachments)
2019-06-14 14:45:28 +02:00
{
var page = new AttachmentsPage(_vm.CipherId);
await Navigation.PushModalAsync(new NavigationPage(page));
}
else if (selection == AppResources.Collections)
2019-06-14 14:45:28 +02:00
{
var page = new CollectionsPage(_vm.CipherId);
await Navigation.PushModalAsync(new NavigationPage(page));
}
else if (selection == AppResources.MoveToOrganization)
2019-06-14 14:45:28 +02:00
{
var page = new SharePage(_vm.CipherId);
await Navigation.PushModalAsync(new NavigationPage(page));
}
else if (selection == AppResources.Clone)
{
var page = new AddEditPage(_vm.CipherId, cloneMode: true, viewPage: this);
await Navigation.PushModalAsync(new NavigationPage(page));
}
2019-06-14 14:45:28 +02:00
}
2019-06-12 03:31:51 +02:00
private async void Close_Clicked(object sender, System.EventArgs e)
{
if (DoOnce())
2019-06-12 03:31:51 +02:00
{
await Navigation.PopModalAsync();
}
}
2019-06-05 23:25:12 +02:00
private void AdjustToolbar()
{
if (_vm.Cipher == null)
{
return;
}
_editItem.Text = _vm.Cipher.IsDeleted ? AppResources.Restore :
AppResources.Edit;
if (_vm.Cipher.IsDeleted)
{
_absLayout.Children.Remove(_fab);
}
if (Device.RuntimePlatform != Device.Android)
2019-06-12 03:31:51 +02:00
{
return;
}
if (_vm.Cipher.OrganizationId == null)
2019-06-05 23:25:12 +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
}
if (!ToolbarItems.Contains(_cloneItem))
{
ToolbarItems.Insert(1, _cloneItem);
}
if (!ToolbarItems.Contains(_shareItem))
2019-06-05 23:25:12 +02:00
{
ToolbarItems.Insert(2, _shareItem);
2019-06-05 23:25:12 +02:00
}
2019-06-12 03:31:51 +02:00
}
else
{
if (ToolbarItems.Contains(_cloneItem))
{
ToolbarItems.Remove(_cloneItem);
}
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))
2019-06-12 03:31:51 +02:00
{
ToolbarItems.Insert(1, _collectionsItem);
2019-06-05 23:25:12 +02:00
}
}
if (_vm.Cipher.IsDeleted && !ToolbarItems.Contains(_editItem))
{
ToolbarItems.Insert(1, _editItem);
}
2019-06-05 23:25:12 +02:00
}
2019-04-24 17:23:03 +02:00
}
}