using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace ksBroadcastingTestClient
{
///
/// Just a cheap implementation of Viewmodels with auto-update capabilities - the laziest way to get this testclient doing it's job
///
public abstract class KSObservableObject : INotifyPropertyChanged
{
#region Notification handling
private Dictionary _propertyDict = new Dictionary();
public event PropertyChangedEventHandler PropertyChanged;
protected bool Set(T value, [CallerMemberName] string propertyName = null)
{
bool hasChanged = false;
if (!_propertyDict.ContainsKey(propertyName))
{
_propertyDict.Add(propertyName, value);
hasChanged = true;
}
else
{
hasChanged = !object.Equals(_propertyDict[propertyName], value);
_propertyDict[propertyName] = value;
}
if (hasChanged)
{
NotifyUpdate(propertyName);
}
return hasChanged;
}
protected void NotifyUpdate(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected T Get([CallerMemberName] string propertyName = null)
{
if (!_propertyDict.ContainsKey(propertyName))
return default(T);
return (T)_propertyDict[propertyName];
}
#endregion
}
}