2018-07-02 23:31:16 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace butterflow_ui
|
|
|
|
|
{
|
2018-07-03 18:59:28 -05:00
|
|
|
|
/// <summary> A class responsible for implementing the <see cref="INotifyPropertyChanged"/> interface and helper functions. </summary>
|
2018-07-02 23:31:16 -05:00
|
|
|
|
public abstract class PropertyChangedAlerter : INotifyPropertyChanged
|
|
|
|
|
{
|
|
|
|
|
#region Members
|
|
|
|
|
|
|
|
|
|
/// <summary> Occurs when a property value changes. </summary>
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
/// <summary> A list of properties to always call as updated. Generally used for composite properties. </summary>
|
|
|
|
|
private List<string> alwaysCall = new List<string>();
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Methods
|
|
|
|
|
|
2018-07-09 14:49:48 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes the property changed action. This alerts subscribers to its change in value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name"> (Optional) The name of the property. </param>
|
|
|
|
|
/// <example>
|
|
|
|
|
/// This will automatically pass in "SomeProperty" as the property name, derived useing the
|
|
|
|
|
/// <see cref="CallerMemberNameAttribute" /> attribute.
|
|
|
|
|
/// <code lang="cs" title="Automatic Property Detection">
|
|
|
|
|
/// public bool SomeProperty
|
|
|
|
|
/// {
|
|
|
|
|
/// get
|
|
|
|
|
/// {
|
|
|
|
|
/// return this.someProperty;
|
|
|
|
|
/// }
|
|
|
|
|
/// set
|
|
|
|
|
/// {
|
|
|
|
|
/// this.someProperty = value;
|
|
|
|
|
/// OnPropertyChanged();
|
|
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// </code>
|
|
|
|
|
/// </example>
|
2018-07-02 23:31:16 -05:00
|
|
|
|
protected virtual void OnPropertyChanged([CallerMemberName]string name = null)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(name))
|
|
|
|
|
{
|
|
|
|
|
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
|
|
|
|
foreach (var updatedProperty in this.alwaysCall)
|
|
|
|
|
{
|
|
|
|
|
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(updatedProperty));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-09 14:49:48 -05:00
|
|
|
|
/// <summary> Executes when all properties are changed and should be updated. </summary>
|
2018-07-05 02:00:37 -05:00
|
|
|
|
protected virtual void OnAllPropertiesChanged()
|
|
|
|
|
{
|
|
|
|
|
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(string.Empty));
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-02 23:31:16 -05:00
|
|
|
|
/// <summary> Adds a property that will always be called when any property is updated.. </summary>
|
|
|
|
|
/// <param name="name"> The name of the property. </param>
|
|
|
|
|
public void AddConstantCallProperty(string name)
|
|
|
|
|
{
|
2018-07-09 14:49:48 -05:00
|
|
|
|
if (this.alwaysCall == null)
|
2018-07-05 02:00:37 -05:00
|
|
|
|
{
|
|
|
|
|
// This item has been deserialized and the list needs to be reinitialized.
|
|
|
|
|
this.alwaysCall = new List<string>();
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-02 23:31:16 -05:00
|
|
|
|
if (!this.alwaysCall.Any(c => c.Equals(name, StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
{
|
|
|
|
|
this.alwaysCall.Add(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|