2016-05-02 09:52:09 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
using Acr.UserDialogs;
|
|
|
|
|
using Bit.App.Abstractions;
|
2016-05-13 07:11:32 +03:00
|
|
|
|
using Bit.App.Controls;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
using Bit.App.Resources;
|
|
|
|
|
using Plugin.Connectivity.Abstractions;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Xamarin.Forms;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
using XLabs.Ioc;
|
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 ILoginService _loginService;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
private readonly IFolderService _folderService;
|
|
|
|
|
private readonly IUserDialogs _userDialogs;
|
|
|
|
|
private readonly IConnectivity _connectivity;
|
2016-08-05 02:35:56 +03:00
|
|
|
|
private readonly IGoogleAnalyticsService _googleAnalyticsService;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
|
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;
|
|
|
|
|
_loginService = Resolver.Resolve<ILoginService>();
|
2016-05-08 06:11:47 +03:00
|
|
|
|
_folderService = Resolver.Resolve<IFolderService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
|
|
|
|
_connectivity = Resolver.Resolve<IConnectivity>();
|
2016-08-05 02:35:56 +03:00
|
|
|
|
_googleAnalyticsService = Resolver.Resolve<IGoogleAnalyticsService>();
|
2016-05-08 06:11:47 +03:00
|
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 07:27:29 +03:00
|
|
|
|
public FormEntryCell PasswordCell { get; private set; }
|
2017-02-15 08:28:05 +03:00
|
|
|
|
public FormEntryCell UsernameCell { get; private set; }
|
|
|
|
|
public FormEntryCell UriCell { get; private set; }
|
|
|
|
|
public FormEntryCell NameCell { get; private set; }
|
|
|
|
|
public FormEditorCell NotesCell { get; private set; }
|
|
|
|
|
public FormPickerCell FolderCell { get; private set; }
|
|
|
|
|
public ExtendedTextCell GenerateCell { get; private set; }
|
|
|
|
|
public ExtendedTextCell DeleteCell { get; private set; }
|
2016-07-07 07:27:29 +03:00
|
|
|
|
|
2016-05-08 06:11:47 +03:00
|
|
|
|
private void Init()
|
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var login = _loginService.GetByIdAsync(_loginId).GetAwaiter().GetResult();
|
|
|
|
|
if(login == null)
|
2016-05-08 06:11:47 +03:00
|
|
|
|
{
|
|
|
|
|
// TODO: handle error. navigate back? should never happen...
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-03 02:41:10 +03:00
|
|
|
|
NotesCell = new FormEditorCell(height: 180);
|
2017-04-20 18:23:30 +03:00
|
|
|
|
NotesCell.Editor.Text = login.Notes?.Decrypt(login.OrganizationId);
|
2016-11-08 06:07:33 +03:00
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
PasswordCell = new FormEntryCell(AppResources.Password, isPassword: true, nextElement: NotesCell.Editor,
|
2016-11-08 06:07:33 +03:00
|
|
|
|
useButton: true);
|
2017-04-20 18:23:30 +03:00
|
|
|
|
PasswordCell.Entry.Text = login.Password?.Decrypt(login.OrganizationId);
|
2016-11-08 06:07:33 +03:00
|
|
|
|
PasswordCell.Button.Image = "eye";
|
2016-11-08 08:21:36 +03:00
|
|
|
|
PasswordCell.Entry.DisableAutocapitalize = true;
|
|
|
|
|
PasswordCell.Entry.Autocorrect = false;
|
2016-11-09 05:29:24 +03:00
|
|
|
|
PasswordCell.Entry.FontFamily = Device.OnPlatform(iOS: "Courier", Android: "monospace", WinPhone: "Courier");
|
2016-11-08 06:07:33 +03:00
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
UsernameCell = new FormEntryCell(AppResources.Username, nextElement: PasswordCell.Entry);
|
2017-04-20 18:23:30 +03:00
|
|
|
|
UsernameCell.Entry.Text = login.Username?.Decrypt(login.OrganizationId);
|
2017-02-15 08:28:05 +03:00
|
|
|
|
UsernameCell.Entry.DisableAutocapitalize = true;
|
|
|
|
|
UsernameCell.Entry.Autocorrect = false;
|
2016-06-14 03:03:16 +03:00
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
UriCell = new FormEntryCell(AppResources.URI, Keyboard.Url, nextElement: UsernameCell.Entry);
|
2017-04-20 18:23:30 +03:00
|
|
|
|
UriCell.Entry.Text = login.Uri?.Decrypt(login.OrganizationId);
|
2017-02-15 08:28:05 +03:00
|
|
|
|
NameCell = new FormEntryCell(AppResources.Name, nextElement: UriCell.Entry);
|
2017-04-20 18:23:30 +03:00
|
|
|
|
NameCell.Entry.Text = login.Name?.Decrypt(login.OrganizationId);
|
2016-05-08 06:11:47 +03:00
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
GenerateCell = new ExtendedTextCell
|
2016-07-07 07:27:29 +03:00
|
|
|
|
{
|
2016-11-26 00:42:52 +03:00
|
|
|
|
Text = AppResources.GeneratePassword,
|
2016-07-07 07:27:29 +03:00
|
|
|
|
ShowDisclousure = true
|
|
|
|
|
};
|
|
|
|
|
|
2016-05-13 07:11:32 +03:00
|
|
|
|
var folderOptions = new List<string> { AppResources.FolderNone };
|
2016-07-27 02:21:57 +03:00
|
|
|
|
var folders = _folderService.GetAllAsync().GetAwaiter().GetResult()
|
|
|
|
|
.OrderBy(f => f.Name?.Decrypt()).ToList();
|
2016-05-08 06:11:47 +03:00
|
|
|
|
int selectedIndex = 0;
|
|
|
|
|
int i = 0;
|
|
|
|
|
foreach(var folder in folders)
|
|
|
|
|
{
|
|
|
|
|
i++;
|
2017-01-03 08:17:15 +03:00
|
|
|
|
if(folder.Id == login.FolderId)
|
2016-05-08 06:11:47 +03:00
|
|
|
|
{
|
|
|
|
|
selectedIndex = i;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 07:11:32 +03:00
|
|
|
|
folderOptions.Add(folder.Name.Decrypt());
|
|
|
|
|
}
|
2017-02-15 08:28:05 +03:00
|
|
|
|
FolderCell = new FormPickerCell(AppResources.Folder, folderOptions.ToArray());
|
|
|
|
|
FolderCell.Picker.SelectedIndex = selectedIndex;
|
2016-05-13 07:11:32 +03:00
|
|
|
|
|
2016-06-15 05:36:37 +03:00
|
|
|
|
var favoriteCell = new ExtendedSwitchCell
|
|
|
|
|
{
|
2016-11-26 00:42:52 +03:00
|
|
|
|
Text = AppResources.Favorite,
|
2017-01-03 08:17:15 +03:00
|
|
|
|
On = login.Favorite
|
2016-06-15 05:36:37 +03:00
|
|
|
|
};
|
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
DeleteCell = new ExtendedTextCell { Text = AppResources.Delete, TextColor = Color.Red };
|
2016-07-07 07:00:12 +03:00
|
|
|
|
|
2016-05-13 07:11:32 +03:00
|
|
|
|
var table = new ExtendedTableView
|
|
|
|
|
{
|
|
|
|
|
Intent = TableIntent.Settings,
|
2016-05-24 06:48:34 +03:00
|
|
|
|
EnableScrolling = true,
|
2016-05-13 07:11:32 +03:00
|
|
|
|
HasUnevenRows = true,
|
|
|
|
|
Root = new TableRoot
|
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
new TableSection(AppResources.LoginInformation)
|
2016-05-13 07:11:32 +03:00
|
|
|
|
{
|
2017-02-15 08:28:05 +03:00
|
|
|
|
NameCell,
|
|
|
|
|
UriCell,
|
|
|
|
|
UsernameCell,
|
2016-07-07 07:27:29 +03:00
|
|
|
|
PasswordCell,
|
2017-02-15 08:28:05 +03:00
|
|
|
|
GenerateCell
|
2016-05-13 07:11:32 +03:00
|
|
|
|
},
|
2017-04-20 21:18:16 +03:00
|
|
|
|
new TableSection(" ")
|
2016-06-15 05:36:37 +03:00
|
|
|
|
{
|
2017-02-15 08:28:05 +03:00
|
|
|
|
FolderCell,
|
2016-06-15 05:36:37 +03:00
|
|
|
|
favoriteCell
|
|
|
|
|
},
|
2016-05-13 07:11:32 +03:00
|
|
|
|
new TableSection(AppResources.Notes)
|
|
|
|
|
{
|
2017-02-15 08:28:05 +03:00
|
|
|
|
NotesCell
|
2016-07-07 07:00:12 +03:00
|
|
|
|
},
|
2017-04-20 21:18:16 +03:00
|
|
|
|
new TableSection(" ")
|
2016-07-07 07:00:12 +03:00
|
|
|
|
{
|
2017-02-15 08:28:05 +03:00
|
|
|
|
DeleteCell
|
2016-05-13 07:11:32 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
|
|
|
|
table.RowHeight = -1;
|
|
|
|
|
table.EstimatedRowHeight = 70;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
}
|
2016-11-08 07:45:46 +03:00
|
|
|
|
else if(Device.OS == TargetPlatform.Android)
|
|
|
|
|
{
|
|
|
|
|
PasswordCell.Button.WidthRequest = 40;
|
|
|
|
|
}
|
2016-05-08 06:11:47 +03:00
|
|
|
|
|
|
|
|
|
var saveToolBarItem = new ToolbarItem(AppResources.Save, null, async () =>
|
|
|
|
|
{
|
|
|
|
|
if(!_connectivity.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(NameCell.Entry.Text))
|
2016-05-08 06:11:47 +03:00
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired,
|
|
|
|
|
AppResources.Name), AppResources.Ok);
|
2016-05-08 06:11:47 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 18:40:39 +03:00
|
|
|
|
login.Uri = UriCell.Entry.Text?.Encrypt(login.OrganizationId);
|
|
|
|
|
login.Name = NameCell.Entry.Text?.Encrypt(login.OrganizationId);
|
|
|
|
|
login.Username = UsernameCell.Entry.Text?.Encrypt(login.OrganizationId);
|
|
|
|
|
login.Password = PasswordCell.Entry.Text?.Encrypt(login.OrganizationId);
|
|
|
|
|
login.Notes = NotesCell.Editor.Text?.Encrypt(login.OrganizationId);
|
2017-01-03 08:17:15 +03:00
|
|
|
|
login.Favorite = favoriteCell.On;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
|
2017-02-15 08:28:05 +03:00
|
|
|
|
if(FolderCell.Picker.SelectedIndex > 0)
|
2016-05-13 07:11:32 +03:00
|
|
|
|
{
|
2017-02-15 08:28:05 +03:00
|
|
|
|
login.FolderId = folders.ElementAt(FolderCell.Picker.SelectedIndex - 1).Id;
|
2016-05-13 07:11:32 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
2016-05-08 06:11:47 +03:00
|
|
|
|
{
|
2017-01-03 08:17:15 +03:00
|
|
|
|
login.FolderId = null;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-26 00:42:52 +03:00
|
|
|
|
_userDialogs.ShowLoading(AppResources.Saving, MaskType.Black);
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var saveTask = await _loginService.SaveAsync(login);
|
2016-05-08 06:11:47 +03:00
|
|
|
|
|
|
|
|
|
_userDialogs.HideLoading();
|
2016-07-20 01:46:39 +03:00
|
|
|
|
|
2016-08-17 02:20:41 +03:00
|
|
|
|
if(saveTask.Succeeded)
|
2016-07-20 01:46:39 +03:00
|
|
|
|
{
|
2016-08-30 06:50:22 +03:00
|
|
|
|
await Navigation.PopForDeviceAsync();
|
2017-01-03 08:17:15 +03:00
|
|
|
|
_userDialogs.Toast(AppResources.LoginUpdated);
|
|
|
|
|
_googleAnalyticsService.TrackAppEvent("EditeLogin");
|
2016-07-20 01:46:39 +03:00
|
|
|
|
}
|
2016-08-17 02:20:41 +03:00
|
|
|
|
else if(saveTask.Errors.Count() > 0)
|
2016-07-20 01:46:39 +03:00
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
await _userDialogs.AlertAsync(saveTask.Errors.First().Message, AppResources.AnErrorHasOccurred);
|
2016-07-20 01:46:39 +03:00
|
|
|
|
}
|
2016-07-31 01:16:09 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _userDialogs.AlertAsync(AppResources.AnErrorHasOccurred);
|
|
|
|
|
}
|
2016-05-08 06:11:47 +03:00
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
|
|
|
|
|
2017-01-03 08:17:15 +03:00
|
|
|
|
Title = AppResources.EditLogin;
|
2016-05-24 06:48:34 +03:00
|
|
|
|
Content = table;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
ToolbarItems.Add(saveToolBarItem);
|
2016-05-14 08:34:42 +03:00
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
2016-11-26 00:42:52 +03:00
|
|
|
|
ToolbarItems.Add(new DismissModalToolBarItem(this, AppResources.Cancel));
|
2016-05-14 08:34:42 +03:00
|
|
|
|
}
|
2016-07-31 00:29:04 +03:00
|
|
|
|
}
|
2016-05-08 06:11:47 +03:00
|
|
|
|
|
2016-07-31 00:29:04 +03:00
|
|
|
|
protected override void OnAppearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnAppearing();
|
2016-05-08 06:11:47 +03:00
|
|
|
|
if(!_connectivity.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
}
|
2017-02-15 08:28:05 +03:00
|
|
|
|
|
2017-02-26 00:10:18 +03:00
|
|
|
|
PasswordCell?.InitEvents();
|
|
|
|
|
UsernameCell?.InitEvents();
|
|
|
|
|
UriCell?.InitEvents();
|
|
|
|
|
NameCell?.InitEvents();
|
|
|
|
|
NotesCell?.InitEvents();
|
|
|
|
|
FolderCell?.InitEvents();
|
|
|
|
|
|
|
|
|
|
if(PasswordCell?.Button != null)
|
|
|
|
|
{
|
|
|
|
|
PasswordCell.Button.Clicked += PasswordButton_Clicked;
|
|
|
|
|
}
|
|
|
|
|
if(GenerateCell != null)
|
|
|
|
|
{
|
|
|
|
|
GenerateCell.Tapped += GenerateCell_Tapped;
|
|
|
|
|
}
|
|
|
|
|
if(DeleteCell != null)
|
|
|
|
|
{
|
|
|
|
|
DeleteCell.Tapped += DeleteCell_Tapped;
|
|
|
|
|
}
|
2017-02-15 08:28:05 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnDisappearing()
|
|
|
|
|
{
|
|
|
|
|
base.OnDisappearing();
|
2017-02-26 00:10:18 +03:00
|
|
|
|
PasswordCell?.Dispose();
|
|
|
|
|
UsernameCell?.Dispose();
|
|
|
|
|
UriCell?.Dispose();
|
|
|
|
|
NameCell?.Dispose();
|
|
|
|
|
NotesCell?.Dispose();
|
|
|
|
|
FolderCell?.Dispose();
|
|
|
|
|
|
|
|
|
|
if(PasswordCell?.Button != null)
|
|
|
|
|
{
|
|
|
|
|
PasswordCell.Button.Clicked -= PasswordButton_Clicked;
|
|
|
|
|
}
|
|
|
|
|
if(GenerateCell != null)
|
|
|
|
|
{
|
|
|
|
|
GenerateCell.Tapped -= GenerateCell_Tapped;
|
|
|
|
|
}
|
|
|
|
|
if(DeleteCell != null)
|
|
|
|
|
{
|
|
|
|
|
DeleteCell.Tapped -= DeleteCell_Tapped;
|
|
|
|
|
}
|
2016-05-08 06:11:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-08 06:46:15 +03:00
|
|
|
|
private void PasswordButton_Clicked(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
PasswordCell.Entry.InvokeToggleIsPassword();
|
|
|
|
|
PasswordCell.Button.Image = "eye" + (!PasswordCell.Entry.IsPasswordFromToggled ? "_slash" : string.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 07:27:29 +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))
|
2016-07-07 07:27:29 +03:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var page = new ToolsPasswordGeneratorPage((password) =>
|
|
|
|
|
{
|
|
|
|
|
PasswordCell.Entry.Text = password;
|
2016-11-26 00:42:52 +03:00
|
|
|
|
_userDialogs.Toast(AppResources.PasswordGenerated);
|
2016-07-07 07:27:29 +03:00
|
|
|
|
});
|
2016-08-30 06:50:22 +03:00
|
|
|
|
await Navigation.PushForDeviceAsync(page);
|
2016-07-07 07:27:29 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 07:00:12 +03:00
|
|
|
|
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);
|
2017-01-03 08:17:15 +03:00
|
|
|
|
var deleteTask = await _loginService.DeleteAsync(_loginId);
|
2016-07-07 07:00:12 +03:00
|
|
|
|
_userDialogs.HideLoading();
|
|
|
|
|
|
2016-08-17 02:20:41 +03:00
|
|
|
|
if(deleteTask.Succeeded)
|
2016-07-07 07:00:12 +03:00
|
|
|
|
{
|
2016-08-30 06:50:22 +03:00
|
|
|
|
await Navigation.PopForDeviceAsync();
|
2017-01-03 08:17:15 +03:00
|
|
|
|
_userDialogs.Toast(AppResources.LoginDeleted);
|
|
|
|
|
_googleAnalyticsService.TrackAppEvent("DeletedLogin");
|
2016-07-07 07:00:12 +03:00
|
|
|
|
}
|
2016-08-17 02:20:41 +03:00
|
|
|
|
else if(deleteTask.Errors.Count() > 0)
|
2016-07-31 01:16:09 +03:00
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
await _userDialogs.AlertAsync(deleteTask.Errors.First().Message, AppResources.AnErrorHasOccurred);
|
2016-07-31 01:16:09 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _userDialogs.AlertAsync(AppResources.AnErrorHasOccurred);
|
|
|
|
|
}
|
2016-07-07 07:00:12 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 06:11:47 +03:00
|
|
|
|
private void AlertNoConnection()
|
|
|
|
|
{
|
2016-08-17 02:20:41 +03:00
|
|
|
|
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage,
|
|
|
|
|
AppResources.Ok);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|