OpenDatabase control now handles status changes with VisualStateManager instead of Vm

Opening DB shows a progress ring instead
This commit is contained in:
Geoffroy BONNEVILLE
2020-04-30 11:10:10 +02:00
parent 9fdc727787
commit d2814c6c67
10 changed files with 72 additions and 90 deletions

View File

@@ -1,27 +0,0 @@
using System;
using Windows.UI;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
namespace ModernKeePass.Converters
{
public class DiscreteIntToSolidColorBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var status = System.Convert.ToInt32(value);
switch (status)
{
case 1: return new SolidColorBrush(Colors.Red);
case 3: return new SolidColorBrush(Colors.Yellow);
case 5: return new SolidColorBrush(Colors.Green);
default: return new SolidColorBrush(Colors.Black);
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}

View File

@@ -14,14 +14,6 @@ namespace ModernKeePass.ViewModels
{
public class OpenDatabaseControlVm : ViewModelBase
{
public enum StatusTypes
{
Normal = 0,
Error = 1,
Warning = 3,
Success = 5
}
public bool HasPassword
{
get { return _hasPassword; }
@@ -44,27 +36,19 @@ namespace ModernKeePass.ViewModels
}
}
public bool IsValid => !_isOpening && (HasPassword || HasKeyFile && !string.IsNullOrEmpty(KeyFilePath));
public string Status
{
get { return _status; }
set { Set(() => Status, ref _status, value); }
}
public int StatusType
{
get { return _statusType; }
set { Set(() => StatusType, ref _statusType, value); }
}
public string Password
{
get { return _password; }
set
{
_password = value;
StatusType = (int)StatusTypes.Normal;
IsError = false;
Status = string.Empty;
}
}
@@ -75,6 +59,7 @@ namespace ModernKeePass.ViewModels
set
{
_keyFilePath = value;
IsError = false;
RaisePropertyChanged(nameof(IsValid));
OpenDatabaseCommand.RaiseCanExecuteChanged();
}
@@ -86,14 +71,22 @@ namespace ModernKeePass.ViewModels
set { Set(() => KeyFileText, ref _keyFileText, value); }
}
public string OpenButtonLabel
public bool IsOpening
{
get { return _openButtonLabel; }
set { Set(() => OpenButtonLabel, ref _openButtonLabel, value); }
get { return _isOpening; }
set { Set(() => IsOpening, ref _isOpening, value); }
}
public bool IsError
{
get { return _isError; }
set { Set(() => IsError, ref _isError, value); }
}
public bool IsValid => !IsOpening && (HasPassword || HasKeyFile && !string.IsNullOrEmpty(KeyFilePath));
public RelayCommand<string> OpenDatabaseCommand { get; }
private readonly IMediator _mediator;
private readonly IResourceProxy _resource;
private readonly INotificationService _notification;
@@ -102,11 +95,10 @@ namespace ModernKeePass.ViewModels
private bool _isOpening;
private string _password = string.Empty;
private string _status;
private int _statusType;
private string _keyFilePath;
private string _keyFileText;
private string _openButtonLabel;
private bool _isError;
public OpenDatabaseControlVm(IMediator mediator, IResourceProxy resource, INotificationService notification)
{
_mediator = mediator;
@@ -114,7 +106,6 @@ namespace ModernKeePass.ViewModels
_notification = notification;
OpenDatabaseCommand = new RelayCommand<string>(async databaseFilePath => await TryOpenDatabase(databaseFilePath), _ => IsValid);
_keyFileText = _resource.GetResourceValue("CompositeKeyDefaultKeyFile");
_openButtonLabel = _resource.GetResourceValue("CompositeKeyOpenButtonLabel");
}
public async Task TryOpenDatabase(string databaseFilePath)
@@ -132,9 +123,7 @@ namespace ModernKeePass.ViewModels
public async Task OpenDatabase(string databaseFilePath)
{
var oldLabel = _openButtonLabel;
OpenButtonLabel = _resource.GetResourceValue("CompositeKeyOpening");
_isOpening = true;
IsOpening = true;
try
{
await _mediator.Send(new OpenDatabaseQuery
@@ -152,7 +141,8 @@ namespace ModernKeePass.ViewModels
var errorMessage = new StringBuilder($"{_resource.GetResourceValue("CompositeKeyErrorOpen")}\n");
if (HasPassword) errorMessage.AppendLine(_resource.GetResourceValue("CompositeKeyErrorUserPassword"));
if (HasKeyFile) errorMessage.AppendLine(_resource.GetResourceValue("CompositeKeyErrorUserKeyFile"));
UpdateStatus(errorMessage.ToString(), StatusTypes.Error);
Status = errorMessage.ToString();
IsError = true;
}
catch (FileNotFoundException)
{
@@ -160,23 +150,17 @@ namespace ModernKeePass.ViewModels
$"{_resource.GetResourceValue("FileNotFoundTitle")}",
$"{_resource.GetResourceValue("FileNotFoundDescription")}");
MessengerInstance.Send(new FileNotFoundMessage());
IsError = true;
}
catch (Exception e)
{
var error = $"{_resource.GetResourceValue("CompositeKeyErrorOpen")}{e.Message}";
UpdateStatus(error, StatusTypes.Error);
Status = $"{_resource.GetResourceValue("CompositeKeyErrorOpen")}{e.Message}";
IsError = true;
}
finally
{
_isOpening = false;
OpenButtonLabel = oldLabel;
IsOpening = false;
}
}
private void UpdateStatus(string text, StatusTypes type)
{
Status = text;
StatusType = (int)type;
}
}
}

View File

@@ -21,7 +21,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\TextBoxWithButton.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\BooleanToVisibilityConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\ColorToBrushConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\DiscreteIntToSolidColorBrushConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\DoubleToSolidColorBrushConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\EmptyStringToVisibilityConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\IconToSymbolConverter.cs" />