Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit fbebb96

Browse files
committed
?
Can't remember what this was about ¯\_(ツ)_/¯
1 parent 9c13f0b commit fbebb96

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

Toastify/src/Core/Spotify.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using ToastifyAPI.Model.Interfaces;
2323
using ToastifyAPI.Native;
2424
using Settings = Toastify.Model.Settings;
25+
using Timer = System.Threading.Timer;
2526

2627
namespace Toastify.Core
2728
{
@@ -41,10 +42,15 @@ public static Spotify Instance
4142
#endregion
4243

4344
private readonly string spotifyPath;
45+
private readonly TimeSpan songChangeBuffer = TimeSpan.FromMilliseconds(1500);
4446

4547
private SpotifyWindow spotifyWindow;
4648
private Process spotifyProcess;
4749

50+
private Timer apiTrackDelayedUpdateTimer;
51+
private DateTime lastSongChange = DateTime.Now;
52+
private int songChangesInTimespan;
53+
4854
#region Public Properties
4955

5056
[PropertyDependency]
@@ -853,8 +859,25 @@ private async void SpotifyWindowTitleWatcher_TitleChanged(object sender, WindowT
853859
if (logger.IsDebugEnabled)
854860
logger.Debug($"Spotify's window title changed: \"{e.NewTitle}\". Fetching song info...");
855861

856-
if (!(Settings.Current.EnableSpotifyWebApi && this.IsWebApiRunning &&
857-
await this.UpdateTrackInfoUsingWebApi().ConfigureAwait(false)))
862+
bool shouldUpdateUsingWindowTitle = !(Settings.Current.EnableSpotifyWebApi && this.IsWebApiRunning);
863+
864+
this.apiTrackDelayedUpdateTimer?.Dispose();
865+
bool tooFast = this.songChangesInTimespan >= 3;
866+
if (!shouldUpdateUsingWindowTitle && tooFast)
867+
{
868+
logger.Debug($"Songs are being changed too fast ({this.songChangesInTimespan} times in {this.songChangeBuffer.TotalMilliseconds} ms)!");
869+
this.apiTrackDelayedUpdateTimer = new Timer(async state =>
870+
{
871+
logger.Debug($"Executing delayed track update using WebAPI (\"{state}\")");
872+
await this.UpdateTrackInfoUsingWebApi().ConfigureAwait(false);
873+
}, e.NewTitle, this.songChangeBuffer, TimeSpan.FromMilliseconds(-1));
874+
shouldUpdateUsingWindowTitle = true;
875+
}
876+
877+
if (!shouldUpdateUsingWindowTitle)
878+
shouldUpdateUsingWindowTitle = !await this.UpdateTrackInfoUsingWebApi().ConfigureAwait(false);
879+
880+
if (shouldUpdateUsingWindowTitle)
858881
{
859882
// If the WebAPIs are disabled or they weren't able to retrieve the song info, fallback to
860883
// the old method based on the title of Spotify's window.
@@ -882,6 +905,11 @@ await this.UpdateTrackInfoUsingWebApi().ConfigureAwait(false)))
882905
}
883906
}
884907
}
908+
909+
this.songChangesInTimespan++;
910+
if (DateTime.Now - this.lastSongChange > this.songChangeBuffer)
911+
this.songChangesInTimespan = 0;
912+
this.lastSongChange = DateTime.Now;
885913
}
886914
catch (Exception exception)
887915
{

0 commit comments

Comments
 (0)