Create database works with new Vm

Refactoring
This commit is contained in:
Geoffroy BONNEVILLE
2020-04-22 16:21:47 +02:00
parent a88051bc0c
commit a7da427ded
36 changed files with 371 additions and 274 deletions

View File

@@ -22,7 +22,7 @@ namespace ModernKeePass.Application.Common.Interfaces
Task Open(byte[] file, Credentials credentials);
Task ReOpen(byte[] file);
Task Create(Credentials credentials, string name, DatabaseVersion version = DatabaseVersion.V2);
Task Create(Credentials credentials, string name, DatabaseVersion version = DatabaseVersion.V3);
Task<byte[]> SaveDatabase();
Task<byte[]> SaveDatabase(byte[] newFileContents);
void UpdateCredentials(Credentials credentials);

View File

@@ -13,6 +13,7 @@ namespace ModernKeePass.Application.Database.Commands.CreateDatabase
public string Password { get; set; }
public string KeyFilePath { get; set; }
public string Name { get; set; }
public string Version { get; set; }
public bool CreateSampleData { get; set; }
public class CreateDatabaseCommandHandler : IAsyncRequestHandler<CreateDatabaseCommand>
@@ -30,12 +31,25 @@ namespace ModernKeePass.Application.Database.Commands.CreateDatabase
{
if (_database.IsOpen) throw new DatabaseOpenException();
var version = DatabaseVersion.V2;
switch (message.Version)
{
case "4":
version = DatabaseVersion.V4;
break;
case "3":
version = DatabaseVersion.V3;
break;
}
await _database.Create(new Credentials
{
KeyFileContents = !string.IsNullOrEmpty(message.KeyFilePath) ? await _file.OpenBinaryFile(message.KeyFilePath) : null,
Password = message.Password
}, message.Name);
}, message.Name, version);
_database.FileAccessToken = message.FilePath;
var contents = await _database.SaveDatabase();
await _file.WriteBinaryContentsToFile(_database.FileAccessToken, contents);
if (message.CreateSampleData)
{

View File

@@ -1,28 +0,0 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace ModernKeePass.Domain.AOP
{
public class NotifyPropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetProperty<T>(ref T property, T value, [CallerMemberName] string propertyName = "")
{
if (EqualityComparer<T>.Default.Equals(property, value))
{
return false;
}
property = value;
OnPropertyChanged(propertyName);
return true;
}
}
}

View File

@@ -75,7 +75,6 @@
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="AOP\NotifyPropertyChangedBase.cs" />
<Compile Include="Common\Constants.cs" />
<Compile Include="Dtos\Credentials.cs" />
<Compile Include="Dtos\FileInfo.cs" />

View File

@@ -3,6 +3,7 @@
public enum DatabaseVersion
{
V2,
V3,
V4
}
}

View File

@@ -11,6 +11,7 @@ using ModernKeePass.Domain.Enums;
using ModernKeePass.Domain.Interfaces;
using ModernKeePassLib;
using ModernKeePassLib.Collections;
using ModernKeePassLib.Cryptography.Cipher;
using ModernKeePassLib.Cryptography.KeyDerivation;
using ModernKeePassLib.Interfaces;
using ModernKeePassLib.Keys;
@@ -112,7 +113,7 @@ namespace ModernKeePass.Infrastructure.KeePass
await Open(file, _credentials);
}
public async Task Create(Credentials credentials, string name, DatabaseVersion version = DatabaseVersion.V2)
public async Task Create(Credentials credentials, string name, DatabaseVersion version = DatabaseVersion.V4)
{
try
{
@@ -131,6 +132,7 @@ namespace ModernKeePass.Infrastructure.KeePass
{
case DatabaseVersion.V4:
_pwDatabase.KdfParameters = KdfPool.Get("Argon2").GetDefaultParameters();
_pwDatabase.DataCipherUuid = CipherPool.GlobalPool[1].CipherUuid;
break;
}
});

View File

@@ -116,6 +116,10 @@ namespace ModernKeePass
}
});
}
else
{
await _dialog.ShowError(realException, realException.Message, "OK", () => {});
}
}
/// <summary>

View File

@@ -1,6 +1,7 @@
using System;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
@@ -10,12 +11,11 @@ using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Application.Database.Queries.OpenDatabase;
using ModernKeePass.Application.Security.Commands.GenerateKeyFile;
using ModernKeePass.Application.Security.Queries.EstimatePasswordComplexity;
using ModernKeePass.Domain.AOP;
using ModernKeePass.Domain.Dtos;
namespace ModernKeePass.ViewModels
{
public class CompositeKeyVm: NotifyPropertyChangedBase
public class CompositeKeyVm: ObservableObject
{
public enum StatusTypes
{
@@ -30,8 +30,8 @@ namespace ModernKeePass.ViewModels
get { return _hasPassword; }
set
{
SetProperty(ref _hasPassword, value);
OnPropertyChanged(nameof(IsValid));
Set(() => HasPassword, ref _hasPassword, value);
RaisePropertyChanged(nameof(IsValid));
}
}
@@ -40,8 +40,8 @@ namespace ModernKeePass.ViewModels
get { return _hasKeyFile; }
set
{
SetProperty(ref _hasKeyFile, value);
OnPropertyChanged(nameof(IsValid));
Set(() => HasKeyFile, ref _hasKeyFile, value);
RaisePropertyChanged(nameof(IsValid));
}
}
@@ -50,13 +50,13 @@ namespace ModernKeePass.ViewModels
public string Status
{
get { return _status; }
set { SetProperty(ref _status, value); }
set { Set(() => Status, ref _status, value); }
}
public int StatusType
{
get { return (int)_statusType; }
set { SetProperty(ref _statusType, (StatusTypes)value); }
get { return _statusType; }
set { Set(() => StatusType, ref _statusType, value); }
}
public string Password
@@ -65,7 +65,7 @@ namespace ModernKeePass.ViewModels
set
{
_password = value;
OnPropertyChanged(nameof(PasswordComplexityIndicator));
RaisePropertyChanged(nameof(PasswordComplexityIndicator));
StatusType = (int)StatusTypes.Normal;
Status = string.Empty;
}
@@ -77,14 +77,14 @@ namespace ModernKeePass.ViewModels
set
{
_keyFilePath = value;
OnPropertyChanged(nameof(IsValid));
RaisePropertyChanged(nameof(IsValid));
}
}
public string KeyFileText
{
get { return _keyFileText; }
set { SetProperty(ref _keyFileText, value); }
set { Set(() => KeyFileText, ref _keyFileText, value); }
}
public string RootGroupId { get; set; }
@@ -96,7 +96,7 @@ namespace ModernKeePass.ViewModels
private bool _isOpening;
private string _password = string.Empty;
private string _status;
private StatusTypes _statusType;
private int _statusType;
private string _keyFilePath;
private string _keyFileText;
private readonly IMediator _mediator;
@@ -121,7 +121,7 @@ namespace ModernKeePass.ViewModels
try
{
_isOpening = true;
OnPropertyChanged(nameof(IsValid));
RaisePropertyChanged(nameof(IsValid));
if (createNew)
{
await _mediator.Send(new CreateDatabaseCommand
@@ -159,7 +159,7 @@ namespace ModernKeePass.ViewModels
finally
{
_isOpening = false;
OnPropertyChanged(nameof(IsValid));
RaisePropertyChanged(nameof(IsValid));
}
return false;
}

View File

@@ -5,6 +5,7 @@ using System.Linq;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Views;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
@@ -27,13 +28,12 @@ using ModernKeePass.Application.Security.Queries.EstimatePasswordComplexity;
using ModernKeePass.Domain.Enums;
using ModernKeePass.Interfaces;
using ModernKeePass.Application.Group.Models;
using ModernKeePass.Domain.AOP;
using ModernKeePass.Extensions;
using RelayCommand = GalaSoft.MvvmLight.Command.RelayCommand;
namespace ModernKeePass.ViewModels
{
public class EntryDetailVm : NotifyPropertyChangedBase, IVmEntity
public class EntryDetailVm : ObservableObject, IVmEntity
{
public bool IsRevealPasswordEnabled => !string.IsNullOrEmpty(Password);
public bool HasExpired => HasExpirationDate && ExpiryDate < DateTime.Now;
@@ -71,8 +71,8 @@ namespace ModernKeePass.ViewModels
get { return _selectedItem; }
set
{
SetProperty(ref _selectedItem, value);
if (value != null) OnPropertyChanged();
Set(() => SelectedItem, ref _selectedItem, value);
if (value != null) RaisePropertyChanged();
}
}
public int SelectedIndex
@@ -80,15 +80,15 @@ namespace ModernKeePass.ViewModels
get { return _selectedIndex; }
set
{
SetProperty(ref _selectedIndex, value);
OnPropertyChanged(nameof(IsCurrentEntry));
Set(() => SelectedIndex, ref _selectedIndex, value);
RaisePropertyChanged(nameof(IsCurrentEntry));
}
}
public double PasswordLength
{
get { return _passwordLength; }
set { SetProperty(ref _passwordLength, value); }
set { Set(() => PasswordLength, ref _passwordLength, value); }
}
public string Title
@@ -114,8 +114,8 @@ namespace ModernKeePass.ViewModels
{
SelectedItem.Password = value;
SetFieldValue(nameof(Password), value).Wait();
OnPropertyChanged(nameof(Password));
OnPropertyChanged(nameof(PasswordComplexityIndicator));
RaisePropertyChanged(nameof(Password));
RaisePropertyChanged(nameof(PasswordComplexityIndicator));
}
}
@@ -180,7 +180,7 @@ namespace ModernKeePass.ViewModels
{
SelectedItem.HasExpirationDate = value;
SetFieldValue(nameof(HasExpirationDate), value).Wait();
OnPropertyChanged(nameof(HasExpirationDate));
RaisePropertyChanged(nameof(HasExpirationDate));
}
}
@@ -208,13 +208,13 @@ namespace ModernKeePass.ViewModels
public bool IsEditMode
{
get { return IsCurrentEntry && _isEditMode; }
set { SetProperty(ref _isEditMode, value); }
set { Set(() => IsEditMode, ref _isEditMode, value); }
}
public bool IsRevealPassword
{
get { return _isRevealPassword; }
set { SetProperty(ref _isRevealPassword, value); }
set { Set(() => IsRevealPassword, ref _isRevealPassword, value); }
}
public RelayCommand SaveCommand { get; }
@@ -332,7 +332,7 @@ namespace ModernKeePass.ViewModels
UnderscorePatternSelected = UnderscorePatternSelected,
UpperCasePatternSelected = UpperCasePatternSelected
});
OnPropertyChanged(nameof(IsRevealPasswordEnabled));
RaisePropertyChanged(nameof(IsRevealPasswordEnabled));
}
public async Task Move(GroupVm destination)

View File

@@ -5,6 +5,7 @@ using System.Collections.Specialized;
using System.Linq;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Views;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
@@ -27,7 +28,6 @@ using ModernKeePass.Application.Group.Models;
using ModernKeePass.Application.Group.Queries.GetGroup;
using ModernKeePass.Application.Group.Queries.SearchEntries;
using ModernKeePass.Common;
using ModernKeePass.Domain.AOP;
using ModernKeePass.Domain.Enums;
using ModernKeePass.Interfaces;
using ModernKeePass.Models;
@@ -35,7 +35,7 @@ using RelayCommand = GalaSoft.MvvmLight.Command.RelayCommand;
namespace ModernKeePass.ViewModels
{
public class GroupDetailVm : NotifyPropertyChangedBase, IVmEntity
public class GroupDetailVm : ObservableObject, IVmEntity
{
public ObservableCollection<EntryVm> Entries { get; }
@@ -75,7 +75,7 @@ namespace ModernKeePass.ViewModels
get { return _isEditMode; }
set
{
SetProperty(ref _isEditMode, value);
Set(() => IsEditMode, ref _isEditMode, value);
SortEntriesCommand.RaiseCanExecuteChanged();
SortGroupsCommand.RaiseCanExecuteChanged();
}
@@ -234,14 +234,14 @@ namespace ModernKeePass.ViewModels
private async Task SortEntriesAsync()
{
await _mediator.Send(new SortEntriesCommand {Group = _group});
OnPropertyChanged(nameof(Entries));
RaisePropertyChanged(nameof(Entries));
SaveCommand.RaiseCanExecuteChanged();
}
private async Task SortGroupsAsync()
{
await _mediator.Send(new SortGroupsCommand {Group = _group});
OnPropertyChanged(nameof(Groups));
RaisePropertyChanged(nameof(Groups));
SaveCommand.RaiseCanExecuteChanged();
}
}

View File

@@ -2,11 +2,11 @@
using System.Linq;
using Windows.ApplicationModel;
using Windows.UI.Xaml.Controls;
using GalaSoft.MvvmLight;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Domain.AOP;
using ModernKeePass.Domain.Dtos;
using ModernKeePass.Domain.Interfaces;
using ModernKeePass.Models;
@@ -15,17 +15,17 @@ using ModernKeePass.Views;
namespace ModernKeePass.ViewModels
{
public class MainVm : NotifyPropertyChangedBase, IHasSelectableObject
public class MainVm : ObservableObject, IHasSelectableObject
{
private IOrderedEnumerable<IGrouping<string, MainMenuItemVm>> _mainMenuItems;
private MainMenuItemVm _selectedItem;
private ISelectableModel _selectedItem;
public string Name { get; } = Package.Current.DisplayName;
public IOrderedEnumerable<IGrouping<string, MainMenuItemVm>> MainMenuItems
{
get { return _mainMenuItems; }
set { SetProperty(ref _mainMenuItems, value); }
set { Set(() => MainMenuItems, ref _mainMenuItems, value); }
}
public ISelectableModel SelectedItem
@@ -39,7 +39,7 @@ namespace ModernKeePass.ViewModels
_selectedItem.IsSelected = false;
}
SetProperty(ref _selectedItem, (MainMenuItemVm)value);
Set(() => SelectedItem, ref _selectedItem, value);
if (_selectedItem != null)
{

View File

@@ -1,28 +0,0 @@
using Windows.Storage;
using ModernKeePass.Application.Common.Interfaces;
namespace ModernKeePass.ViewModels
{
public class NewVm : OpenVm
{
private string _importFormatHelp;
public bool IsImportChecked { get; set; }
public IStorageFile ImportFile { get; set; }
public string ImportFileExtensionFilter { get; set; } = "*";
public IImportFormat ImportFormat { get; set; }
public string ImportFormatHelp
{
get { return _importFormatHelp; }
set
{
_importFormatHelp = value;
OnPropertyChanged(nameof(ImportFormatHelp));
}
}
}
}

View File

@@ -1,27 +1,27 @@
using System.Collections.ObjectModel;
using System.Linq;
using Windows.UI.Xaml.Controls;
using GalaSoft.MvvmLight;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Domain.AOP;
using ModernKeePass.Domain.Interfaces;
using ModernKeePass.ViewModels.ListItems;
using ModernKeePass.Views;
namespace ModernKeePass.ViewModels
{
public class SettingsVm : NotifyPropertyChangedBase, IHasSelectableObject
public class SettingsVm : ObservableObject, IHasSelectableObject
{
private ListMenuItemVm _selectedItem;
private ISelectableModel _selectedItem;
private IOrderedEnumerable<IGrouping<string, ListMenuItemVm>> _menuItems;
public IOrderedEnumerable<IGrouping<string, ListMenuItemVm>> MenuItems
{
get { return _menuItems; }
set { SetProperty(ref _menuItems, value); }
set { Set(() => MenuItems, ref _menuItems, value); }
}
public ISelectableModel SelectedItem
@@ -35,7 +35,7 @@ namespace ModernKeePass.ViewModels
_selectedItem.IsSelected = false;
}
SetProperty(ref _selectedItem, (ListMenuItemVm)value);
Set(() => SelectedItem, ref _selectedItem, value);
if (_selectedItem != null)
{

View File

@@ -7,29 +7,19 @@
xmlns:converters="using:ModernKeePass.Converters"
xmlns:viewModels="using:ModernKeePass.ViewModels"
xmlns:userControls="using:ModernKeePass.Views.UserControls"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
mc:Ignorable="d">
<Page.Resources>
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<viewModels:NewVm x:Key="ViewModel"/>
</Page.Resources>
<Page.DataContext>
<viewModels:NewVm />
</Page.DataContext>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{StaticResource ViewModel}">
<HyperlinkButton x:Uid="NewCreateButton" Click="CreateDatabaseButton_OnClick" Foreground="{StaticResource MainColor}" Style="{StaticResource MainColorHyperlinkButton}" />
<TextBlock Style="{StaticResource BodyTextBlockStyle}" Margin="15,0,0,30" x:Uid="NewCreateDesc" />
<Border HorizontalAlignment="Left" BorderThickness="1" BorderBrush="AliceBlue" Width="550" Visibility="{Binding IsFileSelected, Converter={StaticResource BooleanToVisibilityConverter}}">
<StackPanel Margin="25,0,25,0">
<TextBlock Text="{Binding Name}" />
<userControls:CompositeKeyUserControl x:Uid="CompositeKeyNewButton" CreateNew="True" DatabaseFilePath="{Binding Token}">
<interactivity:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="ValidationChecked">
<core:NavigateToPageAction TargetPage="ModernKeePass.Views.GroupDetailPage" />
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</userControls:CompositeKeyUserControl>
<userControls:SetCredentialsUserControl x:Uid="CompositeKeyNewButton" />
</StackPanel>
</Border>
<CheckBox x:Name="CheckBox" x:Uid="NewImportCheckbox" Margin="15,10,0,0" IsChecked="{Binding IsImportChecked, Mode=TwoWay}" Visibility="{Binding IsFileSelected, Converter={StaticResource BooleanToVisibilityConverter}}" />

View File

@@ -20,7 +20,7 @@ namespace ModernKeePass.Views
public sealed partial class NewDatabasePage
{
private readonly IResourceProxy _resource;
public NewVm Model => (NewVm)DataContext;
private NewVm Model => (NewVm)Resources["ViewModel"];
public NewDatabasePage(): this(App.Services.GetRequiredService<IResourceProxy>()) { }
public NewDatabasePage(IResourceProxy resource)

View File

@@ -8,16 +8,18 @@
xmlns:listItems="using:ModernKeePass.ViewModels.ListItems"
mc:Ignorable="d">
<Page.Resources>
<CollectionViewSource x:Name="RecycleBinGroups" Source="{Binding Groups}" />
<CollectionViewSource x:Name="Ciphers" Source="{Binding Ciphers}" />
<CollectionViewSource x:Name="Compressions" Source="{Binding Compressions}" />
<CollectionViewSource x:Name="KeyDerivations" Source="{Binding KeyDerivations}" />
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<converters:NullToBooleanConverter x:Key="NullToBooleanConverter"/>
<listItems:SettingsDatabaseVm x:Key="ViewModel"/>
</Page.Resources>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{StaticResource ViewModel}">
<StackPanel.Resources>
<CollectionViewSource x:Name="RecycleBinGroups" Source="{Binding Groups}" />
<CollectionViewSource x:Name="Ciphers" Source="{Binding Ciphers}" />
<CollectionViewSource x:Name="Compressions" Source="{Binding Compressions}" />
<CollectionViewSource x:Name="KeyDerivations" Source="{Binding KeyDerivations}" />
</StackPanel.Resources>
<ToggleSwitch x:Uid="SettingsDatabaseRecycleBin" IsOn="{Binding HasRecycleBin, Mode=TwoWay}" Style="{StaticResource MainColorToggleSwitch}" />
<StackPanel Visibility="{Binding HasRecycleBin, Converter={StaticResource BooleanToVisibilityConverter}}">
<RadioButton x:Uid="SettingsDatabaseRecycleBinCreate" GroupName="Recycle" IsChecked="{Binding IsNewRecycleBin, Mode=TwoWay}" />

View File

@@ -7,11 +7,13 @@
xmlns:listItems="using:ModernKeePass.ViewModels.ListItems"
mc:Ignorable="d">
<Page.Resources>
<CollectionViewSource x:Name="KeyDerivations" Source="{Binding FileFormats}" />
<listItems:SettingsNewVm x:Key="ViewModel"/>
</Page.Resources>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DataContext="{StaticResource ViewModel}">
<StackPanel.Resources>
<CollectionViewSource x:Name="KeyDerivations" Source="{Binding FileFormats}" />
</StackPanel.Resources>
<TextBlock x:Uid="SettingsNewDatabaseDesc" Style="{StaticResource TextBlockSettingsHeaderStyle}" Margin="5,0,0,10"/>
<ToggleSwitch x:Uid="SettingsNewDatabaseSample" IsOn="{Binding IsCreateSample, Mode=TwoWay}" Style="{StaticResource MainColorToggleSwitch}" />
<TextBlock x:Uid="SettingsNewDatabaseKdf" Style="{StaticResource TextBlockSettingsHeaderStyle}" Margin="5,20,0,10" />
@@ -21,7 +23,7 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ComboBox Grid.Column="1" ItemsSource="{Binding Source={StaticResource KeyDerivations}}" SelectedItem="{Binding FileFormatVersion, Mode=TwoWay}" ItemContainerStyle="{StaticResource MainColorComboBoxItem}" Style="{StaticResource MainColorComboBox}" />
<ComboBox Grid.Column="1" ItemsSource="{Binding Source={StaticResource KeyDerivations}}" SelectedItem="{Binding DatabaseFormatVersion, Mode=TwoWay}" DisplayMemberPath="DisplayText" ItemContainerStyle="{StaticResource MainColorComboBoxItem}" Style="{StaticResource MainColorComboBox}" />
<Button Grid.Column="2" Style="{StaticResource TextBlockButtonStyle}">
<SymbolIcon Symbol="Help" RenderTransformOrigin="0.5,0.5" >
<SymbolIcon.RenderTransform>

View File

@@ -12,12 +12,9 @@
<UserControl.Resources>
<converters:DiscreteIntToSolidColorBrushConverter x:Key="DiscreteIntToSolidColorBrushConverter"/>
<converters:EmptyStringToVisibilityConverter x:Key="EmptyStringToVisibilityConverter"/>
<viewModels:OpenDatabaseControlVm x:Key="ViewModel"/>
</UserControl.Resources>
<Grid x:Name="Grid">
<!-- DataContext is not set at the root of the control because of issues happening when displaying it -->
<Grid.DataContext>
<viewModels:OpenDatabaseControlVm />
</Grid.DataContext>
<Grid DataContext="{StaticResource ViewModel}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="*" />

View File

@@ -12,8 +12,8 @@ namespace ModernKeePass.Views.UserControls
{
public sealed partial class OpenDatabaseUserControl
{
public OpenDatabaseControlVm Model => Grid.DataContext as OpenDatabaseControlVm;
private OpenDatabaseControlVm Model => (OpenDatabaseControlVm)Resources["ViewModel"];
public string DatabaseFilePath
{
get { return (string)GetValue(DatabaseFilePathProperty); }

View File

@@ -1,5 +1,5 @@
<UserControl x:Name="UserControl"
x:Class="ModernKeePass.Views.UserControls.UpdateCredentialsUserControl"
x:Class="ModernKeePass.Views.UserControls.SetCredentialsUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
@@ -14,17 +14,15 @@
<converters:DoubleToSolidColorBrushConverter x:Key="DoubleToSolidColorBrushConverter"/>
<converters:DiscreteIntToSolidColorBrushConverter x:Key="DiscreteIntToSolidColorBrushConverter"/>
<converters:EmptyStringToVisibilityConverter x:Key="EmptyStringToVisibilityConverter"/>
<viewModels:SetCredentialsViewModel x:Key="ViewModel"/>
</UserControl.Resources>
<Grid x:Name="Grid">
<!-- DataContext is not set at the root of the control because of issues happening when displaying it -->
<Grid.DataContext>
<viewModels:SetCredentialsViewModel />
</Grid.DataContext>
<Grid DataContext="{StaticResource ViewModel}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="45" />
<RowDefinition Height="45" />
<RowDefinition Height="40" />
<RowDefinition Height="40" />
@@ -38,19 +36,19 @@
</core:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
</PasswordBox>
<PasswordBox Grid.Row="0" Grid.Column="1" x:Uid="CompositeKeyPassword" Password="{Binding Password, Mode=TwoWay}" Height="30" IsPasswordRevealButtonEnabled="True" BorderBrush="{Binding StatusType, Converter={StaticResource DiscreteIntToSolidColorBrushConverter}}" SelectionHighlightColor="{StaticResource MainColor}" />
<ProgressBar Grid.Row="0" Grid.Column="1"
<PasswordBox Grid.Row="1" Grid.Column="1" x:Uid="CompositeKeyPassword" Password="{Binding ConfirmPassword, Mode=TwoWay}" Height="30" IsPasswordRevealButtonEnabled="True" BorderBrush="{Binding StatusType, Converter={StaticResource DiscreteIntToSolidColorBrushConverter}}" SelectionHighlightColor="{StaticResource MainColor}" />
<ProgressBar Grid.Row="1" Grid.Column="1"
Maximum="128" VerticalAlignment="Bottom"
Value="{Binding PasswordComplexityIndicator, ConverterParameter=0\,128, Converter={StaticResource ProgressBarLegalValuesConverter}, Mode=OneWay}"
Foreground="{Binding PasswordComplexityIndicator, ConverterParameter=128, Converter={StaticResource DoubleToSolidColorBrushConverter}, Mode=OneWay}"/>
<CheckBox Grid.Row="1" Grid.Column="0" IsChecked="{Binding HasKeyFile, Mode=TwoWay}" />
Value="{Binding PasswordComplexityIndicator, ConverterParameter=0\,128, Converter={StaticResource ProgressBarLegalValuesConverter}}"
Foreground="{Binding PasswordComplexityIndicator, ConverterParameter=128, Converter={StaticResource DoubleToSolidColorBrushConverter}}"/>
<CheckBox Grid.Row="2" Grid.Column="0" IsChecked="{Binding HasKeyFile, Mode=TwoWay}" />
<HyperlinkButton Grid.Row="1" Grid.Column="1" Margin="-15,0,0,0"
Content="{Binding KeyFileText, Mode=OneWay}"
IsEnabled="{Binding HasKeyFile, Mode=OneWay}"
Content="{Binding KeyFileText}"
IsEnabled="{Binding HasKeyFile}"
Click="KeyFileButton_Click"
Style="{StaticResource MainColorHyperlinkButton}" />
<HyperlinkButton Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right"
IsEnabled="{Binding HasKeyFile, Mode=OneWay}"
<HyperlinkButton Grid.Row="2" Grid.Column="1" HorizontalAlignment="Right"
IsEnabled="{Binding HasKeyFile}"
Style="{StaticResource MainColorHyperlinkButton}"
Click="CreateKeyFileButton_Click">
<SymbolIcon Symbol="Add">
@@ -59,14 +57,13 @@
</ToolTipService.ToolTip>
</SymbolIcon>
</HyperlinkButton>
<Button Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2"
<Button Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="3"
Command="{Binding GenerateCredentialsCommand}"
Content="{Binding ButtonLabel, ElementName=UserControl}"
Click="UpdateButton_OnClick"
Style="{StaticResource MainColorButton}"
IsEnabled="{Binding IsValid, Mode=OneWay}" />
<TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="3" Height="Auto" FontSize="14" FontWeight="Light"
Text="{Binding Status, Mode=OneWay}"
Foreground="{Binding StatusType, Mode=OneWay, Converter={StaticResource DiscreteIntToSolidColorBrushConverter}}"
Visibility="{Binding Status, Mode=OneWay, Converter={StaticResource EmptyStringToVisibilityConverter}}" />
Style="{StaticResource MainColorButton}" />
<TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="4" Height="Auto" FontSize="14" FontWeight="Light"
Text="{Binding Status}"
Foreground="{Binding StatusType, Converter={StaticResource DiscreteIntToSolidColorBrushConverter}}"
Visibility="{Binding Status, Converter={StaticResource EmptyStringToVisibilityConverter}}" />
</Grid>
</UserControl>

View File

@@ -3,18 +3,16 @@ using System.Collections.Generic;
using Windows.Storage.AccessCache;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using ModernKeePass.Domain.Dtos;
using ModernKeePass.ViewModels;
using ModernKeePass.Extensions;
// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236
namespace ModernKeePass.Views.UserControls
{
public sealed partial class UpdateCredentialsUserControl
public sealed partial class SetCredentialsUserControl
{
public SetCredentialsViewModel ViewModel => Grid.DataContext as SetCredentialsViewModel;
private SetCredentialsViewModel Model => (SetCredentialsViewModel)Resources["ViewModel"];
public string ButtonLabel
{
get { return (string)GetValue(ButtonLabelProperty); }
@@ -24,24 +22,10 @@ namespace ModernKeePass.Views.UserControls
DependencyProperty.Register(
nameof(ButtonLabel),
typeof(string),
typeof(UpdateCredentialsUserControl),
typeof(SetCredentialsUserControl),
new PropertyMetadata("OK", (o, args) => { }));
public string DatabaseFilePath
{
get { return (string) GetValue(DatabaseFilePathProperty); }
set { SetValue(DatabaseFilePathProperty, value); }
}
public static readonly DependencyProperty DatabaseFilePathProperty =
DependencyProperty.Register(
"DatabaseFilePath",
typeof(string),
typeof(UpdateCredentialsUserControl),
new PropertyMetadata(null, (o, args) => { }));
public event EventHandler CredentialsUpdated;
public UpdateCredentialsUserControl()
public SetCredentialsUserControl()
{
InitializeComponent();
}
@@ -61,7 +45,7 @@ namespace ModernKeePass.Views.UserControls
if (file == null) return;
var token = StorageApplicationPermissions.FutureAccessList.Add(file);
ViewModel.KeyFilePath = token;
Model.KeyFilePath = token;
}
private async void CreateKeyFileButton_Click(object sender, RoutedEventArgs e)
@@ -77,25 +61,7 @@ namespace ModernKeePass.Views.UserControls
if (file == null) return;
var token = StorageApplicationPermissions.FutureAccessList.Add(file);
ViewModel.KeyFilePath = token;
}
private async void UpdateButton_OnClick(object sender, RoutedEventArgs e)
{
//throw new NotImplementedException();
if (await Dispatcher.RunTaskAsync(async () =>
{
var fileInfo = new FileInfo
{
Path = DatabaseFilePath
};
await ViewModel.CreateDatabase(fileInfo);
return true;
}))
{
CredentialsUpdated?.Invoke(this, new EventArgs());
}
Model.KeyFilePath = token;
}
}
}

View File

@@ -143,7 +143,6 @@
<Compile Include="Views\MainPageFrames\WelcomePage.xaml.cs">
<DependentUpon>WelcomePage.xaml</DependentUpon>
</Compile>
<Compile Include="ViewModels\AboutVm.cs" />
<Compile Include="ViewModels\CompositeKeyVm.cs" />
<Compile Include="Views\EntryDetailPage.xaml.cs">
<DependentUpon>EntryDetailPage.xaml</DependentUpon>
@@ -165,10 +164,6 @@
<Compile Include="ViewModels\GroupDetailVm.cs" />
<Compile Include="ViewModels\SettingsVm.cs" />
<Compile Include="ViewModels\MainVm.cs" />
<Compile Include="ViewModels\NewVm.cs" />
<Compile Include="ViewModels\OpenVm.cs" />
<Compile Include="ViewModels\RecentVm.cs" />
<Compile Include="ViewModels\SaveVm.cs" />
<Compile Include="Views\UserControls\OpenDatabaseUserControl.xaml.cs">
<DependentUpon>OpenDatabaseUserControl.xaml</DependentUpon>
</Compile>
@@ -181,8 +176,8 @@
<Compile Include="Views\UserControls\TopMenuUserControl.xaml.cs">
<DependentUpon>TopMenuUserControl.xaml</DependentUpon>
</Compile>
<Compile Include="Views\UserControls\UpdateCredentialsUserControl.xaml.cs">
<DependentUpon>UpdateCredentialsUserControl.xaml</DependentUpon>
<Compile Include="Views\UserControls\SetCredentialsUserControl.xaml.cs">
<DependentUpon>SetCredentialsUserControl.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
@@ -332,7 +327,7 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\UserControls\UpdateCredentialsUserControl.xaml">
<Page Include="Views\UserControls\SetCredentialsUserControl.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>

View File

@@ -1,7 +1,8 @@
using System;
using Windows.UI.Xaml;
using GalaSoft.MvvmLight.Views;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Xaml.Interactivity;
using ModernKeePass.Common;
namespace ModernKeePass.Actions
{
@@ -25,7 +26,8 @@ namespace ModernKeePass.Actions
}
catch (Exception ex)
{
MessageDialogHelper.ShowErrorDialog(ex).GetAwaiter();
var dialogService = App.Services.GetRequiredService<IDialogService>();
dialogService.ShowError(ex, ex.Message, null, () => {}).Wait();
return false;
}
}

View File

@@ -0,0 +1,8 @@
namespace Messages
{
public class CredentialsSetMessage
{
public string KeyFilePath { get; set; }
public string Password { get; set; }
}
}

View File

@@ -1,11 +1,11 @@
using System;
using Windows.UI.Xaml.Controls;
using ModernKeePass.Domain.AOP;
using GalaSoft.MvvmLight;
using ModernKeePass.Domain.Interfaces;
namespace ModernKeePass.ViewModels.ListItems
{
public class ListMenuItemVm : NotifyPropertyChangedBase, IIsEnabled, ISelectableModel
public class ListMenuItemVm : ObservableObject, IIsEnabled, ISelectableModel
{
private bool _isSelected;
@@ -19,7 +19,7 @@ namespace ModernKeePass.ViewModels.ListItems
public bool IsSelected
{
get { return _isSelected; }
set { SetProperty(ref _isSelected, value); }
set { Set(() => IsSelected, ref _isSelected, value); }
}
public override string ToString()

View File

@@ -1,10 +1,10 @@
using ModernKeePass.Domain.AOP;
using GalaSoft.MvvmLight;
using ModernKeePass.Domain.Dtos;
using ModernKeePass.Domain.Interfaces;
namespace ModernKeePass.ViewModels.ListItems
{
public class RecentItemVm: NotifyPropertyChangedBase, ISelectableModel
public class RecentItemVm: ObservableObject, ISelectableModel
{
private bool _isSelected;
private string _name;
@@ -14,25 +14,25 @@ namespace ModernKeePass.ViewModels.ListItems
public string Token
{
get { return _token; }
set { SetProperty(ref _token, value); }
set { Set(() => Token, ref _token, value); }
}
public string Name
{
get { return _name; }
set { SetProperty(ref _name, value); }
set { Set(() => Name, ref _name, value); }
}
public string Path
{
get { return _path; }
set { SetProperty(ref _path, value); }
set { Set(() => Path, ref _path, value); }
}
public bool IsSelected
{
get { return _isSelected; }
set { SetProperty(ref _isSelected, value); }
set { Set(() => IsSelected, ref _isSelected, value); }
}
public RecentItemVm(FileInfo file)

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using GalaSoft.MvvmLight;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
@@ -16,12 +17,11 @@ using ModernKeePass.Application.Parameters.Models;
using ModernKeePass.Application.Parameters.Queries.GetCiphers;
using ModernKeePass.Application.Parameters.Queries.GetCompressions;
using ModernKeePass.Application.Parameters.Queries.GetKeyDerivations;
using ModernKeePass.Domain.AOP;
namespace ModernKeePass.ViewModels.ListItems
{
// TODO: implement Kdf settings
public class SettingsDatabaseVm: NotifyPropertyChangedBase
public class SettingsDatabaseVm: ObservableObject
{
private readonly IMediator _mediator;
private readonly DatabaseVm _database;
@@ -32,7 +32,7 @@ namespace ModernKeePass.ViewModels.ListItems
set
{
_mediator.Send(new SetHasRecycleBinCommand {HasRecycleBin = value}).Wait();
OnPropertyChanged(nameof(HasRecycleBin));
RaisePropertyChanged(nameof(HasRecycleBin));
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Common;
@@ -23,12 +24,34 @@ namespace ModernKeePass.ViewModels.ListItems
set { _settings.PutSetting(Constants.Settings.Sample, value); }
}
public IEnumerable<string> FileFormats => new []{"2", "4"};
public string FileFormatVersion
public IEnumerable<DatabaseFormat> FileFormats => new []
{
get { return _settings.GetSetting<string>(Constants.Settings.DefaultFileFormat); }
set { _settings.PutSetting(Constants.Settings.DefaultFileFormat, value); }
new DatabaseFormat
{
Version = "4",
DisplayText = "4 (Argon2, ChaCha20)"
},
new DatabaseFormat
{
Version = "3",
DisplayText = "3 (AES-KDF, AES/Rijndael)"
}
};
public DatabaseFormat DatabaseFormatVersion
{
get
{
var version = _settings.GetSetting<string>(Constants.Settings.DefaultFileFormat);
return FileFormats.FirstOrDefault(f => f.Version == version);
}
set { _settings.PutSetting(Constants.Settings.DefaultFileFormat, value.Version); }
}
}
public class DatabaseFormat
{
public string Version { get; set; }
public string DisplayText { get; set; }
}
}

View File

@@ -0,0 +1,71 @@
using System.Threading.Tasks;
using Windows.Storage;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Views;
using MediatR;
using Messages;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Application.Database.Commands.CreateDatabase;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Common;
using ModernKeePass.Models;
namespace ModernKeePass.ViewModels
{
public class NewVm : OpenVm
{
private readonly IMediator _mediator;
private readonly ISettingsProxy _settings;
private readonly INavigationService _navigation;
private string _importFormatHelp;
public bool IsImportChecked { get; set; }
public IStorageFile ImportFile { get; set; }
public string ImportFileExtensionFilter { get; set; } = "*";
public IImportFormat ImportFormat { get; set; }
public string ImportFormatHelp
{
get { return _importFormatHelp; }
set
{
_importFormatHelp = value;
RaisePropertyChanged(nameof(ImportFormatHelp));
}
}
public NewVm(): this(
App.Services.GetRequiredService<IMediator>(),
App.Services.GetRequiredService<ISettingsProxy>(),
App.Services.GetRequiredService<IMessenger>(),
App.Services.GetRequiredService<INavigationService>()) { }
public NewVm(IMediator mediator, ISettingsProxy settings, IMessenger messenger, INavigationService navigation)
{
_mediator = mediator;
_settings = settings;
_navigation = navigation;
messenger.Register<CredentialsSetMessage>(this, async message => await CreateDatabase(message));
}
public async Task CreateDatabase(CredentialsSetMessage message)
{
await _mediator.Send(new CreateDatabaseCommand
{
FilePath = Token,
KeyFilePath = message.KeyFilePath,
Password = message.Password,
Name = "ModernKeePass",
Version = _settings.GetSetting(Constants.Settings.DefaultFileFormat, "4"),
CreateSampleData = _settings.GetSetting<bool>(Constants.Settings.Sample)
});
var database = await _mediator.Send(new GetDatabaseQuery());
_navigation.NavigateTo(Constants.Navigation.GroupPage, new NavigationItem { Id = database.RootGroupId });
}
}
}

View File

@@ -1,12 +1,12 @@
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Domain.AOP;
using ModernKeePass.Domain.Dtos;
namespace ModernKeePass.ViewModels
{
public class OpenVm: NotifyPropertyChangedBase
public class OpenVm: ObservableObject
{
private readonly IRecentProxy _recent;
private string _name;
@@ -17,19 +17,19 @@ namespace ModernKeePass.ViewModels
public string Token
{
get { return _token; }
set { SetProperty(ref _token, value); }
set { Set(() => Token, ref _token, value); }
}
public string Name
{
get { return _name; }
private set { SetProperty(ref _name, value); }
private set { Set(() => Name, ref _name, value); }
}
public string Path
{
get { return _path; }
private set { SetProperty(ref _path, value); }
private set { Set(() => Path, ref _path, value); }
}
public OpenVm(): this(App.Services.GetRequiredService<IRecentProxy>()) { }
@@ -44,7 +44,7 @@ namespace ModernKeePass.ViewModels
Token = file.Id;
Name = file.Name;
Path = file.Path;
OnPropertyChanged(nameof(IsFileSelected));
RaisePropertyChanged(nameof(IsFileSelected));
await AddToRecentList(file);
}

View File

@@ -1,16 +1,16 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Domain.AOP;
using ModernKeePass.Domain.Interfaces;
using ModernKeePass.ViewModels.ListItems;
namespace ModernKeePass.ViewModels
{
public class RecentVm : NotifyPropertyChangedBase, IHasSelectableObject
public class RecentVm : ObservableObject, IHasSelectableObject
{
private readonly IRecentProxy _recent;
private ISelectableModel _selectedItem;
@@ -19,7 +19,7 @@ namespace ModernKeePass.ViewModels
public ObservableCollection<RecentItemVm> RecentItems
{
get { return _recentItems; }
set { SetProperty(ref _recentItems, value); }
set { Set(() => RecentItems, ref _recentItems, value); }
}
public ISelectableModel SelectedItem
@@ -33,8 +33,8 @@ namespace ModernKeePass.ViewModels
_selectedItem.IsSelected = false;
}
SetProperty(ref _selectedItem, value);
Set(() => SelectedItem, ref _selectedItem, value);
if (_selectedItem == null) return;
_selectedItem.IsSelected = true;
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Text;
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Views;
@@ -13,11 +14,10 @@ using ModernKeePass.Application.Database.Commands.SaveDatabase;
using ModernKeePass.Application.Database.Queries.GetDatabase;
using ModernKeePass.Application.Database.Queries.OpenDatabase;
using ModernKeePass.Common;
using ModernKeePass.Domain.AOP;
namespace ModernKeePass.ViewModels
{
public class OpenDatabaseControlVm : NotifyPropertyChangedBase
public class OpenDatabaseControlVm : ObservableObject
{
public enum StatusTypes
{
@@ -32,8 +32,8 @@ namespace ModernKeePass.ViewModels
get { return _hasPassword; }
set
{
SetProperty(ref _hasPassword, value);
OnPropertyChanged(nameof(IsValid));
Set(() => HasPassword, ref _hasPassword, value);
RaisePropertyChanged(nameof(IsValid));
OpenDatabaseCommand.RaiseCanExecuteChanged();
}
}
@@ -43,8 +43,8 @@ namespace ModernKeePass.ViewModels
get { return _hasKeyFile; }
set
{
SetProperty(ref _hasKeyFile, value);
OnPropertyChanged(nameof(IsValid));
Set(() => HasKeyFile, ref _hasKeyFile, value);
RaisePropertyChanged(nameof(IsValid));
OpenDatabaseCommand.RaiseCanExecuteChanged();
}
}
@@ -54,13 +54,13 @@ namespace ModernKeePass.ViewModels
public string Status
{
get { return _status; }
set { SetProperty(ref _status, value); }
set { Set(() => Status, ref _status, value); }
}
public int StatusType
{
get { return (int)_statusType; }
set { SetProperty(ref _statusType, (StatusTypes)value); }
get { return _statusType; }
set { Set(() => StatusType, ref _statusType, value); }
}
public string Password
@@ -80,7 +80,7 @@ namespace ModernKeePass.ViewModels
set
{
_keyFilePath = value;
OnPropertyChanged(nameof(IsValid));
RaisePropertyChanged(nameof(IsValid));
OpenDatabaseCommand.RaiseCanExecuteChanged();
}
}
@@ -88,18 +88,18 @@ namespace ModernKeePass.ViewModels
public string KeyFileText
{
get { return _keyFileText; }
set { SetProperty(ref _keyFileText, value); }
set { Set(() => KeyFileText, ref _keyFileText, value); }
}
public string OpenButtonLabel
{
get { return _openButtonLabel; }
set { SetProperty(ref _openButtonLabel, value); }
set { Set(() => OpenButtonLabel, ref _openButtonLabel, value); }
}
public RelayCommand<string> OpenDatabaseCommand { get; }
protected readonly IMediator Mediator;
private readonly IMediator _mediator;
private readonly IResourceProxy _resource;
private readonly IMessenger _messenger;
private readonly IDialogService _dialog;
@@ -108,7 +108,7 @@ namespace ModernKeePass.ViewModels
private bool _isOpening;
private string _password = string.Empty;
private string _status;
private StatusTypes _statusType;
private int _statusType;
private string _keyFilePath;
private string _keyFileText;
private string _openButtonLabel;
@@ -123,7 +123,7 @@ namespace ModernKeePass.ViewModels
public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource, IMessenger messenger, IDialogService dialog)
{
Mediator = mediator;
_mediator = mediator;
_resource = resource;
_messenger = messenger;
_dialog = dialog;
@@ -136,7 +136,7 @@ namespace ModernKeePass.ViewModels
{
_messenger.Send(new DatabaseOpeningMessage {Token = databaseFilePath});
var database = await Mediator.Send(new GetDatabaseQuery());
var database = await _mediator.Send(new GetDatabaseQuery());
if (database.IsOpen)
{
await _dialog.ShowMessage(_resource.GetResourceValue("MessageDialogDBOpenTitle"),
@@ -147,16 +147,16 @@ namespace ModernKeePass.ViewModels
{
if (isOk)
{
await Mediator.Send(new SaveDatabaseCommand());
await _mediator.Send(new SaveDatabaseCommand());
ToastNotificationHelper.ShowGenericToast(
database.Name,
_resource.GetResourceValue("ToastSavedMessage"));
await Mediator.Send(new CloseDatabaseCommand());
await _mediator.Send(new CloseDatabaseCommand());
await OpenDatabase(databaseFilePath);
}
else
{
await Mediator.Send(new CloseDatabaseCommand());
await _mediator.Send(new CloseDatabaseCommand());
await OpenDatabase(databaseFilePath);
}
});
@@ -171,15 +171,15 @@ namespace ModernKeePass.ViewModels
_isOpening = true;
try
{
OnPropertyChanged(nameof(IsValid));
RaisePropertyChanged(nameof(IsValid));
OpenDatabaseCommand.RaiseCanExecuteChanged();
await Mediator.Send(new OpenDatabaseQuery
await _mediator.Send(new OpenDatabaseQuery
{
FilePath = databaseFilePath,
KeyFilePath = HasKeyFile ? KeyFilePath : null,
Password = HasPassword ? Password : null,
});
var rootGroupId = (await Mediator.Send(new GetDatabaseQuery())).RootGroupId;
var rootGroupId = (await _mediator.Send(new GetDatabaseQuery())).RootGroupId;
_messenger.Send(new DatabaseOpenedMessage { RootGroupId = rootGroupId });
}
@@ -198,7 +198,7 @@ namespace ModernKeePass.ViewModels
finally
{
_isOpening = false;
OnPropertyChanged(nameof(IsValid));
RaisePropertyChanged(nameof(IsValid));
OpenDatabaseCommand.RaiseCanExecuteChanged();
OpenButtonLabel = oldLabel;
}

View File

@@ -1,46 +1,120 @@
using System.Threading.Tasks;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Messages;
using Microsoft.Extensions.DependencyInjection;
using ModernKeePass.Application.Common.Interfaces;
using ModernKeePass.Application.Database.Commands.CreateDatabase;
using ModernKeePass.Common;
using ModernKeePass.Domain.Dtos;
namespace ModernKeePass.ViewModels
{
public class SetCredentialsViewModel : OpenDatabaseControlVm
public class SetCredentialsViewModel : ObservableObject
{
private readonly ICredentialsProxy _credentials;
private readonly ISettingsProxy _settings;
private string _confirmPassword;
private readonly IMessenger _messenger;
public bool HasPassword
{
get { return _hasPassword; }
set
{
Set(() => HasPassword, ref _hasPassword, value);
RaisePropertyChanged(nameof(IsValid));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public bool HasKeyFile
{
get { return _hasKeyFile; }
set
{
Set(() => HasKeyFile, ref _hasKeyFile, value);
RaisePropertyChanged(nameof(IsValid));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public string Status
{
get { return _status; }
set { Set(() => Status, ref _status, value); }
}
public string Password
{
get { return _password; }
set
{
_password = value;
RaisePropertyChanged(nameof(IsValid));
RaisePropertyChanged(nameof(PasswordComplexityIndicator));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public string ConfirmPassword
{
get { return _confirmPassword; }
set { SetProperty(ref _confirmPassword, value); }
set
{
_confirmPassword = value;
RaisePropertyChanged(nameof(IsValid));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public string KeyFilePath
{
get { return _keyFilePath; }
set
{
_keyFilePath = value;
RaisePropertyChanged(nameof(IsValid));
GenerateCredentialsCommand.RaiseCanExecuteChanged();
}
}
public string KeyFileText
{
get { return _keyFileText; }
set { Set(() => KeyFileText, ref _keyFileText, value); }
}
public string OpenButtonLabel
{
get { return _openButtonLabel; }
set { Set(() => OpenButtonLabel, ref _openButtonLabel, value); }
}
public double PasswordComplexityIndicator => _credentials.EstimatePasswordComplexity(Password);
public new bool IsValid => HasPassword && Password == ConfirmPassword || HasKeyFile && KeyFilePath != string.Empty;
public bool IsValid => HasPassword && Password == ConfirmPassword || HasKeyFile && KeyFilePath != string.Empty;
public SetCredentialsViewModel(): this(App.Services.GetRequiredService<ICredentialsProxy>(), App.Services.GetRequiredService<ISettingsProxy>()) { }
public RelayCommand GenerateCredentialsCommand{ get; }
public SetCredentialsViewModel(ICredentialsProxy credentials, ISettingsProxy settings)
private bool _hasPassword;
private bool _hasKeyFile;
private string _password = string.Empty;
private string _confirmPassword;
private string _status;
private string _keyFilePath;
private string _keyFileText;
private string _openButtonLabel;
public SetCredentialsViewModel(): this(App.Services.GetRequiredService<ICredentialsProxy>(), App.Services.GetRequiredService<IMessenger>()) { }
public SetCredentialsViewModel(ICredentialsProxy credentials, IMessenger messenger)
{
_credentials = credentials;
_settings = settings;
_messenger = messenger;
GenerateCredentialsCommand = new RelayCommand(GenerateCredentials, () => IsValid);
}
public async Task CreateDatabase(FileInfo fileInfo)
private void GenerateCredentials()
{
await Mediator.Send(new CreateDatabaseCommand
_messenger.Send(new CredentialsSetMessage
{
FilePath = fileInfo.Path,
KeyFilePath = HasKeyFile ? KeyFilePath : null,
Password = HasPassword ? Password : null,
Name = "New Database",
CreateSampleData = _settings.GetSetting<bool>(Constants.Settings.Sample)
KeyFilePath = HasKeyFile ? KeyFilePath : null
});
}
}

View File

@@ -36,6 +36,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Events\PasswordEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ColorExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\DispatcherTaskExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\CredentialsMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\DatabaseAlreadyOpenedMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\DatabaseClosedMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Messages\DatabaseOpenedMessage.cs" />
@@ -44,12 +45,17 @@
<Compile Include="$(MSBuildThisFileDirectory)Messages\SaveErrorMessage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TemplateSelectors\FirstItemDataTemplateSelector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TemplateSelectors\SelectableDataTemplateSelector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\AboutVm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\Items\ListMenuItemVm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\Items\MainMenuItemVm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\Items\RecentItemVm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\Items\SettingsDatabaseVm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\Items\SettingsNewVm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\Items\SettingsSaveVm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\NewVm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\OpenVm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\RecentVm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\SaveVm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\UserControls\OpenDatabaseControlVm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ViewModels\UserControls\SetCredentialsViewModel.cs" />
</ItemGroup>