@@ -513,6 +513,28 @@ private void ChangeProxySettings()
513
513
514
514
private CancellationTokenSource webApiInitCancellationTokenSource ;
515
515
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
+
516
538
private void BeginInitializeWebAPI ( )
517
539
{
518
540
if ( this . TokenManager == null || this . Web == null )
@@ -662,17 +684,21 @@ private async void OnWebAPIInitializationSucceeded()
662
684
this . IsPlaying = currentlyPlayingObject . IsPlaying ;
663
685
}
664
686
687
+ this . IsWebApiRunning = true ;
665
688
this . WebAPIInitializationSucceeded ? . Invoke ( this , new SpotifyStateEventArgs ( this . CurrentSong , this . IsPlaying , this . CurrentSong ? . Length ?? 0.0 , 1.0 ) ) ;
666
689
}
667
690
668
691
private void OnWebAPIInitializationFailed ( SpotifyWebAPIInitializationFailedReason reason )
669
692
{
670
693
logger . Debug ( $ "Spotify WebAPI initialization failed with reason: { reason } ") ;
694
+ this . IsWebApiRunning = false ;
671
695
this . WebAPIInitializationFailed ? . Invoke ( this , new SpotifyWebAPIInitializationFailedEventArgs ( reason ) ) ;
672
696
}
673
697
674
698
private void OnWebApiDisabled ( )
675
699
{
700
+ logger . Debug ( "Spotify WebAPI disabled" ) ;
701
+ this . IsWebApiRunning = false ;
676
702
this . WebApiDisabled ? . Invoke ( this , EventArgs . Empty ) ;
677
703
}
678
704
@@ -745,17 +771,9 @@ private async void Settings_CurrentSettingsChanged(object sender, CurrentSetting
745
771
if ( e . PreviousSettings ? . EnableSpotifyWebApi != e . CurrentSettings ? . EnableSpotifyWebApi )
746
772
{
747
773
if ( e . CurrentSettings ? . EnableSpotifyWebApi == true )
748
- {
749
- this . DisposeWebApiInitializer ( ) ;
750
- this . webApiInitCancellationTokenSource = new CancellationTokenSource ( ) ;
751
- this . BeginInitializeWebAPI ( ) ;
752
- }
774
+ this . EnableWebApi ( ) ;
753
775
else
754
- {
755
- this . DisposeWebApiInitializer ( ) ;
756
- this . TokenManager . ReleaseToken ( ) ;
757
- this . OnWebApiDisabled ( ) ;
758
- }
776
+ this . DisableWebApi ( ) ;
759
777
}
760
778
}
761
779
catch ( Exception exception )
@@ -834,8 +852,8 @@ private async void SpotifyWindowTitleWatcher_TitleChanged(object sender, WindowT
834
852
if ( logger . IsDebugEnabled )
835
853
logger . Debug ( $ "Spotify's window title changed: \" { e . NewTitle } \" . Fetching song info...") ;
836
854
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 ) ) )
839
857
{
840
858
// If the WebAPIs are disabled or they weren't able to retrieve the song info, fallback to
841
859
// the old method based on the title of Spotify's window.
0 commit comments