9797import io .reactivex .rxjava3 .android .schedulers .AndroidSchedulers ;
9898import io .reactivex .rxjava3 .core .Observable ;
9999import io .reactivex .rxjava3 .core .Single ;
100+ import io .reactivex .rxjava3 .core .SingleTransformer ;
100101import io .reactivex .rxjava3 .disposables .CompositeDisposable ;
101102import io .reactivex .rxjava3 .disposables .Disposable ;
102103import io .reactivex .rxjava3 .functions .Consumer ;
@@ -790,12 +791,32 @@ public void onResume(@NonNull final LifecycleOwner owner) {
790791 }
791792 }
792793
794+ <T > SingleTransformer <T , T > pleaseWait () {
795+ return single -> single
796+ // 'abuse' ambWith() here to cancel the toast for us when the wait is over
797+ .ambWith (Single .create (emitter -> {
798+ if (!activityGone ()) {
799+ getActivityContext ().runOnUiThread (() -> {
800+ // Getting the stream info usually takes a moment
801+ // Notifying the user here to ensure that no confusion arises
802+ final Toast t = Toast .makeText (
803+ getActivityContext ().getApplicationContext (),
804+ getString (R .string .processing_may_take_a_moment ),
805+ Toast .LENGTH_LONG );
806+ t .show ();
807+ emitter .setCancellable (t ::cancel );
808+ });
809+ }
810+ }));
811+ }
812+
793813 @ SuppressLint ("CheckResult" )
794814 private void openDownloadDialog (final int currentServiceId , final String currentUrl ) {
795815 inFlight (true );
796816 disposables .add (ExtractorHelper .getStreamInfo (currentServiceId , currentUrl , true )
797817 .subscribeOn (Schedulers .io ())
798818 .observeOn (AndroidSchedulers .mainThread ())
819+ .compose (pleaseWait ())
799820 .subscribe (result ->
800821 runOnVisible (ctx -> {
801822 final FragmentManager fm = ctx .getSupportFragmentManager ();
@@ -812,17 +833,23 @@ private void openAddToPlaylistDialog(final int currentServiceId, final String cu
812833 disposables .add (ExtractorHelper .getStreamInfo (currentServiceId , currentUrl , false )
813834 .subscribeOn (Schedulers .io ())
814835 .observeOn (AndroidSchedulers .mainThread ())
836+ .compose (pleaseWait ())
815837 .subscribe (
816- info -> runOnVisible (ctx ->
838+ info -> {
839+ if (!activityGone ()) {
817840 PlaylistDialog .createCorrespondingDialog (
818- (( RouterActivity ) ctx ). getThemeWrapperContext (),
841+ getActivityContext (),
819842 List .of (new StreamEntity (info )),
820- playlistDialog -> {
821- // dismiss listener to be handled by FragmentManager
822- final FragmentManager fm = ctx .getSupportFragmentManager ();
823- playlistDialog .show (fm , "addToPlaylistDialog" );
824- }
825- )),
843+ playlistDialog ->
844+ runOnVisible (ctx -> {
845+ // dismiss listener to be handled by FragmentManager
846+ final FragmentManager fm =
847+ ctx .getSupportFragmentManager ();
848+ playlistDialog .show (fm , "addToPlaylistDialog" );
849+ })
850+ );
851+ }
852+ },
826853 throwable -> runOnVisible (ctx -> handleError (ctx , new ErrorInfo (
827854 throwable ,
828855 UserAction .REQUESTED_STREAM ,
@@ -835,14 +862,10 @@ private void openAddToPlaylistDialog(final int currentServiceId, final String cu
835862 }
836863
837864 private void openAddToPlaylistDialog () {
838- pleaseWait ();
839-
840865 getPersistFragment ().openAddToPlaylistDialog (currentServiceId , currentUrl );
841866 }
842867
843868 private void openDownloadDialog () {
844- pleaseWait ();
845-
846869 getPersistFragment ().openDownloadDialog (currentServiceId , currentUrl );
847870 }
848871
@@ -859,16 +882,6 @@ private PersistentFragment getPersistFragment() {
859882 return persistFragment ;
860883 }
861884
862- private void pleaseWait () {
863- // Getting the stream info usually takes a moment
864- // Notifying the user here to ensure that no confusion arises
865- Toast .makeText (
866- getApplicationContext (),
867- getString (R .string .processing_may_take_a_moment ),
868- Toast .LENGTH_LONG )
869- .show ();
870- }
871-
872885 @ Override
873886 public void onRequestPermissionsResult (final int requestCode ,
874887 @ NonNull final String [] permissions ,
0 commit comments