2020-06-10 13:38:04 +02:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-06-09 20:18:17 +02:00
|
|
|
|
using GalaSoft.MvvmLight.Command;
|
|
|
|
|
using GalaSoft.MvvmLight.Views;
|
2020-06-10 13:38:04 +02:00
|
|
|
|
using MediatR;
|
2020-06-09 20:18:17 +02:00
|
|
|
|
using ModernKeePass.Application.Common.Models;
|
2020-06-10 13:38:04 +02:00
|
|
|
|
using ModernKeePass.Application.Group.Models;
|
|
|
|
|
using ModernKeePass.Application.Group.Queries.GetGroup;
|
2020-06-09 20:18:17 +02:00
|
|
|
|
using ModernKeePass.Common;
|
2020-06-10 13:38:04 +02:00
|
|
|
|
using ModernKeePass.Domain.Enums;
|
2020-06-09 20:18:17 +02:00
|
|
|
|
using ModernKeePass.Models;
|
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.ViewModels
|
|
|
|
|
{
|
|
|
|
|
public class BreadcrumbControlVm
|
|
|
|
|
{
|
2020-06-10 13:38:04 +02:00
|
|
|
|
public ObservableCollection<BreadcrumbItem> BreadcrumbItems { get; }
|
|
|
|
|
public string ParentGroupName { get; private set; }
|
2020-06-26 01:16:44 +02:00
|
|
|
|
public Icon ParentGroupIcon { get; private set; } = Icon.Folder;
|
2020-06-09 20:18:17 +02:00
|
|
|
|
|
|
|
|
|
public RelayCommand GoBackCommand { get; }
|
2020-06-10 13:38:04 +02:00
|
|
|
|
public RelayCommand GoUpCommand { get; private set; }
|
2020-06-09 20:18:17 +02:00
|
|
|
|
public RelayCommand<int> GoToCommand { get; }
|
|
|
|
|
|
2020-06-10 13:38:04 +02:00
|
|
|
|
private readonly IMediator _mediator;
|
2020-06-09 20:18:17 +02:00
|
|
|
|
private readonly INavigationService _navigation;
|
|
|
|
|
|
2020-06-10 13:38:04 +02:00
|
|
|
|
public BreadcrumbControlVm(IMediator mediator, INavigationService navigation)
|
2020-06-09 20:18:17 +02:00
|
|
|
|
{
|
2020-06-10 13:38:04 +02:00
|
|
|
|
_mediator = mediator;
|
2020-06-09 20:18:17 +02:00
|
|
|
|
_navigation = navigation;
|
2020-06-10 13:38:04 +02:00
|
|
|
|
|
|
|
|
|
BreadcrumbItems = new ObservableCollection<BreadcrumbItem>();
|
|
|
|
|
GoBackCommand = new RelayCommand(() => _navigation.GoBack());
|
2020-06-09 20:18:17 +02:00
|
|
|
|
GoToCommand = new RelayCommand<int>(GoTo);
|
2020-06-26 01:16:44 +02:00
|
|
|
|
GoUpCommand = new RelayCommand(() => GoTo(BreadcrumbItems.Count - 1), () => !string.IsNullOrEmpty(ParentGroupName));
|
2020-06-09 20:18:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 13:38:04 +02:00
|
|
|
|
public async Task Initialize(GroupVm group)
|
2020-06-09 20:18:17 +02:00
|
|
|
|
{
|
2020-06-10 13:38:04 +02:00
|
|
|
|
if (group == null) return;
|
2020-06-26 01:16:44 +02:00
|
|
|
|
GoBackCommand.RaiseCanExecuteChanged();
|
2020-06-10 13:38:04 +02:00
|
|
|
|
ParentGroupName = group.Title;
|
|
|
|
|
ParentGroupIcon = group.Icon;
|
|
|
|
|
|
2020-06-26 01:16:44 +02:00
|
|
|
|
BreadcrumbItems.Add(new BreadcrumbItem { Path = group.Id, Name = group.Title, Icon = group.Icon });
|
2020-06-10 13:38:04 +02:00
|
|
|
|
var parentGroup = group;
|
|
|
|
|
while (!string.IsNullOrEmpty(parentGroup.ParentGroupId))
|
|
|
|
|
{
|
|
|
|
|
parentGroup = await _mediator.Send(new GetGroupQuery {Id = parentGroup.ParentGroupId});
|
2020-06-26 01:16:44 +02:00
|
|
|
|
BreadcrumbItems.Add(new BreadcrumbItem {Path = parentGroup.Id, Name = parentGroup.Title, Icon = parentGroup.Icon});
|
2020-06-10 13:38:04 +02:00
|
|
|
|
}
|
2020-06-09 20:18:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-10 13:38:04 +02:00
|
|
|
|
private void GoTo(int index)
|
2020-06-09 20:18:17 +02:00
|
|
|
|
{
|
2020-06-10 13:38:04 +02:00
|
|
|
|
if (BreadcrumbItems.Count == 0) return;
|
|
|
|
|
var breadcrumb = BreadcrumbItems[index];
|
|
|
|
|
_navigation.NavigateTo(Constants.Navigation.GroupPage, new NavigationItem { Id = breadcrumb.Path });
|
2020-06-09 20:18:17 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|