using Avalonia; using Avalonia.Data.Converters; using Shaker.Models; using ShakerApp.Tools; using ShakerApp.ViewModels; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; namespace ShakerApp.Convert { public class EnumToBooleanConverter : IValueConverter { public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { if(value is Enum e1 && parameter is Enum e2 && e1.GetType() == e2.GetType()) { return e1.ToString() == e2.ToString(); } return false; } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) { throw new NotImplementedException(); } } public class EnumToDescription : IValueConverter { public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { if (value == null || value == AvaloniaProperty.UnsetValue || !value.GetType().IsEnum) return ""; if( value is Enum e) return e.Description(); return ""; } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) { throw new NotImplementedException(); } } public class EnumToCollectionConverter : IValueConverter { public static string Value { get; } = nameof(ValueDescription.Value); public static string Key { get; } = nameof(ValueDescription.Key); public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) { if (value == null || value == AvaloniaProperty.UnsetValue || !value.GetType().IsEnum) return null; try { string parstr = (parameter ==null ?"":parameter.ToString())!; List showValues = new List(); if (!string.IsNullOrEmpty(parstr)) showValues = parstr.Split(",".ToCharArray()).Select(x => (Enum)Enum.ToObject(value.GetType(), int.Parse(x))).ToList(); List valueDescriptions = new List(); Enum.GetValues(value.GetType()) .Cast() .ToList().ForEach(e => { if (parameter != null && showValues.Count > 0) { if (showValues.FindIndex(x => x.ToString() == e.ToString()) >= 0) valueDescriptions.Add(new ValueDescription() { Value = e, Key = e.Description() }); } else valueDescriptions.Add(new ValueDescription() { Value = e, Key = e.Description() }); }); return valueDescriptions; } catch { } return null; } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) { return null; } } internal class ValueDescription : DisplayViewModelBase { private Enum? value; private string key = string.Empty; private bool visibility = true; private bool isEnabled = true; public ValueDescription() { } public Enum? Value { get => value; set => SetProperty(ref this.value, value); } public string Key { get => key; set => SetProperty(ref key, value); } public bool Visibility { get => visibility; set => SetProperty(ref visibility, value); } public bool IsEnabled { get => isEnabled; set => SetProperty(ref isEnabled, value); } public override string ToString() { return Key; } } }