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

Commit

Permalink
?
Browse files Browse the repository at this point in the history
Can't remember what this was about ¯\_(ツ)_/¯
  • Loading branch information
aleab committed Sep 11, 2019
1 parent 9c13f0b commit fbebb96
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions Toastify/src/Core/Spotify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using ToastifyAPI.Model.Interfaces;
using ToastifyAPI.Native;
using Settings = Toastify.Model.Settings;
using Timer = System.Threading.Timer;

namespace Toastify.Core
{
Expand All @@ -41,10 +42,15 @@ public static Spotify Instance
#endregion

private readonly string spotifyPath;
private readonly TimeSpan songChangeBuffer = TimeSpan.FromMilliseconds(1500);

private SpotifyWindow spotifyWindow;
private Process spotifyProcess;

private Timer apiTrackDelayedUpdateTimer;
private DateTime lastSongChange = DateTime.Now;
private int songChangesInTimespan;

#region Public Properties

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

if (!(Settings.Current.EnableSpotifyWebApi && this.IsWebApiRunning &&
await this.UpdateTrackInfoUsingWebApi().ConfigureAwait(false)))
bool shouldUpdateUsingWindowTitle = !(Settings.Current.EnableSpotifyWebApi && this.IsWebApiRunning);

this.apiTrackDelayedUpdateTimer?.Dispose();
bool tooFast = this.songChangesInTimespan >= 3;
if (!shouldUpdateUsingWindowTitle && tooFast)
{
logger.Debug($"Songs are being changed too fast ({this.songChangesInTimespan} times in {this.songChangeBuffer.TotalMilliseconds} ms)!");
this.apiTrackDelayedUpdateTimer = new Timer(async state =>
{
logger.Debug($"Executing delayed track update using WebAPI (\"{state}\")");
await this.UpdateTrackInfoUsingWebApi().ConfigureAwait(false);
}, e.NewTitle, this.songChangeBuffer, TimeSpan.FromMilliseconds(-1));
shouldUpdateUsingWindowTitle = true;
}

if (!shouldUpdateUsingWindowTitle)
shouldUpdateUsingWindowTitle = !await this.UpdateTrackInfoUsingWebApi().ConfigureAwait(false);

if (shouldUpdateUsingWindowTitle)
{
// If the WebAPIs are disabled or they weren't able to retrieve the song info, fallback to
// the old method based on the title of Spotify's window.
Expand Down Expand Up @@ -882,6 +905,11 @@ await this.UpdateTrackInfoUsingWebApi().ConfigureAwait(false)))
}
}
}

this.songChangesInTimespan++;
if (DateTime.Now - this.lastSongChange > this.songChangeBuffer)
this.songChangesInTimespan = 0;
this.lastSongChange = DateTime.Now;
}
catch (Exception exception)
{
Expand Down

1 comment on commit fbebb96

@Vankog
Copy link

@Vankog Vankog commented on fbebb96 Oct 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol

Please sign in to comment.