mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-01-09 08:50:34 +00:00
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:
parent
40442f3f82
commit
28109fef38
@ -97,6 +97,7 @@ import icepick.State;
|
|||||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||||
import io.reactivex.rxjava3.core.Observable;
|
import io.reactivex.rxjava3.core.Observable;
|
||||||
import io.reactivex.rxjava3.core.Single;
|
import io.reactivex.rxjava3.core.Single;
|
||||||
|
import io.reactivex.rxjava3.core.SingleTransformer;
|
||||||
import io.reactivex.rxjava3.disposables.CompositeDisposable;
|
import io.reactivex.rxjava3.disposables.CompositeDisposable;
|
||||||
import io.reactivex.rxjava3.disposables.Disposable;
|
import io.reactivex.rxjava3.disposables.Disposable;
|
||||||
import io.reactivex.rxjava3.functions.Consumer;
|
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")
|
@SuppressLint("CheckResult")
|
||||||
private void openDownloadDialog(final int currentServiceId, final String currentUrl) {
|
private void openDownloadDialog(final int currentServiceId, final String currentUrl) {
|
||||||
inFlight(true);
|
inFlight(true);
|
||||||
disposables.add(ExtractorHelper.getStreamInfo(currentServiceId, currentUrl, true)
|
disposables.add(ExtractorHelper.getStreamInfo(currentServiceId, currentUrl, true)
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.compose(pleaseWait())
|
||||||
.subscribe(result ->
|
.subscribe(result ->
|
||||||
runOnVisible(ctx -> {
|
runOnVisible(ctx -> {
|
||||||
final FragmentManager fm = ctx.getSupportFragmentManager();
|
final FragmentManager fm = ctx.getSupportFragmentManager();
|
||||||
@ -812,17 +833,23 @@ public class RouterActivity extends AppCompatActivity {
|
|||||||
disposables.add(ExtractorHelper.getStreamInfo(currentServiceId, currentUrl, false)
|
disposables.add(ExtractorHelper.getStreamInfo(currentServiceId, currentUrl, false)
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.observeOn(AndroidSchedulers.mainThread())
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.compose(pleaseWait())
|
||||||
.subscribe(
|
.subscribe(
|
||||||
info -> runOnVisible(ctx ->
|
info -> {
|
||||||
|
if (!activityGone()) {
|
||||||
PlaylistDialog.createCorrespondingDialog(
|
PlaylistDialog.createCorrespondingDialog(
|
||||||
((RouterActivity) ctx).getThemeWrapperContext(),
|
getActivityContext(),
|
||||||
List.of(new StreamEntity(info)),
|
List.of(new StreamEntity(info)),
|
||||||
playlistDialog -> {
|
playlistDialog ->
|
||||||
// dismiss listener to be handled by FragmentManager
|
runOnVisible(ctx -> {
|
||||||
final FragmentManager fm = ctx.getSupportFragmentManager();
|
// dismiss listener to be handled by FragmentManager
|
||||||
playlistDialog.show(fm, "addToPlaylistDialog");
|
final FragmentManager fm =
|
||||||
}
|
ctx.getSupportFragmentManager();
|
||||||
)),
|
playlistDialog.show(fm, "addToPlaylistDialog");
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
throwable -> runOnVisible(ctx -> handleError(ctx, new ErrorInfo(
|
throwable -> runOnVisible(ctx -> handleError(ctx, new ErrorInfo(
|
||||||
throwable,
|
throwable,
|
||||||
UserAction.REQUESTED_STREAM,
|
UserAction.REQUESTED_STREAM,
|
||||||
@ -835,14 +862,10 @@ public class RouterActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void openAddToPlaylistDialog() {
|
private void openAddToPlaylistDialog() {
|
||||||
pleaseWait();
|
|
||||||
|
|
||||||
getPersistFragment().openAddToPlaylistDialog(currentServiceId, currentUrl);
|
getPersistFragment().openAddToPlaylistDialog(currentServiceId, currentUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void openDownloadDialog() {
|
private void openDownloadDialog() {
|
||||||
pleaseWait();
|
|
||||||
|
|
||||||
getPersistFragment().openDownloadDialog(currentServiceId, currentUrl);
|
getPersistFragment().openDownloadDialog(currentServiceId, currentUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -859,16 +882,6 @@ public class RouterActivity extends AppCompatActivity {
|
|||||||
return persistFragment;
|
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
|
@Override
|
||||||
public void onRequestPermissionsResult(final int requestCode,
|
public void onRequestPermissionsResult(final int requestCode,
|
||||||
@NonNull final String[] permissions,
|
@NonNull final String[] permissions,
|
||||||
|
Loading…
Reference in New Issue
Block a user