2020-03-27 13:27:29 +01:00
|
|
|
|
using System.Collections.Generic;
|
2017-11-28 18:53:10 +01:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Collections.Specialized;
|
2017-09-12 18:20:32 +02:00
|
|
|
|
using System.Linq;
|
2018-06-20 18:41:56 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Input;
|
2020-03-30 19:43:04 +02:00
|
|
|
|
using Windows.UI.Xaml.Controls;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
using MediatR;
|
|
|
|
|
using ModernKeePass.Application.Database.Commands.SaveDatabase;
|
2020-03-27 18:45:13 +01:00
|
|
|
|
using ModernKeePass.Application.Database.Models;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
using ModernKeePass.Application.Database.Queries.GetDatabase;
|
2020-03-27 13:27:29 +01:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.AddEntry;
|
2020-03-27 18:45:13 +01:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.AddGroup;
|
2020-03-27 13:27:29 +01:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.CreateEntry;
|
|
|
|
|
using ModernKeePass.Application.Group.Commands.CreateGroup;
|
|
|
|
|
using ModernKeePass.Application.Group.Commands.DeleteGroup;
|
|
|
|
|
using ModernKeePass.Application.Group.Commands.InsertEntry;
|
|
|
|
|
using ModernKeePass.Application.Group.Commands.RemoveEntry;
|
2020-03-27 18:45:13 +01:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.RemoveGroup;
|
2020-03-27 13:27:29 +01:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.SortEntries;
|
|
|
|
|
using ModernKeePass.Application.Group.Commands.SortGroups;
|
2017-10-03 16:06:49 +02:00
|
|
|
|
using ModernKeePass.Common;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
using ModernKeePass.Domain.Enums;
|
2020-03-28 16:13:17 +01:00
|
|
|
|
using ModernKeePass.Domain.Interfaces;
|
2017-10-17 18:46:05 +02:00
|
|
|
|
using ModernKeePass.Interfaces;
|
2017-09-12 18:20:32 +02:00
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.ViewModels
|
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public class GroupVm : NotifyPropertyChangedBase, IVmEntity, ISelectableModel
|
2017-09-12 18:20:32 +02:00
|
|
|
|
{
|
2020-03-30 19:43:04 +02:00
|
|
|
|
public ObservableCollection<Application.Entry.Models.EntryVm> Entries => new ObservableCollection<Application.Entry.Models.EntryVm>(_group.Entries);
|
2017-12-04 18:07:03 +01:00
|
|
|
|
|
2020-03-30 19:43:04 +02:00
|
|
|
|
public ObservableCollection<Application.Group.Models.GroupVm> Groups => new ObservableCollection<Application.Group.Models.GroupVm>(_group.SubGroups);
|
2018-06-08 12:08:06 +02:00
|
|
|
|
|
2020-03-30 19:43:04 +02:00
|
|
|
|
public IEnumerable<Application.Entry.Models.EntryVm> SubEntries
|
2018-06-08 12:08:06 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-03-30 19:43:04 +02:00
|
|
|
|
var subEntries = new List<Application.Entry.Models.EntryVm>();
|
2018-06-08 12:08:06 +02:00
|
|
|
|
subEntries.AddRange(Entries);
|
|
|
|
|
foreach (var group in Groups)
|
|
|
|
|
{
|
2020-03-30 19:43:04 +02:00
|
|
|
|
subEntries.AddRange(group.Entries);
|
2018-06-08 12:08:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return subEntries;
|
|
|
|
|
}
|
2017-12-04 18:07:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 18:29:19 +01:00
|
|
|
|
|
2020-03-30 19:43:04 +02:00
|
|
|
|
public bool IsNotRoot => _database.RootGroup != _group;
|
2018-06-08 12:08:06 +02:00
|
|
|
|
|
2020-03-30 19:43:04 +02:00
|
|
|
|
public bool ShowRestore => IsNotRoot && _database.RecycleBin != _group;
|
2018-06-15 18:07:44 +02:00
|
|
|
|
|
2017-10-30 18:34:38 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Is the Group the database Recycle Bin?
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsSelected
|
2017-10-06 16:21:12 -04:00
|
|
|
|
{
|
2020-03-27 18:45:13 +01:00
|
|
|
|
get
|
|
|
|
|
{
|
2020-03-30 19:43:04 +02:00
|
|
|
|
return _database.IsRecycleBinEnabled && _database.RecycleBin == _group;
|
2020-03-27 18:45:13 +01:00
|
|
|
|
}
|
2017-10-30 18:34:38 +01:00
|
|
|
|
set
|
2017-10-06 16:21:12 -04:00
|
|
|
|
{
|
2020-03-30 19:43:04 +02:00
|
|
|
|
if (value && _group != null) _database.RecycleBin = _group;
|
2017-10-06 16:21:12 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 19:43:04 +02:00
|
|
|
|
public IOrderedEnumerable<IGrouping<char, Application.Entry.Models.EntryVm>> EntriesZoomedOut => from e in Entries
|
2020-03-27 13:27:29 +01:00
|
|
|
|
group e by e.Title.ToUpper().FirstOrDefault() into grp
|
2017-10-30 18:34:38 +01:00
|
|
|
|
orderby grp.Key
|
|
|
|
|
select grp;
|
|
|
|
|
|
2020-03-30 19:43:04 +02:00
|
|
|
|
public string Id => _group.Id;
|
|
|
|
|
|
2020-03-27 13:27:29 +01:00
|
|
|
|
public string Title
|
2017-10-06 14:56:16 +02:00
|
|
|
|
{
|
2020-03-30 19:43:04 +02:00
|
|
|
|
get { return _group.Title; }
|
2020-03-26 12:25:22 +01:00
|
|
|
|
set { _group.Title = value; }
|
2017-10-06 14:56:16 +02:00
|
|
|
|
}
|
2017-10-06 16:21:12 -04:00
|
|
|
|
|
2020-03-30 19:43:04 +02:00
|
|
|
|
public Symbol Icon
|
2017-09-15 15:58:51 +02:00
|
|
|
|
{
|
2020-03-30 19:43:04 +02:00
|
|
|
|
get { return (Symbol) _group.Icon; }
|
2020-03-26 12:25:22 +01:00
|
|
|
|
set { _group.Icon = (Icon)value; }
|
2017-09-15 15:58:51 +02:00
|
|
|
|
}
|
2017-10-25 18:29:50 +02:00
|
|
|
|
|
2017-10-06 14:56:16 +02:00
|
|
|
|
public bool IsEditMode
|
|
|
|
|
{
|
|
|
|
|
get { return _isEditMode; }
|
2018-08-02 17:40:30 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
SetProperty(ref _isEditMode, value);
|
|
|
|
|
((RelayCommand)SortEntriesCommand).RaiseCanExecuteChanged();
|
|
|
|
|
((RelayCommand)SortGroupsCommand).RaiseCanExecuteChanged();
|
|
|
|
|
}
|
2017-10-06 14:56:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 18:29:19 +01:00
|
|
|
|
public bool IsMenuClosed
|
|
|
|
|
{
|
|
|
|
|
get { return _isMenuClosed; }
|
|
|
|
|
set { SetProperty(ref _isMenuClosed, value); }
|
|
|
|
|
}
|
2018-06-08 18:46:07 +02:00
|
|
|
|
|
2020-03-31 19:19:02 +02:00
|
|
|
|
public IEnumerable<Application.Group.Models.GroupVm> BreadCrumb => _group.Breadcrumb;
|
2018-06-20 18:41:56 +02:00
|
|
|
|
|
|
|
|
|
public ICommand SaveCommand { get; }
|
|
|
|
|
public ICommand SortEntriesCommand { get; }
|
|
|
|
|
public ICommand SortGroupsCommand { get; }
|
|
|
|
|
public ICommand UndoDeleteCommand { get; }
|
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
private readonly Application.Group.Models.GroupVm _group;
|
|
|
|
|
private readonly IMediator _mediator;
|
2020-03-30 19:43:04 +02:00
|
|
|
|
private readonly DatabaseVm _database;
|
2017-10-06 14:56:16 +02:00
|
|
|
|
private bool _isEditMode;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
private Application.Entry.Models.EntryVm _reorderedEntry;
|
2017-12-06 18:29:19 +01:00
|
|
|
|
private bool _isMenuClosed = true;
|
2017-10-05 14:50:42 +02:00
|
|
|
|
|
2017-10-02 18:40:54 +02:00
|
|
|
|
public GroupVm() {}
|
2017-12-01 17:59:38 +01:00
|
|
|
|
|
2020-03-30 19:43:04 +02:00
|
|
|
|
internal GroupVm(Application.Group.Models.GroupVm group) : this(group, App.Mediator)
|
2017-11-23 15:26:57 +01:00
|
|
|
|
{ }
|
|
|
|
|
|
2020-03-30 19:43:04 +02:00
|
|
|
|
public GroupVm(Application.Group.Models.GroupVm group, IMediator mediator, bool isEditMode = false)
|
2017-09-13 18:37:44 +02:00
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
_group = group;
|
|
|
|
|
_mediator = mediator;
|
2020-03-30 19:43:04 +02:00
|
|
|
|
_database = _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult();
|
|
|
|
|
_isEditMode = isEditMode;
|
2017-10-30 18:34:38 +01:00
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
SaveCommand = new RelayCommand(async () => await _mediator.Send(new SaveDatabaseCommand()));
|
2018-06-20 18:41:56 +02:00
|
|
|
|
SortEntriesCommand = new RelayCommand(async () =>
|
2018-08-02 17:40:30 +02:00
|
|
|
|
await SortEntriesAsync().ConfigureAwait(false), () => IsEditMode);
|
2018-06-20 18:41:56 +02:00
|
|
|
|
SortGroupsCommand = new RelayCommand(async () =>
|
2018-08-02 17:40:30 +02:00
|
|
|
|
await SortGroupsAsync().ConfigureAwait(false), () => IsEditMode);
|
2020-03-30 19:43:04 +02:00
|
|
|
|
UndoDeleteCommand = new RelayCommand(async () => await Move(group.ParentGroup), () => _group.ParentGroup != null);
|
|
|
|
|
|
2017-11-28 18:53:10 +01:00
|
|
|
|
Entries.CollectionChanged += Entries_CollectionChanged;
|
2017-09-13 18:37:44 +02:00
|
|
|
|
}
|
2018-02-23 18:09:21 +01:00
|
|
|
|
|
2020-03-27 13:27:29 +01:00
|
|
|
|
private async void Entries_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
2017-11-28 18:53:10 +01:00
|
|
|
|
{
|
|
|
|
|
switch (e.Action)
|
|
|
|
|
{
|
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
2020-03-27 13:27:29 +01:00
|
|
|
|
var oldIndex = e.OldStartingIndex;
|
|
|
|
|
_reorderedEntry = _group.Entries[oldIndex];
|
|
|
|
|
await _mediator.Send(new RemoveEntryCommand {Entry = _reorderedEntry, ParentGroup = _group});
|
2017-11-28 18:53:10 +01:00
|
|
|
|
break;
|
|
|
|
|
case NotifyCollectionChangedAction.Add:
|
2020-03-27 13:27:29 +01:00
|
|
|
|
if (_reorderedEntry == null)
|
|
|
|
|
{
|
|
|
|
|
var entry = ((EntryVm) e.NewItems[0]).GetEntry();
|
|
|
|
|
await _mediator.Send(new AddEntryCommand {Entry = entry, ParentGroup = _group});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _mediator.Send(new InsertEntryCommand {Entry = _reorderedEntry, ParentGroup = _group, Index = e.NewStartingIndex});
|
|
|
|
|
}
|
2017-11-28 18:53:10 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-23 18:09:21 +01:00
|
|
|
|
|
2020-03-30 19:43:04 +02:00
|
|
|
|
public async Task<Application.Group.Models.GroupVm> AddNewGroup(string name = "")
|
2017-09-18 18:40:43 +02:00
|
|
|
|
{
|
2020-03-30 19:43:04 +02:00
|
|
|
|
return await _mediator.Send(new CreateGroupCommand {Name = name, ParentGroup = _group});
|
2017-09-18 18:40:43 +02:00
|
|
|
|
}
|
2017-09-25 18:34:27 +02:00
|
|
|
|
|
2020-03-30 19:43:04 +02:00
|
|
|
|
public async Task<Application.Entry.Models.EntryVm> AddNewEntry()
|
2017-09-25 18:34:27 +02:00
|
|
|
|
{
|
2020-03-30 19:43:04 +02:00
|
|
|
|
return await _mediator.Send(new CreateEntryCommand { ParentGroup = _group });
|
2017-10-02 18:40:54 +02:00
|
|
|
|
}
|
2017-10-31 12:14:26 +01:00
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public async Task MarkForDelete(string recycleBinTitle)
|
2017-10-17 18:46:05 +02:00
|
|
|
|
{
|
2020-03-30 19:43:04 +02:00
|
|
|
|
if (_database.IsRecycleBinEnabled && _database.RecycleBin == null)
|
|
|
|
|
await _mediator.Send(new CreateGroupCommand {ParentGroup = _database.RootGroup, IsRecycleBin = true, Name = recycleBinTitle});
|
|
|
|
|
await Move(_database.IsRecycleBinEnabled && !IsSelected ? _database.RecycleBin : null);
|
2018-08-02 17:40:30 +02:00
|
|
|
|
((RelayCommand)UndoDeleteCommand).RaiseCanExecuteChanged();
|
2017-10-17 18:46:05 +02:00
|
|
|
|
}
|
2018-06-04 18:38:48 +02:00
|
|
|
|
|
2020-03-30 19:43:04 +02:00
|
|
|
|
public async Task Move(Application.Group.Models.GroupVm destination)
|
2017-10-31 18:49:18 +01:00
|
|
|
|
{
|
2020-03-30 19:43:04 +02:00
|
|
|
|
await _mediator.Send(new RemoveGroupCommand {ParentGroup = _group.ParentGroup, Group = _group});
|
2017-10-31 18:49:18 +01:00
|
|
|
|
if (destination == null)
|
|
|
|
|
{
|
2020-03-27 18:45:13 +01:00
|
|
|
|
await _mediator.Send(new DeleteGroupCommand { Group = _group });
|
2017-10-31 18:49:18 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2020-03-30 19:43:04 +02:00
|
|
|
|
await _mediator.Send(new AddGroupCommand {ParentGroup = destination, Group = _group});
|
2017-10-31 18:49:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
public async Task CommitDelete()
|
2017-10-31 18:49:18 +01:00
|
|
|
|
{
|
2020-03-27 13:27:29 +01:00
|
|
|
|
await _mediator.Send(new DeleteGroupCommand { Group = _group });
|
2017-10-18 10:32:51 +02:00
|
|
|
|
}
|
2018-06-20 18:41:56 +02:00
|
|
|
|
|
|
|
|
|
private async Task SortEntriesAsync()
|
2017-11-28 18:53:10 +01:00
|
|
|
|
{
|
2020-03-27 13:27:29 +01:00
|
|
|
|
await _mediator.Send(new SortEntriesCommand {Group = _group});
|
2020-03-30 19:43:04 +02:00
|
|
|
|
OnPropertyChanged(nameof(Entries));
|
2017-11-30 11:05:47 +01:00
|
|
|
|
}
|
2018-06-04 18:38:48 +02:00
|
|
|
|
|
2018-06-20 18:41:56 +02:00
|
|
|
|
private async Task SortGroupsAsync()
|
2017-11-30 11:05:47 +01:00
|
|
|
|
{
|
2020-03-27 13:27:29 +01:00
|
|
|
|
await _mediator.Send(new SortGroupsCommand {Group = _group});
|
|
|
|
|
OnPropertyChanged(nameof(Groups));
|
2017-11-28 18:53:10 +01:00
|
|
|
|
}
|
2017-09-12 18:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|