From 7e2ab0d38448e382b92a1ffacea7ae1538104820 Mon Sep 17 00:00:00 2001 From: Rishab Aggarwal Date: Mon, 18 Sep 2023 05:24:03 +0530 Subject: [PATCH] Improved downloading experience (#10407) * added LoadingDialog for improving download experience * [LoadingDialog] Apply some review comments and make title customizable. * removed permission handling from loading Dialog * fix checks * remove

Tag from first sentence --------- Co-authored-by: rishabaggarwal Co-authored-by: TobiGr --- .../org/schabi/newpipe/RouterActivity.java | 16 ++-- .../newpipe/download/LoadingDialog.java | 87 +++++++++++++++++++ .../res/layout/download_loading_dialog.xml | 18 ++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/download/LoadingDialog.java create mode 100644 app/src/main/res/layout/download_loading_dialog.xml diff --git a/app/src/main/java/org/schabi/newpipe/RouterActivity.java b/app/src/main/java/org/schabi/newpipe/RouterActivity.java index e9c19a22d..70c377de9 100644 --- a/app/src/main/java/org/schabi/newpipe/RouterActivity.java +++ b/app/src/main/java/org/schabi/newpipe/RouterActivity.java @@ -45,6 +45,7 @@ import org.schabi.newpipe.database.stream.model.StreamEntity; import org.schabi.newpipe.databinding.ListRadioIconItemBinding; import org.schabi.newpipe.databinding.SingleChoiceDialogViewBinding; import org.schabi.newpipe.download.DownloadDialog; +import org.schabi.newpipe.download.LoadingDialog; import org.schabi.newpipe.error.ErrorInfo; import org.schabi.newpipe.error.ErrorUtil; import org.schabi.newpipe.error.ReCaptchaActivity; @@ -789,10 +790,10 @@ public class RouterActivity extends AppCompatActivity { } } - }, () -> { + }, () -> // this branch is executed if there is no activity context - inFlight(false); - }); + inFlight(false) + ); } Single pleaseWait(final Single single) { @@ -812,19 +813,24 @@ public class RouterActivity extends AppCompatActivity { @SuppressLint("CheckResult") private void openDownloadDialog(final int currentServiceId, final String currentUrl) { inFlight(true); + final LoadingDialog loadingDialog = new LoadingDialog(R.string.loading_metadata_title); + loadingDialog.show(getParentFragmentManager(), "loadingDialog"); disposables.add(ExtractorHelper.getStreamInfo(currentServiceId, currentUrl, true) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .compose(this::pleaseWait) .subscribe(result -> runOnVisible(ctx -> { + loadingDialog.dismiss(); final FragmentManager fm = ctx.getSupportFragmentManager(); final DownloadDialog downloadDialog = new DownloadDialog(ctx, result); // dismiss listener to be handled by FragmentManager downloadDialog.show(fm, "downloadDialog"); } - ), throwable -> runOnVisible(ctx -> - ((RouterActivity) ctx).showUnsupportedUrlDialog(currentUrl)))); + ), throwable -> runOnVisible(ctx -> { + loadingDialog.dismiss(); + ((RouterActivity) ctx).showUnsupportedUrlDialog(currentUrl); + }))); } private void openAddToPlaylistDialog(final int currentServiceId, final String currentUrl) { diff --git a/app/src/main/java/org/schabi/newpipe/download/LoadingDialog.java b/app/src/main/java/org/schabi/newpipe/download/LoadingDialog.java new file mode 100644 index 000000000..9e6861908 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/download/LoadingDialog.java @@ -0,0 +1,87 @@ +package org.schabi.newpipe.download; + +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.StringRes; +import androidx.appcompat.widget.Toolbar; +import androidx.fragment.app.DialogFragment; + +import org.schabi.newpipe.MainActivity; +import org.schabi.newpipe.R; +import org.schabi.newpipe.databinding.DownloadLoadingDialogBinding; + +/** + * This class contains a dialog which shows a loading indicator and has a customizable title. + */ +public class LoadingDialog extends DialogFragment { + private static final String TAG = "LoadingDialog"; + private static final boolean DEBUG = MainActivity.DEBUG; + private DownloadLoadingDialogBinding dialogLoadingBinding; + private final @StringRes int title; + + /** + * Create a new LoadingDialog. + * + *

+ * The dialog contains a loading indicator and has a customizable title. + *
+ * Use {@code show()} to display the dialog to the user. + *

+ * + * @param title an informative title shown in the dialog's toolbar + */ + public LoadingDialog(final @StringRes int title) { + this.title = title; + } + + @Override + public void onCreate(@Nullable final Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (DEBUG) { + Log.d(TAG, "onCreate() called with: " + + "savedInstanceState = [" + savedInstanceState + "]"); + } + this.setCancelable(false); + } + + @Override + public View onCreateView( + @NonNull final LayoutInflater inflater, + final ViewGroup container, + final Bundle savedInstanceState) { + if (DEBUG) { + Log.d(TAG, "onCreateView() called with: " + + "inflater = [" + inflater + "], container = [" + container + "], " + + "savedInstanceState = [" + savedInstanceState + "]"); + } + return inflater.inflate(R.layout.download_loading_dialog, container); + } + + @Override + public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + dialogLoadingBinding = DownloadLoadingDialogBinding.bind(view); + initToolbar(dialogLoadingBinding.toolbarLayout.toolbar); + } + + private void initToolbar(final Toolbar toolbar) { + if (DEBUG) { + Log.d(TAG, "initToolbar() called with: toolbar = [" + toolbar + "]"); + } + toolbar.setTitle(requireContext().getString(title)); + toolbar.setNavigationOnClickListener(v -> dismiss()); + + } + + @Override + public void onDestroyView() { + dialogLoadingBinding = null; + super.onDestroyView(); + } +} diff --git a/app/src/main/res/layout/download_loading_dialog.xml b/app/src/main/res/layout/download_loading_dialog.xml new file mode 100644 index 000000000..3a7869f3f --- /dev/null +++ b/app/src/main/res/layout/download_loading_dialog.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5cf214c0d..a37bfeb82 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -167,6 +167,7 @@ Live Downloads Downloads + Loading Metadata… Error report All Channels