2016-05-02 09:52:09 +03:00
|
|
|
|
using System;
|
2016-05-13 07:11:32 +03:00
|
|
|
|
using System.Collections.Generic;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using System.Linq;
|
2016-05-03 09:08:50 +03:00
|
|
|
|
using Acr.UserDialogs;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Bit.App.Abstractions;
|
2016-05-10 06:25:37 +03:00
|
|
|
|
using Bit.App.Controls;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Bit.App.Models;
|
2016-05-07 20:42:09 +03:00
|
|
|
|
using Bit.App.Resources;
|
2016-05-07 01:49:01 +03:00
|
|
|
|
using Plugin.Connectivity.Abstractions;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
using XLabs.Ioc;
|
|
|
|
|
|
2016-05-03 00:50:16 +03:00
|
|
|
|
namespace Bit.App.Pages
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
|
|
|
|
public class VaultAddSitePage : ContentPage
|
|
|
|
|
{
|
2016-05-08 06:11:47 +03:00
|
|
|
|
private readonly ISiteService _siteService;
|
|
|
|
|
private readonly IFolderService _folderService;
|
|
|
|
|
private readonly IUserDialogs _userDialogs;
|
|
|
|
|
private readonly IConnectivity _connectivity;
|
|
|
|
|
|
2016-05-02 09:52:09 +03:00
|
|
|
|
public VaultAddSitePage()
|
|
|
|
|
{
|
2016-05-08 06:11:47 +03:00
|
|
|
|
_siteService = Resolver.Resolve<ISiteService>();
|
|
|
|
|
_folderService = Resolver.Resolve<IFolderService>();
|
|
|
|
|
_userDialogs = Resolver.Resolve<IUserDialogs>();
|
|
|
|
|
_connectivity = Resolver.Resolve<IConnectivity>();
|
2016-05-10 01:16:42 +03:00
|
|
|
|
|
|
|
|
|
Init();
|
2016-05-08 06:11:47 +03:00
|
|
|
|
}
|
2016-05-03 01:35:01 +03:00
|
|
|
|
|
2016-05-08 06:11:47 +03:00
|
|
|
|
private void Init()
|
|
|
|
|
{
|
2016-05-13 07:11:32 +03:00
|
|
|
|
var uriCell = new FormEntryCell(AppResources.URI, Keyboard.Url);
|
|
|
|
|
var nameCell = new FormEntryCell(AppResources.Name);
|
|
|
|
|
var usernameCell = new FormEntryCell(AppResources.Username);
|
|
|
|
|
var passwordCell = new FormEntryCell(AppResources.Password, IsPassword: true);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-13 07:11:32 +03:00
|
|
|
|
var folderOptions = new List<string> { AppResources.FolderNone };
|
|
|
|
|
var folders = _folderService.GetAllAsync().GetAwaiter().GetResult().OrderBy(f => f.Name?.Decrypt());
|
2016-05-12 00:30:09 +03:00
|
|
|
|
foreach(var folder in folders)
|
2016-05-03 01:35:01 +03:00
|
|
|
|
{
|
2016-05-13 07:11:32 +03:00
|
|
|
|
folderOptions.Add(folder.Name.Decrypt());
|
2016-05-03 01:35:01 +03:00
|
|
|
|
}
|
2016-05-13 07:11:32 +03:00
|
|
|
|
var folderCell = new FormPickerCell(AppResources.Folder, folderOptions.ToArray());
|
|
|
|
|
|
|
|
|
|
var notesCell = new FormEditorCell(height:90);
|
2016-05-12 00:30:09 +03:00
|
|
|
|
|
2016-05-12 07:09:06 +03:00
|
|
|
|
var mainTable = new ExtendedTableView
|
2016-05-12 00:30:09 +03:00
|
|
|
|
{
|
2016-05-12 07:09:06 +03:00
|
|
|
|
Intent = TableIntent.Settings,
|
2016-05-13 04:30:02 +03:00
|
|
|
|
EnableScrolling = false,
|
2016-05-12 00:30:09 +03:00
|
|
|
|
HasUnevenRows = true,
|
2016-05-13 04:30:02 +03:00
|
|
|
|
EnableSelection = false,
|
2016-05-12 00:30:09 +03:00
|
|
|
|
Root = new TableRoot
|
|
|
|
|
{
|
2016-05-13 07:11:32 +03:00
|
|
|
|
new TableSection("Site Information")
|
2016-05-12 00:30:09 +03:00
|
|
|
|
{
|
|
|
|
|
uriCell,
|
|
|
|
|
nameCell,
|
|
|
|
|
folderCell,
|
|
|
|
|
usernameCell,
|
2016-05-12 07:09:06 +03:00
|
|
|
|
passwordCell
|
2016-05-13 04:30:02 +03:00
|
|
|
|
},
|
2016-05-12 07:09:06 +03:00
|
|
|
|
new TableSection(AppResources.Notes)
|
|
|
|
|
{
|
2016-05-12 00:30:09 +03:00
|
|
|
|
notesCell
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-12 07:09:06 +03:00
|
|
|
|
if(Device.OS == TargetPlatform.iOS)
|
|
|
|
|
{
|
2016-05-13 04:30:02 +03:00
|
|
|
|
mainTable.RowHeight = -1;
|
|
|
|
|
mainTable.EstimatedRowHeight = 70;
|
2016-05-12 07:09:06 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-02 09:52:09 +03:00
|
|
|
|
var scrollView = new ScrollView
|
|
|
|
|
{
|
2016-05-13 04:30:02 +03:00
|
|
|
|
Content = mainTable,
|
2016-05-02 09:52:09 +03:00
|
|
|
|
Orientation = ScrollOrientation.Vertical
|
|
|
|
|
};
|
|
|
|
|
|
2016-05-12 00:30:09 +03:00
|
|
|
|
var saveToolBarItem = new ToolbarItem(AppResources.Save, null, async () =>
|
|
|
|
|
{
|
|
|
|
|
if(!_connectivity.IsConnected)
|
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 07:11:32 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(uriCell.Entry.Text))
|
2016-05-12 00:30:09 +03:00
|
|
|
|
{
|
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.URI), AppResources.Ok);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 07:11:32 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(nameCell.Entry.Text))
|
2016-05-12 00:30:09 +03:00
|
|
|
|
{
|
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Name), AppResources.Ok);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var site = new Site
|
|
|
|
|
{
|
2016-05-13 07:11:32 +03:00
|
|
|
|
Uri = uriCell.Entry.Text.Encrypt(),
|
|
|
|
|
Name = nameCell.Entry.Text.Encrypt(),
|
|
|
|
|
Username = usernameCell.Entry.Text?.Encrypt(),
|
|
|
|
|
Password = passwordCell.Entry.Text?.Encrypt(),
|
|
|
|
|
Notes = notesCell.Editor.Text?.Encrypt(),
|
2016-05-12 00:30:09 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-05-13 07:11:32 +03:00
|
|
|
|
if(folderCell.Picker.SelectedIndex > 0)
|
2016-05-12 00:30:09 +03:00
|
|
|
|
{
|
2016-05-13 07:11:32 +03:00
|
|
|
|
site.FolderId = folders.ElementAt(folderCell.Picker.SelectedIndex - 1).Id;
|
2016-05-12 00:30:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var saveTask = _siteService.SaveAsync(site);
|
|
|
|
|
_userDialogs.ShowLoading("Saving...", MaskType.Black);
|
|
|
|
|
await saveTask;
|
|
|
|
|
|
|
|
|
|
_userDialogs.HideLoading();
|
|
|
|
|
await Navigation.PopAsync();
|
2016-05-13 07:11:32 +03:00
|
|
|
|
_userDialogs.SuccessToast(nameCell.Entry.Text, "New site created.");
|
2016-05-12 00:30:09 +03:00
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-07 20:42:09 +03:00
|
|
|
|
Title = AppResources.AddSite;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
Content = scrollView;
|
2016-05-12 00:30:09 +03:00
|
|
|
|
ToolbarItems.Add(saveToolBarItem);
|
2016-05-13 07:11:32 +03:00
|
|
|
|
ToolbarItems.Add(new DismissModalToolBarItem(this, "Cancel"));
|
2016-05-07 01:49:01 +03:00
|
|
|
|
|
2016-05-12 00:30:09 +03:00
|
|
|
|
if(!_connectivity.IsConnected)
|
2016-05-07 01:49:01 +03:00
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 06:11:47 +03:00
|
|
|
|
private void AlertNoConnection()
|
2016-05-07 01:49:01 +03:00
|
|
|
|
{
|
2016-05-12 00:30:09 +03:00
|
|
|
|
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage, AppResources.Ok);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class FormEntryStackLayout : StackLayout
|
|
|
|
|
{
|
|
|
|
|
public FormEntryStackLayout()
|
|
|
|
|
{
|
|
|
|
|
Padding = new Thickness(15, 15, 15, 0);
|
2016-05-12 07:09:06 +03:00
|
|
|
|
BackgroundColor = Color.White;
|
2016-05-12 00:30:09 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-07 01:49:01 +03:00
|
|
|
|
|