Skip to content

Commit

Permalink
fix: insert null frame into battery history on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
timschneeb committed May 3, 2024
1 parent 1ea58f9 commit 7c7fcbd
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions GalaxyBudsClient/Utils/BatteryHistoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using GalaxyBudsClient.Interface;
using GalaxyBudsClient.Interface.ViewModels.Pages;
using GalaxyBudsClient.Message;
Expand All @@ -24,7 +24,7 @@ public class BatteryHistoryManager
private string? _currentMac;
private HistoryRecord? _lastRecord;

private const int RetainDays = 7;
private const int RetainDays = 30;

private BatteryHistoryManager()
{
Expand All @@ -35,13 +35,20 @@ private BatteryHistoryManager()
MainWindow.Instance.MainView.ResolveViewModelByType<NoiseControlPageViewModel>()!
.PropertyChanged += async (_, _) => await CollectNow();

if(Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime lifetime)
{
lifetime.Exit += (_, _) => _ = CollectNow(insertNullFrame: true);
}

_currentMac = BluetoothImpl.Instance.Device.Current?.MacAddress;
_ = CollectNow(insertNullFrame: true);
}

private void OnDeviceChanged(object? sender, Device? e)
private async void OnDeviceChanged(object? sender, Device? e)
{
_currentMac = BluetoothImpl.Instance.Device.Current?.MacAddress;
_lastRecord = null;
await CollectNow(insertNullFrame: true);
}

private async void OnBluetoothPropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand Down Expand Up @@ -74,7 +81,7 @@ public void DeleteAllDatabases()

private async Task CleanupNow()
{
if (_currentMac == null)
if (_currentMac == null || !Settings.Data.CollectBatteryHistory)
return;

if (_lock.WaitOne(500))
Expand All @@ -96,16 +103,16 @@ private async Task CleanupNow()
}
}

private async Task CollectNow()
private async Task CollectNow(bool insertNullFrame = false)
{
if (_currentMac == null || !Settings.Data.CollectBatteryHistory)
{
_lastRecord = null;
return;
};

var status = BluetoothImpl.Instance.IsConnected ? DeviceMessageCache.Instance.StatusUpdate : null;
var noiseControlVm = BluetoothImpl.Instance.IsConnected ? MainWindow.Instance.MainView.ResolveViewModelByType<NoiseControlPageViewModel>() : null;
var status = !insertNullFrame && BluetoothImpl.Instance.IsConnected ? DeviceMessageCache.Instance.StatusUpdate : null;
var noiseControlVm = !insertNullFrame && BluetoothImpl.Instance.IsConnected ? MainWindow.Instance.MainView.ResolveViewModelByType<NoiseControlPageViewModel>() : null;
var record = new HistoryRecord
{
PlacementL = status?.PlacementL ?? PlacementStates.Disconnected,
Expand Down

0 comments on commit 7c7fcbd

Please sign in to comment.