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

153 lines
5.5 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
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;
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 : ExtendedContentPage
2016-05-02 09:52:09 +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()
{
_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();
}
private void Init()
{
var notesCell = new FormEditorCell(height: 90);
var passwordCell = new FormEntryCell(AppResources.Password, IsPassword: true, nextElement: notesCell.Editor);
var usernameCell = new FormEntryCell(AppResources.Username, nextElement: passwordCell.Entry);
usernameCell.Entry.DisableAutocapitalize = true;
usernameCell.Entry.Autocorrect = false;
usernameCell.Entry.FontFamily = passwordCell.Entry.FontFamily = "Courier";
var uriCell = new FormEntryCell(AppResources.URI, Keyboard.Url, nextElement: usernameCell.Entry);
var nameCell = new FormEntryCell(AppResources.Name, nextElement: uriCell.Entry);
2016-05-02 09:52:09 +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)
{
folderOptions.Add(folder.Name.Decrypt());
}
var folderCell = new FormPickerCell(AppResources.Folder, folderOptions.ToArray());
2016-06-15 05:36:37 +03:00
var favoriteCell = new ExtendedSwitchCell { Text = "Favorite" };
2016-05-24 06:48:34 +03:00
var table = new ExtendedTableView
2016-05-12 00:30:09 +03:00
{
Intent = TableIntent.Settings,
2016-05-24 06:48:34 +03:00
EnableScrolling = true,
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
{
new TableSection("Site Information")
2016-05-12 00:30:09 +03:00
{
nameCell,
2016-05-17 06:54:24 +03:00
uriCell,
2016-05-12 00:30:09 +03:00
usernameCell,
2016-05-17 06:54:24 +03:00
passwordCell,
folderCell
2016-05-13 04:30:02 +03:00
},
2016-06-15 05:36:37 +03:00
new TableSection
{
favoriteCell
},
new TableSection(AppResources.Notes)
{
2016-05-12 00:30:09 +03:00
notesCell
}
}
};
2016-05-02 09:52:09 +03:00
if(Device.OS == TargetPlatform.iOS)
{
2016-05-24 06:48:34 +03:00
table.RowHeight = -1;
table.EstimatedRowHeight = 70;
}
2016-05-12 00:30:09 +03:00
var saveToolBarItem = new ToolbarItem(AppResources.Save, null, async () =>
{
if(!_connectivity.IsConnected)
{
AlertNoConnection();
return;
}
if(string.IsNullOrWhiteSpace(passwordCell.Entry.Text))
2016-05-12 00:30:09 +03:00
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Password), AppResources.Ok);
2016-05-12 00:30:09 +03:00
return;
}
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
{
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-06-15 05:36:37 +03:00
Favorite = favoriteCell.On
2016-05-12 00:30:09 +03:00
};
if(folderCell.Picker.SelectedIndex > 0)
2016-05-12 00:30:09 +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.PopModalAsync();
_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
Title = AppResources.AddSite;
2016-05-24 06:48:34 +03:00
Content = table;
2016-05-12 00:30:09 +03:00
ToolbarItems.Add(saveToolBarItem);
if(Device.OS == TargetPlatform.iOS)
{
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();
}
}
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);
}
2016-05-02 09:52:09 +03:00
}
}
2016-05-07 01:49:01 +03:00