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

@@ -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;
}
}
}