Replaced almost all CallMethodActions with RelayCommands (allows using async)

SonarQube bug correction
This commit is contained in:
BONNEVILLE Geoffroy
2018-06-20 18:41:56 +02:00
parent ca377a6684
commit 803dab0fb5
8 changed files with 87 additions and 108 deletions

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Input;
using ModernKeePass.Common;
using ModernKeePass.Interfaces;
using ModernKeePass.Services;
using ModernKeePassLib;
@@ -158,6 +160,10 @@ namespace ModernKeePass.ViewModels
}
}
public ICommand SaveCommand { get; }
public ICommand GeneratePasswordCommand { get; }
public ICommand UndoDeleteCommand { get; }
public event PropertyChangedEventHandler PropertyChanged;
private readonly PwEntry _pwEntry;
@@ -184,6 +190,10 @@ namespace ModernKeePass.ViewModels
_resource = resource;
_pwEntry = entry;
ParentGroup = parent;
SaveCommand = new RelayCommand(() => _database.Save());
GeneratePasswordCommand = new RelayCommand(GeneratePassword);
UndoDeleteCommand = new RelayCommand(() => Move(PreviousGroup));
}
public void GeneratePassword()
@@ -215,21 +225,6 @@ namespace ModernKeePass.ViewModels
NotifyPropertyChanged("PasswordComplexityIndicator");
}
private string GetEntryValue(string key)
{
return _pwEntry?.Strings.GetSafe(key).ReadString();
}
private void SetEntryValue(string key, string newValue)
{
if (!_isDirty)
{
_pwEntry.Touch(true);
_pwEntry?.CreateBackup(null);
}
_pwEntry?.Strings.Set(key, new ProtectedString(true, newValue));
_isDirty = true;
}
public void MarkForDelete(string recycleBinTitle)
{
@@ -237,12 +232,7 @@ namespace ModernKeePass.ViewModels
_database.CreateRecycleBin(recycleBinTitle);
Move(_database.RecycleBinEnabled && !ParentGroup.IsSelected ? _database.RecycleBin : null);
}
public void UndoDelete()
{
Move(PreviousGroup);
}
public void Move(GroupVm destination)
{
PreviousGroup = ParentGroup;
@@ -261,12 +251,7 @@ namespace ModernKeePass.ViewModels
_pwEntry.ParentGroup.Entries.Remove(_pwEntry);
if (!_database.RecycleBinEnabled || PreviousGroup.IsSelected) _database.AddDeletedItem(IdUuid);
}
public void Save()
{
_database.Save();
}
public PwEntry GetPwEntry()
{
return _pwEntry;
@@ -281,5 +266,20 @@ namespace ModernKeePass.ViewModels
return IsSelected ? _resource.GetResourceValue("EntryCurrent") : _pwEntry.LastModificationTime.ToString("g");
}
private string GetEntryValue(string key)
{
return _pwEntry?.Strings.GetSafe(key).ReadString();
}
private void SetEntryValue(string key, string newValue)
{
if (!_isDirty)
{
_pwEntry.Touch(true);
_pwEntry?.CreateBackup(null);
}
_pwEntry?.Strings.Set(key, new ProtectedString(true, newValue));
_isDirty = true;
}
}
}

View File

@@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using ModernKeePass.Common;
using ModernKeePass.Interfaces;
using ModernKeePass.Services;
@@ -106,7 +108,12 @@ namespace ModernKeePass.ViewModels
return groups;
}
}
public ICommand SaveCommand { get; }
public ICommand SortEntriesCommand { get; }
public ICommand SortGroupsCommand { get; }
public ICommand UndoDeleteCommand { get; }
private readonly PwGroup _pwGroup;
private readonly IDatabaseService _database;
private bool _isEditMode;
@@ -126,6 +133,13 @@ namespace ModernKeePass.ViewModels
_database = database;
ParentGroup = parent;
SaveCommand = new RelayCommand(() => _database.Save());
SortEntriesCommand = new RelayCommand(async () =>
await SortEntriesAsync().ConfigureAwait(false));
SortGroupsCommand = new RelayCommand(async () =>
await SortGroupsAsync().ConfigureAwait(false));
UndoDeleteCommand = new RelayCommand(() => Move(PreviousGroup));
if (recycleBinId != null && _pwGroup.Uuid.Equals(recycleBinId)) _database.RecycleBin = this;
Entries = new ObservableCollection<EntryVm>(pwGroup.Entries.Select(e => new EntryVm(e, this)));
Entries.CollectionChanged += Entries_CollectionChanged;
@@ -199,13 +213,13 @@ namespace ModernKeePass.ViewModels
if (_database.RecycleBinEnabled && !PreviousGroup.IsSelected) _database.RecycleBin._pwGroup.AddGroup(_pwGroup, true);
else _database.AddDeletedItem(IdUuid);
}
public void Save()
{
_database.Save();
}
public void SortEntries()
public override string ToString()
{
return Name;
}
private async Task SortEntriesAsync()
{
var comparer = new PwEntryComparer(PwDefs.TitleField, true, false);
try
@@ -215,11 +229,11 @@ namespace ModernKeePass.ViewModels
}
catch (Exception e)
{
MessageDialogHelper.ShowErrorDialog(e).Wait();
await MessageDialogHelper.ShowErrorDialog(e);
}
}
public void SortGroups()
private async Task SortGroupsAsync()
{
try
{
@@ -229,13 +243,9 @@ namespace ModernKeePass.ViewModels
}
catch (Exception e)
{
MessageDialogHelper.ShowErrorDialog(e).Wait();
await MessageDialogHelper.ShowErrorDialog(e);
}
}
public override string ToString()
{
return Name;
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System.Windows.Input;
using ModernKeePass.Common;
using ModernKeePass.Interfaces;
using ModernKeePass.Services;
@@ -34,6 +35,8 @@ namespace ModernKeePass.ViewModels
_selectedItem.IsSelected = true;
}
}
public ICommand ClearAllCommand { get; }
public RecentVm() : this (RecentService.Instance)
{ }
@@ -41,12 +44,14 @@ namespace ModernKeePass.ViewModels
public RecentVm(IRecentService recent)
{
_recent = recent;
ClearAllCommand = new RelayCommand(ClearAll);
RecentItems = _recent.GetAllFiles();
if (RecentItems.Count > 0)
SelectedItem = RecentItems[0] as RecentItemVm;
}
public void ClearAll()
private void ClearAll()
{
_recent.ClearAll();
RecentItems.Clear();