Skip to content

Commit

Permalink
refactor: load plot asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
timschneeb committed May 4, 2024
1 parent 7e8b6b5 commit a38a213
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 5 additions & 1 deletion GalaxyBudsClient/Interface/Pages/BatteryHistoryPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
xmlns:config="clr-namespace:GalaxyBudsClient.Model.Config"
xmlns:ui="clr-namespace:FluentAvalonia.UI.Controls;assembly=FluentAvalonia"
xmlns:plot="clr-namespace:ScottPlot.Avalonia;assembly=ScottPlot.Avalonia"
xmlns:control="clr-namespace:ScottPlot.Control;assembly=ScottPlot"
mc:Ignorable="d" d:DesignWidth="800"
x:Class="GalaxyBudsClient.Interface.Pages.BatteryHistoryPage"
x:DataType="pages:BatteryHistoryPageViewModel"
Expand All @@ -32,6 +31,11 @@

<!-- AvaPlot does not support bindings :( -->
<plot:AvaPlot Name="PlotControl"
IsVisible="{Binding !IsPlotLoading}"
Grid.Row="1" />

<ui:ProgressRing IsVisible="{Binding IsPlotLoading}"
Grid.Row="1" />

</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Threading;
using GalaxyBudsClient.Generated.I18N;
using GalaxyBudsClient.Interface.Pages;
using GalaxyBudsClient.Utils;
Expand All @@ -13,6 +15,7 @@
using ScottPlot.AxisRules;
using ScottPlot.Control;
using ScottPlot.TickGenerators;
using Serilog;

namespace GalaxyBudsClient.Interface.ViewModels.Pages;

Expand All @@ -21,16 +24,25 @@ public class BatteryHistoryPageViewModel : SubPageViewModelBase
public override Control CreateView() => new BatteryHistoryPage { DataContext = this };
public override string TitleKey => Keys.SystemBatteryStatistics;
public Plot? Plot { set; get; }

[Reactive] public bool IsPlotLoading { set;get; }

public BatteryHistoryPageViewModel()
{
}

public override async void OnNavigatedTo()
public override void OnNavigatedTo()
{
Task.Run(UpdatePlotAsync);
}

private async Task UpdatePlotAsync()
{
if(Plot == null)
return;

IsPlotLoading = true;

Plot.Clear();
Plot.Add.Palette = new ScottPlot.Palettes.Nord();

Expand Down Expand Up @@ -69,7 +81,7 @@ public override async void OnNavigatedTo()
Top = 105,
Bottom = 0
})));*/
Plot.Axes.Left.TickGenerator = new NumericAutomatic()
Plot.Axes.Left.TickGenerator = new NumericAutomatic
{
LabelFormatter = value => value is < 0 or > 100 ? string.Empty : NumericAutomatic.DefaultLabelFormatter(value),
};
Expand All @@ -78,6 +90,8 @@ public override async void OnNavigatedTo()
Plot.ShowLegend();

Plot?.Axes.DateTimeTicksBottom();

IsPlotLoading = false;
}
}

Expand Down

0 comments on commit a38a213

Please sign in to comment.