|
|
|
|
using Connected.Components;
|
|
|
|
|
|
|
|
|
|
namespace Connected.Utilities.BindingConverters;
|
|
|
|
|
|
|
|
|
|
public class RangeConverter<T> : ToStringConverter<Range<T>>
|
|
|
|
|
{
|
|
|
|
|
private readonly DefaultConverter<T> _converter;
|
|
|
|
|
|
|
|
|
|
public RangeConverter() => _converter = new DefaultConverter<T>();
|
|
|
|
|
|
|
|
|
|
protected override string? ConvertValue(Range<T>? value) => value is not null
|
|
|
|
|
? Join(_converter.Convert(value.Start), _converter.Convert(value.End))
|
|
|
|
|
: string.Empty;
|
|
|
|
|
|
|
|
|
|
protected override Range<T>? ConvertValueBack(string? value)
|
|
|
|
|
{
|
|
|
|
|
return !Split(value, out var valueStart, out var valueEnd)
|
|
|
|
|
? null
|
|
|
|
|
: new Range<T>(_converter.ConvertBack(valueStart), _converter.ConvertBack(valueEnd));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Join(string? valueStart, string? valueEnd) => string.IsNullOrEmpty(valueStart) && string.IsNullOrEmpty(valueEnd) ? string.Empty : $"[{valueStart};{valueEnd}]";
|
|
|
|
|
|
|
|
|
|
public static bool Split(string? value, out string? valueStart, out string? valueEnd)
|
|
|
|
|
{
|
|
|
|
|
valueStart = valueEnd = string.Empty;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(value) || value[0] != '[' || value[^1] != ']')
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var idx = value.IndexOf(';');
|
|
|
|
|
|
|
|
|
|
if (idx < 1)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
valueStart = value[1..idx];
|
|
|
|
|
valueEnd = value[(idx + 1)..^1];
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|