2020-04-14 17:49:29 +02:00
|
|
|
|
using System;
|
|
|
|
|
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;
|
2020-03-30 19:43:04 +02:00
|
|
|
|
using Windows.UI.Xaml.Controls;
|
2020-04-21 13:33:15 +02:00
|
|
|
|
using GalaSoft.MvvmLight.Views;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
using MediatR;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-04-21 19:12:26 +02:00
|
|
|
|
using ModernKeePass.Application.Common.Interfaces;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
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-04-09 19:43:06 +02:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.MoveEntry;
|
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-16 17:16:03 +02:00
|
|
|
|
using ModernKeePass.Application.Group.Commands.UpdateGroup;
|
2020-04-02 19:12:16 +02:00
|
|
|
|
using ModernKeePass.Application.Group.Models;
|
|
|
|
|
using ModernKeePass.Application.Group.Queries.GetGroup;
|
2020-04-15 12:02:30 +02:00
|
|
|
|
using ModernKeePass.Application.Group.Queries.SearchEntries;
|
2020-04-21 13:33:15 +02:00
|
|
|
|
using ModernKeePass.Common;
|
2020-04-06 20:20:45 +02:00
|
|
|
|
using ModernKeePass.Domain.AOP;
|
2020-03-26 12:25:22 +01:00
|
|
|
|
using ModernKeePass.Domain.Enums;
|
2017-10-17 18:46:05 +02:00
|
|
|
|
using ModernKeePass.Interfaces;
|
2020-04-21 13:33:15 +02:00
|
|
|
|
using ModernKeePass.Models;
|
|
|
|
|
using RelayCommand = GalaSoft.MvvmLight.Command.RelayCommand;
|
2017-09-12 18:20:32 +02:00
|
|
|
|
|
|
|
|
|
namespace ModernKeePass.ViewModels
|
|
|
|
|
{
|
2020-04-14 13:44:07 +02:00
|
|
|
|
public class GroupDetailVm : NotifyPropertyChangedBase, IVmEntity
|
2017-09-12 18:20:32 +02:00
|
|
|
|
{
|
2020-04-09 19:43:06 +02:00
|
|
|
|
public ObservableCollection<EntryVm> Entries { get; }
|
2017-12-04 18:07:03 +01:00
|
|
|
|
|
2020-04-09 19:43:06 +02:00
|
|
|
|
public ObservableCollection<GroupVm> Groups { get; }
|
2020-04-15 12:02:30 +02:00
|
|
|
|
|
2020-04-14 13:44:07 +02:00
|
|
|
|
public bool IsNotRoot => Database.RootGroupId != _group.Id;
|
2020-04-16 17:16:03 +02: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-04-16 19:43:17 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_mediator.Send(new UpdateGroupCommand {Group = _group, Title = value, Icon = _group.Icon}).Wait();
|
2020-04-20 20:02:43 +02:00
|
|
|
|
SaveCommand.RaiseCanExecuteChanged();
|
2020-04-16 19:43:17 +02:00
|
|
|
|
}
|
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-04-14 17:49:29 +02:00
|
|
|
|
get { return (Symbol) Enum.Parse(typeof(Symbol), _group.Icon.ToString()); }
|
2020-04-16 19:43:17 +02:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_mediator.Send(new UpdateGroupCommand { Group = _group, Title = _group.Title, Icon = (Icon)Enum.Parse(typeof(Icon), value.ToString()) }).Wait();
|
2020-04-20 20:02:43 +02:00
|
|
|
|
SaveCommand.RaiseCanExecuteChanged();
|
2020-04-16 19:43:17 +02:00
|
|
|
|
}
|
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);
|
2020-04-20 20:02:43 +02:00
|
|
|
|
SortEntriesCommand.RaiseCanExecuteChanged();
|
|
|
|
|
SortGroupsCommand.RaiseCanExecuteChanged();
|
2018-08-02 17:40:30 +02:00
|
|
|
|
}
|
2017-10-06 14:56:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 13:33:15 +02:00
|
|
|
|
public bool IsRecycleOnDelete => Database.IsRecycleBinEnabled && !IsInRecycleBin;
|
|
|
|
|
|
|
|
|
|
public bool IsInRecycleBin => _parent != null && _parent.Id == Database.RecycleBinId;
|
|
|
|
|
|
2020-04-02 19:12:16 +02:00
|
|
|
|
public IEnumerable<GroupVm> BreadCrumb { get; }
|
2018-06-20 18:41:56 +02:00
|
|
|
|
|
2020-04-20 20:02:43 +02:00
|
|
|
|
public RelayCommand SaveCommand { get; }
|
|
|
|
|
public RelayCommand SortEntriesCommand { get; }
|
|
|
|
|
public RelayCommand SortGroupsCommand { get; }
|
|
|
|
|
public RelayCommand MoveCommand { get; }
|
2020-04-21 13:33:15 +02:00
|
|
|
|
public RelayCommand CreateEntryCommand { get; }
|
|
|
|
|
public RelayCommand CreateGroupCommand { get; }
|
2020-04-21 19:12:26 +02:00
|
|
|
|
public RelayCommand DeleteCommand { get; set; }
|
|
|
|
|
public RelayCommand GoBackCommand { get; set; }
|
2020-04-14 13:44:07 +02:00
|
|
|
|
|
|
|
|
|
private DatabaseVm Database => _mediator.Send(new GetDatabaseQuery()).GetAwaiter().GetResult();
|
2018-06-20 18:41:56 +02:00
|
|
|
|
|
2020-03-26 12:25:22 +01:00
|
|
|
|
private readonly IMediator _mediator;
|
2020-04-21 19:12:26 +02:00
|
|
|
|
private readonly IResourceProxy _resource;
|
2020-04-21 13:33:15 +02:00
|
|
|
|
private readonly INavigationService _navigation;
|
2020-04-21 19:12:26 +02:00
|
|
|
|
private readonly IDialogService _dialog;
|
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-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-21 19:12:26 +02:00
|
|
|
|
internal GroupDetailVm(string groupId) : this(groupId,
|
|
|
|
|
App.Services.GetRequiredService<IMediator>(),
|
|
|
|
|
App.Services.GetRequiredService<IResourceProxy>(),
|
|
|
|
|
App.Services.GetRequiredService<INavigationService>(),
|
|
|
|
|
App.Services.GetRequiredService<IDialogService>())
|
2017-11-23 15:26:57 +01:00
|
|
|
|
{ }
|
|
|
|
|
|
2020-04-21 19:12:26 +02:00
|
|
|
|
public GroupDetailVm(string groupId, IMediator mediator, IResourceProxy resource, INavigationService navigation, IDialogService dialog)
|
2017-09-13 18:37:44 +02:00
|
|
|
|
{
|
2020-03-26 12:25:22 +01:00
|
|
|
|
_mediator = mediator;
|
2020-04-21 19:12:26 +02:00
|
|
|
|
_resource = resource;
|
2020-04-21 13:33:15 +02:00
|
|
|
|
_navigation = navigation;
|
2020-04-21 19:12:26 +02:00
|
|
|
|
_dialog = dialog;
|
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};
|
|
|
|
|
}
|
2017-10-30 18:34:38 +01:00
|
|
|
|
|
2020-04-14 13:44:07 +02:00
|
|
|
|
SaveCommand = new RelayCommand(async () => await SaveChanges(), () => Database.IsDirty);
|
|
|
|
|
SortEntriesCommand = new RelayCommand(async () => await SortEntriesAsync(), () => IsEditMode);
|
|
|
|
|
SortGroupsCommand = new RelayCommand(async () => await SortGroupsAsync(), () => IsEditMode);
|
|
|
|
|
MoveCommand = new RelayCommand(async () => await Move(_parent), () => IsNotRoot);
|
2020-04-21 13:39:53 +02:00
|
|
|
|
CreateEntryCommand = new RelayCommand(async () => await AddNewEntry(), () => !IsInRecycleBin && Database.RecycleBinId != Id);
|
|
|
|
|
CreateGroupCommand = new RelayCommand(async () => await AddNewGroup(), () => !IsInRecycleBin && Database.RecycleBinId != Id);
|
2020-04-21 19:12:26 +02:00
|
|
|
|
DeleteCommand = new RelayCommand(async () => await AskForDelete());
|
|
|
|
|
GoBackCommand = new RelayCommand(() => _navigation.GoBack());
|
|
|
|
|
|
2020-04-09 19:43:06 +02:00
|
|
|
|
Entries = new ObservableCollection<EntryVm>(_group.Entries);
|
2017-11-28 18:53:10 +01:00
|
|
|
|
Entries.CollectionChanged += Entries_CollectionChanged;
|
2020-04-09 19:43:06 +02:00
|
|
|
|
Groups = new ObservableCollection<GroupVm>(_group.SubGroups);
|
2017-09-13 18:37:44 +02:00
|
|
|
|
}
|
2020-04-21 19:12:26 +02:00
|
|
|
|
|
|
|
|
|
private async Task AskForDelete()
|
|
|
|
|
{
|
|
|
|
|
var message = IsRecycleOnDelete
|
|
|
|
|
? _resource.GetResourceValue("GroupRecyclingConfirmation")
|
|
|
|
|
: _resource.GetResourceValue("GroupDeletingConfirmation");
|
|
|
|
|
|
|
|
|
|
await _dialog.ShowMessage(message, _resource.GetResourceValue("EntityDeleteTitle"),
|
|
|
|
|
_resource.GetResourceValue("EntityDeleteActionButton"),
|
|
|
|
|
_resource.GetResourceValue("EntityDeleteCancelButton"),
|
|
|
|
|
async isOk =>
|
|
|
|
|
{
|
2020-04-22 11:58:06 +02:00
|
|
|
|
if (isOk)
|
|
|
|
|
{
|
|
|
|
|
var text = IsRecycleOnDelete
|
|
|
|
|
? _resource.GetResourceValue("GroupRecycled")
|
|
|
|
|
: _resource.GetResourceValue("GroupDeleted");
|
|
|
|
|
//ToastNotificationHelper.ShowMovedToast(Entity, resource.GetResourceValue("EntityDeleting"), text);
|
|
|
|
|
await _mediator.Send(new DeleteGroupCommand
|
|
|
|
|
{
|
|
|
|
|
GroupId = _group.Id, ParentGroupId = _group.ParentGroupId,
|
|
|
|
|
RecycleBinName = _resource.GetResourceValue("RecycleBinTitle")
|
|
|
|
|
});
|
|
|
|
|
_navigation.GoBack();
|
|
|
|
|
}
|
2020-04-21 19:12:26 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-04-21 13:33:15 +02:00
|
|
|
|
public async Task AddNewGroup(string name = "")
|
2020-04-14 13:44:07 +02:00
|
|
|
|
{
|
2020-04-21 13:33:15 +02:00
|
|
|
|
var group = await _mediator.Send(new CreateGroupCommand {Name = name, ParentGroup = _group});
|
|
|
|
|
_navigation.NavigateTo(Constants.Navigation.GroupPage, new NavigationItem
|
|
|
|
|
{
|
|
|
|
|
Id = group.Id,
|
|
|
|
|
IsNew = true
|
|
|
|
|
});
|
2020-04-14 13:44:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 13:33:15 +02:00
|
|
|
|
public async Task AddNewEntry()
|
2020-04-14 13:44:07 +02:00
|
|
|
|
{
|
2020-04-21 13:33:15 +02:00
|
|
|
|
var entry = await _mediator.Send(new CreateEntryCommand { ParentGroup = _group });
|
|
|
|
|
_navigation.NavigateTo(Constants.Navigation.EntryPage, new NavigationItem
|
|
|
|
|
{
|
|
|
|
|
Id = entry.Id,
|
|
|
|
|
IsNew = true
|
|
|
|
|
});
|
2020-04-14 13:44:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Move(GroupVm destination)
|
|
|
|
|
{
|
|
|
|
|
await _mediator.Send(new AddGroupCommand {ParentGroup = destination, Group = _group });
|
|
|
|
|
await _mediator.Send(new RemoveGroupCommand {ParentGroup = _parent, Group = _group });
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 12:02:30 +02:00
|
|
|
|
public async Task<IEnumerable<EntryVm>> Search(string queryText)
|
|
|
|
|
{
|
|
|
|
|
return await _mediator.Send(new SearchEntriesQuery {GroupId = Id, SearchText = queryText});
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-14 13:44:07 +02:00
|
|
|
|
private async Task SaveChanges()
|
|
|
|
|
{
|
|
|
|
|
await _mediator.Send(new SaveDatabaseCommand());
|
2020-04-20 20:02:43 +02:00
|
|
|
|
SaveCommand.RaiseCanExecuteChanged();
|
2020-04-14 13:44:07 +02: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];
|
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-03 17:33:53 +02:00
|
|
|
|
var entry = (EntryVm) e.NewItems[0];
|
2020-03-27 13:27:29 +01:00
|
|
|
|
await _mediator.Send(new AddEntryCommand {Entry = entry, ParentGroup = _group});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-09 19:43:06 +02:00
|
|
|
|
await _mediator.Send(new MoveEntryCommand {Entry = _reorderedEntry, ParentGroup = _group, Index = e.NewStartingIndex});
|
2020-03-27 13:27:29 +01:00
|
|
|
|
}
|
2017-11-28 18:53:10 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
2020-04-20 20:02:43 +02:00
|
|
|
|
SaveCommand.RaiseCanExecuteChanged();
|
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));
|
2020-04-20 20:02:43 +02:00
|
|
|
|
SaveCommand.RaiseCanExecuteChanged();
|
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));
|
2020-04-20 20:02:43 +02:00
|
|
|
|
SaveCommand.RaiseCanExecuteChanged();
|
2017-11-28 18:53:10 +01:00
|
|
|
|
}
|
2017-09-12 18:20:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|