private async void SpotifyWindowTitleWatcher_TitleChanged(object sender, WindowTitleChangedEventArgs e) { try { bool updateSong = true; if (SpotifyWindow.PausedTitles.Contains(e.NewTitle, StringComparer.InvariantCulture)) { this.SpotifyLocalAPI_OnPlayStateChange(this, new PlayStateEventArgs { Playing = false }); updateSong = false; } else if (SpotifyWindow.PausedTitles.Contains(e.OldTitle, StringComparer.InvariantCulture)) { this.SpotifyLocalAPI_OnPlayStateChange(this, new PlayStateEventArgs { Playing = true }); } if (updateSong) { Song newSong = Song.FromSpotifyWindowTitle(e.NewTitle); if (newSong != null && !Song.Equal(this.CurrentSong, newSong)) { Song oldSong = this.CurrentSong; this.CurrentSong = newSong; await this.OnSongChanged(oldSong).ConfigureAwait(false); } } } catch (Exception exception) { logger.Error($"Unhandled exception in {nameof(this.SpotifyWindowTitleWatcher_TitleChanged)}.", exception); } }
private async void SpotifyWindowTitleWatcher_TitleChanged(object sender, WindowTitleChangedEventArgs e) { // TODO: Refactor this method try { 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))) { // 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. if (logger.IsDebugEnabled) { logger.Debug("Fetching song info using old method based on the title of Spotify's window..."); } bool updateSong = true; if (SpotifyWindow.PausedTitles.Contains(e.NewTitle, StringComparer.InvariantCulture)) { await this.OnPlayStateChanged(false).ConfigureAwait(false); updateSong = false; } else if (!this.IsPlaying) { await this.OnPlayStateChanged(true).ConfigureAwait(false); } if (updateSong) { ISpotifyTrack newSong = Song.FromSpotifyWindowTitle(e.NewTitle); if (!SpotifyTrack.Equal(this.CurrentTrack, newSong)) { ISpotifyTrack oldSong = this.CurrentTrack; this.CurrentTrack = newSong; await this.OnTrackChanged(oldSong).ConfigureAwait(false); } } } } catch (Exception exception) { logger.Error($"Unhandled exception in {nameof(this.SpotifyWindowTitleWatcher_TitleChanged)}.", exception); } }
private async void SpotifyWindowTitleWatcher_TitleChanged(object sender, WindowTitleChangedEventArgs e) { // TODO: Refactor this method try { if (logger.IsDebugEnabled) { logger.Debug($"Spotify's window title changed: \"{e.NewTitle}\". Fetching song info..."); } 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. if (logger.IsDebugEnabled) { logger.Debug("Fetching song info using old method based on the title of Spotify's window..."); } bool updateSong = true; if (SpotifyWindow.PausedTitles.Contains(e.NewTitle, StringComparer.InvariantCulture)) { await this.OnPlayStateChanged(false).ConfigureAwait(false); updateSong = false; } else if (!this.IsPlaying) { await this.OnPlayStateChanged(true).ConfigureAwait(false); } if (updateSong) { ISpotifyTrack newSong = Song.FromSpotifyWindowTitle(e.NewTitle); if (!SpotifyTrack.Equal(this.CurrentTrack, newSong)) { ISpotifyTrack oldSong = this.CurrentTrack; this.CurrentTrack = newSong; await this.OnTrackChanged(oldSong).ConfigureAwait(false); } } } this.songChangesInTimespan++; if (DateTime.Now - this.lastSongChange > this.songChangeBuffer) { this.songChangesInTimespan = 0; } this.lastSongChange = DateTime.Now; } catch (Exception exception) { logger.Error($"Unhandled exception in {nameof(this.SpotifyWindowTitleWatcher_TitleChanged)}.", exception); } }