Skip to content

Commit

Permalink
debounce notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-ward committed Nov 29, 2022
1 parent a37567e commit f7ff8b0
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
24 changes: 22 additions & 2 deletions src/tweetz.core/Models/ObservableCollectionEx.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using tweetz.core.Services;

namespace tweetz.core.Models
{
Expand Down Expand Up @@ -54,11 +56,29 @@ public void RemoveAtNoNotify(int index)
Items.RemoveAt(index);
}

private Action<int>? debounceNotifyActionChanged;

public void NotifyCollectionChanged()
{
debounceNotifyActionChanged ??= DebounceService.Debounce<int>(_ => NotifyCollectionChangedImplementation());
debounceNotifyActionChanged(0);
}

private void NotifyCollectionChangedImplementation()
{
NotifyCollectionCountChanged();
NotifyCollectionItemsChanged();
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}

private void NotifyCollectionCountChanged()
{
OnPropertyChanged(new PropertyChangedEventArgs("Count"));
}

private void NotifyCollectionItemsChanged()
{
OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
}
13 changes: 12 additions & 1 deletion src/tweetz.core/Services/DebounceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ public static Action<T> Debounce<T>(this Action<T> func, int milliseconds = 300)
return arg =>
{
cancelTokenSource?.Cancel();

try
{
cancelTokenSource?.Dispose();
}
catch
{
// ignored
}

cancelTokenSource = new CancellationTokenSource();

var unused = Task.Delay(milliseconds, cancelTokenSource.Token)
var unused = Task
.Delay(milliseconds, cancelTokenSource.Token)
.ContinueWith(t =>
{
if (t.IsCompletedSuccessfully)
Expand Down
2 changes: 1 addition & 1 deletion src/tweetz.core/Services/SoundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace tweetz.core.Services
{
public static class SoundService
{
public static void PlayDefaultNotifySound()
public static void PlayNotifySound()
{
using var notifySound = Application.GetResourceStream(new Uri("pack://application:,,,/notify.wav"))!.Stream;
using var player = new SoundPlayer(notifySound);
Expand Down
2 changes: 1 addition & 1 deletion src/tweetz.core/Services/TruncateStatusCollectionTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static ValueTask Execute(TwitterTimeline timeline)
const int maxNumberOfStatuses = 500;
var truncated = timeline.StatusCollection.Count > maxNumberOfStatuses;

while (timeline.StatusCollection.Count > maxNumberOfStatuses)
while (timeline.StatusCollection.Count > maxNumberOfStatuses - 50)
{
timeline.StatusCollection.RemoveAtNoNotify(timeline.StatusCollection.Count - 1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/tweetz.core/Views/TimelineView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void PlayNotifySound()
{
if (string.IsNullOrWhiteSpace(Settings.NotifySoundSource))
{
SoundService.PlayDefaultNotifySound();
SoundService.PlayNotifySound();
}
else
{
Expand Down

0 comments on commit f7ff8b0

Please sign in to comment.