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

120 lines
4.3 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
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 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
{
public VaultAddSitePage()
{
var cryptoService = Resolver.Resolve<ICryptoService>();
var siteService = Resolver.Resolve<ISiteService>();
var folderService = Resolver.Resolve<IFolderService>();
2016-05-03 09:08:50 +03:00
var userDialogs = Resolver.Resolve<IUserDialogs>();
2016-05-07 01:49:01 +03:00
var connectivity = Resolver.Resolve<IConnectivity>();
2016-05-04 02:49:49 +03:00
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();
var folderPicker = new Picker { Title = "Folder" };
folderPicker.Items.Add("(none)");
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();
stackLayout.Children.Add(new Label { Text = "URI" });
stackLayout.Children.Add(uriEntry);
stackLayout.Children.Add(new Label { Text = "Name" });
stackLayout.Children.Add(nameEntry);
stackLayout.Children.Add(new Label { Text = "Folder" });
stackLayout.Children.Add(folderPicker);
2016-05-02 09:52:09 +03:00
stackLayout.Children.Add(new Label { Text = "Username" });
stackLayout.Children.Add(usernameEntry);
stackLayout.Children.Add(new Label { Text = "Password" });
stackLayout.Children.Add(passwordEntry);
stackLayout.Children.Add(new Label { Text = "Notes" });
stackLayout.Children.Add(notesEditor);
var scrollView = new ScrollView
{
Content = stackLayout,
Orientation = ScrollOrientation.Vertical
};
var saveToolBarItem = new ToolbarItem("Save", null, async () =>
{
2016-05-07 01:49:01 +03:00
if(!connectivity.IsConnected)
{
AlertNoConnection();
return;
}
2016-05-02 09:52:09 +03:00
if(string.IsNullOrWhiteSpace(uriEntry.Text))
{
await DisplayAlert("An error has occurred", "The Uri field is required.", "Ok");
return;
}
if(string.IsNullOrWhiteSpace(nameEntry.Text))
{
await DisplayAlert("An error has occurred", "The Name field is required.", "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(),
2016-05-02 09:52:09 +03:00
};
if(folderPicker.SelectedIndex > 0)
{
site.FolderId = folders.ElementAt(folderPicker.SelectedIndex - 1).Id;
}
2016-05-03 09:08:50 +03:00
var saveTask = siteService.SaveAsync(site);
userDialogs.ShowLoading("Saving...", MaskType.Black);
await saveTask;
userDialogs.HideLoading();
2016-05-02 09:52:09 +03:00
await Navigation.PopAsync();
2016-05-03 09:08:50 +03:00
userDialogs.SuccessToast(nameEntry.Text, "New site created.");
2016-05-02 09:52:09 +03:00
}, ToolbarItemOrder.Default, 0);
Title = "Add Site";
Content = scrollView;
ToolbarItems.Add(saveToolBarItem);
2016-05-07 01:49:01 +03:00
if(!connectivity.IsConnected)
{
AlertNoConnection();
}
}
public void AlertNoConnection()
{
DisplayAlert("No internet connection", "Adding a new folder required an internet connection. Please connect to the internet before continuing.", "Ok");
2016-05-02 09:52:09 +03:00
}
}
}
2016-05-07 01:49:01 +03:00