Avoid redundant database queries when the Viewer tab is not visible

This commit is contained in:
chylex 2022-03-20 22:23:26 +01:00
parent 9341988017
commit 3e891e19c3
No known key found for this signature in database
GPG Key ID: 4DE42C8F19A80548
3 changed files with 26 additions and 2 deletions

View File

@ -5,7 +5,9 @@
xmlns:pages="clr-namespace:DHT.Desktop.Main.Pages"
xmlns:controls="clr-namespace:DHT.Desktop.Main.Controls"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="DHT.Desktop.Main.Pages.ViewerPage">
x:Class="DHT.Desktop.Main.Pages.ViewerPage"
AttachedToVisualTree="OnAttachedToVisualTree"
DetachedFromVisualTree="OnDetachedFromVisualTree">
<Design.DataContext>
<pages:ViewerPageModel />

View File

@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
@ -12,5 +13,17 @@ namespace DHT.Desktop.Main.Pages {
private void InitializeComponent() {
AvaloniaXamlLoader.Load(this);
}
public void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e) {
if (DataContext is ViewerPageModel model) {
model.SetPageVisible(true);
}
}
public void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e) {
if (DataContext is ViewerPageModel model) {
model.SetPageVisible(false);
}
}
}
}

View File

@ -34,6 +34,8 @@ namespace DHT.Desktop.Main.Pages {
private readonly Window window;
private readonly IDatabaseFile db;
private bool isPageVisible = false;
[Obsolete("Designer")]
public ViewerPageModel() : this(null!, DummyDatabaseFile.Instance) {}
@ -52,13 +54,20 @@ namespace DHT.Desktop.Main.Pages {
FilterModel.Dispose();
}
public void SetPageVisible(bool isPageVisible) {
this.isPageVisible = isPageVisible;
if (isPageVisible) {
UpdateStatistics();
}
}
private void OnFilterPropertyChanged(object? sender, PropertyChangedEventArgs e) {
UpdateStatistics();
HasFilters = FilterModel.HasAnyFilters;
}
private void OnDbStatisticsChanged(object? sender, PropertyChangedEventArgs e) {
if (e.PropertyName == nameof(DatabaseStatistics.TotalMessages)) {
if (isPageVisible && e.PropertyName == nameof(DatabaseStatistics.TotalMessages)) {
UpdateStatistics();
}
}