make error descriptor ctor private, validation methods are ugly because always creating error descriptors with only single item in.
MODIFY
WalletWasabi.Gui/ViewModels/Validation/Validator.cs
WalletWasabi.Gui/ViewModels/Validation/Validator.cs
@@ -6,9 +6,7 @@ using WalletWasabi.Models;
namespace WalletWasabi.Gui.ViewModels.Validation
{
- public delegate void AddErrorDelegate(ErrorSeverity severity, string error);
-
- public delegate void ValidateMethod(AddErrorDelegate addError);
+ public delegate void ValidateMethod(IErrorList errors);
public static class Validator
{

MODIFY
WalletWasabi.Gui/ViewModels/ViewModelBase.cs
WalletWasabi.Gui/ViewModels/ViewModelBase.cs
@@ -4,7 +4,6 @@ using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
-using System.Reflection;
using WalletWasabi.Gui.ViewModels.Validation;
using WalletWasabi.Models;
@@ -31,16 +30,14 @@ namespace WalletWasabi.Gui.ViewModels
}
_validationMethods[propertyName] = validateMethod;
+ _errorsByPropertyName[propertyName] = ErrorDescriptors.Create();
}
private void ViewModelBase_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.PropertyName))
{
- foreach (var propertyName in _validationMethods.Keys)
- {
- ValidateProperty(propertyName);
- }
+ Validate();
}
else
{
@@ -48,16 +45,25 @@ namespace WalletWasabi.Gui.ViewModels
}
}
+ protected void Validate()
+ {
+ foreach (var propertyName in _validationMethods.Keys)
+ {
+ ValidateProperty(propertyName);
+ }
+ }
+
private void ValidateProperty(string propertyName)
{
if (_validationMethods.ContainsKey(propertyName))
{
ClearErrors(propertyName);
- _validationMethods[propertyName]((severity, error) =>
- {
- AddError(propertyName, severity, error);
- });
+ _validationMethods[propertyName](_errorsByPropertyName[propertyName]);
+
+ OnErrorsChanged(propertyName);
+
+ this.RaisePropertyChanged(nameof(HasErrors));
}
}
@@ -72,29 +78,15 @@ namespace WalletWasabi.Gui.ViewModels
: ErrorDescriptors.Empty;
}
- private void AddError(string propertyName, ErrorSeverity severity, string error)
- {
- if (!_errorsByPropertyName.ContainsKey(propertyName))
- {
- _errorsByPropertyName[propertyName] = new ErrorDescriptors();
- }
-
- _errorsByPropertyName[propertyName].Add(new ErrorDescriptor(severity, error));
-
- OnErrorsChanged(propertyName);
-
- //this.RaisePropertyChanged(nameof(HasErrors));
- }
-
private void ClearErrors(string propertyName)
{
if (_errorsByPropertyName.ContainsKey(propertyName))
{
- _errorsByPropertyName.Remove(propertyName);
+ _errorsByPropertyName[propertyName].Clear();
OnErrorsChanged(propertyName);
- //this.RaisePropertyChanged(nameof(HasErrors));
+ this.RaisePropertyChanged(nameof(HasErrors));
}
}

MODIFY
WalletWasabi/Models/ErrorDescriptors.cs
WalletWasabi/Models/ErrorDescriptors.cs
@@ -2,19 +2,29 @@ using System.Collections.Generic;
namespace WalletWasabi.Models
{
- public class ErrorDescriptors : List<ErrorDescriptor>
+ public interface IErrorList
{
- public static ErrorDescriptors Empty = new ErrorDescriptors();
+ void Add(ErrorSeverity severity, string error);
+ }
- public ErrorDescriptors() : base()
+ public class ErrorDescriptors : List<ErrorDescriptor>, IErrorList
+ {
+ public static ErrorDescriptors Create ()
{
+ return new ErrorDescriptors();
}
- public ErrorDescriptors(params ErrorDescriptor[] errors)
+ public static ErrorDescriptors Empty = Create();
+
+ private ErrorDescriptors() : base()
{
- AddRange(errors);
}
public bool HasErrors => Count > 0;
+
+ void IErrorList.Add(ErrorSeverity severity, string error)
+ {
+ Add(new ErrorDescriptor(severity, error));
+ }
}
}

make error descriptor ctor private, validation methods are ugly because always creating error descriptors with only single item in.