You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Components/Utilities/BindingConverters/RangeConverter.cs

70 lines
1.4 KiB

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