| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.ViewModels; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Base class for all Chronicis ViewModels. |
| | | 8 | | /// Implements <see cref="INotifyPropertyChanged"/> with a SetField helper |
| | | 9 | | /// so that Blazor components can subscribe to property changes and call |
| | | 10 | | /// <see cref="Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged"/> |
| | | 11 | | /// without pulling MudBlazor or rendering infrastructure into ViewModels. |
| | | 12 | | /// </summary> |
| | | 13 | | public abstract class ViewModelBase : INotifyPropertyChanged |
| | | 14 | | { |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | public event PropertyChangedEventHandler? PropertyChanged; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Sets <paramref name="field"/> to <paramref name="value"/> and raises |
| | | 20 | | /// <see cref="PropertyChanged"/> if the value has changed. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <typeparam name="T">The type of the property.</typeparam> |
| | | 23 | | /// <param name="field">Reference to the backing field.</param> |
| | | 24 | | /// <param name="value">The new value.</param> |
| | | 25 | | /// <param name="propertyName"> |
| | | 26 | | /// The property name, automatically provided by the compiler via |
| | | 27 | | /// <see cref="CallerMemberNameAttribute"/>. |
| | | 28 | | /// </param> |
| | | 29 | | /// <returns> |
| | | 30 | | /// <c>true</c> if the value changed and <see cref="PropertyChanged"/> was raised; |
| | | 31 | | /// <c>false</c> if the value was already equal. |
| | | 32 | | /// </returns> |
| | | 33 | | protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = "") |
| | | 34 | | { |
| | 2887 | 35 | | if (EqualityComparer<T>.Default.Equals(field, value)) |
| | 1175 | 36 | | return false; |
| | | 37 | | |
| | 1712 | 38 | | field = value; |
| | 1712 | 39 | | RaisePropertyChanged(propertyName); |
| | 1712 | 40 | | return true; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Raises <see cref="PropertyChanged"/> for the specified property name. |
| | | 45 | | /// Use this when a computed/dependent property needs to notify separately. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="propertyName">The name of the property that changed.</param> |
| | | 48 | | protected void RaisePropertyChanged(string propertyName) |
| | | 49 | | { |
| | 1722 | 50 | | OnPropertyChanged(propertyName); |
| | 1722 | 51 | | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| | 373 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Called before <see cref="PropertyChanged"/> is raised. |
| | | 56 | | /// Override in subclasses to react to specific property changes. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="propertyName">The name of the property that changed.</param> |
| | | 59 | | protected virtual void OnPropertyChanged(string propertyName) |
| | | 60 | | { |
| | 1709 | 61 | | } |
| | | 62 | | } |