bitwarden-android/src/App/Pages/Vault/AddEditPage.xaml.cs

356 lines
14 KiB
C#
Raw Normal View History

2019-06-04 06:00:48 +03:00
using Bit.App.Abstractions;
using Bit.App.Models;
using Bit.App.Resources;
using Bit.Core;
using Bit.Core.Abstractions;
2019-05-17 19:03:35 +03:00
using Bit.Core.Enums;
2019-06-04 06:00:48 +03:00
using Bit.Core.Utilities;
using System.Collections.Generic;
2019-06-06 00:25:12 +03:00
using System.Threading.Tasks;
2019-05-08 17:42:55 +03:00
using Xamarin.Forms;
2019-05-07 18:25:21 +03:00
namespace Bit.App.Pages
{
public partial class AddEditPage : BaseContentPage
{
2019-05-17 19:03:35 +03:00
private readonly AppOptions _appOptions;
2019-06-04 06:00:48 +03:00
private readonly IStorageService _storageService;
private readonly IDeviceActionService _deviceActionService;
private AddEditPageViewModel _vm;
2019-05-17 22:24:15 +03:00
private bool _fromAutofill;
2019-05-07 18:25:21 +03:00
2019-05-07 19:29:02 +03:00
public AddEditPage(
string cipherId = null,
2019-05-08 17:42:55 +03:00
CipherType? type = null,
2019-05-07 19:29:02 +03:00
string folderId = null,
2019-05-17 19:03:35 +03:00
string collectionId = null,
2019-05-17 22:24:15 +03:00
string name = null,
string uri = null,
bool fromAutofill = false,
2019-05-17 19:03:35 +03:00
AppOptions appOptions = null)
2019-05-07 18:25:21 +03:00
{
2019-06-04 06:00:48 +03:00
_storageService = ServiceContainer.Resolve<IStorageService>("storageService");
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
2019-05-17 19:03:35 +03:00
_appOptions = appOptions;
2019-05-17 22:24:15 +03:00
_fromAutofill = fromAutofill;
FromAutofillFramework = _appOptions?.FromAutofillFramework ?? false;
2019-05-07 18:25:21 +03:00
InitializeComponent();
_vm = BindingContext as AddEditPageViewModel;
_vm.Page = this;
_vm.CipherId = cipherId;
2019-05-07 19:29:02 +03:00
_vm.FolderId = folderId;
_vm.CollectionIds = collectionId != null ? new HashSet<string>(new List<string> { collectionId }) : null;
2019-05-08 17:42:55 +03:00
_vm.Type = type;
2019-05-17 22:24:15 +03:00
_vm.DefaultName = name ?? appOptions?.SaveName;
_vm.DefaultUri = uri ?? appOptions?.Uri;
2019-05-07 19:29:02 +03:00
_vm.Init();
2019-05-07 18:25:21 +03:00
SetActivityIndicator();
2019-06-14 15:45:28 +03:00
if(_vm.EditMode && Device.RuntimePlatform == Device.Android)
2019-05-10 21:09:13 +03:00
{
2019-06-14 15:45:28 +03:00
ToolbarItems.Add(_attachmentsItem);
ToolbarItems.Add(_deleteItem);
2019-05-10 21:09:13 +03:00
}
2019-06-14 15:45:28 +03:00
if(Device.RuntimePlatform == Device.iOS)
2019-06-12 04:31:51 +03:00
{
2019-06-14 15:45:28 +03:00
ToolbarItems.Add(_closeItem);
ToolbarItems.Add(_moreItem);
2019-06-12 04:31:51 +03:00
}
2019-05-08 06:22:55 +03:00
2019-05-08 21:37:12 +03:00
_typePicker.ItemDisplayBinding = new Binding("Key");
2019-05-08 06:22:55 +03:00
_cardBrandPicker.ItemDisplayBinding = new Binding("Key");
_cardExpMonthPicker.ItemDisplayBinding = new Binding("Key");
_identityTitlePicker.ItemDisplayBinding = new Binding("Key");
2019-05-09 19:18:23 +03:00
_folderPicker.ItemDisplayBinding = new Binding("Key");
_ownershipPicker.ItemDisplayBinding = new Binding("Key");
_nameEntry.ReturnType = ReturnType.Next;
_nameEntry.ReturnCommand = new Command(() =>
{
if(_vm.Cipher.Type == CipherType.Login)
{
_loginUsernameEntry.Focus();
}
else if(_vm.Cipher.Type == CipherType.Card)
{
_cardholderNameEntry.Focus();
}
});
_loginUsernameEntry.ReturnType = ReturnType.Next;
_loginUsernameEntry.ReturnCommand = new Command(() => _loginPasswordEntry.Focus());
_loginPasswordEntry.ReturnType = ReturnType.Next;
_loginPasswordEntry.ReturnCommand = new Command(() => _loginTotpEntry.Focus());
_cardholderNameEntry.ReturnType = ReturnType.Next;
_cardholderNameEntry.ReturnCommand = new Command(() => _cardNumberEntry.Focus());
_cardExpYearEntry.ReturnType = ReturnType.Next;
_cardExpYearEntry.ReturnCommand = new Command(() => _cardCodeEntry.Focus());
_identityFirstNameEntry.ReturnType = ReturnType.Next;
_identityFirstNameEntry.ReturnCommand = new Command(() => _identityMiddleNameEntry.Focus());
_identityMiddleNameEntry.ReturnType = ReturnType.Next;
_identityMiddleNameEntry.ReturnCommand = new Command(() => _identityLastNameEntry.Focus());
_identityLastNameEntry.ReturnType = ReturnType.Next;
_identityLastNameEntry.ReturnCommand = new Command(() => _identityUsernameEntry.Focus());
_identityUsernameEntry.ReturnType = ReturnType.Next;
_identityUsernameEntry.ReturnCommand = new Command(() => _identityCompanyEntry.Focus());
_identityCompanyEntry.ReturnType = ReturnType.Next;
_identityCompanyEntry.ReturnCommand = new Command(() => _identitySsnEntry.Focus());
_identitySsnEntry.ReturnType = ReturnType.Next;
_identitySsnEntry.ReturnCommand = new Command(() => _identityPassportNumberEntry.Focus());
_identityPassportNumberEntry.ReturnType = ReturnType.Next;
_identityPassportNumberEntry.ReturnCommand = new Command(() => _identityLicenseNumberEntry.Focus());
_identityLicenseNumberEntry.ReturnType = ReturnType.Next;
_identityLicenseNumberEntry.ReturnCommand = new Command(() => _identityEmailEntry.Focus());
_identityEmailEntry.ReturnType = ReturnType.Next;
_identityEmailEntry.ReturnCommand = new Command(() => _identityPhoneEntry.Focus());
_identityPhoneEntry.ReturnType = ReturnType.Next;
_identityPhoneEntry.ReturnCommand = new Command(() => _identityAddress1Entry.Focus());
_identityAddress1Entry.ReturnType = ReturnType.Next;
_identityAddress1Entry.ReturnCommand = new Command(() => _identityAddress2Entry.Focus());
_identityAddress2Entry.ReturnType = ReturnType.Next;
_identityAddress2Entry.ReturnCommand = new Command(() => _identityAddress3Entry.Focus());
_identityAddress3Entry.ReturnType = ReturnType.Next;
_identityAddress3Entry.ReturnCommand = new Command(() => _identityCityEntry.Focus());
_identityCityEntry.ReturnType = ReturnType.Next;
_identityCityEntry.ReturnCommand = new Command(() => _identityStateEntry.Focus());
_identityStateEntry.ReturnType = ReturnType.Next;
_identityStateEntry.ReturnCommand = new Command(() => _identityPostalCodeEntry.Focus());
_identityPostalCodeEntry.ReturnType = ReturnType.Next;
_identityPostalCodeEntry.ReturnCommand = new Command(() => _identityCountryEntry.Focus());
2019-05-07 18:25:21 +03:00
}
2019-05-17 22:24:15 +03:00
public bool FromAutofillFramework { get; set; }
2019-05-07 18:25:21 +03:00
protected override async void OnAppearing()
{
base.OnAppearing();
2019-05-30 15:40:10 +03:00
await LoadOnAppearedAsync(_scrollView, true, async () =>
{
2019-06-02 05:44:15 +03:00
var success = await _vm.LoadAsync(_appOptions);
2019-05-30 15:40:10 +03:00
if(!success)
{
await Navigation.PopModalAsync();
2019-06-06 00:25:12 +03:00
return;
2019-05-30 15:40:10 +03:00
}
2019-06-06 00:25:12 +03:00
AdjustToolbar();
await ShowAlertsAsync();
if(!_vm.EditMode && string.IsNullOrWhiteSpace(_vm.Cipher?.Name))
{
RequestFocus(_nameEntry);
}
2019-05-30 15:40:10 +03:00
});
2019-05-07 18:25:21 +03:00
}
protected override void OnDisappearing()
{
base.OnDisappearing();
}
2019-05-17 22:24:15 +03:00
protected override bool OnBackButtonPressed()
{
if(FromAutofillFramework)
{
Application.Current.MainPage = new TabsPage();
return true;
}
return base.OnBackButtonPressed();
}
2019-05-07 18:25:21 +03:00
private async void PasswordHistory_Tapped(object sender, System.EventArgs e)
{
if(DoOnce())
{
await Navigation.PushModalAsync(new NavigationPage(new PasswordHistoryPage(_vm.CipherId)));
}
}
2019-05-07 19:29:02 +03:00
private async void Save_Clicked(object sender, System.EventArgs e)
{
if(DoOnce())
{
await _vm.SubmitAsync();
}
}
2019-05-08 23:49:32 +03:00
private void NewUri_Clicked(object sender, System.EventArgs e)
{
_vm.AddUri();
}
2019-05-09 06:13:10 +03:00
private void NewField_Clicked(object sender, System.EventArgs e)
{
_vm.AddField();
}
2019-05-11 06:43:35 +03:00
private async void Attachments_Clicked(object sender, System.EventArgs e)
{
if(DoOnce())
{
2019-05-11 06:43:35 +03:00
var page = new AttachmentsPage(_vm.CipherId);
await Navigation.PushModalAsync(new NavigationPage(page));
}
}
2019-05-10 20:22:35 +03: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 15:35:50 +03:00
if(await _vm.DeleteAsync())
{
await Navigation.PopModalAsync();
}
2019-05-10 20:22:35 +03:00
}
}
2019-05-10 20:47:59 +03:00
2019-05-10 21:28:17 +03:00
private async void Collections_Clicked(object sender, System.EventArgs e)
2019-05-10 20:47:59 +03:00
{
if(DoOnce())
{
2019-05-10 21:28:17 +03:00
var page = new CollectionsPage(_vm.CipherId);
await Navigation.PushModalAsync(new NavigationPage(page));
2019-05-10 20:47:59 +03:00
}
}
2019-05-23 04:10:04 +03:00
private async void ScanTotp_Clicked(object sender, System.EventArgs e)
{
if(DoOnce())
{
var page = new ScanPage(key =>
{
Device.BeginInvokeOnMainThread(async () =>
{
await Navigation.PopModalAsync();
await _vm.UpdateTotpKeyAsync(key);
});
});
await Navigation.PushModalAsync(new NavigationPage(page));
}
}
2019-06-06 00:25:12 +03:00
2019-06-14 15:45:28 +03:00
private async void More_Clicked(object sender, System.EventArgs e)
{
if(!DoOnce())
{
return;
}
var options = new List<string> { AppResources.Attachments };
if(_vm.EditMode)
{
options.Add(_vm.Cipher.OrganizationId != null ? AppResources.Share : AppResources.Collections);
}
var selection = await DisplayActionSheet(AppResources.Options, AppResources.Cancel,
_vm.EditMode ? AppResources.Delete : null, 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 04:31:51 +03:00
private async void Close_Clicked(object sender, System.EventArgs e)
{
if(DoOnce())
{
await Navigation.PopModalAsync();
}
}
2019-06-06 00:25:12 +03:00
private async Task ShowAlertsAsync()
{
if(!_vm.EditMode)
{
if(_vm.Cipher == null)
{
return;
}
var addLoginShown = await _storageService.GetAsync<bool?>(Constants.AddSitePromptShownKey);
if(_vm.Cipher.Type == CipherType.Login && !_fromAutofill && !addLoginShown.GetValueOrDefault())
{
await _storageService.SaveAsync(Constants.AddSitePromptShownKey, true);
if(Device.RuntimePlatform == Device.iOS)
{
if(_deviceActionService.SystemMajorVersion() < 12)
{
await DisplayAlert(AppResources.BitwardenAppExtension,
AppResources.BitwardenAppExtensionAlert2, AppResources.Ok);
}
else
{
await DisplayAlert(AppResources.PasswordAutofill,
AppResources.BitwardenAutofillAlert2, AppResources.Ok);
}
}
else if(Device.RuntimePlatform == Device.Android &&
!_deviceActionService.AutofillAccessibilityServiceRunning() &&
!_deviceActionService.AutofillServiceEnabled())
{
await DisplayAlert(AppResources.BitwardenAutofillService,
AppResources.BitwardenAutofillServiceAlert2, AppResources.Ok);
}
}
}
}
private void AdjustToolbar()
{
if(_vm.EditMode && Device.RuntimePlatform == Device.Android)
{
if(_vm.Cipher == null)
{
return;
}
if(_vm.Cipher.OrganizationId == null)
{
if(ToolbarItems.Contains(_collectionsItem))
{
ToolbarItems.Remove(_collectionsItem);
}
if(!ToolbarItems.Contains(_shareItem))
{
ToolbarItems.Insert(2, _shareItem);
}
}
else
{
if(ToolbarItems.Contains(_shareItem))
{
ToolbarItems.Remove(_shareItem);
}
if(!ToolbarItems.Contains(_collectionsItem))
{
ToolbarItems.Insert(2, _collectionsItem);
}
}
}
}
2019-05-07 18:25:21 +03:00
}
}