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

Commit 09b7c42

Browse files
committed
Refactor enable/disable WebAPI
1 parent 0317dcb commit 09b7c42

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

Toastify/src/Core/Spotify.cs

+30-12
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,28 @@ private void ChangeProxySettings()
513513

514514
private CancellationTokenSource webApiInitCancellationTokenSource;
515515

516+
public bool IsWebApiRunning { get; private set; }
517+
518+
public void EnableWebApi()
519+
{
520+
if (!this.IsWebApiRunning)
521+
{
522+
this.DisposeWebApiInitializer();
523+
this.webApiInitCancellationTokenSource = new CancellationTokenSource();
524+
this.BeginInitializeWebAPI();
525+
}
526+
}
527+
528+
public void DisableWebApi()
529+
{
530+
if (this.IsWebApiRunning)
531+
{
532+
this.DisposeWebApiInitializer();
533+
this.TokenManager.ReleaseToken();
534+
this.OnWebApiDisabled();
535+
}
536+
}
537+
516538
private void BeginInitializeWebAPI()
517539
{
518540
if (this.TokenManager == null || this.Web == null)
@@ -662,17 +684,21 @@ private async void OnWebAPIInitializationSucceeded()
662684
this.IsPlaying = currentlyPlayingObject.IsPlaying;
663685
}
664686

687+
this.IsWebApiRunning = true;
665688
this.WebAPIInitializationSucceeded?.Invoke(this, new SpotifyStateEventArgs(this.CurrentSong, this.IsPlaying, this.CurrentSong?.Length ?? 0.0, 1.0));
666689
}
667690

668691
private void OnWebAPIInitializationFailed(SpotifyWebAPIInitializationFailedReason reason)
669692
{
670693
logger.Debug($"Spotify WebAPI initialization failed with reason: {reason}");
694+
this.IsWebApiRunning = false;
671695
this.WebAPIInitializationFailed?.Invoke(this, new SpotifyWebAPIInitializationFailedEventArgs(reason));
672696
}
673697

674698
private void OnWebApiDisabled()
675699
{
700+
logger.Debug("Spotify WebAPI disabled");
701+
this.IsWebApiRunning = false;
676702
this.WebApiDisabled?.Invoke(this, EventArgs.Empty);
677703
}
678704

@@ -745,17 +771,9 @@ private async void Settings_CurrentSettingsChanged(object sender, CurrentSetting
745771
if (e.PreviousSettings?.EnableSpotifyWebApi != e.CurrentSettings?.EnableSpotifyWebApi)
746772
{
747773
if (e.CurrentSettings?.EnableSpotifyWebApi == true)
748-
{
749-
this.DisposeWebApiInitializer();
750-
this.webApiInitCancellationTokenSource = new CancellationTokenSource();
751-
this.BeginInitializeWebAPI();
752-
}
774+
this.EnableWebApi();
753775
else
754-
{
755-
this.DisposeWebApiInitializer();
756-
this.TokenManager.ReleaseToken();
757-
this.OnWebApiDisabled();
758-
}
776+
this.DisableWebApi();
759777
}
760778
}
761779
catch (Exception exception)
@@ -834,8 +852,8 @@ private async void SpotifyWindowTitleWatcher_TitleChanged(object sender, WindowT
834852
if (logger.IsDebugEnabled)
835853
logger.Debug($"Spotify's window title changed: \"{e.NewTitle}\". Fetching song info...");
836854

837-
if (!(Settings.Current.EnableSpotifyWebApi && this.TokenManager?.Token != null &&
838-
!this.TokenManager.Token.IsExpired() && await this.UpdateSongInfoUsingWebApi().ConfigureAwait(false)))
855+
if (!(Settings.Current.EnableSpotifyWebApi && this.IsWebApiRunning &&
856+
await this.UpdateSongInfoUsingWebApi().ConfigureAwait(false)))
839857
{
840858
// If the WebAPIs are disabled or they weren't able to retrieve the song info, fallback to
841859
// the old method based on the title of Spotify's window.

0 commit comments

Comments
 (0)