|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Avalonia.Markup.Xaml; |
| 5 | +using GalaxyBudsClient.Model.Constants; |
| 6 | +using GalaxyBudsClient.Message; |
| 7 | +using GalaxyBudsClient.Platform; |
| 8 | +using System.Reflection; |
| 9 | +using GalaxyBudsClient.Model.Attributes; |
| 10 | +using GalaxyBudsClient.Model.Config; |
| 11 | + |
| 12 | +namespace GalaxyBudsClient.Interface.MarkupExtensions; |
| 13 | + |
| 14 | +public class DeviceColorBindingSource : MarkupExtension |
| 15 | +{ |
| 16 | + public override object ProvideValue(IServiceProvider serviceProvider) |
| 17 | + { |
| 18 | + var currentDevice = BluetoothImpl.Instance.Device.Current; |
| 19 | + if (currentDevice == null) |
| 20 | + return Array.Empty<DeviceIds>(); |
| 21 | + |
| 22 | + // Get current device's model and color |
| 23 | + var currentModel = currentDevice.Model; |
| 24 | + var currentColor = currentDevice.DeviceColor ?? |
| 25 | + DeviceMessageCache.Instance.ExtendedStatusUpdate?.DeviceColor ?? |
| 26 | + DeviceIds.Unknown; |
| 27 | + |
| 28 | + // Filter DeviceIds based on current model |
| 29 | + var values = Enum.GetValues(typeof(DeviceIds)) |
| 30 | + .Cast<DeviceIds>() |
| 31 | + .Where(x => { |
| 32 | + var field = typeof(DeviceIds).GetField(x.ToString()); |
| 33 | + var attr = field?.GetCustomAttribute<AssociatedModelAttribute>(); |
| 34 | + return attr != null && attr.Model == currentModel; |
| 35 | + }) |
| 36 | + .OrderBy(x => x.ToString()) |
| 37 | + .ToList(); |
| 38 | + |
| 39 | + // Move current color to top if it exists in the list |
| 40 | + if (currentColor != DeviceIds.Unknown && values.Contains(currentColor)) |
| 41 | + { |
| 42 | + values.Remove(currentColor); |
| 43 | + values.Insert(0, currentColor); |
| 44 | + } |
| 45 | + |
| 46 | + // Set the initial value if no override is set |
| 47 | + if (Settings.Data.ColorOverride == null) |
| 48 | + { |
| 49 | + Settings.Data.ColorOverride = currentColor; |
| 50 | + } |
| 51 | + |
| 52 | + return values; |
| 53 | + } |
| 54 | +} |
0 commit comments