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-04-02 19:12:16 +02:00
|
|
|
|
using ModernKeePass.Application.Entry.Models;
|
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;
|
2020-04-02 19:12:16 +02:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.DeleteGroup;
|
2020-03-27 13:27:29 +01:00
|
|
|
|
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;
|
2020-04-02 19:12:16 +02:00
|
|
|
|
using ModernKeePass.Application.Group.Models;
|
|
|
|
|
using ModernKeePass.Application.Group.Queries.GetGroup;
|
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-04-01 12:48:36 +02:00
|
|
|
|
public class GroupDetailVm : NotifyPropertyChangedBase, IVmEntity, ISelectableModel
|
2017-09-12 18:20:32 +02:00
|
|
|
|
{
|
2020-04-02 19:12:16 +02:00
|
|
|
|
public ObservableCollection<EntryVm> Entries => new ObservableCollection<EntryVm>(_group.Entries);
|
2017-12-04 18:07:03 +01:00
|
|
|
|
|
2020-04-02 19:12:16 +02:00
|
|
|
|
public ObservableCollection<GroupVm> Groups => new ObservableCollection<GroupVm>(_group.SubGroups);
|
2018-06-08 12:08:06 +02:00
|
|
|
|
|
2020-04-02 19:12:16 +02:00
|
|
|
|
public IEnumerable<EntryVm> SubEntries
|
2018-06-08 12:08:06 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-04-02 19:12:16 +02:00
|
|
|
|
var subEntries = new List<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
|
|
|
|
}
|
|
|
|
|
|
2020-04-02 19:12:16 +02:00
|
|
|
|
public bool IsNotRoot => _database.RootGroupId != _group.Id;
|
2018-06-08 12:08:06 +02:00
|
|
|
|
|
2020-04-02 19:12:16 +02:00
|
|
|
|
public bool ShowRestore => IsNotRoot && _database.RecycleBinId != _group.Id;
|
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-04-02 19:12:16 +02:00
|
|
|
|
return _database.IsRecycleBinEnabled && _database.RecycleBinId == _group.Id;
|
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-04-02 19:12:16 +02:00
|
|
|
|
if (value && _group != null) _database.RecycleBinId = _group.Id;
|
2017-10-06 16:21:12 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-02 19:12:16 +02:00
|
|
|
|
public IOrderedEnumerable<IGrouping<char, 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-04-02 19:12:16 +02:00
|
|
|
|
public IEnumerable<GroupVm> BreadCrumb { get; }
|
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 IMediator _mediator;
|
2020-03-30 19:43:04 +02:00
|
|
|
|
private readonly DatabaseVm _database;
|
2020-04-02 19:12:16 +02:00
|
|
|
|
private readonly GroupVm _group;
|
|
|
|
|
private readonly GroupVm _parent;
|
2017-10-06 14:56:16 +02:00
|
|
|
|
private bool _isEditMode;
|
2020-04-02 19:12:16 +02:00
|
|
|
|
private EntryVm _reorderedEntry;
|
2017-12-06 18:29:19 +01:00
|
|
|
|
private bool _isMenuClosed = true;
|
2017-10-05 14:50:42 +02:00
|
|
|
|
|
2020-04-01 12:48:36 +02:00
|
|
|
|
public GroupDetailVm() {}
|
2017-12-01 17:59:38 +01:00
|
|
|
|
|
2020-04-02 19:12:16 +02:00
|
|
|
|
internal GroupDetailVm(string groupId) : this(groupId, App.Mediator)
|
2017-11-23 15:26:57 +01:00
|
|
|
|
{ }
|
|
|
|
|
|
2020-04-02 19:12:16 +02:00
|
|
|
|
public GroupDetailVm(string groupId, IMediator mediator, bool isEditMode = false)
|
2017-09-13 18:37:44 +02:00
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
_mediator = mediator;
|
2020-03-30 19:43:04 +02:00
|
|
|
|
_database = _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult();
|
2020-04-02 19:12:16 +02:00
|
|
|
|
_group = _mediator.Send(new GetGroupQuery { Id = groupId }).GetAwaiter().GetResult();
|
|
|
|
|
if (!string.IsNullOrEmpty(_group.ParentGroupId))
|
|
|
|
|
{
|
|
|
|
|
_parent = _mediator.Send(new GetGroupQuery { Id = _group.ParentGroupId }).GetAwaiter().GetResult();
|
|
|
|
|
BreadCrumb = new List<GroupVm> {_parent};
|
|
|
|
|
}
|
2020-03-30 19:43:04 +02:00
|
|
|
|
_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-04-02 19:12:16 +02:00
|
|
|
|
UndoDeleteCommand = new RelayCommand(async () => await Move(_parent), () => _parent != null);
|
2020-03-30 19:43:04 +02:00
|
|
|
|
|
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)
|
|
|
|
|
{
|
2020-04-01 12:48:36 +02:00
|
|
|
|
var entry = ((EntryDetailVm) e.NewItems[0]).GetEntry();
|
2020-03-27 13:27:29 +01:00
|
|
|
|
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-04-02 19:12:16 +02:00
|
|
|
|
public async Task<string> AddNewGroup(string name = "")
|
2017-09-18 18:40:43 +02:00
|
|
|
|
{
|
2020-04-02 19:12:16 +02:00
|
|
|
|
return (await _mediator.Send(new CreateGroupCommand {Name = name, ParentGroup = _group})).Id;
|
2017-09-18 18:40:43 +02:00
|
|
|
|
}
|
2017-09-25 18:34:27 +02:00
|
|
|
|
|
2020-04-02 19:12:16 +02:00
|
|
|
|
public async Task<string> AddNewEntry()
|
2017-09-25 18:34:27 +02:00
|
|
|
|
{
|
2020-04-02 19:12:16 +02:00
|
|
|
|
return (await _mediator.Send(new CreateEntryCommand { ParentGroup = _group })).Id;
|
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-04-02 19:12:16 +02:00
|
|
|
|
await _mediator.Send(new DeleteGroupCommand { GroupId = _group.Id, ParentGroupId = _group.ParentGroupId, RecycleBinName = recycleBinTitle });
|
|
|
|
|
|
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-04-02 19:12:16 +02:00
|
|
|
|
public async Task Move(GroupVm destination)
|
2017-10-31 18:49:18 +01:00
|
|
|
|
{
|
2020-04-02 19:12:16 +02:00
|
|
|
|
await _mediator.Send(new AddGroupCommand {ParentGroup = destination, Group = _group });
|
|
|
|
|
await _mediator.Send(new RemoveGroupCommand {ParentGroup = _parent, 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
|
|
|
|
}
|
|
|
|
|
}
|