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
|
|
|
|
{
|
2016-06-28 02:53:31 +03:00
|
|
|
|
public class VaultEditSitePage : ExtendedContentPage
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-05-08 06:11:47 +03:00
|
|
|
|
private readonly string _siteId;
|
|
|
|
|
private readonly ISiteService _siteService;
|
|
|
|
|
private readonly IFolderService _folderService;
|
|
|
|
|
private readonly IUserDialogs _userDialogs;
|
|
|
|
|
private readonly IConnectivity _connectivity;
|
|
|
|
|
|
2016-05-07 05:29:03 +03:00
|
|
|
|
public VaultEditSitePage(string siteId)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-05-08 06:11:47 +03:00
|
|
|
|
_siteId = siteId;
|
|
|
|
|
_siteService = Resolver.Resolve<ISiteService>();
|
|
|
|
|
_folderService = Resolver.Resolve<IFolderService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
|
|
|
|
_connectivity = Resolver.Resolve<IConnectivity>();
|
|
|
|
|
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 07:27:29 +03:00
|
|
|
|
public FormEntryCell PasswordCell { get; private set; }
|
|
|
|
|
|
2016-05-08 06:11:47 +03:00
|
|
|
|
private void Init()
|
|
|
|
|
{
|
|
|
|
|
var site = _siteService.GetByIdAsync(_siteId).GetAwaiter().GetResult();
|
|
|
|
|
if(site == null)
|
|
|
|
|
{
|
|
|
|
|
// TODO: handle error. navigate back? should never happen...
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-24 06:48:34 +03:00
|
|
|
|
var notesCell = new FormEditorCell(height: 90);
|
|
|
|
|
notesCell.Editor.Text = site.Notes?.Decrypt();
|
2016-07-07 07:27:29 +03:00
|
|
|
|
PasswordCell = new FormEntryCell(AppResources.Password, IsPassword: true, nextElement: notesCell.Editor);
|
|
|
|
|
PasswordCell.Entry.Text = site.Password?.Decrypt();
|
|
|
|
|
var usernameCell = new FormEntryCell(AppResources.Username, nextElement: PasswordCell.Entry);
|
2016-05-24 06:48:34 +03:00
|
|
|
|
usernameCell.Entry.Text = site.Username?.Decrypt();
|
|
|
|
|
usernameCell.Entry.DisableAutocapitalize = true;
|
|
|
|
|
usernameCell.Entry.Autocorrect = false;
|
2016-06-14 03:03:16 +03:00
|
|
|
|
|
2016-07-07 07:27:29 +03:00
|
|
|
|
usernameCell.Entry.FontFamily = PasswordCell.Entry.FontFamily = "Courier";
|
2016-06-14 03:03:16 +03:00
|
|
|
|
|
2016-05-24 06:48:34 +03:00
|
|
|
|
var uriCell = new FormEntryCell(AppResources.URI, Keyboard.Url, nextElement: usernameCell.Entry);
|
2016-05-13 07:11:32 +03:00
|
|
|
|
uriCell.Entry.Text = site.Uri?.Decrypt();
|
2016-05-24 06:48:34 +03:00
|
|
|
|
var nameCell = new FormEntryCell(AppResources.Name, nextElement: uriCell.Entry);
|
2016-05-13 07:11:32 +03:00
|
|
|
|
nameCell.Entry.Text = site.Name?.Decrypt();
|
2016-05-08 06:11:47 +03:00
|
|
|
|
|
2016-07-07 07:27:29 +03:00
|
|
|
|
var generateCell = new ExtendedTextCell
|
|
|
|
|
{
|
|
|
|
|
Text = "Generate Password",
|
|
|
|
|
ShowDisclousure = true
|
|
|
|
|
};
|
|
|
|
|
generateCell.Tapped += GenerateCell_Tapped; ;
|
|
|
|
|
|
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++;
|
|
|
|
|
if(folder.Id == site.FolderId)
|
|
|
|
|
{
|
|
|
|
|
selectedIndex = i;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 07:11:32 +03:00
|
|
|
|
folderOptions.Add(folder.Name.Decrypt());
|
|
|
|
|
}
|
|
|
|
|
var folderCell = new FormPickerCell(AppResources.Folder, folderOptions.ToArray());
|
|
|
|
|
folderCell.Picker.SelectedIndex = selectedIndex;
|
|
|
|
|
|
2016-06-15 05:36:37 +03:00
|
|
|
|
var favoriteCell = new ExtendedSwitchCell
|
|
|
|
|
{
|
|
|
|
|
Text = "Favorite",
|
|
|
|
|
On = site.Favorite
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-07 07:00:12 +03:00
|
|
|
|
var deleteCell = new ExtendedTextCell { Text = AppResources.Delete, TextColor = Color.Red };
|
|
|
|
|
deleteCell.Tapped += DeleteCell_Tapped;
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
new TableSection("Site Information")
|
|
|
|
|
{
|
|
|
|
|
nameCell,
|
2016-05-17 06:54:24 +03:00
|
|
|
|
uriCell,
|
2016-05-13 07:11:32 +03:00
|
|
|
|
usernameCell,
|
2016-07-07 07:27:29 +03:00
|
|
|
|
PasswordCell,
|
|
|
|
|
generateCell
|
2016-05-13 07:11:32 +03:00
|
|
|
|
},
|
2016-06-15 05:36:37 +03:00
|
|
|
|
new TableSection
|
|
|
|
|
{
|
2016-07-07 07:27:29 +03:00
|
|
|
|
folderCell,
|
2016-06-15 05:36:37 +03:00
|
|
|
|
favoriteCell
|
|
|
|
|
},
|
2016-05-13 07:11:32 +03:00
|
|
|
|
new TableSection(AppResources.Notes)
|
|
|
|
|
{
|
|
|
|
|
notesCell
|
2016-07-07 07:00:12 +03:00
|
|
|
|
},
|
|
|
|
|
new TableSection
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var saveToolBarItem = new ToolbarItem(AppResources.Save, null, async () =>
|
|
|
|
|
{
|
|
|
|
|
if(!_connectivity.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 07:27:29 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(PasswordCell.Entry.Text))
|
2016-05-08 06:11:47 +03:00
|
|
|
|
{
|
2016-05-14 08:34:42 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Password), AppResources.Ok);
|
2016-05-08 06:11:47 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 07:11:32 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(nameCell.Entry.Text))
|
2016-05-08 06:11:47 +03:00
|
|
|
|
{
|
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Name), AppResources.Ok);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-16 06:05:11 +03:00
|
|
|
|
site.Uri = uriCell.Entry.Text?.Encrypt();
|
|
|
|
|
site.Name = nameCell.Entry.Text?.Encrypt();
|
2016-05-13 07:11:32 +03:00
|
|
|
|
site.Username = usernameCell.Entry.Text?.Encrypt();
|
2016-07-07 07:27:29 +03:00
|
|
|
|
site.Password = PasswordCell.Entry.Text?.Encrypt();
|
2016-05-13 07:11:32 +03:00
|
|
|
|
site.Notes = notesCell.Editor.Text?.Encrypt();
|
2016-06-15 05:36:37 +03:00
|
|
|
|
site.Favorite = favoriteCell.On;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
|
2016-05-13 07:11:32 +03:00
|
|
|
|
if(folderCell.Picker.SelectedIndex > 0)
|
|
|
|
|
{
|
|
|
|
|
site.FolderId = folders.ElementAt(folderCell.Picker.SelectedIndex - 1).Id;
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-05-08 06:11:47 +03:00
|
|
|
|
{
|
2016-05-13 07:11:32 +03:00
|
|
|
|
site.FolderId = null;
|
2016-05-08 06:11:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var saveTask = _siteService.SaveAsync(site);
|
|
|
|
|
_userDialogs.ShowLoading("Saving...", MaskType.Black);
|
|
|
|
|
await saveTask;
|
|
|
|
|
|
|
|
|
|
_userDialogs.HideLoading();
|
2016-07-20 01:46:39 +03:00
|
|
|
|
|
|
|
|
|
if(saveTask.Result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
await Navigation.PopModalAsync();
|
2016-07-26 07:38:41 +03:00
|
|
|
|
_userDialogs.Toast("Site updated.");
|
2016-07-20 01:46:39 +03:00
|
|
|
|
}
|
|
|
|
|
else if(saveTask.Result.Errors.Count() > 0)
|
|
|
|
|
{
|
|
|
|
|
await _userDialogs.AlertAsync(saveTask.Result.Errors.First().Message, AppResources.AnErrorHasOccurred);
|
|
|
|
|
}
|
2016-05-08 06:11:47 +03:00
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
|
|
|
|
|
|
|
|
|
Title = "Edit Site";
|
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)
|
|
|
|
|
{
|
|
|
|
|
ToolbarItems.Add(new DismissModalToolBarItem(this, "Cancel"));
|
|
|
|
|
}
|
2016-05-08 06:11:47 +03:00
|
|
|
|
|
|
|
|
|
if(!_connectivity.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 07:27:29 +03:00
|
|
|
|
private async void GenerateCell_Tapped(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(PasswordCell.Entry.Text)
|
|
|
|
|
&& !await _userDialogs.ConfirmAsync("Are you sure you want to overwrite the current password?", null, AppResources.Yes, AppResources.No))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var page = new ToolsPasswordGeneratorPage((password) =>
|
|
|
|
|
{
|
|
|
|
|
PasswordCell.Entry.Text = password;
|
2016-07-26 07:38:41 +03:00
|
|
|
|
_userDialogs.Toast("Password generated.");
|
2016-07-07 07:27:29 +03:00
|
|
|
|
});
|
|
|
|
|
await Navigation.PushModalAsync(new ExtendedNavigationPage(page));
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var deleteTask = _siteService.DeleteAsync(_siteId);
|
|
|
|
|
_userDialogs.ShowLoading("Deleting...", MaskType.Black);
|
|
|
|
|
await deleteTask;
|
|
|
|
|
_userDialogs.HideLoading();
|
|
|
|
|
|
|
|
|
|
if((await deleteTask).Succeeded)
|
|
|
|
|
{
|
|
|
|
|
await Navigation.PopModalAsync();
|
2016-07-26 07:38:41 +03:00
|
|
|
|
_userDialogs.Toast("Site deleted.");
|
2016-07-07 07:00:12 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 06:11:47 +03:00
|
|
|
|
private void AlertNoConnection()
|
|
|
|
|
{
|
|
|
|
|
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage, AppResources.Ok);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|