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)
This commit is contained in:
devlearner 2022-12-11 18:10:00 +00:00 committed by Stypox
parent 40442f3f82
commit 28109fef38
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23

View file

@ -97,6 +97,7 @@ import icepick.State;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleTransformer;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
@ -790,12 +791,32 @@ public class RouterActivity extends AppCompatActivity {
}
}
<T> SingleTransformer<T, T> pleaseWait() {
return single -> single
// 'abuse' ambWith() here to cancel the toast for us when the wait is over
.ambWith(Single.create(emitter -> {
if (!activityGone()) {
getActivityContext().runOnUiThread(() -> {
// Getting the stream info usually takes a moment
// Notifying the user here to ensure that no confusion arises
final Toast t = Toast.makeText(
getActivityContext().getApplicationContext(),
getString(R.string.processing_may_take_a_moment),
Toast.LENGTH_LONG);
t.show();
emitter.setCancellable(t::cancel);
});
}
}));
}
@SuppressLint("CheckResult")
private void openDownloadDialog(final int currentServiceId, final String currentUrl) {
inFlight(true);
disposables.add(ExtractorHelper.getStreamInfo(currentServiceId, currentUrl, true)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(pleaseWait())
.subscribe(result ->
runOnVisible(ctx -> {
final FragmentManager fm = ctx.getSupportFragmentManager();
@ -812,17 +833,23 @@ public class RouterActivity extends AppCompatActivity {
disposables.add(ExtractorHelper.getStreamInfo(currentServiceId, currentUrl, false)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.compose(pleaseWait())
.subscribe(
info -> runOnVisible(ctx ->
info -> {
if (!activityGone()) {
PlaylistDialog.createCorrespondingDialog(
((RouterActivity) ctx).getThemeWrapperContext(),
getActivityContext(),
List.of(new StreamEntity(info)),
playlistDialog -> {
// dismiss listener to be handled by FragmentManager
final FragmentManager fm = ctx.getSupportFragmentManager();
playlistDialog.show(fm, "addToPlaylistDialog");
}
)),
playlistDialog ->
runOnVisible(ctx -> {
// dismiss listener to be handled by FragmentManager
final FragmentManager fm =
ctx.getSupportFragmentManager();
playlistDialog.show(fm, "addToPlaylistDialog");
})
);
}
},
throwable -> runOnVisible(ctx -> handleError(ctx, new ErrorInfo(
throwable,
UserAction.REQUESTED_STREAM,
@ -835,14 +862,10 @@ public class RouterActivity extends AppCompatActivity {
}
private void openAddToPlaylistDialog() {
pleaseWait();
getPersistFragment().openAddToPlaylistDialog(currentServiceId, currentUrl);
}
private void openDownloadDialog() {
pleaseWait();
getPersistFragment().openDownloadDialog(currentServiceId, currentUrl);
}
@ -859,16 +882,6 @@ public class RouterActivity extends AppCompatActivity {
return persistFragment;
}
private void pleaseWait() {
// Getting the stream info usually takes a moment
// Notifying the user here to ensure that no confusion arises
Toast.makeText(
getApplicationContext(),
getString(R.string.processing_may_take_a_moment),
Toast.LENGTH_LONG)
.show();
}
@Override
public void onRequestPermissionsResult(final int requestCode,
@NonNull final String[] permissions,