| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="BoundedValue.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Numerics; |
| | | 9 | | using MyNet.Globalization.Facade; |
| | | 10 | | using MyNet.Observable.Behaviors; |
| | | 11 | | using MyNet.Observable.Localization; |
| | | 12 | | using MyNet.Observable.Validation.Validators; |
| | | 13 | | using MyNet.Primitives; |
| | | 14 | | using MyNet.Primitives.Intervals; |
| | | 15 | | |
| | | 16 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 17 | | namespace MyNet.Observable; |
| | | 18 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Observable numeric value with optional min/max bounds, reset support, and localized validation. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <typeparam name="T">The numeric type.</typeparam> |
| | | 24 | | public class BoundedValue<T> : ObservableObject, IBoundedValue<T> |
| | | 25 | | where T : struct, INumber<T> |
| | | 26 | | { |
| | 3 | 27 | | private static readonly Interval<T> UnboundedInterval = new(start: null, to: null); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Initializes a new instance of the <see cref="BoundedValue{T}"/> class without bounds. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="propertyDisplayKey">Translation key used as the field name in validation messages.</param> |
| | | 33 | | /// <param name="defaultValue">Optional default value used by <see cref="Reset"/>.</param> |
| | | 34 | | public BoundedValue(string propertyDisplayKey = nameof(Value), T? defaultValue = null) |
| | 12 | 35 | | : this(UnboundedInterval, propertyDisplayKey, defaultValue) { } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Initializes a new instance of the <see cref="BoundedValue{T}"/> class with inclusive bounds. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="min">Inclusive minimum, if any.</param> |
| | | 41 | | /// <param name="max">Inclusive maximum, if any.</param> |
| | | 42 | | /// <param name="propertyDisplayKey">Translation key used as the field name in validation messages.</param> |
| | | 43 | | /// <param name="defaultValue">Optional default value used by <see cref="Reset"/>.</param> |
| | | 44 | | public BoundedValue(T? min, T? max, string propertyDisplayKey = nameof(Value), T? defaultValue = null) |
| | 30 | 45 | | : this(new(min, max), propertyDisplayKey, defaultValue) { } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Initializes a new instance of the <see cref="BoundedValue{T}"/> class. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="range">Inclusive bounds.</param> |
| | | 51 | | /// <param name="propertyDisplayKey">Translation key used as the field name in validation messages.</param> |
| | | 52 | | /// <param name="defaultValue">Optional default value used by <see cref="Reset"/>.</param> |
| | 24 | 53 | | public BoundedValue(Interval<T> range, string propertyDisplayKey = nameof(Value), T? defaultValue = null) |
| | | 54 | | { |
| | 24 | 55 | | Range = range; |
| | 24 | 56 | | DefaultValue = defaultValue; |
| | 24 | 57 | | this.UseValidation(new BoundedValueValidator<T>(propertyDisplayKey)); |
| | 24 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets the configured inclusive bounds. |
| | | 62 | | /// </summary> |
| | | 63 | | public Interval<T> Range { get; private set; } |
| | | 64 | | |
| | | 65 | | /// <inheritdoc /> |
| | | 66 | | public T? Value |
| | | 67 | | { |
| | | 68 | | get; |
| | | 69 | | set |
| | | 70 | | { |
| | 27 | 71 | | if (SetProperty(ref field, value)) |
| | 24 | 72 | | Behaviors.GetOrDefault<ValidationBehavior<BoundedValue<T>>>()?.ValidateProperty(nameof(Value)); |
| | 27 | 73 | | } |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <inheritdoc /> |
| | | 77 | | public T? Min |
| | | 78 | | { |
| | 18 | 79 | | get => GetBound(Range.Start); |
| | | 80 | | set |
| | | 81 | | { |
| | 0 | 82 | | if (Equals(value, Min)) |
| | 0 | 83 | | return; |
| | | 84 | | |
| | 0 | 85 | | var before = Min; |
| | 0 | 86 | | Range = new(value, Max); |
| | 0 | 87 | | NotifyPropertyChanged(nameof(Min), before, value); |
| | 0 | 88 | | Behaviors.GetOrDefault<ValidationBehavior<BoundedValue<T>>>()?.ValidateProperty(nameof(Value)); |
| | 0 | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <inheritdoc /> |
| | | 93 | | public T? Max |
| | | 94 | | { |
| | 18 | 95 | | get => GetBound(Range.End); |
| | | 96 | | set |
| | | 97 | | { |
| | 0 | 98 | | if (Equals(value, Max)) |
| | 0 | 99 | | return; |
| | | 100 | | |
| | 0 | 101 | | var before = Max; |
| | 0 | 102 | | Range = new(Min, value); |
| | 0 | 103 | | NotifyPropertyChanged(nameof(Max), before, value); |
| | 0 | 104 | | Behaviors.GetOrDefault<ValidationBehavior<BoundedValue<T>>>()?.ValidateProperty(nameof(Value)); |
| | 0 | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <inheritdoc /> |
| | | 109 | | public T? DefaultValue { get; } |
| | | 110 | | |
| | | 111 | | /// <inheritdoc /> |
| | 0 | 112 | | public bool CanReset() => DefaultValue.HasValue && !Equals(Value, DefaultValue); |
| | | 113 | | |
| | | 114 | | /// <inheritdoc /> |
| | 3 | 115 | | public void Reset() => Value = DefaultValue; |
| | | 116 | | |
| | | 117 | | /// <inheritdoc /> |
| | 0 | 118 | | public override string? ToString() => Value?.ToString(); |
| | | 119 | | |
| | | 120 | | internal string BuildRangeValidationMessage(string propertyDisplayKey) |
| | | 121 | | { |
| | 18 | 122 | | var label = propertyDisplayKey.Translate(); |
| | | 123 | | |
| | 18 | 124 | | return Min is { } min && Max is { } max |
| | 18 | 125 | | ? ValidationResources.FieldMustBeBetween.FormatWith(CultureInfo.CurrentCulture, label, min, max) |
| | 18 | 126 | | : Min is { } minOnly |
| | 18 | 127 | | ? ValidationResources.FieldMustBeGreaterThanOrEqualTo.FormatWith(CultureInfo.CurrentCulture, label, minOnly) |
| | 18 | 128 | | : Max is { } maxOnly |
| | 18 | 129 | | ? ValidationResources.FieldMustBeLessThanOrEqualTo.FormatWith(CultureInfo.CurrentCulture, label, maxOnly) |
| | 18 | 130 | | : string.Empty; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | private static T? GetBound(IntervalBoundary<T>? boundary) => |
| | 36 | 134 | | boundary is { IsInclusive: true } ? boundary.Value.Value : null; |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Implicit conversion to the current value. |
| | | 138 | | /// </summary> |
| | 0 | 139 | | public static implicit operator T?(BoundedValue<T> value) => value.Value; |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Returns the current value as a nullable type, which is useful when the value may be null and you want to avoid n |
| | | 143 | | /// </summary> |
| | 0 | 144 | | public T? ToNullable() => Value; |
| | | 145 | | } |
| | | 146 | | |