| | | 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.Linq; |
| | | 9 | | using System.Linq.Expressions; |
| | | 10 | | using System.Reactive; |
| | | 11 | | using System.Reactive.Disposables; |
| | | 12 | | using System.Reactive.Linq; |
| | | 13 | | using ReactiveUI.Validation.Abstractions; |
| | | 14 | | using ReactiveUI.Validation.Extensions; |
| | | 15 | | using ReactiveUI.Validation.Formatters; |
| | | 16 | | using ReactiveUI.Validation.Formatters.Abstractions; |
| | | 17 | | using ReactiveUI.Validation.Helpers; |
| | | 18 | | using ReactiveUI.Validation.States; |
| | | 19 | | using ReactiveUI.Validation.ValidationBindings.Abstractions; |
| | | 20 | | using Splat; |
| | | 21 | | |
| | | 22 | | namespace ReactiveUI.Validation.ValidationBindings |
| | | 23 | | { |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public sealed class ValidationBinding : IValidationBinding |
| | | 26 | | { |
| | 26 | 27 | | private readonly CompositeDisposable _disposables = new CompositeDisposable(); |
| | | 28 | | |
| | 26 | 29 | | private ValidationBinding(IObservable<Unit> validationObservable) |
| | | 30 | | { |
| | 26 | 31 | | _disposables.Add(validationObservable.Subscribe()); |
| | 26 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Creates a binding between a ViewModel property and a view property. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <typeparam name="TView">ViewFor of ViewModel type.</typeparam> |
| | | 38 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 39 | | /// <typeparam name="TViewModelProperty">ViewModel property type.</typeparam> |
| | | 40 | | /// <typeparam name="TViewProperty">View property type.</typeparam> |
| | | 41 | | /// <param name="view">View instance.</param> |
| | | 42 | | /// <param name="viewModelProperty">ViewModel property.</param> |
| | | 43 | | /// <param name="viewProperty">View property.</param> |
| | | 44 | | /// <param name="formatter">Validation formatter. Defaults to the <see cref="SingleLineFormatter"/>.</param> |
| | | 45 | | /// <param name="strict">Indicates if the ViewModel property to find is unique.</param> |
| | | 46 | | /// <returns>Returns a validation component.</returns> |
| | | 47 | | public static IValidationBinding ForProperty<TView, TViewModel, TViewModelProperty, TViewProperty>( |
| | | 48 | | TView view, |
| | | 49 | | Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty, |
| | | 50 | | Expression<Func<TView, TViewProperty>> viewProperty, |
| | | 51 | | IValidationTextFormatter<string>? formatter = null, |
| | | 52 | | bool strict = true) |
| | | 53 | | where TView : IViewFor<TViewModel> |
| | | 54 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 55 | | { |
| | 16 | 56 | | if (view is null) |
| | | 57 | | { |
| | 0 | 58 | | throw new ArgumentNullException(nameof(view)); |
| | | 59 | | } |
| | | 60 | | |
| | 16 | 61 | | if (viewModelProperty is null) |
| | | 62 | | { |
| | 0 | 63 | | throw new ArgumentNullException(nameof(viewModelProperty)); |
| | | 64 | | } |
| | | 65 | | |
| | 16 | 66 | | if (viewProperty is null) |
| | | 67 | | { |
| | 0 | 68 | | throw new ArgumentNullException(nameof(viewProperty)); |
| | | 69 | | } |
| | | 70 | | |
| | 16 | 71 | | formatter ??= SingleLineFormatter.Default; |
| | | 72 | | |
| | 16 | 73 | | var vcObs = view |
| | 16 | 74 | | .WhenAnyValue(v => v.ViewModel) |
| | 32 | 75 | | .Where(vm => vm != null) |
| | 16 | 76 | | .Select( |
| | 32 | 77 | | viewModel => viewModel! |
| | 32 | 78 | | .ValidationContext |
| | 32 | 79 | | .ResolveFor(viewModelProperty, strict) |
| | 52 | 80 | | .Select(x => x.ValidationStatusChange) |
| | 32 | 81 | | .CombineLatest()) |
| | 16 | 82 | | .Switch() |
| | 16 | 83 | | .Select( |
| | 36 | 84 | | states => states |
| | 60 | 85 | | .Select(state => formatter.Format(state.Text)) |
| | 60 | 86 | | .FirstOrDefault(msg => !string.IsNullOrEmpty(msg)) ?? string.Empty); |
| | | 87 | | |
| | 16 | 88 | | var updateObs = BindToView(vcObs, view, viewProperty) |
| | 36 | 89 | | .Select(_ => Unit.Default); |
| | | 90 | | |
| | 16 | 91 | | return new ValidationBinding(updateObs); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Creates a binding from a specified ViewModel property to a provided action. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <typeparam name="TView">ViewFor of ViewModel type.</typeparam> |
| | | 98 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 99 | | /// <typeparam name="TViewModelProperty">ViewModel property type.</typeparam> |
| | | 100 | | /// <typeparam name="TOut">Action return type.</typeparam> |
| | | 101 | | /// <param name="view">View instance.</param> |
| | | 102 | | /// <param name="viewModelProperty">ViewModel property.</param> |
| | | 103 | | /// <param name="action">Action to be executed.</param> |
| | | 104 | | /// <param name="formatter">Validation formatter.</param> |
| | | 105 | | /// <param name="strict">Indicates if the ViewModel property to find is unique.</param> |
| | | 106 | | /// <returns>Returns a validation component.</returns> |
| | | 107 | | public static IValidationBinding ForProperty<TView, TViewModel, TViewModelProperty, TOut>( |
| | | 108 | | TView view, |
| | | 109 | | Expression<Func<TViewModel, TViewModelProperty>> viewModelProperty, |
| | | 110 | | Action<IList<ValidationState>, IList<TOut>> action, |
| | | 111 | | IValidationTextFormatter<TOut> formatter, |
| | | 112 | | bool strict = true) |
| | | 113 | | where TView : IViewFor<TViewModel> |
| | | 114 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 115 | | { |
| | 0 | 116 | | if (view is null) |
| | | 117 | | { |
| | 0 | 118 | | throw new ArgumentNullException(nameof(view)); |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | if (viewModelProperty is null) |
| | | 122 | | { |
| | 0 | 123 | | throw new ArgumentNullException(nameof(viewModelProperty)); |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | if (action is null) |
| | | 127 | | { |
| | 0 | 128 | | throw new ArgumentNullException(nameof(action)); |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | if (formatter == null) |
| | | 132 | | { |
| | 0 | 133 | | throw new ArgumentNullException(nameof(formatter)); |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | var vcObs = view |
| | 0 | 137 | | .WhenAnyValue(v => v.ViewModel) |
| | 0 | 138 | | .Where(vm => vm != null) |
| | 0 | 139 | | .Select( |
| | 0 | 140 | | viewModel => viewModel! |
| | 0 | 141 | | .ValidationContext |
| | 0 | 142 | | .ResolveFor(viewModelProperty, strict) |
| | 0 | 143 | | .Select(x => x.ValidationStatusChange) |
| | 0 | 144 | | .CombineLatest()) |
| | 0 | 145 | | .Switch() |
| | 0 | 146 | | .Select(states => new |
| | 0 | 147 | | { |
| | 0 | 148 | | ValidationChange = states, |
| | 0 | 149 | | Formatted = states |
| | 0 | 150 | | .Select(state => formatter.Format(state.Text)) |
| | 0 | 151 | | .ToList() |
| | 0 | 152 | | }) |
| | 0 | 153 | | .Do(r => action(r.ValidationChange, r.Formatted)) |
| | 0 | 154 | | .Select(_ => Unit.Default); |
| | | 155 | | |
| | 0 | 156 | | return new ValidationBinding(vcObs); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Creates a binding between a <see cref="ValidationHelper" /> and a specified View property. |
| | | 161 | | /// </summary> |
| | | 162 | | /// <typeparam name="TView">ViewFor of ViewModel type.</typeparam> |
| | | 163 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 164 | | /// <typeparam name="TViewProperty">View property type.</typeparam> |
| | | 165 | | /// <param name="view">View instance.</param> |
| | | 166 | | /// <param name="viewModelHelperProperty">ViewModel's ValidationHelper property.</param> |
| | | 167 | | /// <param name="viewProperty">View property to bind the validation message.</param> |
| | | 168 | | /// <param name="formatter">Validation formatter.</param> |
| | | 169 | | /// <returns>Returns a validation component.</returns> |
| | | 170 | | public static IValidationBinding ForValidationHelperProperty<TView, TViewModel, TViewProperty>( |
| | | 171 | | TView view, |
| | | 172 | | Expression<Func<TViewModel?, ValidationHelper>> viewModelHelperProperty, |
| | | 173 | | Expression<Func<TView, TViewProperty>> viewProperty, |
| | | 174 | | IValidationTextFormatter<string>? formatter = null) |
| | | 175 | | where TView : IViewFor<TViewModel> |
| | | 176 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 177 | | { |
| | 6 | 178 | | if (view is null) |
| | | 179 | | { |
| | 0 | 180 | | throw new ArgumentNullException(nameof(view)); |
| | | 181 | | } |
| | | 182 | | |
| | 6 | 183 | | if (viewModelHelperProperty is null) |
| | | 184 | | { |
| | 0 | 185 | | throw new ArgumentNullException(nameof(viewModelHelperProperty)); |
| | | 186 | | } |
| | | 187 | | |
| | 6 | 188 | | if (viewProperty is null) |
| | | 189 | | { |
| | 0 | 190 | | throw new ArgumentNullException(nameof(viewProperty)); |
| | | 191 | | } |
| | | 192 | | |
| | 6 | 193 | | formatter ??= SingleLineFormatter.Default; |
| | | 194 | | |
| | 6 | 195 | | var vcObs = view |
| | 6 | 196 | | .WhenAnyValue(v => v.ViewModel) |
| | 12 | 197 | | .Where(vm => vm != null) |
| | 6 | 198 | | .Select( |
| | 12 | 199 | | viewModel => viewModel |
| | 12 | 200 | | .WhenAnyValue(viewModelHelperProperty) |
| | 18 | 201 | | .SelectMany(vy => vy.ValidationChanged)) |
| | 6 | 202 | | .Switch() |
| | 20 | 203 | | .Select(vc => formatter.Format(vc.Text)); |
| | | 204 | | |
| | 6 | 205 | | var updateObs = BindToView(vcObs, view, viewProperty) |
| | 20 | 206 | | .Select(_ => Unit.Default); |
| | | 207 | | |
| | 6 | 208 | | return new ValidationBinding(updateObs); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | /// <summary> |
| | | 212 | | /// Creates a binding from a <see cref="ValidationHelper" /> to a specified action. |
| | | 213 | | /// </summary> |
| | | 214 | | /// <typeparam name="TView">ViewFor of ViewModel type.</typeparam> |
| | | 215 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 216 | | /// <typeparam name="TOut">Action return type.</typeparam> |
| | | 217 | | /// <param name="view">View instance.</param> |
| | | 218 | | /// <param name="viewModelHelperProperty">ViewModel's ValidationHelper property.</param> |
| | | 219 | | /// <param name="action">Action to be executed.</param> |
| | | 220 | | /// <param name="formatter">Validation formatter.</param> |
| | | 221 | | /// <returns>Returns a validation component.</returns> |
| | | 222 | | public static IValidationBinding ForValidationHelperProperty<TView, TViewModel, TOut>( |
| | | 223 | | TView view, |
| | | 224 | | Expression<Func<TViewModel?, ValidationHelper>> viewModelHelperProperty, |
| | | 225 | | Action<ValidationState, TOut> action, |
| | | 226 | | IValidationTextFormatter<TOut> formatter) |
| | | 227 | | where TView : IViewFor<TViewModel> |
| | | 228 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 229 | | { |
| | 0 | 230 | | if (view is null) |
| | | 231 | | { |
| | 0 | 232 | | throw new ArgumentNullException(nameof(view)); |
| | | 233 | | } |
| | | 234 | | |
| | 0 | 235 | | if (viewModelHelperProperty is null) |
| | | 236 | | { |
| | 0 | 237 | | throw new ArgumentNullException(nameof(viewModelHelperProperty)); |
| | | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | if (action is null) |
| | | 241 | | { |
| | 0 | 242 | | throw new ArgumentNullException(nameof(action)); |
| | | 243 | | } |
| | | 244 | | |
| | 0 | 245 | | if (formatter is null) |
| | | 246 | | { |
| | 0 | 247 | | throw new ArgumentNullException(nameof(formatter)); |
| | | 248 | | } |
| | | 249 | | |
| | 0 | 250 | | var vcObs = view |
| | 0 | 251 | | .WhenAnyValue(v => v.ViewModel) |
| | 0 | 252 | | .Where(vm => vm != null) |
| | 0 | 253 | | .Select( |
| | 0 | 254 | | viewModel => viewModel |
| | 0 | 255 | | .WhenAnyValue(viewModelHelperProperty) |
| | 0 | 256 | | .SelectMany(vy => vy.ValidationChanged)) |
| | 0 | 257 | | .Switch() |
| | 0 | 258 | | .Select(vc => new { ValidationChange = vc, Formatted = formatter.Format(vc.Text) }); |
| | | 259 | | |
| | 0 | 260 | | var updateObs = vcObs |
| | 0 | 261 | | .Do(r => action(r.ValidationChange, r.Formatted)) |
| | 0 | 262 | | .Select(_ => Unit.Default); |
| | | 263 | | |
| | 0 | 264 | | return new ValidationBinding(updateObs); |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Creates a binding between a ViewModel and a specified action. |
| | | 269 | | /// </summary> |
| | | 270 | | /// <typeparam name="TView">ViewFor of ViewModel type.</typeparam> |
| | | 271 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 272 | | /// <typeparam name="TOut">Action return type.</typeparam> |
| | | 273 | | /// <param name="view">View instance.</param> |
| | | 274 | | /// <param name="action">Action to be executed.</param> |
| | | 275 | | /// <param name="formatter">Validation formatter.</param> |
| | | 276 | | /// <returns>Returns a validation component.</returns> |
| | | 277 | | public static IValidationBinding ForViewModel<TView, TViewModel, TOut>( |
| | | 278 | | TView view, |
| | | 279 | | Action<TOut> action, |
| | | 280 | | IValidationTextFormatter<TOut> formatter) |
| | | 281 | | where TView : IViewFor<TViewModel> |
| | | 282 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 283 | | { |
| | 0 | 284 | | if (view is null) |
| | | 285 | | { |
| | 0 | 286 | | throw new ArgumentNullException(nameof(view)); |
| | | 287 | | } |
| | | 288 | | |
| | 0 | 289 | | if (action is null) |
| | | 290 | | { |
| | 0 | 291 | | throw new ArgumentNullException(nameof(action)); |
| | | 292 | | } |
| | | 293 | | |
| | 0 | 294 | | if (formatter is null) |
| | | 295 | | { |
| | 0 | 296 | | throw new ArgumentNullException(nameof(formatter)); |
| | | 297 | | } |
| | | 298 | | |
| | 0 | 299 | | var vcObs = view |
| | 0 | 300 | | .WhenAnyValue(v => v.ViewModel) |
| | 0 | 301 | | .Where(vm => vm != null) |
| | 0 | 302 | | .Select(vm => vm!.ValidationContext.Text) |
| | 0 | 303 | | .Select(formatter.Format); |
| | | 304 | | |
| | 0 | 305 | | var updateObs = vcObs |
| | 0 | 306 | | .Do(action) |
| | 0 | 307 | | .Select(_ => Unit.Default); |
| | | 308 | | |
| | 0 | 309 | | return new ValidationBinding(updateObs); |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// Creates a binding between a ViewModel and a View property. |
| | | 314 | | /// </summary> |
| | | 315 | | /// <typeparam name="TView">ViewFor of ViewModel type.</typeparam> |
| | | 316 | | /// <typeparam name="TViewModel">ViewModel type.</typeparam> |
| | | 317 | | /// <typeparam name="TViewProperty">View property type.</typeparam> |
| | | 318 | | /// <param name="view">View instance.</param> |
| | | 319 | | /// <param name="viewProperty">View property to bind the validation message.</param> |
| | | 320 | | /// <param name="formatter">Validation formatter.</param> |
| | | 321 | | /// <returns>Returns a validation component.</returns> |
| | | 322 | | public static IValidationBinding ForViewModel<TView, TViewModel, TViewProperty>( |
| | | 323 | | TView view, |
| | | 324 | | Expression<Func<TView, TViewProperty>> viewProperty, |
| | | 325 | | IValidationTextFormatter<string>? formatter = null) |
| | | 326 | | where TView : IViewFor<TViewModel> |
| | | 327 | | where TViewModel : ReactiveObject, IValidatableViewModel |
| | | 328 | | { |
| | 4 | 329 | | if (view is null) |
| | | 330 | | { |
| | 0 | 331 | | throw new ArgumentNullException(nameof(view)); |
| | | 332 | | } |
| | | 333 | | |
| | 4 | 334 | | if (viewProperty is null) |
| | | 335 | | { |
| | 0 | 336 | | throw new ArgumentNullException(nameof(view)); |
| | | 337 | | } |
| | | 338 | | |
| | 4 | 339 | | formatter ??= SingleLineFormatter.Default; |
| | | 340 | | |
| | 4 | 341 | | var vcObs = view |
| | 4 | 342 | | .WhenAnyValue(v => v.ViewModel) |
| | 8 | 343 | | .Where(vm => vm != null) |
| | 8 | 344 | | .SelectMany(vm => vm!.ValidationContext.ValidationStatusChange) |
| | 8 | 345 | | .Select(vc => formatter.Format(vc.Text)); |
| | | 346 | | |
| | 4 | 347 | | var updateObs = BindToView(vcObs, view, viewProperty) |
| | 8 | 348 | | .Select(_ => Unit.Default); |
| | | 349 | | |
| | 4 | 350 | | return new ValidationBinding(updateObs); |
| | | 351 | | } |
| | | 352 | | |
| | | 353 | | /// <inheritdoc/> |
| | | 354 | | public void Dispose() |
| | | 355 | | { |
| | | 356 | | // Dispose of unmanaged resources. |
| | 0 | 357 | | Dispose(true); |
| | 0 | 358 | | } |
| | | 359 | | |
| | | 360 | | /// <summary> |
| | | 361 | | /// Creates a binding to a View property. |
| | | 362 | | /// </summary> |
| | | 363 | | /// <typeparam name="TView">ViewFor of ViewModel type.</typeparam> |
| | | 364 | | /// <typeparam name="TViewProperty">ViewModel type.</typeparam> |
| | | 365 | | /// <typeparam name="TTarget">Target type.</typeparam> |
| | | 366 | | /// <param name="valueChange">Observable value change.</param> |
| | | 367 | | /// <param name="target">Target instance.</param> |
| | | 368 | | /// <param name="viewProperty">View property.</param> |
| | | 369 | | /// <returns>Returns a validation component.</returns> |
| | | 370 | | private static IObservable<string> BindToView<TView, TViewProperty, TTarget>( |
| | | 371 | | IObservable<string> valueChange, |
| | | 372 | | TTarget target, |
| | | 373 | | Expression<Func<TView, TViewProperty>> viewProperty) |
| | | 374 | | where TTarget : notnull |
| | | 375 | | { |
| | 26 | 376 | | var viewExpression = Reflection.Rewrite(viewProperty.Body); |
| | | 377 | | |
| | 26 | 378 | | var setter = Reflection.GetValueSetterOrThrow(viewExpression.GetMemberInfo())!; |
| | | 379 | | |
| | 26 | 380 | | if (viewExpression.GetParent().NodeType == ExpressionType.Parameter) |
| | | 381 | | { |
| | 24 | 382 | | return valueChange |
| | 24 | 383 | | .Do( |
| | 60 | 384 | | x => setter(target, x, viewExpression.GetArgumentsArray()), |
| | 0 | 385 | | ex => LogHost.Default.Error(ex, $"{viewExpression} Binding received an Exception!")); |
| | | 386 | | } |
| | | 387 | | |
| | 2 | 388 | | var bindInfo = valueChange.CombineLatest( |
| | 4 | 389 | | target.WhenAnyDynamic(viewExpression.GetParent(), x => x.Value), |
| | 4 | 390 | | (val, host) => new { val, host }); |
| | | 391 | | |
| | 2 | 392 | | return bindInfo |
| | 4 | 393 | | .Where(x => x.host != null) |
| | 2 | 394 | | .Do( |
| | 4 | 395 | | x => setter(x.host, x.val, viewExpression.GetArgumentsArray()), |
| | 0 | 396 | | ex => LogHost.Default.Error(ex, $"{viewExpression} Binding received an Exception!")) |
| | 4 | 397 | | .Select(v => v.val); |
| | | 398 | | } |
| | | 399 | | |
| | | 400 | | /// <summary> |
| | | 401 | | /// Disposes of the managed resources. |
| | | 402 | | /// </summary> |
| | | 403 | | /// <param name="disposing">If its getting called by the <see cref="Dispose()"/> method.</param> |
| | | 404 | | private void Dispose(bool disposing) |
| | | 405 | | { |
| | 0 | 406 | | if (disposing) |
| | | 407 | | { |
| | 0 | 408 | | _disposables.Dispose(); |
| | | 409 | | } |
| | 0 | 410 | | } |
| | | 411 | | } |
| | | 412 | | } |