1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Data model conditions - Improved right-side type detection

This commit is contained in:
SpoinkyNL 2020-10-22 20:54:35 +02:00
parent b8fa8779d9
commit eb1c99a944
5 changed files with 64 additions and 23 deletions

View File

@ -181,6 +181,24 @@ namespace Artemis.Core
return true;
}
/// <summary>
/// Determines the best type to use for the right side op this predicate
/// </summary>
public Type? GetPreferredRightSideType()
{
Type? preferredType = Operator?.RightSideType;
Type? leftSideType = DataModelConditionList.IsPrimitiveList
? DataModelConditionList.ListType
: LeftPath?.GetPropertyType();
if (preferredType == null)
return null;
if (leftSideType != null && preferredType.IsAssignableFrom(leftSideType))
preferredType = leftSideType;
return preferredType;
}
#region IDisposable
/// <inheritdoc />
@ -411,13 +429,14 @@ namespace Artemis.Core
}
// If not null ensure the types match and if not, convert it
if (staticValue != null && staticValue.GetType() == Operator.RightSideType)
Type? preferredType = GetPreferredRightSideType();
if (staticValue != null && staticValue.GetType() == preferredType || preferredType == null)
RightStaticValue = staticValue;
else if (staticValue != null)
RightStaticValue = Convert.ChangeType(staticValue, Operator.RightSideType);
RightStaticValue = Convert.ChangeType(staticValue, preferredType);
// If null create a default instance for value types or simply make it null for reference types
else if (Operator.RightSideType.IsValueType)
RightStaticValue = Activator.CreateInstance(Operator.RightSideType);
else if (preferredType.IsValueType)
RightStaticValue = Activator.CreateInstance(preferredType);
else
RightStaticValue = null;
}

View File

@ -1,6 +1,4 @@
using System;
using System.IO;
using Artemis.Core.DataModelExpansions;
using Artemis.Storage.Entities.Profile.Abstract;
using Artemis.Storage.Entities.Profile.Conditions;
using Newtonsoft.Json;
@ -113,6 +111,7 @@ namespace Artemis.Core
RightStaticValue = staticValue;
return;
}
// If the operator does not support a right side, always set it to null
if (Operator.RightSideType == null)
{
@ -121,13 +120,14 @@ namespace Artemis.Core
}
// If not null ensure the types match and if not, convert it
if (staticValue != null && staticValue.GetType() == Operator.RightSideType)
Type? preferredType = GetPreferredRightSideType();
if (staticValue != null && staticValue.GetType() == preferredType || preferredType == null)
RightStaticValue = staticValue;
else if (staticValue != null)
RightStaticValue = Convert.ChangeType(staticValue, Operator.RightSideType);
RightStaticValue = Convert.ChangeType(staticValue, preferredType);
// If null create a default instance for value types or simply make it null for reference types
else if (Operator.RightSideType.IsValueType)
RightStaticValue = Activator.CreateInstance(Operator.RightSideType);
else if (preferredType.IsValueType)
RightStaticValue = Activator.CreateInstance(preferredType);
else
RightStaticValue = null;
}
@ -184,6 +184,22 @@ namespace Artemis.Core
return Operator.InternalEvaluate(LeftPath.GetValue(), RightPath.GetValue());
}
/// <summary>
/// Determines the best type to use for the right side op this predicate
/// </summary>
public Type? GetPreferredRightSideType()
{
Type? preferredType = Operator?.RightSideType;
Type? leftSideType = LeftPath?.GetPropertyType();
if (preferredType == null)
return null;
if (leftSideType != null && preferredType.IsAssignableFrom(leftSideType))
preferredType = leftSideType;
return preferredType;
}
/// <inheritdoc />
public override string ToString()
{

View File

@ -69,7 +69,7 @@ namespace Artemis.UI.Shared.Input
public Type TargetType
{
get => _targetType;
set => SetAndNotify(ref _targetType, value);
private set => SetAndNotify(ref _targetType, value);
}
public DataModelPropertyAttribute TargetDescription

View File

@ -156,14 +156,16 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
{
DisposeRightSideDynamicViewModel();
if (RightSideInputViewModel == null)
CreateRightSideInputViewModel(SelectedOperator.RightSideType);
CreateRightSideInputViewModel();
if (SelectedOperator.RightSideType.IsValueType && DataModelConditionListPredicate.RightStaticValue == null)
RightSideInputViewModel.Value = SelectedOperator.RightSideType.GetDefault();
else
RightSideInputViewModel.Value = DataModelConditionListPredicate.RightStaticValue;
if (RightSideInputViewModel.TargetType != SelectedOperator.RightSideType)
RightSideInputViewModel.UpdateTargetType(SelectedOperator.RightSideType);
Type preferredType = DataModelConditionListPredicate.GetPreferredRightSideType();
if (RightSideInputViewModel.TargetType != preferredType)
RightSideInputViewModel.UpdateTargetType(preferredType);
}
}
@ -222,7 +224,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
SelectedOperator = dataModelConditionOperator;
ApplyOperator();
}
#region IDisposable
public void Dispose()
@ -286,9 +288,10 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
RightSideSelectionViewModel.ExtraDataModelViewModels.Add(listValue);
}
private void CreateRightSideInputViewModel(Type leftSideType)
private void CreateRightSideInputViewModel()
{
RightSideInputViewModel = _dataModelUIService.GetStaticInputViewModel(leftSideType, LeftSideSelectionViewModel.DataModelPath?.GetPropertyDescription());
Type preferredType = DataModelConditionListPredicate.GetPreferredRightSideType();
RightSideInputViewModel = _dataModelUIService.GetStaticInputViewModel(preferredType, LeftSideSelectionViewModel.DataModelPath?.GetPropertyDescription());
RightSideInputViewModel.ButtonBrush = (SolidColorBrush) Application.Current.FindResource("PrimaryHueMidBrush");
RightSideInputViewModel.DisplaySwitchButton = true;
RightSideInputViewModel.ValueUpdated += RightSideOnValueEntered;

View File

@ -131,14 +131,16 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
{
DisposeRightSideDynamicViewModel();
if (RightSideInputViewModel == null)
CreateRightSideInputViewModel(SelectedOperator.RightSideType);
CreateRightSideInputViewModel();
if (SelectedOperator.RightSideType.IsValueType && DataModelConditionPredicate.RightStaticValue == null)
RightSideInputViewModel.Value = SelectedOperator.RightSideType.GetDefault();
else
RightSideInputViewModel.Value = DataModelConditionPredicate.RightStaticValue;
if (RightSideInputViewModel.TargetType != SelectedOperator.RightSideType)
RightSideInputViewModel.UpdateTargetType(SelectedOperator.RightSideType);
Type preferredType = DataModelConditionPredicate.GetPreferredRightSideType();
if (RightSideInputViewModel.TargetType != preferredType)
RightSideInputViewModel.UpdateTargetType(preferredType);
}
}
@ -188,7 +190,7 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
SelectedOperator = DataModelConditionOperator;
ApplyOperator();
}
#region IDisposable
public void Dispose()
@ -217,9 +219,10 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
RightSideSelectionViewModel.SwitchToStaticRequested += RightSideSelectionViewModelOnSwitchToStaticRequested;
}
private void CreateRightSideInputViewModel(Type leftSideType)
private void CreateRightSideInputViewModel()
{
RightSideInputViewModel = _dataModelUIService.GetStaticInputViewModel(leftSideType, LeftSideSelectionViewModel.DataModelPath?.GetPropertyDescription());
Type preferredType = DataModelConditionPredicate.GetPreferredRightSideType();
RightSideInputViewModel = _dataModelUIService.GetStaticInputViewModel(preferredType, LeftSideSelectionViewModel.DataModelPath?.GetPropertyDescription());
RightSideInputViewModel.ButtonBrush = (SolidColorBrush) Application.Current.FindResource("PrimaryHueMidBrush");
RightSideInputViewModel.DisplaySwitchButton = true;
RightSideInputViewModel.ValueUpdated += RightSideOnValueEntered;