2023-07-12 22:41:31 +00:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Reflection;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
|
|
|
|
namespace MSFSPopoutPanelManager.Shared
|
|
|
|
|
{
|
|
|
|
|
public class ObservableObject : INotifyPropertyChanged
|
|
|
|
|
{
|
|
|
|
|
// Implements Fody.Changed
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
2023-07-12 22:41:31 +00:00
|
|
|
|
protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
{
|
2024-02-28 02:44:21 +00:00
|
|
|
|
var hasJsonIgnoreAttribute = false;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
|
|
|
|
|
var type = sender.GetType();
|
2024-02-28 02:44:21 +00:00
|
|
|
|
if (e.PropertyName != null)
|
2023-07-12 22:41:31 +00:00
|
|
|
|
{
|
2024-02-28 02:44:21 +00:00
|
|
|
|
var propertyInfo = type.GetProperty(e.PropertyName);
|
|
|
|
|
|
|
|
|
|
if (propertyInfo != null)
|
|
|
|
|
{
|
|
|
|
|
if (Attribute.IsDefined(propertyInfo, typeof(IgnorePropertyChanged)))
|
|
|
|
|
return;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
|
2024-02-28 02:44:21 +00:00
|
|
|
|
hasJsonIgnoreAttribute = Attribute.IsDefined(propertyInfo, typeof(JsonIgnoreAttribute));
|
|
|
|
|
}
|
2023-07-12 22:41:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 02:44:21 +00:00
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedExtendedEventArgs(e.PropertyName, sender.ToString(), hasJsonIgnoreAttribute));
|
2023-07-12 22:41:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void InitializeChildPropertyChangeBinding()
|
|
|
|
|
{
|
2024-02-28 02:44:21 +00:00
|
|
|
|
var type = this.GetType();
|
|
|
|
|
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
|
2023-07-12 22:41:31 +00:00
|
|
|
|
|
2024-02-28 02:44:21 +00:00
|
|
|
|
foreach (var propertyInfo in properties)
|
2023-07-12 22:41:31 +00:00
|
|
|
|
{
|
|
|
|
|
if (Attribute.IsDefined(propertyInfo, typeof(IgnorePropertyChanged)))
|
|
|
|
|
continue;
|
|
|
|
|
|
2024-02-28 02:44:21 +00:00
|
|
|
|
var childType = propertyInfo.PropertyType;
|
2023-07-12 22:41:31 +00:00
|
|
|
|
|
2024-02-28 02:44:21 +00:00
|
|
|
|
if (!childType.IsSubclassOf(typeof(ObservableObject)))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var eventInfo = childType.GetEvent("PropertyChanged");
|
|
|
|
|
var methodInfo = type.GetMethod("OnPropertyChanged", BindingFlags.NonPublic | BindingFlags.Instance);
|
|
|
|
|
|
|
|
|
|
if (eventInfo == null || eventInfo.EventHandlerType == null || methodInfo == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var dg = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, methodInfo);
|
|
|
|
|
|
|
|
|
|
if(propertyInfo.GetValue(this, null) != null)
|
2023-07-12 22:41:31 +00:00
|
|
|
|
eventInfo.AddEventHandler(propertyInfo.GetValue(this, null), dg);
|
|
|
|
|
}
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class PropertyChangedExtendedEventArgs : PropertyChangedEventArgs
|
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
public virtual bool DisableSave { get; private set; }
|
2022-07-23 19:23:32 +00:00
|
|
|
|
|
2023-08-14 19:51:28 +00:00
|
|
|
|
public virtual string ObjectName { get; private set; }
|
|
|
|
|
|
|
|
|
|
public PropertyChangedExtendedEventArgs(string propertyName, string objectName, bool disableSave) : base(propertyName)
|
2022-07-23 19:23:32 +00:00
|
|
|
|
{
|
2023-07-12 22:41:31 +00:00
|
|
|
|
DisableSave = disableSave;
|
2023-08-14 19:51:28 +00:00
|
|
|
|
ObjectName = objectName;
|
2022-07-23 19:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|