2016-05-02 09:52:09 +03:00
|
|
|
|
using System;
|
|
|
|
|
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;
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
var folders = _folderService.GetAllAsync().GetAwaiter().GetResult().OrderBy(f => f.Name?.Decrypt());
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
|
|
|
|
var uriEntry = new Entry { Keyboard = Keyboard.Url };
|
|
|
|
|
var nameEntry = new Entry();
|
2016-05-08 06:11:47 +03:00
|
|
|
|
var folderPicker = new Picker { Title = AppResources.Folder };
|
2016-05-07 20:42:09 +03:00
|
|
|
|
folderPicker.Items.Add(AppResources.FolderNone);
|
2016-05-03 01:35:01 +03:00
|
|
|
|
folderPicker.SelectedIndex = 0;
|
|
|
|
|
foreach(var folder in folders)
|
|
|
|
|
{
|
|
|
|
|
folderPicker.Items.Add(folder.Name.Decrypt());
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
var usernameEntry = new Entry();
|
|
|
|
|
var passwordEntry = new Entry { IsPassword = true };
|
|
|
|
|
var notesEditor = new Editor();
|
|
|
|
|
|
|
|
|
|
var stackLayout = new StackLayout();
|
2016-05-07 20:42:09 +03:00
|
|
|
|
stackLayout.Children.Add(new Label { Text = AppResources.URI });
|
2016-05-02 09:52:09 +03:00
|
|
|
|
stackLayout.Children.Add(uriEntry);
|
2016-05-07 20:42:09 +03:00
|
|
|
|
stackLayout.Children.Add(new Label { Text = AppResources.Name });
|
2016-05-02 09:52:09 +03:00
|
|
|
|
stackLayout.Children.Add(nameEntry);
|
2016-05-07 20:42:09 +03:00
|
|
|
|
stackLayout.Children.Add(new Label { Text = AppResources.Folder });
|
2016-05-03 01:35:01 +03:00
|
|
|
|
stackLayout.Children.Add(folderPicker);
|
2016-05-07 20:42:09 +03:00
|
|
|
|
stackLayout.Children.Add(new Label { Text = AppResources.Username });
|
2016-05-02 09:52:09 +03:00
|
|
|
|
stackLayout.Children.Add(usernameEntry);
|
2016-05-07 20:42:09 +03:00
|
|
|
|
stackLayout.Children.Add(new Label { Text = AppResources.Password });
|
2016-05-02 09:52:09 +03:00
|
|
|
|
stackLayout.Children.Add(passwordEntry);
|
2016-05-07 20:42:09 +03:00
|
|
|
|
stackLayout.Children.Add(new Label { Text = AppResources.Notes });
|
2016-05-02 09:52:09 +03:00
|
|
|
|
stackLayout.Children.Add(notesEditor);
|
|
|
|
|
|
|
|
|
|
var scrollView = new ScrollView
|
|
|
|
|
{
|
|
|
|
|
Content = stackLayout,
|
|
|
|
|
Orientation = ScrollOrientation.Vertical
|
|
|
|
|
};
|
|
|
|
|
|
2016-05-07 20:42:09 +03:00
|
|
|
|
var saveToolBarItem = new ToolbarItem(AppResources.Save, null, async () =>
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-05-08 06:11:47 +03:00
|
|
|
|
if(!_connectivity.IsConnected)
|
2016-05-07 01:49:01 +03:00
|
|
|
|
{
|
|
|
|
|
AlertNoConnection();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-02 09:52:09 +03:00
|
|
|
|
if(string.IsNullOrWhiteSpace(uriEntry.Text))
|
|
|
|
|
{
|
2016-05-07 20:42:09 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.URI), AppResources.Ok);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(nameEntry.Text))
|
|
|
|
|
{
|
2016-05-07 20:42:09 +03:00
|
|
|
|
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Name), AppResources.Ok);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var site = new Site
|
|
|
|
|
{
|
|
|
|
|
Uri = uriEntry.Text.Encrypt(),
|
|
|
|
|
Name = nameEntry.Text.Encrypt(),
|
|
|
|
|
Username = usernameEntry.Text?.Encrypt(),
|
|
|
|
|
Password = passwordEntry.Text?.Encrypt(),
|
2016-05-03 01:35:01 +03:00
|
|
|
|
Notes = notesEditor.Text?.Encrypt(),
|
2016-05-02 09:52:09 +03:00
|
|
|
|
};
|
|
|
|
|
|
2016-05-03 01:35:01 +03:00
|
|
|
|
if(folderPicker.SelectedIndex > 0)
|
|
|
|
|
{
|
|
|
|
|
site.FolderId = folders.ElementAt(folderPicker.SelectedIndex - 1).Id;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-08 06:11:47 +03:00
|
|
|
|
var saveTask = _siteService.SaveAsync(site);
|
|
|
|
|
_userDialogs.ShowLoading("Saving...", MaskType.Black);
|
2016-05-03 09:08:50 +03:00
|
|
|
|
await saveTask;
|
|
|
|
|
|
2016-05-08 06:11:47 +03:00
|
|
|
|
_userDialogs.HideLoading();
|
2016-05-02 09:52:09 +03:00
|
|
|
|
await Navigation.PopAsync();
|
2016-05-08 06:11:47 +03:00
|
|
|
|
_userDialogs.SuccessToast(nameEntry.Text, "New site created.");
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}, ToolbarItemOrder.Default, 0);
|
|
|
|
|
|
2016-05-07 20:42:09 +03:00
|
|
|
|
Title = AppResources.AddSite;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
Content = scrollView;
|
|
|
|
|
ToolbarItems.Add(saveToolBarItem);
|
2016-05-07 01:49:01 +03:00
|
|
|
|
|
2016-05-08 06:11:47 +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-07 20:42:09 +03:00
|
|
|
|
DisplayAlert(AppResources.InternetConnectionRequiredTitle, AppResources.InternetConnectionRequiredMessage, AppResources.Ok);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-07 01:49:01 +03:00
|
|
|
|
|