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

198 lines
7.2 KiB
C#
Raw Normal View History

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;
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 : ContentPage
{
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()
{
2016-05-12 00:30:09 +03:00
var folders = _folderService.GetAllAsync().GetAwaiter().GetResult().OrderBy(f => f.Name?.Decrypt());
2016-05-02 09:52:09 +03:00
2016-05-12 00:30:09 +03:00
var uriEntry = new ExtendedEntry { Keyboard = Keyboard.Url, HasBorder = false };
var nameEntry = new ExtendedEntry { HasBorder = false };
var folderPicker = new ExtendedPicker { Title = AppResources.Folder, HasBorder = false };
folderPicker.Items.Add(AppResources.FolderNone);
folderPicker.SelectedIndex = 0;
2016-05-12 00:30:09 +03:00
foreach(var folder in folders)
{
2016-05-12 00:30:09 +03:00
folderPicker.Items.Add(folder.Name.Decrypt());
}
2016-05-12 00:30:09 +03:00
var usernameEntry = new ExtendedEntry { HasBorder = false };
var passwordEntry = new ExtendedEntry { IsPassword = true, HasBorder = false };
var notesEditor = new ExtendedEditor { HeightRequest = Device.OS == TargetPlatform.iOS ? 70 : 90, HasBorder = false };
2016-05-12 00:30:09 +03:00
var uriStackLayout = new FormEntryStackLayout();
uriStackLayout.Children.Add(new EntryLabel { Text = AppResources.URI });
uriStackLayout.Children.Add(uriEntry);
var uriCell = new ViewCell();
uriCell.View = uriStackLayout;
var nameStackLayout = new FormEntryStackLayout();
nameStackLayout.Children.Add(new EntryLabel { Text = AppResources.Name });
nameStackLayout.Children.Add(nameEntry);
var nameCell = new ViewCell();
nameCell.View = nameStackLayout;
var folderStackLayout = new FormEntryStackLayout();
folderStackLayout.Children.Add(new EntryLabel { Text = AppResources.Folder });
folderStackLayout.Children.Add(folderPicker);
var folderCell = new ViewCell();
folderCell.View = folderStackLayout;
var usernameStackLayout = new FormEntryStackLayout();
usernameStackLayout.Children.Add(new EntryLabel { Text = AppResources.Username });
usernameStackLayout.Children.Add(usernameEntry);
var usernameCell = new ViewCell();
usernameCell.View = usernameStackLayout;
var passwordStackLayout = new FormEntryStackLayout();
passwordStackLayout.Children.Add(new EntryLabel { Text = AppResources.Password });
passwordStackLayout.Children.Add(passwordEntry);
var passwordCell = new ViewCell();
passwordCell.View = passwordStackLayout;
var notesStackLayout = new FormEntryStackLayout();
notesStackLayout.Children.Add(notesEditor);
var notesCell = new ViewCell();
notesCell.View = notesStackLayout;
var mainTable = new ExtendedTableView
2016-05-12 00:30:09 +03:00
{
Intent = TableIntent.Settings,
2016-05-12 00:30:09 +03:00
HasUnevenRows = true,
Root = new TableRoot
{
new TableSection
{
uriCell,
nameCell,
folderCell,
usernameCell,
passwordCell
}
}
};
var notesTable = new ExtendedTableView
{
Intent = TableIntent.Settings,
HasUnevenRows = true,
Root = new TableRoot
{
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)
{
mainTable.RowHeight = 70;
notesTable.RowHeight = 90;
}
var tablesStackLayout = new StackLayout();
tablesStackLayout.Children.Add(mainTable);
tablesStackLayout.Children.Add(notesTable);
2016-05-02 09:52:09 +03:00
var scrollView = new ScrollView
{
Content = tablesStackLayout,
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;
}
if(string.IsNullOrWhiteSpace(uriEntry.Text))
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.URI), AppResources.Ok);
return;
}
if(string.IsNullOrWhiteSpace(nameEntry.Text))
{
await DisplayAlert(AppResources.AnErrorHasOccurred, string.Format(AppResources.ValidationFieldRequired, AppResources.Name), AppResources.Ok);
return;
}
var site = new Site
{
Uri = uriEntry.Text.Encrypt(),
Name = nameEntry.Text.Encrypt(),
Username = usernameEntry.Text?.Encrypt(),
Password = passwordEntry.Text?.Encrypt(),
Notes = notesEditor.Text?.Encrypt(),
};
if(folderPicker.SelectedIndex > 0)
{
site.FolderId = folders.ElementAt(folderPicker.SelectedIndex - 1).Id;
}
var saveTask = _siteService.SaveAsync(site);
_userDialogs.ShowLoading("Saving...", MaskType.Black);
await saveTask;
_userDialogs.HideLoading();
await Navigation.PopAsync();
_userDialogs.SuccessToast(nameEntry.Text, "New site created.");
}, ToolbarItemOrder.Default, 0);
2016-05-02 09:52: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-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);
}
private class FormEntryStackLayout : StackLayout
{
public FormEntryStackLayout()
{
Padding = new Thickness(15, 15, 15, 0);
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