Dynamic data models - Rewrote the visualization to use leverage new core APIs

This commit is contained in:
SpoinkyNL committed 2020-10-04 22:06:21 +02:00
1 parent 26db794467
commit 8f552d0f71
22 files changed
+463 -374

No files matched your search

@@ -30,7 +30,7 @@
<Button Background="{Binding ButtonBrush}"
BorderBrush="{Binding ButtonBrush}"
Style="{StaticResource DataModelConditionButton}"
ToolTip="{Binding SelectedPropertyViewModel.DisplayPropertyPath}"
ToolTip="{Binding SelectedPropertyViewModel.DisplayPath}"
IsEnabled="{Binding IsEnabled}"
HorizontalAlignment="Left"
Click="PropertyButton_OnClick">
@@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.UI.Shared.Services;
@@ -12,7 +11,7 @@ namespace Artemis.UI.Shared
private int _index;
private Type _listType;
public DataModelListPropertiesViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, PropertyInfo propertyInfo) : base(dataModel, parent, propertyInfo)
public DataModelListPropertiesViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath) : base(dataModel, parent, dataModelPath)
{
}
@@ -34,9 +33,7 @@ namespace Artemis.UI.Shared
set => SetAndNotify(ref _displayValue, value);
}
public override string PropertyPath => null;
public override string DisplayPropertyPath => null;
public override string DisplayPath => null;
public override void Update(IDataModelUIService dataModelUIService)
{
@@ -54,18 +51,5 @@ namespace Artemis.UI.Shared
{
return DisplayValue;
}
private void PopulateProperties(IDataModelUIService dataModelUIService)
{
if (Children.Any())
return;
foreach (var propertyInfo in ListType.GetProperties())
{
var child = CreateChild(dataModelUIService, propertyInfo, GetChildDepth());
if (child != null)
Children.Add(child);
}
}
}
}
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.UI.Shared.Services;
@@ -12,7 +10,7 @@ namespace Artemis.UI.Shared
private int _index;
private Type _listType;
public DataModelListPropertyViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, PropertyInfo propertyInfo) : base(dataModel, parent, propertyInfo)
public DataModelListPropertyViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath) : base(dataModel, parent, dataModelPath)
{
}
@@ -1,7 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.UI.Shared.Services;
@@ -15,7 +13,7 @@ namespace Artemis.UI.Shared
private IList _list;
private DataModelVisualizationViewModel _listTypePropertyViewModel;
internal DataModelListViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, PropertyInfo propertyInfo) : base(dataModel, parent, propertyInfo)
internal DataModelListViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath) : base(dataModel, parent, dataModelPath)
{
ListChildren = new BindableCollection<DataModelVisualizationViewModel>();
}
@@ -36,8 +34,12 @@ namespace Artemis.UI.Shared
public DataModelPropertiesViewModel GetListTypeViewModel(IDataModelUIService dataModelUIService)
{
var type = DataModelPath.GetPropertyType();
if (type == null)
return null;
// Create a property VM describing the type of the list
var viewModel = CreateListChild(dataModelUIService, PropertyInfo.PropertyType.GenericTypeArguments[0]);
var viewModel = CreateListChild(dataModelUIService, type.GenericTypeArguments[0]);
// Put an empty value into the list type property view model
if (viewModel is DataModelListPropertiesViewModel dataModelListClassViewModel)
@@ -73,7 +75,7 @@ namespace Artemis.UI.Shared
DataModelVisualizationViewModel child;
if (ListChildren.Count <= index)
{
child = CreateListChild(dataModelUIService, item.GetType());
child = CreateListChild(dataModelUIService, item);
ListChildren.Add(child);
}
else
@@ -100,18 +102,21 @@ namespace Artemis.UI.Shared
Count = $"{ListChildren.Count} {(ListChildren.Count == 1 ? "item" : "items")}";
}
protected DataModelVisualizationViewModel CreateListChild(IDataModelUIService dataModelUIService, Type listType)
protected DataModelVisualizationViewModel CreateListChild(IDataModelUIService dataModelUIService, object listItem)
{
var listType = listItem.GetType();
var path = new DataModelPath(listItem, "");
// If a display VM was found, prefer to use that in any case
var typeViewModel = dataModelUIService.GetDataModelDisplayViewModel(listType);
if (typeViewModel != null)
return new DataModelListPropertyViewModel(DataModel, this, PropertyInfo) {DisplayViewModel = typeViewModel};
return new DataModelListPropertyViewModel(DataModel, this, path) {DisplayViewModel = typeViewModel};
// For primitives, create a property view model, it may be null that is fine
if (listType.IsPrimitive || listType.IsEnum || listType == typeof(string))
return new DataModelListPropertyViewModel(DataModel, this, PropertyInfo) {ListType = listType};
return new DataModelListPropertyViewModel(DataModel, this, path) {ListType = listType};
// For other value types create a child view model
if (listType.IsClass || listType.IsStruct())
return new DataModelListPropertiesViewModel(DataModel, this, PropertyInfo) {ListType = listType};
return new DataModelListPropertiesViewModel(DataModel, this, path) {ListType = listType};
return null;
}
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.UI.Shared.Services;
@@ -8,7 +6,7 @@ namespace Artemis.UI.Shared
{
public class DataModelPropertiesViewModel : DataModelVisualizationViewModel
{
internal DataModelPropertiesViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, PropertyInfo propertyInfo) : base(dataModel, parent, propertyInfo)
internal DataModelPropertiesViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath) : base(dataModel, parent, dataModelPath)
{
}
@@ -30,36 +28,9 @@ namespace Artemis.UI.Shared
return Parent.IsRootViewModel ? DataModel : base.GetCurrentValue();
}
protected int GetChildDepth()
protected override int GetChildDepth()
{
return PropertyDescription != null && !PropertyDescription.ResetsDepth ? Depth + 1 : 1;
}
private void PopulateProperties(IDataModelUIService dataModelUIService)
{
if (IsRootViewModel)
return;
// Add missing children
var modelType = Parent.IsRootViewModel ? DataModel.GetType() : PropertyInfo.PropertyType;
foreach (var propertyInfo in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (Children.Any(c => c.PropertyInfo.Equals(propertyInfo)))
continue;
var child = CreateChild(dataModelUIService, propertyInfo, GetChildDepth());
if (child != null)
Children.Add(child);
}
// Remove children that should be hidden
var childList = new List<DataModelVisualizationViewModel>(Children);
var hiddenProperties = DataModel.GetHiddenProperties();
foreach (var dataModelVisualizationViewModel in childList)
{
if (hiddenProperties.Contains(dataModelVisualizationViewModel.PropertyInfo))
Children.Remove(dataModelVisualizationViewModel);
}
}
}
}
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.UI.Shared.Services;
@@ -11,8 +9,9 @@ namespace Artemis.UI.Shared
{
private object _displayValue;
private DataModelDisplayViewModel _displayViewModel;
private Type _displayValueType;
internal DataModelPropertyViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, PropertyInfo propertyInfo) : base(dataModel, parent, propertyInfo)
internal DataModelPropertyViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath) : base(dataModel, parent, dataModelPath)
{
}
@@ -22,6 +21,12 @@ namespace Artemis.UI.Shared
set => SetAndNotify(ref _displayValue, value);
}
public Type DisplayValueType
{
get => _displayValueType;
set => SetAndNotify(ref _displayValueType, value);
}
public DataModelDisplayViewModel DisplayViewModel
{
get => _displayViewModel;
@@ -34,12 +39,13 @@ namespace Artemis.UI.Shared
return;
if (DisplayViewModel == null)
DisplayViewModel = dataModelUIService.GetDataModelDisplayViewModel(PropertyInfo.PropertyType, true);
DisplayViewModel = dataModelUIService.GetDataModelDisplayViewModel(DataModelPath.GetPropertyType(), true);
DisplayValue = GetCurrentValue();
DisplayValueType = DataModelPath.GetPropertyType();
UpdateDisplayParameters();
}
protected void UpdateDisplayParameters()
{
DisplayViewModel?.UpdateValue(DisplayValue);
@@ -1,12 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Artemis.Core;
using Artemis.Core.DataModelExpansions;
using Artemis.UI.Shared.Services;
using Humanizer;
using Stylet;
namespace Artemis.UI.Shared
@@ -20,23 +18,25 @@ namespace Artemis.UI.Shared
private bool _isVisualizationExpanded;
private DataModelVisualizationViewModel _parent;
private DataModelPropertyAttribute _propertyDescription;
private PropertyInfo _propertyInfo;
internal DataModelVisualizationViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, PropertyInfo propertyInfo)
internal DataModelVisualizationViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath)
{
DataModel = dataModel;
PropertyInfo = propertyInfo;
Parent = parent;
DataModelPath = dataModelPath;
Children = new BindableCollection<DataModelVisualizationViewModel>();
IsMatchingFilteredTypes = true;
if (dataModel == null && parent == null && propertyInfo == null)
if (dataModel == null && parent == null && dataModelPath == null)
IsRootViewModel = true;
else
GetDescription();
PropertyDescription = DataModelPath?.GetPropertyDescription() ?? DataModel.DataModelDescription;
}
public bool IsRootViewModel { get; }
public DataModelPath DataModelPath { get; }
public string Path => DataModelPath?.Path;
public int Depth { get; set; }
public DataModel DataModel
@@ -45,12 +45,6 @@ namespace Artemis.UI.Shared
set => SetAndNotify(ref _dataModel, value);
}
public PropertyInfo PropertyInfo
{
get => _propertyInfo;
protected set => SetAndNotify(ref _propertyInfo, value);
}
public DataModelPropertyAttribute PropertyDescription
{
get => _propertyDescription;
@@ -85,35 +79,7 @@ namespace Artemis.UI.Shared
}
}
public virtual string PropertyPath
{
get
{
if (Parent == null)
return PropertyInfo?.Name;
if (PropertyInfo == null)
return Parent.PropertyPath;
var parentPath = Parent.PropertyPath;
return parentPath != null ? $"{parentPath}.{PropertyInfo.Name}" : PropertyInfo.Name;
}
}
public virtual string DisplayPropertyPath
{
get
{
if (Parent == null)
return PropertyDescription?.Name;
if (PropertyDescription == null)
return Parent.DisplayPropertyPath;
var parentPath = Parent.DisplayPropertyPath;
return parentPath != null ? $"{parentPath} › {PropertyDescription.Name}" : PropertyDescription.Name;
}
}
public virtual string DisplayPath => Path.Replace(".", " › ");
/// <summary>
/// Updates the datamodel and if in an parent, any children
@@ -123,18 +89,10 @@ namespace Artemis.UI.Shared
public virtual object GetCurrentValue()
{
try
{
if (PropertyInfo.GetGetMethod() == null)
return null;
return Parent?.GetCurrentValue() == null ? null : PropertyInfo.GetValue(Parent.GetCurrentValue());
}
catch (Exception)
{
// ignored, who knows what kind of shit can go wrong here...
if (IsRootViewModel)
return null;
}
return DataModelPath.GetValue();
}
public void ApplyTypeFilter(bool looseMatch, params Type[] filteredTypes)
@@ -164,17 +122,18 @@ namespace Artemis.UI.Shared
return;
}
// If this VM has no property info, assume it does not match
if (PropertyInfo == null)
// If the type couldn't be retrieved either way, assume false
var type = DataModelPath.GetPropertyType();
if (type == null)
{
IsMatchingFilteredTypes = false;
return;
}
if (looseMatch)
IsMatchingFilteredTypes = filteredTypes.Any(t => t.IsCastableFrom(PropertyInfo.PropertyType) || t == typeof(Enum) && PropertyInfo.PropertyType.IsEnum);
IsMatchingFilteredTypes = filteredTypes.Any(t => t.IsCastableFrom(type) || t == typeof(Enum) && type.IsEnum);
else
IsMatchingFilteredTypes = filteredTypes.Any(t => t == PropertyInfo.PropertyType || t == typeof(Enum) && PropertyInfo.PropertyType.IsEnum);
IsMatchingFilteredTypes = filteredTypes.Any(t => t == type || t == typeof(Enum) && type.IsEnum);
}
public DataModelVisualizationViewModel GetChildForCondition(DataModelConditionPredicate predicate, DataModelConditionSide side)
@@ -194,8 +153,12 @@ namespace Artemis.UI.Shared
public DataModelVisualizationViewModel GetChildByPath(Guid dataModelGuid, string propertyPath)
{
if (DataModel.PluginInfo.Guid != dataModelGuid)
return null;
if (propertyPath == null)
return null;
if (Path.StartsWith(propertyPath))
return null;
// Ensure children are populated by requesting an update
if (!IsVisualizationExpanded)
@@ -205,50 +168,107 @@ namespace Artemis.UI.Shared
IsVisualizationExpanded = false;
}
var path = propertyPath.Split(".");
var currentPart = path.First();
if (IsRootViewModel)
foreach (var child in Children)
{
var child = Children.FirstOrDefault(c => c.DataModel != null &&
c.DataModel.PluginInfo.Guid == dataModelGuid);
return child?.GetChildByPath(dataModelGuid, propertyPath);
}
else
{
var child = Children.FirstOrDefault(c => c.DataModel != null &&
c.DataModel.PluginInfo.Guid == dataModelGuid && c.PropertyInfo?.Name == currentPart);
if (child == null)
return null;
// Try the child itself first
if (child.Path == propertyPath)
return child;
if (path.Length > 1)
return child.GetChildByPath(dataModelGuid, string.Join(".", path.Skip(1)));
return child;
// Try a child on the child next, this will go recursive
var match = child.GetChildByPath(dataModelGuid, propertyPath);
if (match != null)
return match;
}
return null;
}
protected DataModelVisualizationViewModel CreateChild(IDataModelUIService dataModelUIService, PropertyInfo propertyInfo, int depth)
protected virtual int GetChildDepth()
{
return 0;
}
protected void PopulateProperties(IDataModelUIService dataModelUIService)
{
if (IsRootViewModel)
return;
// Add missing static children
var modelType = Parent.IsRootViewModel ? DataModel.GetType() : DataModelPath.GetPropertyType();
foreach (var propertyInfo in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
var childPath = Path != null ? $"{Path}.{propertyInfo.Name}" : propertyInfo.Name;
if (Children.Any(c => c.Path != null && c.Path.Equals(childPath)))
continue;
var child = CreateChild(dataModelUIService, childPath, GetChildDepth());
if (child != null)
Children.Add(child);
}
// Remove static children that should be hidden
var hiddenProperties = DataModel.GetHiddenProperties();
foreach (var hiddenProperty in hiddenProperties)
{
var childPath = Path != null ? $"{Path}.{hiddenProperty.Name}" : hiddenProperty.Name;
var toRemove = Children.FirstOrDefault(c => c.Path != null && c.Path == childPath);
if (toRemove != null)
Children.Remove(toRemove);
}
// Add missing dynamic children
var value = Parent.IsRootViewModel ? DataModel : DataModelPath.GetValue();
if (value is DataModel dataModel)
{
foreach (var kvp in dataModel.DynamicDataModels)
{
var childPath = Path != null ? $"{Path}.{kvp.Key}" : kvp.Key;
if (Children.Any(c => c.Path != null && c.Path.Equals(childPath)))
continue;
var child = CreateChild(dataModelUIService, childPath, GetChildDepth());
if (child != null)
Children.Add(child);
}
}
// Remove dynamic children that have been removed from the data model
var toRemoveDynamic = Children.Where(c => !c.DataModelPath.IsValid).ToList();
if (toRemoveDynamic.Any())
Children.RemoveRange(toRemoveDynamic);
}
protected DataModelVisualizationViewModel CreateChild(IDataModelUIService dataModelUIService, string path, int depth)
{
if (depth > MaxDepth)
return null;
var dataModelPath = new DataModelPath(DataModel, path);
if (!dataModelPath.IsValid)
return null;
var propertyInfo = dataModelPath.GetPropertyInfo();
var propertyType = dataModelPath.GetPropertyType();
// Skip properties decorated with DataModelIgnore
if (Attribute.IsDefined(propertyInfo, typeof(DataModelIgnoreAttribute)))
if (propertyInfo != null && Attribute.IsDefined(propertyInfo, typeof(DataModelIgnoreAttribute)))
return null;
// Skip properties that are in the ignored properties list of the respective profile module/data model expansion
if (DataModel.GetHiddenProperties().Any(p => p.Equals(propertyInfo)))
return null;
// If a display VM was found, prefer to use that in any case
var typeViewModel = dataModelUIService.GetDataModelDisplayViewModel(propertyInfo.PropertyType);
var typeViewModel = dataModelUIService.GetDataModelDisplayViewModel(propertyType);
if (typeViewModel != null)
return new DataModelPropertyViewModel(DataModel, this, propertyInfo) {DisplayViewModel = typeViewModel, Depth = depth};
return new DataModelPropertyViewModel(DataModel, this, dataModelPath) {DisplayViewModel = typeViewModel, Depth = depth};
// For primitives, create a property view model, it may be null that is fine
if (propertyInfo.PropertyType.IsPrimitive || propertyInfo.PropertyType.IsEnum || propertyInfo.PropertyType == typeof(string))
return new DataModelPropertyViewModel(DataModel, this, propertyInfo) {Depth = depth};
if (typeof(IList).IsAssignableFrom(propertyInfo.PropertyType))
return new DataModelListViewModel(DataModel, this, propertyInfo) {Depth = depth};
if (propertyType.IsPrimitive || propertyType.IsEnum || propertyType == typeof(string))
return new DataModelPropertyViewModel(DataModel, this, dataModelPath) {Depth = depth};
if (typeof(IList).IsAssignableFrom(propertyType))
return new DataModelListViewModel(DataModel, this, dataModelPath) {Depth = depth};
// For other value types create a child view model
if (propertyInfo.PropertyType.IsClass || propertyInfo.PropertyType.IsStruct())
return new DataModelPropertiesViewModel(DataModel, this, propertyInfo) {Depth = depth};
if (propertyType.IsClass || propertyType.IsStruct())
return new DataModelPropertiesViewModel(DataModel, this, dataModelPath) {Depth = depth};
return null;
}
@@ -259,25 +279,6 @@ namespace Artemis.UI.Shared
OnUpdateRequested();
}
private void GetDescription()
{
// If this is the first child of a root view model, use the data model description
if (Parent.IsRootViewModel)
PropertyDescription = DataModel?.DataModelDescription;
// Rely on property info for the description
else if (PropertyInfo != null)
{
PropertyDescription = (DataModelPropertyAttribute) Attribute.GetCustomAttribute(PropertyInfo, typeof(DataModelPropertyAttribute)) ??
new DataModelPropertyAttribute {Name = PropertyInfo.Name.Humanize(), ResetsDepth = false};
}
else
throw new ArtemisSharedUIException("Failed to get property description because plugin info is null but the parent has a datamodel");
// If a property description was provided but the name is null, use the humanized property name
if (PropertyDescription != null && PropertyDescription.Name == null && PropertyInfo != null)
PropertyDescription.Name = PropertyInfo.Name.Humanize();
}
#region Events
public event EventHandler UpdateRequested;
@@ -1,8 +0,0 @@
using Artemis.Core.DataModelExpansions;
namespace Artemis.UI.Shared.Services
{
public class ListDataModelWrapper : DataModel
{
}
}