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

94 lines
3.4 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;
using Bit.App.Abstractions;
using Bit.App.Models;
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>();
var folders = folderService.GetAllAsync().GetAwaiter().GetResult().OrderBy(f => f.Name);
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 () =>
{
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-02 09:52:09 +03:00
await siteService.SaveAsync(site);
await Navigation.PopAsync();
}, ToolbarItemOrder.Default, 0);
Title = "Add Site";
Content = scrollView;
ToolbarItems.Add(saveToolBarItem);
}
}
}