Add DropdownValueResolver

This commit is contained in:
VladV 2023-04-05 21:01:49 +04:00
parent af9d9f561d
commit 69ebc4dc89
3 changed files with 71 additions and 38 deletions

View File

@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using TriInspector;
using TriInspector;
using TriInspector.Drawers;
using TriInspector.Elements;
using TriInspector.Resolvers;
@ -11,22 +9,15 @@ namespace TriInspector.Drawers
{
public class DropdownDrawer<T> : TriAttributeDrawer<DropdownAttribute>
{
private ValueResolver<IEnumerable<TriDropdownItem<T>>> _itemsResolver;
private ValueResolver<IEnumerable<T>> _valuesResolver;
private DropdownValuesResolver<T> _valuesResolver;
public override TriExtensionInitializationResult Initialize(TriPropertyDefinition propertyDefinition)
{
_valuesResolver = ValueResolver.Resolve<IEnumerable<T>>(propertyDefinition, Attribute.Values);
_valuesResolver = DropdownValuesResolver<T>.Resolve(propertyDefinition, Attribute.Values);
if (_valuesResolver.TryGetErrorString(out _))
if (_valuesResolver.TryGetErrorString(out var error))
{
_itemsResolver =
ValueResolver.Resolve<IEnumerable<TriDropdownItem<T>>>(propertyDefinition, Attribute.Values);
if (_itemsResolver.TryGetErrorString(out var itemResolverError))
{
return itemResolverError;
}
return error;
}
return TriExtensionInitializationResult.Ok;
@ -34,30 +25,7 @@ namespace TriInspector.Drawers
public override TriElement CreateElement(TriProperty property, TriElement next)
{
return new TriDropdownElement(property, GetDropdownItems);
}
private IEnumerable<ITriDropdownItem> GetDropdownItems(TriProperty property)
{
if (_valuesResolver != null)
{
var values = _valuesResolver.GetValue(property, Enumerable.Empty<T>());
foreach (var value in values)
{
yield return new TriDropdownItem {Text = $"{value}", Value = value,};
}
}
if (_itemsResolver != null)
{
var values = _itemsResolver.GetValue(property, Enumerable.Empty<TriDropdownItem<T>>());
foreach (var value in values)
{
yield return value;
}
}
return new TriDropdownElement(property, _valuesResolver.GetDropdownItems);
}
}
}

View File

@ -0,0 +1,62 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
namespace TriInspector.Resolvers
{
public class DropdownValuesResolver<T>
{
[CanBeNull] private ValueResolver<IEnumerable<TriDropdownItem<T>>> _itemsResolver;
[CanBeNull] private ValueResolver<IEnumerable<T>> _valuesResolver;
[PublicAPI]
public static DropdownValuesResolver<T> Resolve(TriPropertyDefinition propertyDefinition, string expression)
{
var valuesResolver = ValueResolver.Resolve<IEnumerable<T>>(propertyDefinition, expression);
if (!valuesResolver.TryGetErrorString(out _))
{
return new DropdownValuesResolver<T>
{
_valuesResolver = valuesResolver,
};
}
var itemsResolver = ValueResolver.Resolve<IEnumerable<TriDropdownItem<T>>>(propertyDefinition, expression);
return new DropdownValuesResolver<T>
{
_itemsResolver = itemsResolver,
};
}
[PublicAPI]
public bool TryGetErrorString(out string error)
{
return ValueResolver.TryGetErrorString(_valuesResolver, _itemsResolver, out error);
}
[PublicAPI]
public IEnumerable<ITriDropdownItem> GetDropdownItems(TriProperty property)
{
if (_valuesResolver != null)
{
var values = _valuesResolver.GetValue(property, Enumerable.Empty<T>());
foreach (var value in values)
{
yield return new TriDropdownItem {Text = $"{value}", Value = value,};
}
}
if (_itemsResolver != null)
{
var values = _itemsResolver.GetValue(property, Enumerable.Empty<TriDropdownItem<T>>());
foreach (var value in values)
{
yield return value;
}
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 16fa8c4711a44bdea6facb3bc68822d9
timeCreated: 1680713564