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

157 lines
5.4 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using System.Collections.ObjectModel;
using System.Linq;
2016-05-04 02:49:49 +03:00
using System.Threading.Tasks;
using Acr.UserDialogs;
2016-05-02 09:52:09 +03:00
using Bit.App.Abstractions;
using Bit.App.Models.View;
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 VaultListPage : ContentPage
{
private readonly IFolderService _folderService;
private readonly ISiteService _siteService;
2016-05-04 02:49:49 +03:00
private readonly IUserDialogs _userDialogs;
2016-05-02 09:52:09 +03:00
public VaultListPage()
{
_folderService = Resolver.Resolve<IFolderService>();
_siteService = Resolver.Resolve<ISiteService>();
2016-05-04 02:49:49 +03:00
_userDialogs = Resolver.Resolve<IUserDialogs>();
2016-05-02 09:52:09 +03:00
2016-05-04 02:49:49 +03:00
Init();
}
2016-05-04 02:49:49 +03:00
public ObservableCollection<VaultView.Folder> Folders { get; private set; } = new ObservableCollection<VaultView.Folder>();
2016-05-02 09:52:09 +03:00
2016-05-04 02:49:49 +03:00
private void Init()
{
ToolbarItems.Add(new AddSiteToolBarItem(this));
2016-05-02 09:52:09 +03:00
2016-05-04 02:49:49 +03:00
var moreAction = new MenuItem { Text = "More" };
moreAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
moreAction.Clicked += MoreClickedAsync;
var deleteAction = new MenuItem { Text = "Delete", IsDestructive = true };
deleteAction.SetBinding(MenuItem.CommandParameterProperty, new Binding("."));
deleteAction.Clicked += DeleteClickedAsync;
var listView = new ListView { IsGroupingEnabled = true, ItemsSource = Folders };
listView.GroupDisplayBinding = new Binding("Name");
listView.ItemSelected += SiteSelected;
listView.ItemTemplate = new DataTemplate(() => new VaultListViewCell(moreAction, deleteAction));
2016-05-02 09:52:09 +03:00
Title = "My Vault";
2016-05-04 02:49:49 +03:00
Content = listView;
2016-05-02 09:52:09 +03:00
}
protected override void OnAppearing()
{
base.OnAppearing();
2016-05-04 02:49:49 +03:00
LoadFoldersAsync().Wait();
}
2016-05-02 09:52:09 +03:00
2016-05-04 02:49:49 +03:00
private async Task LoadFoldersAsync()
{
Folders.Clear();
var folders = await _folderService.GetAllAsync();
var sites = await _siteService.GetAllAsync();
foreach(var folder in folders)
{
var f = new VaultView.Folder(folder, sites.Where(s => s.FolderId == folder.Id));
Folders.Add(f);
}
2016-05-02 09:52:09 +03:00
// add the sites with no folder
2016-05-04 02:49:49 +03:00
var noneFolder = new VaultView.Folder(sites.Where(s => s.FolderId == null));
Folders.Add(noneFolder);
2016-05-02 09:52:09 +03:00
}
2016-05-04 02:49:49 +03:00
private void SiteSelected(object sender, SelectedItemChangedEventArgs e)
2016-05-02 09:52:09 +03:00
{
}
2016-05-04 02:49:49 +03:00
private async void MoreClickedAsync(object sender, EventArgs e)
2016-05-02 09:52:09 +03:00
{
2016-05-04 02:49:49 +03:00
var mi = sender as MenuItem;
var site = mi.CommandParameter as VaultView.Site;
var selection = await DisplayActionSheet("More Options", "Cancel", null, "View", "Edit", "Copy Password", "Copy Username", "Go To Website");
2016-05-02 09:52:09 +03:00
2016-05-04 02:49:49 +03:00
switch(selection)
{
case "View":
case "Edit":
case "Copy Password":
case "Copy Username":
case "Go To Website":
default:
break;
}
}
private async void DeleteClickedAsync(object sender, EventArgs e)
{
var mi = sender as MenuItem;
var site = mi.CommandParameter as VaultView.Site;
var deleteCall = await _siteService.DeleteAsync(site.Id);
if(deleteCall.Succeeded)
{
var folder = Folders.Single(f => f.Id == site.FolderId);
var siteIndex = folder.Select((s, i) => new { s, i }).First(s => s.s.Id == site.Id).i;
folder.RemoveAt(siteIndex);
_userDialogs.SuccessToast("Site deleted.");
}
else if(deleteCall.Errors.Count() > 0)
{
await DisplayAlert("An error has occurred", deleteCall.Errors.First().Message, "Ok");
}
}
private class AddSiteToolBarItem : ToolbarItem
{
private readonly VaultListPage _page;
public AddSiteToolBarItem(VaultListPage page)
{
_page = page;
Text = "Add";
Icon = "";
Clicked += ClickedItem;
}
private async void ClickedItem(object sender, EventArgs e)
{
var selection = await _page.DisplayActionSheet("Add", "Cancel", null, "Add New Folder", "Add New Site");
if(selection == "Add New Folder")
{
var addFolderPage = new VaultAddFolderPage();
await _page.Navigation.PushAsync(addFolderPage);
}
else if(selection == "Add New Site")
{
var addSitePage = new VaultAddSitePage();
await _page.Navigation.PushAsync(addSitePage);
}
}
}
private class VaultListViewCell : TextCell
{
public VaultListViewCell(MenuItem moreMenuItem, MenuItem deleteMenuItem)
{
this.SetBinding<VaultView.Site>(TextProperty, s => s.Name);
this.SetBinding<VaultView.Site>(DetailProperty, s => s.Username);
ContextActions.Add(moreMenuItem);
ContextActions.Add(deleteMenuItem);
}
2016-05-02 09:52:09 +03:00
}
}
}