bitwarden-android/src/App/Pages/Vault/VaultEditLoginPage.cs

438 lines
16 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using Acr.UserDialogs;
using Bit.App.Abstractions;
using Bit.App.Controls;
using Bit.App.Resources;
using Plugin.Connectivity.Abstractions;
2016-05-02 09:52:09 +03:00
using Xamarin.Forms;
using XLabs.Ioc;
2017-05-30 21:13:53 +03:00
using Bit.App.Utilities;
2016-05-02 09:52:09 +03:00
2016-05-03 00:50:16 +03:00
namespace Bit.App.Pages
2016-05-02 09:52:09 +03:00
{
2017-01-03 08:17:15 +03:00
public class VaultEditLoginPage : ExtendedContentPage
2016-05-02 09:52:09 +03:00
{
2017-01-03 08:17:15 +03:00
private readonly string _loginId;
private readonly ICipherService _cipherService;
private readonly IFolderService _folderService;
private readonly IUserDialogs _userDialogs;
private readonly IConnectivity _connectivity;
private readonly IDeviceInfoService _deviceInfo;
private readonly IGoogleAnalyticsService _googleAnalyticsService;
private DateTime? _lastAction;
2017-01-03 08:17:15 +03:00
public VaultEditLoginPage(string loginId)
2016-05-02 09:52:09 +03:00
{
2017-01-03 08:17:15 +03:00
_loginId = loginId;
_cipherService = Resolver.Resolve<ICipherService>();
_folderService = Resolver.Resolve<IFolderService>();
_userDialogs = Resolver.Resolve<IUserDialogs>();
_connectivity = Resolver.Resolve<IConnectivity>();
_deviceInfo = Resolver.Resolve<IDeviceInfoService>();
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
Init();
}
public FormEntryCell PasswordCell { get; private set; }
public FormEntryCell UsernameCell { get; private set; }
public FormEntryCell UriCell { get; private set; }
public FormEntryCell NameCell { get; private set; }
2017-07-13 18:52:24 +03:00
public FormEntryCell TotpCell { get; private set; }
public FormEditorCell NotesCell { get; private set; }
public FormPickerCell FolderCell { get; private set; }
public ExtendedTextCell GenerateCell { get; private set; }
2017-07-22 22:38:08 +03:00
public ExtendedTextCell AttachmentsCell { get; private set; }
2017-09-26 00:13:20 +03:00
public ExtendedTextCell CustomFieldsCell { get; private set; }
public ExtendedTextCell DeleteCell { get; private set; }
private void Init()
{
var cipher = _cipherService.GetByIdAsync(_loginId).GetAwaiter().GetResult();
if(cipher == null)
{
// TODO: handle error. navigate back? should never happen...
return;
}
2017-09-27 21:36:55 +03:00
NotesCell = new FormEditorCell(height: 300);
NotesCell.Editor.Keyboard = Keyboard.Text;
NotesCell.Editor.Text = cipher.Notes?.Decrypt(cipher.OrganizationId);
2017-07-13 18:52:24 +03:00
TotpCell = new FormEntryCell(AppResources.AuthenticatorKey, nextElement: NotesCell.Editor,
useButton: _deviceInfo.HasCamera);
if(_deviceInfo.HasCamera)
{
TotpCell.Button.Image = "camera";
}
TotpCell.Entry.Text = cipher.Login?.Totp?.Decrypt(cipher.OrganizationId);
2017-07-13 18:52:24 +03:00
TotpCell.Entry.DisableAutocapitalize = true;
TotpCell.Entry.Autocorrect = false;
TotpCell.Entry.FontFamily = Helpers.OnPlatform(iOS: "Menlo-Regular", Android: "monospace", WinPhone: "Courier");
2017-07-13 18:52:24 +03:00
PasswordCell = new FormEntryCell(AppResources.Password, isPassword: true, nextElement: TotpCell.Entry,
useButton: true);
PasswordCell.Entry.Text = cipher.Login?.Password?.Decrypt(cipher.OrganizationId);
PasswordCell.Button.Image = "eye";
PasswordCell.Entry.DisableAutocapitalize = true;
PasswordCell.Entry.Autocorrect = false;
PasswordCell.Entry.FontFamily = Helpers.OnPlatform(iOS: "Menlo-Regular", Android: "monospace", WinPhone: "Courier");
UsernameCell = new FormEntryCell(AppResources.Username, nextElement: PasswordCell.Entry);
UsernameCell.Entry.Text = cipher.Login?.Username?.Decrypt(cipher.OrganizationId);
UsernameCell.Entry.DisableAutocapitalize = true;
UsernameCell.Entry.Autocorrect = false;
UriCell = new FormEntryCell(AppResources.URI, Keyboard.Url, nextElement: UsernameCell.Entry);
UriCell.Entry.Text = cipher.Login?.Uri?.Decrypt(cipher.OrganizationId);
NameCell = new FormEntryCell(AppResources.Name, nextElement: UriCell.Entry);
NameCell.Entry.Text = cipher.Name?.Decrypt(cipher.OrganizationId);
GenerateCell = new ExtendedTextCell
{
2016-11-26 00:42:52 +03:00
Text = AppResources.GeneratePassword,
ShowDisclousure = true
};
var folderOptions = new List<string> { AppResources.FolderNone };
var folders = _folderService.GetAllAsync().GetAwaiter().GetResult()
.OrderBy(f => f.Name?.Decrypt()).ToList();
int selectedIndex = 0;
int i = 0;
foreach(var folder in folders)
{
i++;
if(folder.Id == cipher.FolderId)
{
selectedIndex = i;
}
folderOptions.Add(folder.Name.Decrypt());
}
FolderCell = new FormPickerCell(AppResources.Folder, folderOptions.ToArray());
FolderCell.Picker.SelectedIndex = selectedIndex;
2016-06-15 05:36:37 +03:00
var favoriteCell = new ExtendedSwitchCell
{
2016-11-26 00:42:52 +03:00
Text = AppResources.Favorite,
On = cipher.Favorite
2016-06-15 05:36:37 +03:00
};
2017-07-22 22:38:08 +03:00
AttachmentsCell = new ExtendedTextCell
{
Text = AppResources.Attachments,
ShowDisclousure = true
};
2017-09-26 00:13:20 +03:00
CustomFieldsCell = new ExtendedTextCell
{
Text = AppResources.CustomFields,
ShowDisclousure = true
};
DeleteCell = new ExtendedTextCell { Text = AppResources.Delete, TextColor = Color.Red };
var table = new ExtendedTableView
{
Intent = TableIntent.Settings,
2016-05-24 06:48:34 +03:00
EnableScrolling = true,
HasUnevenRows = true,
Root = new TableRoot
{
new TableSection(AppResources.ItemInformation)
{
NameCell,
UriCell,
UsernameCell,
PasswordCell,
GenerateCell
},
new TableSection(" ")
2016-06-15 05:36:37 +03:00
{
2017-07-13 18:52:24 +03:00
TotpCell,
FolderCell,
2017-07-22 22:38:08 +03:00
favoriteCell,
2017-09-26 00:13:20 +03:00
AttachmentsCell,
CustomFieldsCell
2016-06-15 05:36:37 +03:00
},
new TableSection(AppResources.Notes)
{
NotesCell
},
new TableSection(" ")
{
DeleteCell
}
}
};
2017-05-30 21:13:53 +03:00
if(Device.RuntimePlatform == Device.iOS)
{
table.RowHeight = -1;
table.EstimatedRowHeight = 70;
}
2017-05-30 21:13:53 +03:00
else if(Device.RuntimePlatform == Device.Android)
{
PasswordCell.Button.WidthRequest = 40;
if(TotpCell.Button != null)
{
TotpCell.Button.WidthRequest = 40;
}
}
var saveToolBarItem = new ToolbarItem(AppResources.Save, null, async () =>
{
if(_lastAction.LastActionWasRecent())
{
return;
}
_lastAction = DateTime.UtcNow;
if(!_connectivity.IsConnected)
{
AlertNoConnection();
return;
}
if(string.IsNullOrWhiteSpace(NameCell.Entry.Text))
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
AppResources.Name), AppResources.Ok);
return;
}
cipher.Name = NameCell.Entry.Text.Encrypt(cipher.OrganizationId);
cipher.Notes = string.IsNullOrWhiteSpace(NotesCell.Editor.Text) ? null :
NotesCell.Editor.Text.Encrypt(cipher.OrganizationId);
cipher.Favorite = favoriteCell.On;
cipher.Login = new Models.Login
{
Uri = string.IsNullOrWhiteSpace(UriCell.Entry.Text) ? null :
UriCell.Entry.Text.Encrypt(cipher.OrganizationId),
Username = string.IsNullOrWhiteSpace(UsernameCell.Entry.Text) ? null :
UsernameCell.Entry.Text.Encrypt(cipher.OrganizationId),
Password = string.IsNullOrWhiteSpace(PasswordCell.Entry.Text) ? null :
PasswordCell.Entry.Text.Encrypt(cipher.OrganizationId),
Totp = string.IsNullOrWhiteSpace(TotpCell.Entry.Text) ? null :
TotpCell.Entry.Text.Encrypt(cipher.OrganizationId)
};
if(FolderCell.Picker.SelectedIndex > 0)
{
cipher.FolderId = folders.ElementAt(FolderCell.Picker.SelectedIndex - 1).Id;
}
else
{
cipher.FolderId = null;
}
2016-11-26 00:42:52 +03:00
_userDialogs.ShowLoading(AppResources.Saving, MaskType.Black);
var saveTask = await _cipherService.SaveAsync(cipher);
_userDialogs.HideLoading();
if(saveTask.Succeeded)
{
_userDialogs.Toast(AppResources.ItemUpdated);
2017-09-26 00:13:20 +03:00
_googleAnalyticsService.TrackAppEvent("EditedLogin");
2017-06-07 17:19:56 +03:00
await Navigation.PopForDeviceAsync();
}
else if(saveTask.Errors.Count() > 0)
{
await _userDialogs.AlertAsync(saveTask.Errors.First().Message, AppResources.AnErrorHasOccurred);
}
else
{
await _userDialogs.AlertAsync(AppResources.AnErrorHasOccurred);
}
}, ToolbarItemOrder.Default, 0);
Title = AppResources.EditItem;
2016-05-24 06:48:34 +03:00
Content = table;
ToolbarItems.Add(saveToolBarItem);
if(Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.Windows)
{
2016-11-26 00:42:52 +03:00
ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Cancel));
}
}
protected override void OnAppearing()
{
base.OnAppearing();
if(!_connectivity.IsConnected)
{
AlertNoConnection();
}
PasswordCell?.InitEvents();
UsernameCell?.InitEvents();
UriCell?.InitEvents();
NameCell?.InitEvents();
NotesCell?.InitEvents();
2017-07-13 18:52:24 +03:00
TotpCell?.InitEvents();
FolderCell?.InitEvents();
if(PasswordCell?.Button != null)
{
PasswordCell.Button.Clicked += PasswordButton_Clicked;
}
2017-07-13 18:52:24 +03:00
if(TotpCell?.Button != null)
{
TotpCell.Button.Clicked += TotpButton_Clicked;
}
if(GenerateCell != null)
{
GenerateCell.Tapped += GenerateCell_Tapped;
}
2017-07-22 22:38:08 +03:00
if(AttachmentsCell != null)
{
AttachmentsCell.Tapped += AttachmentsCell_Tapped;
}
2017-09-26 00:13:20 +03:00
if(CustomFieldsCell != null)
{
CustomFieldsCell.Tapped += CustomFieldsCell_Tapped;
}
if(DeleteCell != null)
{
DeleteCell.Tapped += DeleteCell_Tapped;
}
}
protected override void OnDisappearing()
{
base.OnDisappearing();
PasswordCell?.Dispose();
2017-07-13 18:52:24 +03:00
TotpCell?.Dispose();
UsernameCell?.Dispose();
UriCell?.Dispose();
NameCell?.Dispose();
NotesCell?.Dispose();
FolderCell?.Dispose();
if(PasswordCell?.Button != null)
{
PasswordCell.Button.Clicked -= PasswordButton_Clicked;
}
2017-07-13 18:52:24 +03:00
if(TotpCell?.Button != null)
{
TotpCell.Button.Clicked -= TotpButton_Clicked;
}
if(GenerateCell != null)
{
GenerateCell.Tapped -= GenerateCell_Tapped;
}
2017-07-22 22:38:08 +03:00
if(AttachmentsCell != null)
{
AttachmentsCell.Tapped -= AttachmentsCell_Tapped;
}
2017-09-26 00:13:20 +03:00
if(CustomFieldsCell != null)
{
CustomFieldsCell.Tapped -= CustomFieldsCell_Tapped;
}
if(DeleteCell != null)
{
DeleteCell.Tapped -= DeleteCell_Tapped;
}
}
private void PasswordButton_Clicked(object sender, EventArgs e)
{
PasswordCell.Entry.InvokeToggleIsPassword();
PasswordCell.Button.Image = "eye" + (!PasswordCell.Entry.IsPasswordFromToggled ? "_slash" : string.Empty);
}
private async void TotpButton_Clicked(object sender, EventArgs e)
2017-07-13 18:52:24 +03:00
{
var scanPage = new ScanPage((key) =>
{
Device.BeginInvokeOnMainThread(async () =>
{
await Navigation.PopModalAsync();
if(!string.IsNullOrWhiteSpace(key))
{
TotpCell.Entry.Text = key;
_userDialogs.Toast(AppResources.AuthenticatorKeyAdded);
}
else
{
_userDialogs.Alert(AppResources.AuthenticatorKeyReadError);
}
});
});
await Navigation.PushModalAsync(new ExtendedNavigationPage(scanPage));
2017-07-13 18:52:24 +03:00
}
private async void GenerateCell_Tapped(object sender, EventArgs e)
{
if(!string.IsNullOrWhiteSpace(PasswordCell.Entry.Text)
2016-11-26 00:42:52 +03:00
&& !await _userDialogs.ConfirmAsync(AppResources.PasswordOverrideAlert, null, AppResources.Yes, AppResources.No))
{
return;
}
var page = new ToolsPasswordGeneratorPage((password) =>
{
PasswordCell.Entry.Text = password;
2016-11-26 00:42:52 +03:00
_userDialogs.Toast(AppResources.PasswordGenerated);
});
await Navigation.PushForDeviceAsync(page);
}
2017-07-22 22:38:08 +03:00
private async void AttachmentsCell_Tapped(object sender, EventArgs e)
{
var page = new ExtendedNavigationPage(new VaultAttachmentsPage(_loginId));
await Navigation.PushModalAsync(page);
}
2017-09-26 00:13:20 +03:00
private async void CustomFieldsCell_Tapped(object sender, EventArgs e)
{
var page = new ExtendedNavigationPage(new VaultCustomFieldsPage(_loginId));
await Navigation.PushModalAsync(page);
}
private async void DeleteCell_Tapped(object sender, EventArgs e)
{
if(!_connectivity.IsConnected)
{
AlertNoConnection();
return;
}
if(!await _userDialogs.ConfirmAsync(AppResources.DoYouReallyWantToDelete, null, AppResources.Yes, AppResources.No))
{
return;
}
2016-11-26 00:42:52 +03:00
_userDialogs.ShowLoading(AppResources.Deleting, MaskType.Black);
var deleteTask = await _cipherService.DeleteAsync(_loginId);
_userDialogs.HideLoading();
if(deleteTask.Succeeded)
{
_userDialogs.Toast(AppResources.ItemDeleted);
2017-01-03 08:17:15 +03:00
_googleAnalyticsService.TrackAppEvent("DeletedLogin");
2017-06-07 17:19:56 +03:00
await Navigation.PopForDeviceAsync();
}
else if(deleteTask.Errors.Count() > 0)
{
await _userDialogs.AlertAsync(deleteTask.Errors.First().Message, AppResources.AnErrorHasOccurred);
}
else
{
await _userDialogs.AlertAsync(AppResources.AnErrorHasOccurred);
}
}
private void AlertNoConnection()
{
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage,
AppResources.Ok);
2016-05-02 09:52:09 +03:00
}
}
}