Skip to content

Commit

Permalink
feat: add time span selector for plot
Browse files Browse the repository at this point in the history
  • Loading branch information
timschneeb committed May 8, 2024
1 parent a38a213 commit 7250b29
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
15 changes: 12 additions & 3 deletions GalaxyBudsClient/Interface/Pages/BatteryHistoryPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<pages:BatteryHistoryPageViewModel />
</Design.DataContext>

<Grid RowDefinitions="Auto,*"
<Grid RowDefinitions="Auto,Auto,*"
Margin="{StaticResource AppPageMargin}">

<controls:CustomInfoBar Title="{ext:Translate {x:Static i18N:Keys.Hint}}"
Expand All @@ -28,14 +28,23 @@
Severity="Informational"
Closed="OnHintClosed"
Margin="0,0,0,8"/>

<StackPanel Grid.Row="1"
Spacing="8"
Orientation="Horizontal"
Margin="0,0,0,8">
<ComboBox ItemsSource="{Binding Source={ext:BatteryHistoryTimeSpansBindingSource}}"
DisplayMemberBinding="{Binding ., Converter={StaticResource EnumDescriptionConverter}}"
SelectedValue="{Binding SelectedTimeSpan}" />
</StackPanel>

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

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

</Grid>
</UserControl>
13 changes: 9 additions & 4 deletions GalaxyBudsClient/Interface/Pages/SystemPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

<ScrollViewer>
<StackPanel Spacing="8" Margin="{StaticResource AppPageMargin}">
<Interaction.Behaviors>
<ext:RequiresConnectedDeviceBehavior />
</Interaction.Behaviors>

<controls:SettingsGroup>
<Interaction.Behaviors>
<ext:RequiresConnectedDeviceBehavior />
</Interaction.Behaviors>

<controls:SettingsSymbolItem Content="{ext:Translate {x:Static i18N:Keys.SystemSelftest}}"
Description="{ext:Translate {x:Static i18N:Keys.SystemSelftestDesc}}"
Symbol="Beaker"
Expand Down Expand Up @@ -75,6 +76,7 @@
IsClickEnabled="True">
<Interaction.Behaviors>
<ext:RequiresFeatureBehavior Feature="UsageReport" />
<ext:RequiresConnectedDeviceBehavior />
</Interaction.Behaviors>
</controls:SettingsSymbolItem>

Expand All @@ -87,7 +89,10 @@
</controls:SettingsGroup>

<controls:SettingsGroup>

<Interaction.Behaviors>
<ext:RequiresConnectedDeviceBehavior />
</Interaction.Behaviors>

<controls:SettingsSymbolItem Name="Firmware"
Content="{ext:Translate {x:Static i18N:Keys.SystemFlasher}}"
Description="{ext:Translate {x:Static i18N:Keys.SystemFlasherDesc}}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
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.Model.Constants;
using GalaxyBudsClient.Utils;
using GalaxyBudsClient.Utils.Interface;
using Microsoft.EntityFrameworkCore;
Expand All @@ -26,9 +28,21 @@ public class BatteryHistoryPageViewModel : SubPageViewModelBase
public Plot? Plot { set; get; }

[Reactive] public bool IsPlotLoading { set;get; }
[Reactive] public BatteryHistoryTimeSpans SelectedTimeSpan { set; get; } = BatteryHistoryTimeSpans.Last3Days;

public BatteryHistoryPageViewModel()
{
PropertyChanged += OnPropertyChanged;
}

private void OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(SelectedTimeSpan):
Task.Run(UpdatePlotAsync);
break;
}
}

public override void OnNavigatedTo()
Expand All @@ -45,9 +59,11 @@ private async Task UpdatePlotAsync()

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

var timeLimit = TimeSpan.FromHours((int)SelectedTimeSpan);

await using var disposableQuery = await BatteryHistoryManager.BeginDisposableQueryAsync();
var cutOffDate = DateTime.Now - TimeSpan.FromDays(1);
var cutOffDate = DateTime.Now - timeLimit;

var query = disposableQuery.Queryable
.Where(record => record.Timestamp > cutOffDate);
Expand All @@ -73,14 +89,14 @@ private async Task UpdatePlotAsync()
plotBatteryR.MarkerShape = MarkerShape.None;
plotBatteryR.LegendText = Strings.Right;


/*Plot.Axes.Rules.Add(new MaximumBoundary(Plot.Axes.Bottom, Plot.Axes.Left, new AxisLimits(new CoordinateRect
{
Right = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
Left = (DateTimeOffset.Now - TimeSpan.FromDays(7)).ToUnixTimeMilliseconds(),
Left = (DateTimeOffset.Now - timeLimit).ToUnixTimeMilliseconds(),
Top = 105,
Bottom = 0
})));*/

Plot.Axes.Left.TickGenerator = new NumericAutomatic
{
LabelFormatter = value => value is < 0 or > 100 ? string.Empty : NumericAutomatic.DefaultLabelFormatter(value),
Expand Down
13 changes: 13 additions & 0 deletions GalaxyBudsClient/Model/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,5 +389,18 @@ public enum DynamicTrayIconModes
[LocalizableDescription(Keys.SettingsDynTrayModeBatteryAvg)]
BatteryAvg = 2
}

[CompiledEnum]
public enum BatteryHistoryTimeSpans
{
[LocalizableDescription(Keys.BattHistLast12Hours)]
Last12Hours = 12,
[LocalizableDescription(Keys.BattHistLast24Hours)]
Last24Hours = 24,
[LocalizableDescription(Keys.BattHistLast3Days)]
Last3Days = 72,
[LocalizableDescription(Keys.BattHistLast7Days)]
Last7Days = 168,
}
}
}
4 changes: 4 additions & 0 deletions GalaxyBudsClient/i18n/en.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ If the transmission is stuck or the device disconnected prematurely, you need to

<!-- Battery history -->
<sys:String x:Key="batt_hist_hint">This app can collect battery levels and basic usage data over time, allowing you to analyze battery drain. Data can only be collected while this app is active in the background and your earbuds are connected to this device.</sys:String>
<sys:String x:Key="batt_hist_last_12_hours">Last 12 hours</sys:String>
<sys:String x:Key="batt_hist_last_24_hours">Last 24 hours</sys:String>
<sys:String x:Key="batt_hist_last_3_days">Last 3 days</sys:String>
<sys:String x:Key="batt_hist_last_7_days">Last 7 days</sys:String>

<!--Settings-->
<sys:String x:Key="settings_header">Settings</sys:String>
Expand Down

0 comments on commit 7250b29

Please sign in to comment.