bitwarden-android/src/App/Pages/Vault/GroupingsPage/GroupingsPageViewModel.cs

155 lines
5.6 KiB
C#
Raw Normal View History

2019-04-19 23:45:16 +03:00
using Bit.App.Resources;
using Bit.Core.Abstractions;
using Bit.Core.Models.Domain;
using Bit.Core.Models.View;
2019-03-29 19:52:57 +03:00
using Bit.Core.Utilities;
2019-03-29 06:52:33 +03:00
using System.Collections.Generic;
2019-04-19 23:45:16 +03:00
using System.Linq;
2019-03-29 19:52:57 +03:00
using System.Threading.Tasks;
using Xamarin.Forms;
2019-03-29 06:52:33 +03:00
namespace Bit.App.Pages
{
2019-03-29 20:24:44 +03:00
public class GroupingsPageViewModel : BaseViewModel
2019-03-29 06:52:33 +03:00
{
2019-03-29 19:52:57 +03:00
private bool _loading = false;
2019-04-19 23:45:16 +03:00
private bool _loaded = false;
private List<CipherView> _allCiphers;
private readonly ICipherService _cipherService;
private readonly IFolderService _folderService;
private readonly ICollectionService _collectionService;
private readonly ISyncService _syncService;
2019-03-29 19:52:57 +03:00
2019-03-29 20:24:44 +03:00
public GroupingsPageViewModel()
2019-03-29 06:52:33 +03:00
{
2019-04-19 23:45:16 +03:00
_cipherService = ServiceContainer.Resolve<ICipherService>("cipherService");
_folderService = ServiceContainer.Resolve<IFolderService>("folderService");
_collectionService = ServiceContainer.Resolve<ICollectionService>("collectionService");
_syncService = ServiceContainer.Resolve<ISyncService>("syncService");
2019-03-29 06:52:33 +03:00
PageTitle = "My Vault";
2019-04-19 23:45:16 +03:00
GroupedItems = new ExtendedObservableCollection<GroupingsPageListGroup>();
2019-03-29 19:52:57 +03:00
LoadCommand = new Command(async () => await LoadAsync());
}
2019-04-19 23:45:16 +03:00
public bool ShowFavorites { get; set; } = true;
public bool ShowFolders { get; set; } = true;
public bool ShowCollections { get; set; } = true;
public List<CipherView> Ciphers { get; set; }
public List<CipherView> FavoriteCiphers { get; set; }
public List<CipherView> NoFolderCiphers { get; set; }
public List<FolderView> Folders { get; set; }
public List<TreeNode<FolderView>> NestedFolders { get; set; }
public List<Core.Models.View.CollectionView> Collections { get; set; }
public List<TreeNode<Core.Models.View.CollectionView>> NestedCollections { get; set; }
2019-03-29 19:52:57 +03:00
public bool Loading
{
get => _loading;
set => SetProperty(ref _loading, value);
}
2019-04-19 23:45:16 +03:00
public bool Loaded
2019-03-29 19:52:57 +03:00
{
2019-04-19 23:45:16 +03:00
get => _loaded;
set
2019-03-29 19:52:57 +03:00
{
2019-04-19 23:45:16 +03:00
SetProperty(ref _loaded, value);
SetProperty(ref _loading, !value);
2019-03-29 19:52:57 +03:00
}
2019-04-19 23:45:16 +03:00
}
public ExtendedObservableCollection<GroupingsPageListGroup> GroupedItems { get; set; }
public Command LoadCommand { get; set; }
2019-03-29 19:52:57 +03:00
2019-04-19 23:45:16 +03:00
public async Task LoadAsync()
{
2019-03-29 19:52:57 +03:00
try
{
2019-04-19 23:45:16 +03:00
await LoadFoldersAsync();
await LoadCollectionsAsync();
await LoadCiphersAsync();
var favListItems = FavoriteCiphers?.Select(c => new GroupingsPageListItem { Cipher = c }).ToList();
var folderListItems = NestedFolders?.Select(f => new GroupingsPageListItem { Folder = f.Node }).ToList();
var collectionListItems = NestedCollections?.Select(c =>
new GroupingsPageListItem { Collection = c.Node }).ToList();
var groupedItems = new List<GroupingsPageListGroup>();
if(favListItems?.Any() ?? false)
2019-03-29 19:52:57 +03:00
{
2019-04-23 00:08:37 +03:00
groupedItems.Add(new GroupingsPageListGroup(favListItems, AppResources.Favorites,
Device.RuntimePlatform == Device.iOS));
2019-04-19 23:45:16 +03:00
}
if(folderListItems?.Any() ?? false)
{
2019-04-23 00:08:37 +03:00
groupedItems.Add(new GroupingsPageListGroup(folderListItems, AppResources.Folders,
Device.RuntimePlatform == Device.iOS));
2019-04-19 23:45:16 +03:00
}
if(collectionListItems?.Any() ?? false)
{
2019-04-23 00:08:37 +03:00
groupedItems.Add(new GroupingsPageListGroup(collectionListItems, AppResources.Collections,
Device.RuntimePlatform == Device.iOS));
2019-04-19 23:45:16 +03:00
}
GroupedItems.ResetWithRange(groupedItems);
2019-03-29 19:52:57 +03:00
}
finally
{
2019-04-19 23:45:16 +03:00
Loaded = true;
}
}
2019-04-24 18:23:03 +03:00
public async Task SelectCipherAsync(CipherView cipher)
{
var page = new ViewPage(cipher.Id);
await Page.Navigation.PushModalAsync(new NavigationPage(page));
}
2019-04-19 23:45:16 +03:00
private async Task LoadFoldersAsync()
{
if(!ShowFolders)
{
return;
}
Folders = await _folderService.GetAllDecryptedAsync();
NestedFolders = await _folderService.GetAllNestedAsync();
}
private async Task LoadCollectionsAsync()
{
if(!ShowCollections)
{
return;
2019-03-29 19:52:57 +03:00
}
2019-04-19 23:45:16 +03:00
Collections = await _collectionService.GetAllDecryptedAsync();
NestedCollections = await _collectionService.GetAllNestedAsync(Collections);
}
2019-03-29 19:52:57 +03:00
2019-04-19 23:45:16 +03:00
private async Task LoadCiphersAsync()
{
_allCiphers = await _cipherService.GetAllDecryptedAsync();
Ciphers = _allCiphers;
foreach(var c in _allCiphers)
{
if(c.Favorite)
{
if(FavoriteCiphers == null)
{
FavoriteCiphers = new List<CipherView>();
}
FavoriteCiphers.Add(c);
}
if(c.FolderId == null)
{
if(NoFolderCiphers == null)
{
NoFolderCiphers = new List<CipherView>();
}
NoFolderCiphers.Add(c);
}
}
FavoriteCiphers = _allCiphers.Where(c => c.Favorite).ToList();
2019-03-29 06:52:33 +03:00
}
}
}