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

154 lines
5.2 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 : 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()
{
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
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());
var notesCell = new FormEditorCell(height:90);
2016-05-12 00:30:09 +03:00
var mainTable = new ExtendedTableView
2016-05-12 00:30:09 +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
{
new TableSection("Site Information")
2016-05-12 00:30:09 +03:00
{
uriCell,
nameCell,
folderCell,
usernameCell,
passwordCell
2016-05-13 04:30:02 +03:00
},
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-13 04:30:02 +03:00
mainTable.RowHeight = -1;
mainTable.EstimatedRowHeight = 70;
}
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;
}
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;
}
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-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.PopAsync();
_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-02 09:52:09 +03:00
Content = scrollView;
2016-05-12 00:30:09 +03:00
ToolbarItems.Add(saveToolBarItem);
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);
}
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