| | | 1 | | // Copyright (c) 2020 .NET Foundation and Contributors. All rights reserved. |
| | | 2 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 3 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 4 | | // See the LICENSE file in the project root for full license information. |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Diagnostics.CodeAnalysis; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Linq.Expressions; |
| | | 11 | | using System.Reactive.Disposables; |
| | | 12 | | using System.Reactive.Linq; |
| | | 13 | | using System.Reactive.Subjects; |
| | | 14 | | using ReactiveUI.Validation.Collections; |
| | | 15 | | using ReactiveUI.Validation.Components.Abstractions; |
| | | 16 | | using ReactiveUI.Validation.Extensions; |
| | | 17 | | using ReactiveUI.Validation.States; |
| | | 18 | | |
| | | 19 | | namespace ReactiveUI.Validation.Components |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc cref="ReactiveObject" /> |
| | | 22 | | /// <inheritdoc cref="IPropertyValidationComponent{TViewModel}" /> |
| | | 23 | | /// <inheritdoc cref="IDisposable" /> |
| | | 24 | | /// <summary> |
| | | 25 | | /// More generic observable for determination of validity. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <remarks> |
| | | 28 | | /// We probably need a more 'complex' one, where the params of the validation block are |
| | | 29 | | /// passed through? |
| | | 30 | | /// Also, what about access to the view model to output the error message?. |
| | | 31 | | /// </remarks> |
| | | 32 | | public abstract class ModelObservableValidationBase<TViewModel> : ReactiveObject, IDisposable, IPropertyValidationCo |
| | | 33 | | { |
| | | 34 | | [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Disposed by field _dis |
| | 10 | 35 | | private readonly ReplaySubject<ValidationState> _lastValidationStateSubject = |
| | 10 | 36 | | new ReplaySubject<ValidationState>(1); |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// The list of property names this validator is referencing. |
| | | 40 | | /// </summary> |
| | 10 | 41 | | private readonly HashSet<string> _propertyNames = new HashSet<string>(); |
| | | 42 | | |
| | | 43 | | // the underlying connected observable for the validation change which is published |
| | | 44 | | private readonly IConnectableObservable<ValidationState> _validityConnectedObservable; |
| | | 45 | | |
| | 10 | 46 | | private readonly CompositeDisposable _disposables = new CompositeDisposable(); |
| | | 47 | | |
| | | 48 | | private bool _isActive; |
| | | 49 | | |
| | | 50 | | private bool _isValid; |
| | | 51 | | |
| | | 52 | | private ValidationText? _text; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Initializes a new instance of the <see cref="ModelObservableValidationBase{TViewModel}"/> class. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 58 | | /// <param name="validityObservable">Func to define if the viewModel is valid or not.</param> |
| | | 59 | | /// <param name="messageFunc">Func to define the validation error message based on the viewModel and validityObs |
| | | 60 | | public ModelObservableValidationBase( |
| | | 61 | | TViewModel viewModel, |
| | | 62 | | Func<TViewModel, IObservable<bool>> validityObservable, |
| | | 63 | | Func<TViewModel, bool, string> messageFunc) |
| | 0 | 64 | | : this(viewModel, validityObservable, (vm, state) => new ValidationText(messageFunc(vm, state))) |
| | | 65 | | { |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Initializes a new instance of the <see cref="ModelObservableValidationBase{TViewModel}"/> class. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="viewModel">ViewModel instance.</param> |
| | | 72 | | /// <param name="validityObservable">Func to define if the viewModel is valid or not.</param> |
| | | 73 | | /// <param name="messageFunc">Func to define the validation error message based on the viewModel and validityObs |
| | 10 | 74 | | protected ModelObservableValidationBase( |
| | 10 | 75 | | TViewModel viewModel, |
| | 10 | 76 | | Func<TViewModel, IObservable<bool>> validityObservable, |
| | 10 | 77 | | Func<TViewModel, bool, ValidationText> messageFunc) |
| | | 78 | | { |
| | 10 | 79 | | _disposables.Add(_lastValidationStateSubject.Do(s => |
| | 10 | 80 | | { |
| | 26 | 81 | | _isValid = s.IsValid; |
| | 26 | 82 | | _text = s.Text; |
| | 26 | 83 | | }).Subscribe()); |
| | | 84 | | |
| | 20 | 85 | | _validityConnectedObservable = Observable.Defer(() => validityObservable(viewModel)) |
| | 26 | 86 | | .Select(v => new ValidationState(v, messageFunc(viewModel, v), this)) |
| | 10 | 87 | | .Multicast(_lastValidationStateSubject); |
| | 10 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc/> |
| | 6 | 91 | | public int PropertyCount => _propertyNames.Count; |
| | | 92 | | |
| | | 93 | | /// <inheritdoc/> |
| | 6 | 94 | | public IEnumerable<string> Properties => _propertyNames.AsEnumerable(); |
| | | 95 | | |
| | | 96 | | /// <inheritdoc/> |
| | | 97 | | public ValidationText? Text |
| | | 98 | | { |
| | | 99 | | get |
| | | 100 | | { |
| | 32 | 101 | | Activate(); |
| | 32 | 102 | | return _text; |
| | | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <inheritdoc/> |
| | | 107 | | public bool IsValid |
| | | 108 | | { |
| | | 109 | | get |
| | | 110 | | { |
| | 48 | 111 | | Activate(); |
| | 48 | 112 | | return _isValid; |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <inheritdoc/> |
| | | 117 | | public IObservable<ValidationState> ValidationStatusChange |
| | | 118 | | { |
| | | 119 | | get |
| | | 120 | | { |
| | 18 | 121 | | Activate(); |
| | 18 | 122 | | return _validityConnectedObservable; |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <inheritdoc/> |
| | | 127 | | public void Dispose() |
| | | 128 | | { |
| | | 129 | | // Dispose of unmanaged resources. |
| | 0 | 130 | | Dispose(true); |
| | | 131 | | |
| | | 132 | | // Suppress finalization. |
| | 0 | 133 | | GC.SuppressFinalize(this); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <inheritdoc/> |
| | | 137 | | public bool ContainsProperty<TProp>(Expression<Func<TViewModel, TProp>> property, bool exclusively = false) |
| | | 138 | | { |
| | 2 | 139 | | if (property is null) |
| | | 140 | | { |
| | 0 | 141 | | throw new ArgumentNullException(nameof(property)); |
| | | 142 | | } |
| | | 143 | | |
| | 2 | 144 | | var propertyName = property.Body.GetPropertyPath(); |
| | 2 | 145 | | return ContainsPropertyName(propertyName, exclusively); |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <inheritdoc/> |
| | | 149 | | public bool ContainsPropertyName(string propertyName, bool exclusively = false) |
| | | 150 | | { |
| | 6 | 151 | | return exclusively |
| | 6 | 152 | | ? _propertyNames.Contains(propertyName) && _propertyNames.Count == 1 |
| | 6 | 153 | | : _propertyNames.Contains(propertyName); |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Disposes of the managed resources. |
| | | 158 | | /// </summary> |
| | | 159 | | /// <param name="disposing">If its getting called by the <see cref="BasePropertyValidation{TViewModel}.Dispose() |
| | | 160 | | protected virtual void Dispose(bool disposing) |
| | | 161 | | { |
| | 0 | 162 | | if (disposing) |
| | | 163 | | { |
| | 0 | 164 | | _disposables?.Dispose(); |
| | | 165 | | } |
| | 0 | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Adds a property to the list of this which this validation is associated with. |
| | | 170 | | /// </summary> |
| | | 171 | | /// <typeparam name="TProp">Any type.</typeparam> |
| | | 172 | | /// <param name="property">ViewModel property.</param> |
| | | 173 | | protected void AddProperty<TProp>(Expression<Func<TViewModel, TProp>> property) |
| | | 174 | | { |
| | 6 | 175 | | if (property is null) |
| | | 176 | | { |
| | 0 | 177 | | throw new ArgumentNullException(nameof(property)); |
| | | 178 | | } |
| | | 179 | | |
| | 6 | 180 | | var propertyName = property.Body.GetPropertyPath(); |
| | 6 | 181 | | _propertyNames.Add(propertyName); |
| | 6 | 182 | | } |
| | | 183 | | |
| | | 184 | | private void Activate() |
| | | 185 | | { |
| | 98 | 186 | | if (_isActive) |
| | | 187 | | { |
| | 88 | 188 | | return; |
| | | 189 | | } |
| | | 190 | | |
| | 10 | 191 | | _isActive = true; |
| | 10 | 192 | | _disposables.Add(_validityConnectedObservable.Connect()); |
| | 10 | 193 | | } |
| | | 194 | | } |
| | | 195 | | } |