| | | 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.Collections.ObjectModel; |
| | | 9 | | using System.Diagnostics.CodeAnalysis; |
| | | 10 | | using System.Linq; |
| | | 11 | | using System.Reactive; |
| | | 12 | | using System.Reactive.Concurrency; |
| | | 13 | | using System.Reactive.Disposables; |
| | | 14 | | using System.Reactive.Linq; |
| | | 15 | | using System.Reactive.Subjects; |
| | | 16 | | using DynamicData; |
| | | 17 | | using DynamicData.Binding; |
| | | 18 | | using ReactiveUI.Validation.Collections; |
| | | 19 | | using ReactiveUI.Validation.Components.Abstractions; |
| | | 20 | | using ReactiveUI.Validation.States; |
| | | 21 | | |
| | | 22 | | namespace ReactiveUI.Validation.Contexts |
| | | 23 | | { |
| | | 24 | | /// <inheritdoc cref="ReactiveObject" /> |
| | | 25 | | /// <inheritdoc cref="IDisposable" /> |
| | | 26 | | /// <inheritdoc cref="IValidationComponent" /> |
| | | 27 | | /// <summary> |
| | | 28 | | /// The overall context for a view model under which validation takes place. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <remarks> |
| | | 31 | | /// Contains all of the <see cref="ReactiveUI.Validation.Components.Abstractions.IValidationComponent" /> instances |
| | | 32 | | /// applicable to the view model. |
| | | 33 | | /// </remarks> |
| | | 34 | | [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Field _disposables dispose |
| | | 35 | | public class ValidationContext : ReactiveObject, IDisposable, IValidationComponent |
| | | 36 | | { |
| | 66 | 37 | | private readonly SourceList<IValidationComponent> _validationSource = new SourceList<IValidationComponent>(); |
| | 66 | 38 | | private readonly ReplaySubject<ValidationState> _validationStatusChange = new ReplaySubject<ValidationState>(1); |
| | 66 | 39 | | private readonly ReplaySubject<bool> _validSubject = new ReplaySubject<bool>(1); |
| | | 40 | | |
| | | 41 | | private readonly ReadOnlyObservableCollection<IValidationComponent> _validations; |
| | | 42 | | private readonly IConnectableObservable<bool> _validationConnectable; |
| | | 43 | | private readonly ObservableAsPropertyHelper<ValidationText> _validationText; |
| | | 44 | | private readonly ObservableAsPropertyHelper<bool> _isValid; |
| | | 45 | | |
| | 66 | 46 | | private readonly CompositeDisposable _disposables = new CompositeDisposable(); |
| | | 47 | | private bool _isActive; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Initializes a new instance of the <see cref="ValidationContext"/> class. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="scheduler">Optional scheduler to use for the properties. Uses the current thread scheduler by d |
| | 66 | 53 | | public ValidationContext(IScheduler? scheduler = null) |
| | | 54 | | { |
| | 66 | 55 | | scheduler ??= CurrentThreadScheduler.Instance; |
| | 66 | 56 | | _validationSource |
| | 66 | 57 | | .Connect() |
| | 66 | 58 | | .ObserveOn(scheduler) |
| | 66 | 59 | | .Bind(out _validations) |
| | 66 | 60 | | .Subscribe() |
| | 66 | 61 | | .DisposeWith(_disposables); |
| | | 62 | | |
| | 66 | 63 | | _validationConnectable = _validations |
| | 66 | 64 | | .ToObservableChangeSet() |
| | 66 | 65 | | .ToCollection() |
| | 66 | 66 | | .StartWithEmpty() |
| | 66 | 67 | | .Select(validations => |
| | 132 | 68 | | validations |
| | 174 | 69 | | .Select(v => v.ValidationStatusChange) |
| | 132 | 70 | | .Merge()) |
| | 66 | 71 | | .Switch() |
| | 140 | 72 | | .Select(_ => GetIsValid()) |
| | 66 | 73 | | .Multicast(_validSubject); |
| | | 74 | | |
| | 66 | 75 | | _isValid = _validSubject |
| | 66 | 76 | | .StartWith(true) |
| | 66 | 77 | | .ToProperty(this, m => m.IsValid, scheduler: scheduler) |
| | 66 | 78 | | .DisposeWith(_disposables); |
| | | 79 | | |
| | 66 | 80 | | _validationText = _validSubject |
| | 66 | 81 | | .StartWith(true) |
| | 206 | 82 | | .Select(_ => BuildText()) |
| | 66 | 83 | | .ToProperty(this, m => m.Text, new ValidationText(), scheduler: scheduler) |
| | 66 | 84 | | .DisposeWith(_disposables); |
| | | 85 | | |
| | 66 | 86 | | _validSubject |
| | 140 | 87 | | .Select(_ => new ValidationState(IsValid, BuildText(), this)) |
| | 66 | 88 | | .Do(_validationStatusChange.OnNext) |
| | 66 | 89 | | .Subscribe() |
| | 66 | 90 | | .DisposeWith(_disposables); |
| | 66 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets an observable for the Valid state. |
| | | 95 | | /// </summary> |
| | | 96 | | public IObservable<bool> Valid |
| | | 97 | | { |
| | | 98 | | get |
| | | 99 | | { |
| | 2 | 100 | | Activate(); |
| | 2 | 101 | | return _validSubject.AsObservable(); |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Gets get the list of validations. |
| | | 107 | | /// </summary> |
| | 80 | 108 | | public ReadOnlyObservableCollection<IValidationComponent> Validations => _validations; |
| | | 109 | | |
| | | 110 | | /// <inheritdoc/> |
| | | 111 | | [SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "Reviewed." |
| | | 112 | | public bool IsValid |
| | | 113 | | { |
| | | 114 | | get |
| | | 115 | | { |
| | 122 | 116 | | Activate(); |
| | 122 | 117 | | return _isValid.Value; |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <inheritdoc /> |
| | | 122 | | public IObservable<ValidationState> ValidationStatusChange |
| | | 123 | | { |
| | | 124 | | get |
| | | 125 | | { |
| | 10 | 126 | | Activate(); |
| | 10 | 127 | | return _validationStatusChange.AsObservable(); |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <inheritdoc /> |
| | | 132 | | public ValidationText Text |
| | | 133 | | { |
| | | 134 | | get |
| | | 135 | | { |
| | 18 | 136 | | Activate(); |
| | 18 | 137 | | return _validationText.Value; |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Adds a validation into the validations collection. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <param name="validation">Validation component to be added into the collection.</param> |
| | 54 | 145 | | public void Add(IValidationComponent validation) => _validationSource.Add(validation); |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Returns if the whole context is valid checking all the validations. |
| | | 149 | | /// </summary> |
| | | 150 | | /// <returns>Returns true if the <see cref="ValidationContext"/> is valid, otherwise false.</returns> |
| | 244 | 151 | | public bool GetIsValid() => _validations.Count == 0 || _validations.All(v => v.IsValid); |
| | | 152 | | |
| | | 153 | | /// <inheritdoc/> |
| | | 154 | | public void Dispose() |
| | | 155 | | { |
| | | 156 | | // Dispose of unmanaged resources. |
| | 0 | 157 | | Dispose(true); |
| | | 158 | | |
| | | 159 | | // Suppress finalization. |
| | 0 | 160 | | GC.SuppressFinalize(this); |
| | 0 | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Disposes of the managed resources. |
| | | 165 | | /// </summary> |
| | | 166 | | /// <param name="disposing">If its getting called by the <see cref="Dispose()"/> method.</param> |
| | | 167 | | protected virtual void Dispose(bool disposing) |
| | | 168 | | { |
| | 0 | 169 | | if (disposing) |
| | | 170 | | { |
| | 0 | 171 | | _disposables?.Dispose(); |
| | | 172 | | } |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | private void Activate() |
| | | 176 | | { |
| | 152 | 177 | | if (_isActive) |
| | | 178 | | { |
| | 118 | 179 | | return; |
| | | 180 | | } |
| | | 181 | | |
| | 34 | 182 | | _isActive = true; |
| | 34 | 183 | | _disposables.Add(_validationConnectable.Connect()); |
| | 34 | 184 | | } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Build a list of the validation text for each invalid component. |
| | | 188 | | /// </summary> |
| | | 189 | | /// <returns> |
| | | 190 | | /// Returns the <see cref="ValidationText"/> with all the error messages from the non valid components. |
| | | 191 | | /// </returns> |
| | | 192 | | private ValidationText BuildText() => |
| | 214 | 193 | | new ValidationText(_validations |
| | 418 | 194 | | .Where(p => !p.IsValid && p.Text != null) |
| | 334 | 195 | | .Select(p => p.Text!)); |
| | | 196 | | } |
| | | 197 | | } |