using System.Collections.Generic; using System.ComponentModel; using System.Runtime.CompilerServices; using Artemis.Core.Properties; namespace Artemis.Core; /// /// Represents a basic bindable class which notifies when a property value changes. /// public abstract class CorePropertyChanged : INotifyPropertyChanged { /// /// Occurs when a property value changes. /// public event PropertyChangedEventHandler? PropertyChanged; #region Methods /// /// Checks if the property already matches the desired value or needs to be updated. /// /// Type of the property. /// Reference to the backing-filed. /// Value to apply. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] protected bool RequiresUpdate(ref T storage, T value) { return !EqualityComparer.Default.Equals(storage, value); } /// /// Checks if the property already matches the desired value and updates it if not. /// /// Type of the property. /// Reference to the backing-filed. /// Value to apply. /// /// Name of the property used to notify listeners. This value is optional /// and can be provided automatically when invoked from compilers that support /// . /// /// true if the value was changed, false if the existing value matched the desired value. [NotifyPropertyChangedInvocator] protected bool SetAndNotify(ref T storage, T value, [CallerMemberName] string? propertyName = null) { if (!RequiresUpdate(ref storage, value)) return false; storage = value; // ReSharper disable once ExplicitCallerInfoArgument OnPropertyChanged(propertyName); return true; } /// /// Triggers the -event when a a property value has changed. /// /// /// Name of the property used to notify listeners. This value is optional /// and can be provided automatically when invoked from compilers that support /// . /// protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion }