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,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Storage;
@@ -81,18 +82,17 @@ namespace ModernKeePass
/// <param name="args">Details about the launch request and process.</param>
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
OnLaunchOrActivated(args);
await OnLaunchOrActivated(args);
await HockeyClient.Current.SendCrashesAsync(/* sendWithoutAsking: true */);
}
protected override void OnActivated(IActivatedEventArgs args)
protected override async void OnActivated(IActivatedEventArgs args)
{
OnLaunchOrActivated(args);
await OnLaunchOrActivated(args);
}
private async void OnLaunchOrActivated(IActivatedEventArgs e)
private async Task OnLaunchOrActivated(IActivatedEventArgs e)
{
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
{

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Windows.Input;
using ModernKeePass.ViewModels;
namespace ModernKeePass.Interfaces
@@ -14,6 +15,14 @@ namespace ModernKeePass.Interfaces
bool IsEditMode { get; }
bool IsRecycleOnDelete { get; }
/// <summary>
/// Save changes to Model
/// </summary>
ICommand SaveCommand { get; }
/// <summary>
/// Restore ViewModel
/// </summary>
ICommand UndoDeleteCommand { get; }
/// <summary>
/// Move a entity to the destination group
/// </summary>
@@ -24,14 +33,6 @@ namespace ModernKeePass.Interfaces
/// </summary>
void CommitDelete();
/// <summary>
/// Restore ViewModel
/// </summary>
void UndoDelete();
/// <summary>
/// Save changes to Model
/// </summary>
void Save();
/// <summary>
/// Delete from ViewModel
/// </summary>
void MarkForDelete(string recycleBinTitle);

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();

View File

@@ -15,11 +15,11 @@
mc:Ignorable="d"
SizeChanged="EntryDetailPage_OnSizeChanged">
<Page.Resources>
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<converters:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter"/>
<converters:ProgressBarLegalValuesConverter x:Key="ProgressBarLegalValuesConverter"/>
<converters:DoubleToSolidColorBrushConverter x:Key="DoubleToForegroungBrushComplexityConverter"/>
<converters:NullToBooleanConverter x:Key="NullToBooleanConverter"/>
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<converters:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter" />
<converters:ProgressBarLegalValuesConverter x:Key="ProgressBarLegalValuesConverter" />
<converters:DoubleToSolidColorBrushConverter x:Key="DoubleToForegroungBrushComplexityConverter" />
<converters:NullToBooleanConverter x:Key="NullToBooleanConverter" />
<Style TargetType="PasswordBox" x:Name="PasswordBoxWithButtonStyle">
<Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}" />
<Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}" />
@@ -353,13 +353,7 @@
<CheckBox IsChecked="{Binding BracketsPatternSelected, Mode=TwoWay}" x:Uid="PasswordGeneratorBrackets" />
<TextBlock x:Uid="PasswordGeneratorAlso" Margin="0,5,0,0"/>
<TextBox Text="{Binding CustomChars, Mode=TwoWay}" />
<Button x:Uid="PasswordGeneratorButton">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:CallMethodAction TargetObject="{Binding}" MethodName="GeneratePassword"/>
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Button>
<Button x:Uid="PasswordGeneratorButton" Command="{Binding GeneratePasswordCommand}" />
</StackPanel>
</Flyout>
</Button.Flyout>
@@ -373,9 +367,9 @@
</Setter>
</Style>
</Page.Resources>
<!--<Page.DataContext>
<Page.DataContext>
<viewModels:EntryVm />
</Page.DataContext>-->
</Page.DataContext>
<Page.BottomAppBar>
<CommandBar x:Name="CommandBar" VerticalAlignment="Center">
<CommandBar.SecondaryCommands>
@@ -386,10 +380,9 @@
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</AppBarButton>
<AppBarButton Icon="Save" x:Uid="AppBarSave">
<AppBarButton Icon="Save" x:Uid="AppBarSave" Command="{Binding SaveCommand}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:CallMethodAction TargetObject="{Binding}" MethodName="Save"/>
<core:ChangePropertyAction TargetObject="{Binding ElementName=CommandBar}" PropertyName="IsOpen" Value="False" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
@@ -409,13 +402,7 @@
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</AppBarToggleButton>
<AppBarButton Icon="Undo" x:Uid="AppBarRestore" Visibility="{Binding ParentGroup.IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}" IsEnabled="{Binding PreviousGroup, Converter={StaticResource NullToBooleanConverter}}" Click="RestoreButton_Click">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:CallMethodAction MethodName="UndoDelete" TargetObject="{Binding}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</AppBarButton>
<AppBarButton Icon="Undo" x:Uid="AppBarRestore" Visibility="{Binding ParentGroup.IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}" IsEnabled="{Binding PreviousGroup, Converter={StaticResource NullToBooleanConverter}}" Click="RestoreButton_Click" Command="{Binding UndoDeleteCommand}" />
<AppBarButton Icon="Delete" x:Uid="AppBarDelete" Click="DeleteButton_Click" />
</CommandBar>
</Page.BottomAppBar>
@@ -434,7 +421,7 @@
<ColumnDefinition Width="50" x:Name="LeftListViewColumn" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<userControls:HamburgerMenuUserControl x:Name="HamburgerMenu" x:Uid="HistoryLeftListView" ItemsSource="{Binding History}" ResizeTarget="{Binding ElementName=LeftListViewColumn}" SelectionChanged="HamburgerMenuUserControl_OnSelectionChanged" />
<userControls:HamburgerMenuUserControl x:Uid="HistoryLeftListView" ItemsSource="{Binding History}" ResizeTarget="{Binding ElementName=LeftListViewColumn}" SelectionChanged="HamburgerMenuUserControl_OnSelectionChanged" />
<StackPanel x:Name="StackPanel" Grid.Column="1" Margin="20,0,0,0">
<StackPanel.Resources>
<Style TargetType="TextBlock">

View File

@@ -34,10 +34,9 @@
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</AppBarButton>
<AppBarButton Icon="Save" x:Uid="AppBarSave">
<AppBarButton Icon="Save" x:Uid="AppBarSave" Command="{Binding SaveCommand}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:CallMethodAction TargetObject="{Binding}" MethodName="Save"/>
<core:ChangePropertyAction TargetObject="{Binding ElementName=CommandBar}" PropertyName="IsOpen" Value="False" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
@@ -53,20 +52,8 @@
<AppBarButton Icon="Sort" x:Uid="AppBarSort" Visibility="{Binding IsEditMode, Converter={StaticResource BooleanToVisibilityConverter}}">
<Button.Flyout>
<MenuFlyout>
<MenuFlyoutItem x:Uid="AppBarSortEntries">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:CallMethodAction MethodName="SortEntries" TargetObject="{Binding}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</MenuFlyoutItem>
<MenuFlyoutItem x:Uid="AppBarSortGroups">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:CallMethodAction MethodName="SortGroups" TargetObject="{Binding}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</MenuFlyoutItem>
<MenuFlyoutItem x:Uid="AppBarSortEntries" Command="{Binding SortEntriesCommand}" />
<MenuFlyoutItem x:Uid="AppBarSortGroups" Command="{Binding SortGroupsCommand}" />
</MenuFlyout>
</Button.Flyout>
</AppBarButton>
@@ -77,13 +64,7 @@
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</AppBarToggleButton>
<AppBarButton Icon="Undo" x:Uid="AppBarRestore" Visibility="{Binding ShowRestore, Converter={StaticResource BooleanToVisibilityConverter}}" IsEnabled="{Binding PreviousGroup, Converter={StaticResource NullToBooleanConverter}}" Click="RestoreButton_Click">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:CallMethodAction MethodName="UndoDelete" TargetObject="{Binding}" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</AppBarButton>
<AppBarButton Icon="Undo" x:Uid="AppBarRestore" Visibility="{Binding ShowRestore, Converter={StaticResource BooleanToVisibilityConverter}}" IsEnabled="{Binding PreviousGroup, Converter={StaticResource NullToBooleanConverter}}" Click="RestoreButton_Click" Command="{Binding UndoDeleteCommand}" />
<AppBarButton Icon="Delete" x:Uid="AppBarDelete" IsEnabled="{Binding IsNotRoot}" Visibility="{Binding IsSelected, Converter={StaticResource InverseBooleanToVisibilityConverter}}" Click="DeleteButton_Click" />
</CommandBar>
</Page.BottomAppBar>

View File

@@ -22,16 +22,11 @@
<RowDefinition Height="40" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<HyperlinkButton Grid.Row="0" HorizontalAlignment="Right" Style="{StaticResource MainColorHyperlinkButton}" Foreground="{StaticResource MainColor}">
<HyperlinkButton Grid.Row="0" HorizontalAlignment="Right" Style="{StaticResource MainColorHyperlinkButton}" Foreground="{StaticResource MainColor}" Command="{Binding ClearAllCommand}">
<StackPanel Orientation="Horizontal">
<SymbolIcon Symbol="Cancel" />
<TextBlock x:Uid="RecentClear" VerticalAlignment="Center" Margin="10,0,0,0" />
</StackPanel>
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Click">
<core:CallMethodAction TargetObject="{Binding}" MethodName="ClearAll" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</HyperlinkButton>
<ListView Grid.Row="1"
ItemsSource="{Binding Source={StaticResource RecentItemsSource}}"