Conditions - Finished migration to new API

Conditions UI - Updated for new API, needs simplifying before I'm happy with it
This commit is contained in:
Robert committed 2020-10-08 20:49:23 +02:00
1 parent 725bb2a128
commit 805cdc6782
10 files changed
+91 -75

No files matched your search

@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Artemis.Core;
using Artemis.UI.Shared.Services;
@@ -10,11 +11,12 @@ namespace Artemis.UI.Shared
private int _index;
private Type _listType;
public DataModelListPropertiesViewModel(object listItem) : base(null, null, null)
public DataModelListPropertiesViewModel(Type listType) : base(null, null, null)
{
DataModel = ListPredicateWrapperDataModel.Create(listItem.GetType());
ListType = listItem.GetType();
DisplayValue = listItem;
DataModel = ListPredicateWrapperDataModel.Create(listType);
ListType = listType;
IsRootViewModel = false;
}
public int Index
@@ -35,18 +37,21 @@ namespace Artemis.UI.Shared
set => SetAndNotify(ref _displayValue, value);
}
public DataModelVisualizationViewModel DisplayViewModel => Children.FirstOrDefault();
public override string DisplayPath => null;
public override void Update(IDataModelUIService dataModelUIService)
{
// Display value gets updated by parent, don't do anything if it is null
if (DisplayValue == null)
((ListPredicateWrapperDataModel) DataModel).UntypedValue = DisplayValue;
PopulateProperties(dataModelUIService);
if (DisplayViewModel == null)
return;
ListType = DisplayValue.GetType();
PopulateProperties(dataModelUIService);
foreach (DataModelVisualizationViewModel dataModelVisualizationViewModel in Children)
dataModelVisualizationViewModel.Update(dataModelUIService);
if (IsVisualizationExpanded && !DisplayViewModel.IsVisualizationExpanded)
DisplayViewModel.IsVisualizationExpanded = IsVisualizationExpanded;
DisplayViewModel.Update(dataModelUIService);
}
public override object GetCurrentValue()
@@ -9,19 +9,17 @@ namespace Artemis.UI.Shared
private int _index;
private Type _listType;
public DataModelListPropertyViewModel(object listItem, DataModelDisplayViewModel displayViewModel) : base(null, null, null)
public DataModelListPropertyViewModel(Type listType, DataModelDisplayViewModel displayViewModel) : base(null, null, null)
{
DataModel = ListPredicateWrapperDataModel.Create(listItem.GetType());
ListType = listItem.GetType();
DisplayValue = listItem;
DataModel = ListPredicateWrapperDataModel.Create(listType);
ListType = listType;
DisplayViewModel = displayViewModel;
}
public DataModelListPropertyViewModel(object listItem) : base(null, null, null)
public DataModelListPropertyViewModel(Type listType) : base(null, null, null)
{
DataModel = ListPredicateWrapperDataModel.Create(listItem.GetType());
ListType = listItem.GetType();
DisplayValue = listItem;
DataModel = ListPredicateWrapperDataModel.Create(listType);
ListType = listType;
}
public int Index
@@ -47,6 +45,8 @@ namespace Artemis.UI.Shared
if (DisplayValue == null)
return;
((ListPredicateWrapperDataModel)DataModel).UntypedValue = DisplayValue;
if (DisplayViewModel == null)
DisplayViewModel = dataModelUIService.GetDataModelDisplayViewModel(DisplayValue.GetType(), true);
@@ -33,19 +33,19 @@ namespace Artemis.UI.Shared
public DataModelPropertiesViewModel GetListTypeViewModel(IDataModelUIService dataModelUIService)
{
Type type = DataModelPath.GetPropertyType();
if (type == null)
Type listType = DataModelPath.GetPropertyType()?.GenericTypeArguments[0];
if (listType == null)
return null;
// Create a property VM describing the type of the list
DataModelVisualizationViewModel viewModel = CreateListChild(dataModelUIService, type.GenericTypeArguments[0]);
DataModelVisualizationViewModel viewModel = CreateListChild(dataModelUIService, listType);
viewModel.Update(dataModelUIService);
// Put an empty value into the list type property view model
if (viewModel is DataModelListPropertiesViewModel dataModelListClassViewModel)
{
return dataModelListClassViewModel;
}
if (viewModel is DataModelListPropertyViewModel dataModelListPropertyViewModel)
{
dataModelListPropertyViewModel.DisplayValue = Activator.CreateInstance(dataModelListPropertyViewModel.ListType);
@@ -67,12 +67,15 @@ namespace Artemis.UI.Shared
return;
int index = 0;
foreach (object? item in List)
foreach (object item in List)
{
if (item == null)
continue;
DataModelVisualizationViewModel child;
if (ListChildren.Count <= index)
{
child = CreateListChild(dataModelUIService, item);
child = CreateListChild(dataModelUIService, item.GetType());
ListChildren.Add(child);
}
else
@@ -99,20 +102,18 @@ namespace Artemis.UI.Shared
Count = $"{ListChildren.Count} {(ListChildren.Count == 1 ? "item" : "items")}";
}
protected DataModelVisualizationViewModel CreateListChild(IDataModelUIService dataModelUIService, object listItem)
protected DataModelVisualizationViewModel CreateListChild(IDataModelUIService dataModelUIService, Type listType)
{
Type listType = listItem.GetType();
// If a display VM was found, prefer to use that in any case
DataModelDisplayViewModel typeViewModel = dataModelUIService.GetDataModelDisplayViewModel(listType);
if (typeViewModel != null)
return new DataModelListPropertyViewModel(listItem, typeViewModel);
return new DataModelListPropertyViewModel(listType, 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(listItem);
return new DataModelListPropertyViewModel(listType);
// For other value types create a child view model
if (listType.IsClass || listType.IsStruct())
return new DataModelListPropertiesViewModel(listItem);
return new DataModelListPropertiesViewModel(listType);
return null;
}
@@ -35,7 +35,7 @@ namespace Artemis.UI.Shared
PropertyDescription = DataModelPath?.GetPropertyDescription() ?? DataModel.DataModelDescription;
}
public bool IsRootViewModel { get; }
public bool IsRootViewModel { get; protected set; }
public DataModelPath DataModelPath { get; }
public string Path => DataModelPath?.Path;
@@ -81,7 +81,7 @@ namespace Artemis.UI.Shared
}
}
public virtual string DisplayPath => Path?.Replace(".", " › ");
public virtual string DisplayPath => string.Join(" › ", DataModelPath.Segments.Select(s => s.GetPropertyDescription()?.Name ?? s.Identifier));
/// <summary>
/// Updates the datamodel and if in an parent, any children
@@ -146,7 +146,7 @@ namespace Artemis.UI.Shared
return null;
if (propertyPath == null)
return null;
if (Path.StartsWith(propertyPath, StringComparison.OrdinalIgnoreCase))
if (Path != null && Path.StartsWith(propertyPath, StringComparison.OrdinalIgnoreCase))
return null;
}
@@ -183,7 +183,7 @@ namespace Artemis.UI.Shared
if (IsRootViewModel)
return;
Type modelType = Parent.IsRootViewModel ? DataModel.GetType() : DataModelPath.GetPropertyType();
Type modelType = Parent == null || Parent.IsRootViewModel ? DataModel.GetType() : DataModelPath.GetPropertyType();
// Add missing static children
foreach (PropertyInfo propertyInfo in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
@@ -210,7 +210,7 @@ namespace Artemis.UI.Shared
}
// Add missing dynamic children
object value = Parent.IsRootViewModel ? DataModel : DataModelPath.GetValue();
object value = Parent == null || Parent.IsRootViewModel ? DataModel : DataModelPath.GetValue();
if (value is DataModel dataModel)
{
foreach (KeyValuePair<string, DataModel> kvp in dataModel.DynamicDataModels)