| | | 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.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Reactive.Disposables; |
| | | 9 | | using System.Reactive.Linq; |
| | | 10 | | using ReactiveUI.Validation.Collections; |
| | | 11 | | using ReactiveUI.Validation.Components.Abstractions; |
| | | 12 | | using ReactiveUI.Validation.States; |
| | | 13 | | |
| | | 14 | | namespace ReactiveUI.Validation.Helpers |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc cref="ReactiveObject" /> |
| | | 17 | | /// <inheritdoc cref="IDisposable" /> |
| | | 18 | | /// <summary> |
| | | 19 | | /// Encapsulation of a validation with bindable properties. |
| | | 20 | | /// </summary> |
| | | 21 | | public class ValidationHelper : ReactiveObject, IDisposable |
| | | 22 | | { |
| | | 23 | | private readonly IValidationComponent _validation; |
| | | 24 | | |
| | | 25 | | [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Disposed with the _dis |
| | | 26 | | private readonly ObservableAsPropertyHelper<bool> _isValid; |
| | | 27 | | |
| | | 28 | | [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Disposed with the _dis |
| | | 29 | | private readonly ObservableAsPropertyHelper<ValidationText> _message; |
| | | 30 | | |
| | 38 | 31 | | private readonly CompositeDisposable _disposables = new CompositeDisposable(); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="ValidationHelper"/> class. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="validation">Validation property.</param> |
| | 38 | 37 | | public ValidationHelper(IValidationComponent validation) |
| | | 38 | | { |
| | 38 | 39 | | _validation = validation ?? throw new ArgumentNullException(nameof(validation)); |
| | | 40 | | |
| | 38 | 41 | | _isValid = _validation.ValidationStatusChange |
| | 94 | 42 | | .Select(v => v.IsValid) |
| | 38 | 43 | | .ToProperty(this, nameof(IsValid)) |
| | 38 | 44 | | .DisposeWith(_disposables); |
| | | 45 | | |
| | 38 | 46 | | _message = _validation.ValidationStatusChange |
| | 94 | 47 | | .Select(v => v.Text) |
| | 38 | 48 | | .ToProperty<ValidationHelper, ValidationText>(this, nameof(Message)) |
| | 38 | 49 | | .DisposeWith(_disposables); |
| | 38 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets a value indicating whether the validation is currently valid or not. |
| | | 54 | | /// </summary> |
| | 8 | 55 | | public bool IsValid => _isValid.Value; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the current (optional) validation message. |
| | | 59 | | /// </summary> |
| | 4 | 60 | | public ValidationText? Message => _message.Value; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets the observable for validation state changes. |
| | | 64 | | /// </summary> |
| | 6 | 65 | | public IObservable<ValidationState> ValidationChanged => _validation.ValidationStatusChange; |
| | | 66 | | |
| | | 67 | | /// <inheritdoc/> |
| | | 68 | | public void Dispose() |
| | | 69 | | { |
| | | 70 | | // Dispose of unmanaged resources. |
| | 0 | 71 | | Dispose(true); |
| | | 72 | | |
| | | 73 | | // Suppress finalization. |
| | 0 | 74 | | GC.SuppressFinalize(this); |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Disposes of the managed resources. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="disposing">If its getting called by the <see cref="Dispose()"/> method.</param> |
| | | 81 | | protected virtual void Dispose(bool disposing) |
| | | 82 | | { |
| | 0 | 83 | | if (disposing) |
| | | 84 | | { |
| | 0 | 85 | | _disposables?.Dispose(); |
| | | 86 | | } |
| | 0 | 87 | | } |
| | | 88 | | } |
| | | 89 | | } |