Skip to content

Commit

Permalink
Merge pull request #71 from kmaki565/improve-performance
Browse files Browse the repository at this point in the history
Improve performance of Auto Refresh and Select All checkbox
  • Loading branch information
kmaki565 authored Aug 25, 2024
2 parents c25251b + 561c502 commit 5821fdc
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
10 changes: 7 additions & 3 deletions EventLook/Model/EventItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
Expand All @@ -12,7 +14,7 @@ namespace EventLook.Model;
/// Represents a event to display.
/// Could not inherit EventLogRecord as it doesn't have a public constructor.
/// </summary>
public class EventItem : IDisposable
public class EventItem : ObservableObject, IDisposable
{
public EventItem(LogSource logSource, EventRecord eventRecord)
{
Expand Down Expand Up @@ -62,10 +64,12 @@ public EventItem(LogSource logSource, EventRecord eventRecord)
/// Indicates the time when the event was loaded by the app.
/// </summary>
public DateTime TimeLoaded { get; set; }

/// <summary>
/// Indicates if the event is newly loaded.
/// </summary>
public bool IsNewLoaded { get; set; }
private bool _isNewLoaded;
public bool IsNewLoaded { get => _isNewLoaded; set => SetProperty(ref _isNewLoaded, value); }

public void Dispose()
{
Expand Down
2 changes: 1 addition & 1 deletion EventLook/Model/FilterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public virtual void Reset()
/// <summary>
/// Applies filter when the user operates the filter UI.
/// </summary>
public void Apply()
public virtual void Apply()
{
if (cvs != null)
{
Expand Down
23 changes: 23 additions & 0 deletions EventLook/Model/LevelFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public override void Reset()
{
levelFilters.Clear();
}
public override void Apply()
{
if (IsFilterSelectionsChanged())
{
base.Apply();
}
}

protected override bool IsFilterMatched(EventItem evt)
{
Expand Down Expand Up @@ -96,6 +103,22 @@ public bool UncheckFilter(byte? level)
}
return true;
}

private readonly List<LevelFilterItem> _prevFilters = new List<LevelFilterItem>();
private bool IsFilterSelectionsChanged()
{
bool changed = levelFilters.Count != _prevFilters.Count || levelFilters.Where((lf, i) => lf.Level != _prevFilters[i].Level || lf.Selected != _prevFilters[i].Selected).Any();

if (changed)
{
_prevFilters.Clear();
foreach (var lf in levelFilters)
{
_prevFilters.Add(new LevelFilterItem { Level = lf.Level, Selected = lf.Selected });
}
}
return changed;
}
}

public class LevelFilterItem : Monitorable
Expand Down
27 changes: 27 additions & 0 deletions EventLook/Model/SourceFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public override void Reset()
{
sourceFilters.Clear();
}
public override void Apply()
{
// When Select All is clicked, this will be called for the number of the checkboxes.
// As I observed, selections of all checkboxes are already set at the first call
// so we can ignore the rest of the calls.
if (IsFilterSelectionsChanged())
{
base.Apply();
}
}

protected override bool IsFilterMatched(EventItem evt)
{
Expand Down Expand Up @@ -100,6 +110,23 @@ public bool UncheckFilter(string name)
}
return true;
}

readonly List<SourceFilterItem> _prevFilters = new List<SourceFilterItem>();
private bool IsFilterSelectionsChanged()
{
bool changed = sourceFilters.Count != _prevFilters.Count || sourceFilters.Where((sf, i) => sf.Name != _prevFilters[i].Name || sf.Selected != _prevFilters[i].Selected).Any();

if (changed)
{
_prevFilters.Clear();
foreach (var sf in sourceFilters)
{
_prevFilters.Add(new SourceFilterItem { Name = sf.Name, Selected = sf.Selected });
}
}

return changed;
}
}

public class SourceFilterItem : Monitorable
Expand Down
9 changes: 3 additions & 6 deletions EventLook/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,12 @@ private void ClearFilters()
{
filters.ForEach(f => f.Clear());
}
private async void ApplySourceFilter()
private void ApplySourceFilter()
{
// Delay is needed to ensure filter update in UI is propagated to the source.
await Task.Delay(10);
sourceFilter.Apply();
}
private async void ApplyLevelFilter()
private void ApplyLevelFilter()
{
await Task.Delay(10);
levelFilter.Apply();
}
private void OpenDetails()
Expand Down Expand Up @@ -537,7 +534,7 @@ private bool UpdateIsNewLoaded(bool all = false)
Events.TakeWhile(e => e.IsNewLoaded)
.Where(e => all || DateTime.Now - e.TimeLoaded >= newLoadedTimeThreshold)
.ToList().ForEach(e => e.IsNewLoaded = false);
CVS.View.Refresh();

return Events.Any(e => e.IsNewLoaded);
}

Expand Down

0 comments on commit 5821fdc

Please sign in to comment.