Skip to content

Commit 28109fe

Browse files
devlearnerStypox
authored andcommitted
Improve showing of toast
We provide visual feedback via a toast to the user that, well, they're supposed to wait; but with the benefit of the cache openAddToPlaylistDialog() may return (almost) immediately, which would render the toast otiose (if not a bit confusing). This commit improves that by cancelling the toast once the wait's over ... (by 'abusing' RxJava's ambWith(); ref on compose() and Transformer: https://blog.danlew.net/2015/03/02/dont-break-the-chain/ and for me, first time laying my hands at RxJava so kindly bear with me; open for suggestions)
1 parent 40442f3 commit 28109fe

1 file changed

Lines changed: 35 additions & 22 deletions

File tree

app/src/main/java/org/schabi/newpipe/RouterActivity.java

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
9898
import io.reactivex.rxjava3.core.Observable;
9999
import io.reactivex.rxjava3.core.Single;
100+
import io.reactivex.rxjava3.core.SingleTransformer;
100101
import io.reactivex.rxjava3.disposables.CompositeDisposable;
101102
import io.reactivex.rxjava3.disposables.Disposable;
102103
import 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

Comments
 (0)