Skip to content

Commit

Permalink
Merge pull request #600 from NilaierMusic/master
Browse files Browse the repository at this point in the history
Fix crash in DeviceSelectStateConverter when switching to Bluetooth tab (#580, #597)
  • Loading branch information
timschneeb authored Feb 9, 2025
2 parents 541a28a + 4678268 commit 6311ce3
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ public class DeviceSelectStateConverter : IMultiValueConverter
{
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{
if(values.Count < 2)
throw new ArgumentException("Expected 2 values");
if (values.Count < 2)
return null;

// If the first value is null, you likely can't compare anything, so pick a fallback:
if (values[0] is null)
return null;
if(values[0] is not Device device)
throw new ArgumentException("Expected Device as first value");

if (values[0] is not Device device)
{
// Instead of throwing, return a fallback
return null;
}

var lastConnectedMac = values[1] as string;
var isSelected = device.MacAddress == lastConnectedMac;

return parameter switch
{
DeviceStateConverterTarget.Label => isSelected ? Strings.DevicesSelectActive : Strings.DevicesSelectInactive,
DeviceStateConverterTarget.Icon => new SymbolIconSource {
DeviceStateConverterTarget.Icon => new SymbolIconSource
{
Symbol = isSelected ? Symbol.CheckboxChecked : Symbol.CheckboxUnchecked,
IsFilled = isSelected
},
Expand All @@ -43,4 +50,4 @@ public class DeviceSelectStateConverter : IMultiValueConverter
_ => isSelected
};
}
}
}

0 comments on commit 6311ce3

Please sign in to comment.