mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 11:56:30 +00:00
Data model debug - Added semi support for lists
This commit is contained in:
1 parent
98ec26304a
commit
40eda4fc2b
11 files changed
+210
-74
No files matched your search
@@ -1,15 +0,0 @@
|
||||
using System.Reflection;
|
||||
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
|
||||
using Stylet;
|
||||
|
||||
namespace Artemis.UI.DataModelVisualization
|
||||
{
|
||||
public abstract class DataModelVisualizationViewModel : PropertyChangedBase
|
||||
{
|
||||
public PropertyInfo PropertyInfo { get; protected set; }
|
||||
public DataModelPropertyAttribute PropertyDescription { get; protected set; }
|
||||
public DataModelViewModel Parent { get; protected set; }
|
||||
|
||||
public abstract void Update();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
|
||||
using Stylet;
|
||||
|
||||
namespace Artemis.UI.DataModelVisualization
|
||||
{
|
||||
public class DataModelListViewModel : DataModelVisualizationViewModel
|
||||
{
|
||||
public DataModelListViewModel(PropertyInfo propertyInfo, DataModelPropertyAttribute propertyDescription, DataModelVisualizationViewModel parent)
|
||||
{
|
||||
PropertyInfo = propertyInfo;
|
||||
Parent = parent;
|
||||
PropertyDescription = propertyDescription;
|
||||
Children = new BindableCollection<DataModelVisualizationViewModel>();
|
||||
}
|
||||
|
||||
public BindableCollection<DataModelVisualizationViewModel> Children { get; set; }
|
||||
public IList List { get; set; }
|
||||
public string Count { get; set; }
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
if (PropertyInfo != null && Parent?.Model != null && PropertyInfo.GetValue(Parent.Model) is IList listValue)
|
||||
{
|
||||
Model = new List<object>(listValue.Cast<object>());
|
||||
List = (IList) Model;
|
||||
}
|
||||
|
||||
var index = 0;
|
||||
foreach (var item in List)
|
||||
{
|
||||
DataModelVisualizationViewModel child;
|
||||
if (Children.Count < index )
|
||||
{
|
||||
child = CreateChild(item);
|
||||
Children.Add(child);
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: This wont fly when mixing DataModels and DataModelProperties
|
||||
child = Children[index];
|
||||
child.Model = item;
|
||||
}
|
||||
|
||||
child.Update();
|
||||
index++;
|
||||
}
|
||||
|
||||
while (Children.Count > List.Count)
|
||||
Children.RemoveAt(Children.Count - 1);
|
||||
|
||||
Count = $"{Children.Count} {(Children.Count == 1 ? "item" : "items")}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,39 +1,36 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
|
||||
|
||||
namespace Artemis.UI.DataModelVisualization
|
||||
{
|
||||
public class DataModelPropertyViewModel<T, TP> : DataModelPropertyViewModel
|
||||
public class DataModelPropertyViewModel : DataModelVisualizationViewModel
|
||||
{
|
||||
private readonly Func<T, TP> _expression;
|
||||
|
||||
public DataModelPropertyViewModel(PropertyInfo propertyInfo, DataModelPropertyAttribute propertyDescription, DataModelViewModel parent)
|
||||
public DataModelPropertyViewModel(PropertyInfo propertyInfo, DataModelPropertyAttribute propertyDescription, DataModelVisualizationViewModel parent)
|
||||
{
|
||||
PropertyInfo = propertyInfo;
|
||||
Parent = parent;
|
||||
PropertyDescription = propertyDescription;
|
||||
|
||||
var instance = Expression.Parameter(typeof(T), "instance");
|
||||
var body = Expression.Property(instance, propertyInfo);
|
||||
_expression = Expression.Lambda<Func<T, TP>>(body, instance).Compile();
|
||||
}
|
||||
|
||||
public TP Value
|
||||
{
|
||||
get => BaseValue is TP value ? value : default;
|
||||
set => BaseValue = value;
|
||||
}
|
||||
public bool IsListProperty { get; set; }
|
||||
public string ListDescription { get; set; }
|
||||
public Type PropertyType { get; set; }
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
Value = _expression((T) Parent.Model);
|
||||
if (PropertyInfo != null && Parent?.Model != null)
|
||||
{
|
||||
IsListProperty = false;
|
||||
Model = PropertyInfo.GetValue(Parent.Model);
|
||||
PropertyType = PropertyInfo.PropertyType;
|
||||
}
|
||||
else if (Parent is DataModelListViewModel listViewModel)
|
||||
{
|
||||
IsListProperty = true;
|
||||
ListDescription = $"List item [{listViewModel.List.IndexOf(Model)}]";
|
||||
PropertyType = Model.GetType();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class DataModelPropertyViewModel : DataModelVisualizationViewModel
|
||||
{
|
||||
public object BaseValue { get; protected set; }
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ namespace Artemis.UI.DataModelVisualization
|
||||
Children = new BindableCollection<DataModelVisualizationViewModel>();
|
||||
}
|
||||
|
||||
public DataModelViewModel(PropertyInfo propertyInfo, object model, DataModelPropertyAttribute propertyDescription, DataModelViewModel parent)
|
||||
public DataModelViewModel(PropertyInfo propertyInfo, object model, DataModelPropertyAttribute propertyDescription, DataModelVisualizationViewModel parent)
|
||||
{
|
||||
PropertyInfo = propertyInfo;
|
||||
Model = model;
|
||||
@@ -25,7 +25,6 @@ namespace Artemis.UI.DataModelVisualization
|
||||
PopulateProperties();
|
||||
}
|
||||
|
||||
public object Model { get; private set; }
|
||||
public BindableCollection<DataModelVisualizationViewModel> Children { get; set; }
|
||||
|
||||
public void PopulateProperties()
|
||||
@@ -33,33 +32,9 @@ namespace Artemis.UI.DataModelVisualization
|
||||
Children.Clear();
|
||||
foreach (var propertyInfo in Model.GetType().GetProperties())
|
||||
{
|
||||
// Skip properties decorated with DataModelIgnore
|
||||
if (Attribute.IsDefined(propertyInfo, typeof(DataModelIgnoreAttribute)))
|
||||
continue;
|
||||
|
||||
var dataModelPropertyAttribute = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute));
|
||||
// If no DataModelProperty attribute was provided, pull one out of our ass
|
||||
if (dataModelPropertyAttribute == null)
|
||||
dataModelPropertyAttribute = new DataModelPropertyAttribute {Name = propertyInfo.Name.Humanize()};
|
||||
|
||||
// For primitives, create a property view model, it may be null that is fine
|
||||
if (propertyInfo.PropertyType.IsPrimitive || propertyInfo.PropertyType == typeof(string))
|
||||
{
|
||||
// This may be slower than avoiding generics and Activator.CreateInstance but it allows for expression trees inside the VM we're creating
|
||||
// here, this means slow creation but fast updates after that
|
||||
var viewModelType = typeof(DataModelPropertyViewModel<,>).MakeGenericType(Model.GetType(), propertyInfo.PropertyType);
|
||||
var viewModel = (DataModelVisualizationViewModel) Activator.CreateInstance(viewModelType, propertyInfo, dataModelPropertyAttribute, this);
|
||||
Children.Add(viewModel);
|
||||
}
|
||||
// For other value types create a child view model if the value type is not null
|
||||
else if (propertyInfo.PropertyType.IsClass || propertyInfo.PropertyType.IsStruct())
|
||||
{
|
||||
var value = propertyInfo.GetValue(Model);
|
||||
if (value == null)
|
||||
continue;
|
||||
|
||||
Children.Add(new DataModelViewModel(propertyInfo, value, dataModelPropertyAttribute, this));
|
||||
}
|
||||
var child = CreateChild(propertyInfo);
|
||||
if (child != null)
|
||||
Children.Add(child);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
using Artemis.Core.Extensions;
|
||||
using Artemis.Core.Plugins.Abstract.DataModels.Attributes;
|
||||
using Humanizer;
|
||||
using Stylet;
|
||||
|
||||
namespace Artemis.UI.DataModelVisualization
|
||||
{
|
||||
public abstract class DataModelVisualizationViewModel : PropertyChangedBase
|
||||
{
|
||||
public PropertyInfo PropertyInfo { get; protected set; }
|
||||
public DataModelPropertyAttribute PropertyDescription { get; protected set; }
|
||||
public DataModelVisualizationViewModel Parent { get; protected set; }
|
||||
public object Model { get; set; }
|
||||
|
||||
public abstract void Update();
|
||||
|
||||
protected DataModelVisualizationViewModel CreateChild(PropertyInfo propertyInfo)
|
||||
{
|
||||
// Skip properties decorated with DataModelIgnore
|
||||
if (Attribute.IsDefined(propertyInfo, typeof(DataModelIgnoreAttribute)))
|
||||
return null;
|
||||
|
||||
var dataModelPropertyAttribute = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(propertyInfo, typeof(DataModelPropertyAttribute));
|
||||
// If no DataModelProperty attribute was provided, pull one out of our ass
|
||||
if (dataModelPropertyAttribute == null)
|
||||
dataModelPropertyAttribute = new DataModelPropertyAttribute {Name = propertyInfo.Name.Humanize()};
|
||||
|
||||
// For primitives, create a property view model, it may be null that is fine
|
||||
if (propertyInfo.PropertyType.IsPrimitive || propertyInfo.PropertyType == typeof(string))
|
||||
return new DataModelPropertyViewModel(propertyInfo, dataModelPropertyAttribute, this);
|
||||
if (typeof(IList).IsAssignableFrom(propertyInfo.PropertyType))
|
||||
return new DataModelListViewModel(propertyInfo, dataModelPropertyAttribute, this);
|
||||
// For other value types create a child view model if the value type is not null
|
||||
if (propertyInfo.PropertyType.IsClass || propertyInfo.PropertyType.IsStruct())
|
||||
{
|
||||
var value = propertyInfo.GetValue(Model);
|
||||
if (value == null)
|
||||
return null;
|
||||
|
||||
return new DataModelViewModel(propertyInfo, value, dataModelPropertyAttribute, this);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected DataModelVisualizationViewModel CreateChild(object value)
|
||||
{
|
||||
var dataModelPropertyAttribute = new DataModelPropertyAttribute {Name = "Unknown property"};
|
||||
|
||||
// For primitives, create a property view model, it may be null that is fine
|
||||
if (value.GetType().IsPrimitive || value is string)
|
||||
return new DataModelPropertyViewModel(null, dataModelPropertyAttribute, this) {Model = value};
|
||||
// For other value types create a child view model if the value type is not null
|
||||
if (value.GetType().IsClass || value.GetType().IsStruct())
|
||||
return new DataModelViewModel(null, value, dataModelPropertyAttribute, this);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user