Skip to content

Commit 7e2ab0d

Browse files
rishab247rishabaggarwalTobiGr
authored
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 <p> Tag from first sentence --------- Co-authored-by: rishabaggarwal <Rishabaggarwal@sharechat.com> Co-authored-by: TobiGr <tobigr@users.noreply.github.com>
1 parent b1ab261 commit 7e2ab0d

4 files changed

Lines changed: 117 additions & 5 deletions

File tree

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.schabi.newpipe.databinding.ListRadioIconItemBinding;
4646
import org.schabi.newpipe.databinding.SingleChoiceDialogViewBinding;
4747
import org.schabi.newpipe.download.DownloadDialog;
48+
import org.schabi.newpipe.download.LoadingDialog;
4849
import org.schabi.newpipe.error.ErrorInfo;
4950
import org.schabi.newpipe.error.ErrorUtil;
5051
import org.schabi.newpipe.error.ReCaptchaActivity;
@@ -789,10 +790,10 @@ public void onResume(@NonNull final LifecycleOwner owner) {
789790
}
790791
}
791792

792-
}, () -> {
793+
}, () ->
793794
// this branch is executed if there is no activity context
794-
inFlight(false);
795-
});
795+
inFlight(false)
796+
);
796797
}
797798

798799
<T> Single<T> pleaseWait(final Single<T> single) {
@@ -812,19 +813,24 @@ <T> Single<T> pleaseWait(final Single<T> single) {
812813
@SuppressLint("CheckResult")
813814
private void openDownloadDialog(final int currentServiceId, final String currentUrl) {
814815
inFlight(true);
816+
final LoadingDialog loadingDialog = new LoadingDialog(R.string.loading_metadata_title);
817+
loadingDialog.show(getParentFragmentManager(), "loadingDialog");
815818
disposables.add(ExtractorHelper.getStreamInfo(currentServiceId, currentUrl, true)
816819
.subscribeOn(Schedulers.io())
817820
.observeOn(AndroidSchedulers.mainThread())
818821
.compose(this::pleaseWait)
819822
.subscribe(result ->
820823
runOnVisible(ctx -> {
824+
loadingDialog.dismiss();
821825
final FragmentManager fm = ctx.getSupportFragmentManager();
822826
final DownloadDialog downloadDialog = new DownloadDialog(ctx, result);
823827
// dismiss listener to be handled by FragmentManager
824828
downloadDialog.show(fm, "downloadDialog");
825829
}
826-
), throwable -> runOnVisible(ctx ->
827-
((RouterActivity) ctx).showUnsupportedUrlDialog(currentUrl))));
830+
), throwable -> runOnVisible(ctx -> {
831+
loadingDialog.dismiss();
832+
((RouterActivity) ctx).showUnsupportedUrlDialog(currentUrl);
833+
})));
828834
}
829835

830836
private void openAddToPlaylistDialog(final int currentServiceId, final String currentUrl) {
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.schabi.newpipe.download;
2+
3+
import android.os.Bundle;
4+
import android.util.Log;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
9+
import androidx.annotation.NonNull;
10+
import androidx.annotation.Nullable;
11+
import androidx.annotation.StringRes;
12+
import androidx.appcompat.widget.Toolbar;
13+
import androidx.fragment.app.DialogFragment;
14+
15+
import org.schabi.newpipe.MainActivity;
16+
import org.schabi.newpipe.R;
17+
import org.schabi.newpipe.databinding.DownloadLoadingDialogBinding;
18+
19+
/**
20+
* This class contains a dialog which shows a loading indicator and has a customizable title.
21+
*/
22+
public class LoadingDialog extends DialogFragment {
23+
private static final String TAG = "LoadingDialog";
24+
private static final boolean DEBUG = MainActivity.DEBUG;
25+
private DownloadLoadingDialogBinding dialogLoadingBinding;
26+
private final @StringRes int title;
27+
28+
/**
29+
* Create a new LoadingDialog.
30+
*
31+
* <p>
32+
* The dialog contains a loading indicator and has a customizable title.
33+
* <br/>
34+
* Use {@code show()} to display the dialog to the user.
35+
* </p>
36+
*
37+
* @param title an informative title shown in the dialog's toolbar
38+
*/
39+
public LoadingDialog(final @StringRes int title) {
40+
this.title = title;
41+
}
42+
43+
@Override
44+
public void onCreate(@Nullable final Bundle savedInstanceState) {
45+
super.onCreate(savedInstanceState);
46+
if (DEBUG) {
47+
Log.d(TAG, "onCreate() called with: "
48+
+ "savedInstanceState = [" + savedInstanceState + "]");
49+
}
50+
this.setCancelable(false);
51+
}
52+
53+
@Override
54+
public View onCreateView(
55+
@NonNull final LayoutInflater inflater,
56+
final ViewGroup container,
57+
final Bundle savedInstanceState) {
58+
if (DEBUG) {
59+
Log.d(TAG, "onCreateView() called with: "
60+
+ "inflater = [" + inflater + "], container = [" + container + "], "
61+
+ "savedInstanceState = [" + savedInstanceState + "]");
62+
}
63+
return inflater.inflate(R.layout.download_loading_dialog, container);
64+
}
65+
66+
@Override
67+
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
68+
super.onViewCreated(view, savedInstanceState);
69+
dialogLoadingBinding = DownloadLoadingDialogBinding.bind(view);
70+
initToolbar(dialogLoadingBinding.toolbarLayout.toolbar);
71+
}
72+
73+
private void initToolbar(final Toolbar toolbar) {
74+
if (DEBUG) {
75+
Log.d(TAG, "initToolbar() called with: toolbar = [" + toolbar + "]");
76+
}
77+
toolbar.setTitle(requireContext().getString(title));
78+
toolbar.setNavigationOnClickListener(v -> dismiss());
79+
80+
}
81+
82+
@Override
83+
public void onDestroyView() {
84+
dialogLoadingBinding = null;
85+
super.onDestroyView();
86+
}
87+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<include
7+
android:id="@+id/toolbar_layout"
8+
layout="@layout/toolbar_layout" />
9+
10+
<ProgressBar
11+
android:id="@+id/loader"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:layout_marginTop="150dp"
15+
android:layout_marginBottom="100dp"
16+
android:layout_centerHorizontal="true"
17+
android:indeterminate="true" />
18+
</RelativeLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
<string name="duration_live">Live</string>
168168
<string name="downloads">Downloads</string>
169169
<string name="downloads_title">Downloads</string>
170+
<string name="loading_metadata_title">Loading Metadata…</string>
170171
<string name="error_report_title">Error report</string>
171172
<string name="all">All</string>
172173
<string name="channels">Channels</string>

0 commit comments

Comments
 (0)