Data model - Support all lists implementing IEnumerable<>

This commit is contained in:
Robert committed 2020-10-12 19:31:02 +02:00
1 parent 17d41647b6
commit 3b6753a0ff
9 files changed
+91 -128

No files matched your search

@@ -9,31 +9,38 @@ namespace Artemis.UI.Shared
{
public class DataModelListViewModel : DataModelVisualizationViewModel
{
private string _count;
private IList _list;
private string _countDisplay;
private IEnumerable _list;
private int _listCount;
internal DataModelListViewModel(DataModel dataModel, DataModelVisualizationViewModel parent, DataModelPath dataModelPath) : base(dataModel, parent, dataModelPath)
{
ListChildren = new BindableCollection<DataModelVisualizationViewModel>();
}
public IList List
public IEnumerable List
{
get => _list;
set => SetAndNotify(ref _list, value);
}
public BindableCollection<DataModelVisualizationViewModel> ListChildren { get; set; }
public string Count
public int ListCount
{
get => _count;
set => SetAndNotify(ref _count, value);
get => _listCount;
set => SetAndNotify(ref _listCount, value);
}
public string CountDisplay
{
get => _countDisplay;
set => SetAndNotify(ref _countDisplay, value);
}
public BindableCollection<DataModelVisualizationViewModel> ListChildren { get; set; }
public DataModelPropertiesViewModel GetListTypeViewModel(IDataModelUIService dataModelUIService)
{
Type listType = DataModelPath.GetPropertyType()?.GenericTypeArguments[0];
Type listType = DataModelPath.GetPropertyType()?.GetGenericEnumerableType();
if (listType == null)
return null;
@@ -42,10 +49,8 @@ namespace Artemis.UI.Shared
viewModel.Update(dataModelUIService);
// Put an empty value into the list type property view model
if (viewModel is DataModelListPropertiesViewModel dataModelListClassViewModel)
{
return dataModelListClassViewModel;
}
if (viewModel is DataModelListPropertiesViewModel dataModelListClassViewModel) return dataModelListClassViewModel;
if (viewModel is DataModelListPropertyViewModel dataModelListPropertyViewModel)
{
dataModelListPropertyViewModel.DisplayValue = Activator.CreateInstance(dataModelListPropertyViewModel.ListType);
@@ -62,7 +67,7 @@ namespace Artemis.UI.Shared
if (Parent != null && !Parent.IsVisualizationExpanded)
return;
List = GetCurrentValue() as IList;
List = GetCurrentValue() as IEnumerable;
if (List == null)
return;
@@ -71,7 +76,7 @@ namespace Artemis.UI.Shared
{
if (item == null)
continue;
DataModelVisualizationViewModel child;
if (ListChildren.Count <= index)
{
@@ -79,7 +84,9 @@ namespace Artemis.UI.Shared
ListChildren.Add(child);
}
else
{
child = ListChildren[index];
}
if (child is DataModelListPropertiesViewModel dataModelListClassViewModel)
{
@@ -96,10 +103,18 @@ namespace Artemis.UI.Shared
index++;
}
while (ListChildren.Count > List.Count)
ListCount = index + 1;
while (ListChildren.Count > ListCount)
ListChildren.RemoveAt(ListChildren.Count - 1);
Count = $"{ListChildren.Count} {(ListChildren.Count == 1 ? "item" : "items")}";
CountDisplay = $"{ListChildren.Count} {(ListChildren.Count == 1 ? "item" : "items")}";
}
/// <inheritdoc />
public override string ToString()
{
return $"[List] {DisplayPath ?? Path} - {ListCount} item(s)";
}
protected DataModelVisualizationViewModel CreateListChild(IDataModelUIService dataModelUIService, Type listType)
@@ -117,11 +132,5 @@ namespace Artemis.UI.Shared
return null;
}
/// <inheritdoc />
public override string ToString()
{
return $"[List] {DisplayPath ?? Path} - {List?.Count ?? 0} item(s)";
}
}
}
@@ -133,7 +133,9 @@ namespace Artemis.UI.Shared
}
if (looseMatch)
IsMatchingFilteredTypes = filteredTypes.Any(t => t.IsCastableFrom(type) || t == typeof(Enum) && type.IsEnum);
IsMatchingFilteredTypes = filteredTypes.Any(t => t.IsCastableFrom(type) ||
t == typeof(Enum) && type.IsEnum ||
t == typeof(IEnumerable<>) && type.IsGenericEnumerable());
else
IsMatchingFilteredTypes = filteredTypes.Any(t => t == type || t == typeof(Enum) && type.IsEnum);
}
@@ -260,12 +262,12 @@ namespace Artemis.UI.Shared
// For primitives, create a property view model, it may be null that is fine
if (propertyType.IsPrimitive || propertyType.IsEnum || propertyType == typeof(string))
return new DataModelPropertyViewModel(DataModel, this, dataModelPath) {Depth = depth};
if (typeof(IList).IsAssignableFrom(propertyType))
if (propertyType.IsGenericEnumerable())
return new DataModelListViewModel(DataModel, this, dataModelPath) {Depth = depth};
// For other value types create a child view model
if (propertyType.IsClass || propertyType.IsStruct())
return new DataModelPropertiesViewModel(DataModel, this, dataModelPath) {Depth = depth};
return null;
}