mirror of
https://github.com/Artemis-RGB/Artemis
synced 2025-12-31 17:53:32 +00:00
Data bindings - Some WIP stuff
This commit is contained in:
parent
2708e190cb
commit
20e6aa1135
@ -4,6 +4,8 @@ using System.Linq;
|
|||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Artemis.Core.DataModelExpansions;
|
using Artemis.Core.DataModelExpansions;
|
||||||
|
using Artemis.Core.Services;
|
||||||
|
using Artemis.Storage.Entities.Profile.DataBindings;
|
||||||
|
|
||||||
namespace Artemis.Core
|
namespace Artemis.Core
|
||||||
{
|
{
|
||||||
@ -14,15 +16,28 @@ namespace Artemis.Core
|
|||||||
{
|
{
|
||||||
private readonly List<DataBindingModifier> _modifiers = new List<DataBindingModifier>();
|
private readonly List<DataBindingModifier> _modifiers = new List<DataBindingModifier>();
|
||||||
|
|
||||||
|
public DataBinding(BaseLayerProperty layerProperty, PropertyInfo targetProperty)
|
||||||
|
{
|
||||||
|
LayerProperty = layerProperty;
|
||||||
|
TargetProperty = targetProperty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataBinding(BaseLayerProperty layerProperty, PropertyInfo targetProperty, DataBindingEntity entity)
|
||||||
|
{
|
||||||
|
LayerProperty = layerProperty;
|
||||||
|
TargetProperty = targetProperty;
|
||||||
|
Entity = entity;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the layer property this data binding targets
|
/// Gets the layer property this data binding targets
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public BaseLayerProperty LayerProperty { get; private set; }
|
public BaseLayerProperty LayerProperty { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the inner property this data binding targets
|
/// Gets the inner property this data binding targets
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public PropertyInfo TargetProperty { get; private set; }
|
public PropertyInfo TargetProperty { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the currently used instance of the data model that contains the source of the data binding
|
/// Gets the currently used instance of the data model that contains the source of the data binding
|
||||||
@ -39,8 +54,13 @@ namespace Artemis.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public IReadOnlyList<DataBindingModifier> Modifiers => _modifiers.AsReadOnly();
|
public IReadOnlyList<DataBindingModifier> Modifiers => _modifiers.AsReadOnly();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the compiled function that gets the current value of the data binding target
|
||||||
|
/// </summary>
|
||||||
public Func<DataModel, object> CompiledTargetAccessor { get; private set; }
|
public Func<DataModel, object> CompiledTargetAccessor { get; private set; }
|
||||||
|
|
||||||
|
internal DataBindingEntity Entity { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a modifier to the data binding's <see cref="Modifiers" /> collection
|
/// Adds a modifier to the data binding's <see cref="Modifiers" /> collection
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -49,7 +69,6 @@ namespace Artemis.Core
|
|||||||
if (!_modifiers.Contains(modifier))
|
if (!_modifiers.Contains(modifier))
|
||||||
{
|
{
|
||||||
modifier.DataBinding = this;
|
modifier.DataBinding = this;
|
||||||
modifier.CreateExpression();
|
|
||||||
_modifiers.Add(modifier);
|
_modifiers.Add(modifier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,11 +81,33 @@ namespace Artemis.Core
|
|||||||
if (_modifiers.Contains(modifier))
|
if (_modifiers.Contains(modifier))
|
||||||
{
|
{
|
||||||
modifier.DataBinding = null;
|
modifier.DataBinding = null;
|
||||||
modifier.CreateExpression();
|
|
||||||
_modifiers.Remove(modifier);
|
_modifiers.Remove(modifier);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates the source of the data binding and re-compiles the expression
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dataModel">The data model of the source</param>
|
||||||
|
/// <param name="path">The path pointing to the source inside the data model</param>
|
||||||
|
public void UpdateSource(DataModel dataModel, string path)
|
||||||
|
{
|
||||||
|
if (dataModel != null && path == null)
|
||||||
|
throw new ArtemisCoreException("If a data model is provided, a path is also required");
|
||||||
|
if (dataModel == null && path != null)
|
||||||
|
throw new ArtemisCoreException("If path is provided, a data model is also required");
|
||||||
|
|
||||||
|
if (dataModel != null)
|
||||||
|
{
|
||||||
|
if (!dataModel.ContainsPath(path))
|
||||||
|
throw new ArtemisCoreException($"Data model of type {dataModel.GetType().Name} does not contain a property at path '{path}'");
|
||||||
|
}
|
||||||
|
|
||||||
|
SourceDataModel = dataModel;
|
||||||
|
SourcePropertyPath = path;
|
||||||
|
CreateExpression();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the current value of the data binding
|
/// Gets the current value of the data binding
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -90,7 +131,18 @@ namespace Artemis.Core
|
|||||||
return dataBindingValue;
|
return dataBindingValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update()
|
internal void Initialize(IDataModelService dataModelService, IDataBindingService dataBindingService)
|
||||||
|
{
|
||||||
|
// Source
|
||||||
|
|
||||||
|
// Modifiers
|
||||||
|
|
||||||
|
|
||||||
|
foreach (var dataBindingModifier in Modifiers)
|
||||||
|
dataBindingModifier.Initialize(dataModelService, dataBindingService);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CreateExpression()
|
||||||
{
|
{
|
||||||
var listType = SourceDataModel.GetListTypeInPath(SourcePropertyPath);
|
var listType = SourceDataModel.GetListTypeInPath(SourcePropertyPath);
|
||||||
if (listType != null)
|
if (listType != null)
|
||||||
|
|||||||
@ -13,6 +13,8 @@ namespace Artemis.Core
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class DataBindingModifier
|
public class DataBindingModifier
|
||||||
{
|
{
|
||||||
|
private DataBinding _dataBinding;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of the <see cref="DataBindingModifier" /> class
|
/// Creates a new instance of the <see cref="DataBindingModifier" /> class
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -37,7 +39,15 @@ namespace Artemis.Core
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the data binding this modifier is applied to
|
/// Gets the data binding this modifier is applied to
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DataBinding DataBinding { get; internal set; }
|
public DataBinding DataBinding
|
||||||
|
{
|
||||||
|
get => _dataBinding;
|
||||||
|
internal set
|
||||||
|
{
|
||||||
|
_dataBinding = value;
|
||||||
|
CreateExpression();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the type of modifier that is being applied
|
/// Gets the type of modifier that is being applied
|
||||||
@ -89,7 +99,7 @@ namespace Artemis.Core
|
|||||||
/// <returns>The modified value</returns>
|
/// <returns>The modified value</returns>
|
||||||
public object Apply(object currentValue)
|
public object Apply(object currentValue)
|
||||||
{
|
{
|
||||||
var targetType = DataBinding.LayerProperty.GetPropertyType();
|
var targetType = DataBinding.TargetProperty.GetType();
|
||||||
if (currentValue.GetType() != targetType)
|
if (currentValue.GetType() != targetType)
|
||||||
{
|
{
|
||||||
throw new ArtemisCoreException("The current value of the data binding does not match the type of the target property." +
|
throw new ArtemisCoreException("The current value of the data binding does not match the type of the target property." +
|
||||||
@ -118,7 +128,7 @@ namespace Artemis.Core
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var targetType = DataBinding.LayerProperty.GetPropertyType();
|
var targetType = DataBinding.TargetProperty.GetType();
|
||||||
if (!modifierType.SupportsType(targetType))
|
if (!modifierType.SupportsType(targetType))
|
||||||
{
|
{
|
||||||
throw new ArtemisCoreException($"Cannot apply modifier type {modifierType.GetType().Name} to this modifier because " +
|
throw new ArtemisCoreException($"Cannot apply modifier type {modifierType.GetType().Name} to this modifier because " +
|
||||||
@ -164,7 +174,7 @@ namespace Artemis.Core
|
|||||||
ParameterDataModel = null;
|
ParameterDataModel = null;
|
||||||
ParameterPropertyPath = null;
|
ParameterPropertyPath = null;
|
||||||
|
|
||||||
var targetType = DataBinding.LayerProperty.GetPropertyType();
|
var targetType = DataBinding.TargetProperty.GetType();
|
||||||
|
|
||||||
// If not null ensure the types match and if not, convert it
|
// If not null ensure the types match and if not, convert it
|
||||||
if (staticValue != null && staticValue.GetType() == targetType)
|
if (staticValue != null && staticValue.GetType() == targetType)
|
||||||
@ -200,7 +210,7 @@ namespace Artemis.Core
|
|||||||
else if (ParameterType == ProfileRightSideType.Static && Entity.ParameterStaticValue != null)
|
else if (ParameterType == ProfileRightSideType.Static && Entity.ParameterStaticValue != null)
|
||||||
{
|
{
|
||||||
// Use the target type so JSON.NET has a better idea what to do
|
// Use the target type so JSON.NET has a better idea what to do
|
||||||
var targetType = DataBinding.LayerProperty.GetPropertyType();
|
var targetType = DataBinding.TargetProperty.GetType();
|
||||||
object staticValue;
|
object staticValue;
|
||||||
|
|
||||||
try
|
try
|
||||||
@ -221,7 +231,7 @@ namespace Artemis.Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
internal void CreateExpression()
|
private void CreateExpression()
|
||||||
{
|
{
|
||||||
CompiledDynamicPredicate = null;
|
CompiledDynamicPredicate = null;
|
||||||
CompiledStaticPredicate = null;
|
CompiledStaticPredicate = null;
|
||||||
@ -240,15 +250,15 @@ namespace Artemis.Core
|
|||||||
if (ParameterDataModel == null)
|
if (ParameterDataModel == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var currentValueParameter = Expression.Parameter(DataBinding.LayerProperty.GetPropertyType());
|
var currentValueParameter = Expression.Parameter(DataBinding.TargetProperty.GetType());
|
||||||
|
|
||||||
// If the right side value is null, the constant type cannot be inferred and must be provided based on the data binding target
|
// If the right side value is null, the constant type cannot be inferred and must be provided based on the data binding target
|
||||||
var rightSideAccessor = CreateAccessor(ParameterDataModel, ParameterPropertyPath, "right", out var rightSideParameter);
|
var rightSideAccessor = CreateAccessor(ParameterDataModel, ParameterPropertyPath, "right", out var rightSideParameter);
|
||||||
|
|
||||||
// A conversion may be required if the types differ
|
// A conversion may be required if the types differ
|
||||||
// This can cause issues if the DisplayConditionOperator wasn't accurate in it's supported types but that is not a concern here
|
// This can cause issues if the DisplayConditionOperator wasn't accurate in it's supported types but that is not a concern here
|
||||||
if (rightSideAccessor.Type != DataBinding.LayerProperty.GetPropertyType())
|
if (rightSideAccessor.Type != DataBinding.TargetProperty.GetType())
|
||||||
rightSideAccessor = Expression.Convert(rightSideAccessor, DataBinding.LayerProperty.GetPropertyType());
|
rightSideAccessor = Expression.Convert(rightSideAccessor, DataBinding.TargetProperty.GetType());
|
||||||
|
|
||||||
var modifierExpression = ModifierType.CreateExpression(currentValueParameter, rightSideAccessor);
|
var modifierExpression = ModifierType.CreateExpression(currentValueParameter, rightSideAccessor);
|
||||||
var lambda = Expression.Lambda<Func<object, DataModel, object>>(modifierExpression, currentValueParameter, rightSideParameter);
|
var lambda = Expression.Lambda<Func<object, DataModel, object>>(modifierExpression, currentValueParameter, rightSideParameter);
|
||||||
@ -257,12 +267,12 @@ namespace Artemis.Core
|
|||||||
|
|
||||||
private void CreateStaticExpression()
|
private void CreateStaticExpression()
|
||||||
{
|
{
|
||||||
var currentValueParameter = Expression.Parameter(DataBinding.LayerProperty.GetPropertyType());
|
var currentValueParameter = Expression.Parameter(DataBinding.TargetProperty.GetType());
|
||||||
|
|
||||||
// If the right side value is null, the constant type cannot be inferred and must be provided based on the data binding target
|
// If the right side value is null, the constant type cannot be inferred and must be provided based on the data binding target
|
||||||
var rightSideConstant = ParameterStaticValue != null
|
var rightSideConstant = ParameterStaticValue != null
|
||||||
? Expression.Constant(ParameterStaticValue)
|
? Expression.Constant(ParameterStaticValue)
|
||||||
: Expression.Constant(null, DataBinding.LayerProperty.GetPropertyType());
|
: Expression.Constant(null, DataBinding.TargetProperty.GetType());
|
||||||
|
|
||||||
var modifierExpression = ModifierType.CreateExpression(currentValueParameter, rightSideConstant);
|
var modifierExpression = ModifierType.CreateExpression(currentValueParameter, rightSideConstant);
|
||||||
var lambda = Expression.Lambda<Func<object, object>>(modifierExpression, currentValueParameter);
|
var lambda = Expression.Lambda<Func<object, object>>(modifierExpression, currentValueParameter);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user