Skip to content

Commit

Permalink
Merge pull request #594 from NilaierMusic/master
Browse files Browse the repository at this point in the history
Fix cross-thread UI calls in EarbudStatusUnit (#593)
  • Loading branch information
timschneeb authored Feb 2, 2025
2 parents 6a0637f + 374f51d commit b39e90c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
23 changes: 15 additions & 8 deletions GalaxyBudsClient/Interface/Controls/EarbudStatusUnit.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System;
using System.Collections.Specialized;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Threading;
using GalaxyBudsClient.Interface.ViewModels.Controls;
using GalaxyBudsClient.Model.Config;
using GalaxyBudsClient.Model.Constants;
Expand All @@ -17,17 +18,23 @@ public EarbudStatusUnit()
var vm = new EarbudStatusUnitViewModel();
vm.Changed.Subscribe(new Action<object>(_ =>
{
// FIXME: Avalonia bug: After upgrading to Avalonia 11.2.2 from 11.2.0-beta, the label bounds are initially too small
LeftBatteryText.InvalidateMeasure();
RightBatteryText.InvalidateMeasure();
// FIXME: Avalonia bug: After upgrading to Avalonia 11.2.2 from 11.2.0-beta, the label bounds are initially too small
Dispatcher.UIThread.Post(() =>
{
LeftBatteryText.InvalidateMeasure();
RightBatteryText.InvalidateMeasure();
});
}));

DataContext = vm;
}

private void OnTemperaturePointerPressed(object? sender, PointerPressedEventArgs e)
{
Settings.Data.TemperatureUnit = Settings.Data.TemperatureUnit == TemperatureUnits.Celsius
? TemperatureUnits.Fahrenheit : TemperatureUnits.Celsius;
Dispatcher.UIThread.Post(() =>
{
Settings.Data.TemperatureUnit = Settings.Data.TemperatureUnit == TemperatureUnits.Celsius
? TemperatureUnits.Fahrenheit : TemperatureUnits.Celsius;
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel;
using System.Threading.Tasks;
using Avalonia.Threading;
using GalaxyBudsClient.Message;
using GalaxyBudsClient.Message.Decoder;
using GalaxyBudsClient.Model.Config;
Expand Down Expand Up @@ -45,16 +46,22 @@ public EarbudStatusUnitViewModel()

private void OnBluetoothPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(BluetoothImpl.Instance.IsConnected) &&
DeviceMessageCache.Instance.BasicStatusUpdate != null)
OnStatusUpdated(null, DeviceMessageCache.Instance.BasicStatusUpdate);
Dispatcher.UIThread.Post(() =>
{
if (e.PropertyName == nameof(BluetoothImpl.Instance.IsConnected) &&
DeviceMessageCache.Instance.BasicStatusUpdate != null)
OnStatusUpdated(null, DeviceMessageCache.Instance.BasicStatusUpdate);
});
}

private void OnMainSettingsPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
// Recalculate temperature values when temperature unit changes
if (e.PropertyName == nameof(Settings.Data.TemperatureUnit))
LoadFromCache();
Dispatcher.UIThread.Post(() =>
{
// Recalculate temperature values when temperature unit changes
if (e.PropertyName == nameof(Settings.Data.TemperatureUnit))
LoadFromCache();
});
}

private void LoadFromCache()
Expand All @@ -69,28 +76,34 @@ private void LoadFromCache()

private void OnStatusUpdated(object? sender, IBasicStatusUpdate e)
{
var connected = BluetoothImpl.Instance.IsConnected;
LeftBattery = e.BatteryL;
RightBattery = e.BatteryR;
CaseBattery = e.BatteryCase is <= 0 or > 100 ? null : e.BatteryCase;
LeftWearState = e.PlacementL;
RightWearState = e.PlacementR;
IsLeftOnline = connected && e.BatteryL > 0 && e.PlacementL != PlacementStates.Disconnected;
IsRightOnline = connected && e.BatteryR > 0 && e.PlacementR != PlacementStates.Disconnected;
Dispatcher.UIThread.Post(() =>
{
var connected = BluetoothImpl.Instance.IsConnected;
LeftBattery = e.BatteryL;
RightBattery = e.BatteryR;
CaseBattery = e.BatteryCase is <= 0 or > 100 ? null : e.BatteryCase;
LeftWearState = e.PlacementL;
RightWearState = e.PlacementR;
IsLeftOnline = connected && e.BatteryL > 0 && e.PlacementL != PlacementStates.Disconnected;
IsRightOnline = connected && e.BatteryR > 0 && e.PlacementR != PlacementStates.Disconnected;
});
}

private void OnGetAllDataResponse(object? sender, DebugGetAllDataDecoder e)
{
// Buds2 Pro seem to send 0 for all values sometimes. Ignore those updates.
if(e is { LeftThermistor: <= 0, RightThermistor: <= 0 })
return;

var useF = Settings.Data.TemperatureUnit == TemperatureUnits.Fahrenheit;
LeftVoltage = e.LeftAdcVCell;
RightVoltage = e.RightAdcVCell;
LeftCurrent = e.LeftAdcCurrent;
RightCurrent = e.RightAdcCurrent;
LeftTemperature = useF ? 9.0 / 5.0 * e.LeftThermistor + 32 : e.LeftThermistor;
RightTemperature = useF ? 9.0 / 5.0 * e.RightThermistor + 32 : e.RightThermistor;
Dispatcher.UIThread.Post(() =>
{
// Buds2 Pro seem to send 0 for all values sometimes. Ignore those updates.
if(e is { LeftThermistor: <= 0, RightThermistor: <= 0 })
return;

var useF = Settings.Data.TemperatureUnit == TemperatureUnits.Fahrenheit;
LeftVoltage = e.LeftAdcVCell;
RightVoltage = e.RightAdcVCell;
LeftCurrent = e.LeftAdcCurrent;
RightCurrent = e.RightAdcCurrent;
LeftTemperature = useF ? 9.0 / 5.0 * e.LeftThermistor + 32 : e.LeftThermistor;
RightTemperature = useF ? 9.0 / 5.0 * e.RightThermistor + 32 : e.RightThermistor;
});
}
}
}

0 comments on commit b39e90c

Please sign in to comment.