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; 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 #region IDisposable
/// <inheritdoc /> /// <inheritdoc />
@ -411,13 +429,14 @@ namespace Artemis.Core
} }
// 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() == Operator.RightSideType) Type? preferredType = GetPreferredRightSideType();
if (staticValue != null && staticValue.GetType() == preferredType || preferredType == null)
RightStaticValue = staticValue; RightStaticValue = staticValue;
else if (staticValue != null) 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 // If null create a default instance for value types or simply make it null for reference types
else if (Operator.RightSideType.IsValueType) else if (preferredType.IsValueType)
RightStaticValue = Activator.CreateInstance(Operator.RightSideType); RightStaticValue = Activator.CreateInstance(preferredType);
else else
RightStaticValue = null; RightStaticValue = null;
} }

View File

@ -1,6 +1,4 @@
using System; using System;
using System.IO;
using Artemis.Core.DataModelExpansions;
using Artemis.Storage.Entities.Profile.Abstract; using Artemis.Storage.Entities.Profile.Abstract;
using Artemis.Storage.Entities.Profile.Conditions; using Artemis.Storage.Entities.Profile.Conditions;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -113,6 +111,7 @@ namespace Artemis.Core
RightStaticValue = staticValue; RightStaticValue = staticValue;
return; return;
} }
// If the operator does not support a right side, always set it to null // If the operator does not support a right side, always set it to null
if (Operator.RightSideType == 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 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; RightStaticValue = staticValue;
else if (staticValue != null) 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 // If null create a default instance for value types or simply make it null for reference types
else if (Operator.RightSideType.IsValueType) else if (preferredType.IsValueType)
RightStaticValue = Activator.CreateInstance(Operator.RightSideType); RightStaticValue = Activator.CreateInstance(preferredType);
else else
RightStaticValue = null; RightStaticValue = null;
} }
@ -184,6 +184,22 @@ namespace Artemis.Core
return Operator.InternalEvaluate(LeftPath.GetValue(), RightPath.GetValue()); 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 /> /// <inheritdoc />
public override string ToString() public override string ToString()
{ {

View File

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

View File

@ -156,14 +156,16 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
{ {
DisposeRightSideDynamicViewModel(); DisposeRightSideDynamicViewModel();
if (RightSideInputViewModel == null) if (RightSideInputViewModel == null)
CreateRightSideInputViewModel(SelectedOperator.RightSideType); CreateRightSideInputViewModel();
if (SelectedOperator.RightSideType.IsValueType && DataModelConditionListPredicate.RightStaticValue == null) if (SelectedOperator.RightSideType.IsValueType && DataModelConditionListPredicate.RightStaticValue == null)
RightSideInputViewModel.Value = SelectedOperator.RightSideType.GetDefault(); RightSideInputViewModel.Value = SelectedOperator.RightSideType.GetDefault();
else else
RightSideInputViewModel.Value = DataModelConditionListPredicate.RightStaticValue; RightSideInputViewModel.Value = DataModelConditionListPredicate.RightStaticValue;
if (RightSideInputViewModel.TargetType != SelectedOperator.RightSideType)
RightSideInputViewModel.UpdateTargetType(SelectedOperator.RightSideType); Type preferredType = DataModelConditionListPredicate.GetPreferredRightSideType();
if (RightSideInputViewModel.TargetType != preferredType)
RightSideInputViewModel.UpdateTargetType(preferredType);
} }
} }
@ -286,9 +288,10 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
RightSideSelectionViewModel.ExtraDataModelViewModels.Add(listValue); 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.ButtonBrush = (SolidColorBrush) Application.Current.FindResource("PrimaryHueMidBrush");
RightSideInputViewModel.DisplaySwitchButton = true; RightSideInputViewModel.DisplaySwitchButton = true;
RightSideInputViewModel.ValueUpdated += RightSideOnValueEntered; RightSideInputViewModel.ValueUpdated += RightSideOnValueEntered;

View File

@ -131,14 +131,16 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
{ {
DisposeRightSideDynamicViewModel(); DisposeRightSideDynamicViewModel();
if (RightSideInputViewModel == null) if (RightSideInputViewModel == null)
CreateRightSideInputViewModel(SelectedOperator.RightSideType); CreateRightSideInputViewModel();
if (SelectedOperator.RightSideType.IsValueType && DataModelConditionPredicate.RightStaticValue == null) if (SelectedOperator.RightSideType.IsValueType && DataModelConditionPredicate.RightStaticValue == null)
RightSideInputViewModel.Value = SelectedOperator.RightSideType.GetDefault(); RightSideInputViewModel.Value = SelectedOperator.RightSideType.GetDefault();
else else
RightSideInputViewModel.Value = DataModelConditionPredicate.RightStaticValue; RightSideInputViewModel.Value = DataModelConditionPredicate.RightStaticValue;
if (RightSideInputViewModel.TargetType != SelectedOperator.RightSideType)
RightSideInputViewModel.UpdateTargetType(SelectedOperator.RightSideType); Type preferredType = DataModelConditionPredicate.GetPreferredRightSideType();
if (RightSideInputViewModel.TargetType != preferredType)
RightSideInputViewModel.UpdateTargetType(preferredType);
} }
} }
@ -217,9 +219,10 @@ namespace Artemis.UI.Screens.ProfileEditor.Conditions
RightSideSelectionViewModel.SwitchToStaticRequested += RightSideSelectionViewModelOnSwitchToStaticRequested; 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.ButtonBrush = (SolidColorBrush) Application.Current.FindResource("PrimaryHueMidBrush");
RightSideInputViewModel.DisplaySwitchButton = true; RightSideInputViewModel.DisplaySwitchButton = true;
RightSideInputViewModel.ValueUpdated += RightSideOnValueEntered; RightSideInputViewModel.ValueUpdated += RightSideOnValueEntered;