Skip to content

Commit 9697112

Browse files
committed
Show error panel in EmptyFragment
1 parent fe58ec8 commit 9697112

5 files changed

Lines changed: 59 additions & 14 deletions

File tree

app/src/main/java/org/schabi/newpipe/error/ErrorPanelHelper.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import java.util.concurrent.TimeUnit
3535
class ErrorPanelHelper(
3636
private val fragment: Fragment,
3737
rootView: View,
38-
onRetry: Runnable
38+
onRetry: Runnable?,
3939
) {
4040
private val context: Context = rootView.context!!
4141

@@ -56,12 +56,15 @@ class ErrorPanelHelper(
5656
errorPanelRoot.findViewById(R.id.error_open_in_browser)
5757

5858
private var errorDisposable: Disposable? = null
59+
private var retryShouldBeShown: Boolean = (onRetry != null)
5960

6061
init {
61-
errorDisposable = errorRetryButton.clicks()
62-
.debounce(300, TimeUnit.MILLISECONDS)
63-
.observeOn(AndroidSchedulers.mainThread())
64-
.subscribe { onRetry.run() }
62+
if (onRetry != null) {
63+
errorDisposable = errorRetryButton.clicks()
64+
.debounce(300, TimeUnit.MILLISECONDS)
65+
.observeOn(AndroidSchedulers.mainThread())
66+
.subscribe { onRetry.run() }
67+
}
6568
}
6669

6770
private fun ensureDefaultVisibility() {
@@ -101,7 +104,7 @@ class ErrorPanelHelper(
101104
errorActionButton.setOnClickListener(null)
102105
}
103106

104-
errorRetryButton.isVisible = true
107+
errorRetryButton.isVisible = retryShouldBeShown
105108
showAndSetOpenInBrowserButtonAction(errorInfo)
106109
} else if (errorInfo.throwable is AccountTerminatedException) {
107110
errorTextView.setText(R.string.account_terminated)
@@ -130,7 +133,7 @@ class ErrorPanelHelper(
130133
errorInfo.throwable !is ContentNotSupportedException
131134
) {
132135
// show retry button only for content which is not unavailable or unsupported
133-
errorRetryButton.isVisible = true
136+
errorRetryButton.isVisible = retryShouldBeShown
134137
}
135138
showAndSetOpenInBrowserButtonAction(errorInfo)
136139
}

app/src/main/java/org/schabi/newpipe/error/UserAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public enum UserAction {
3232
PREFERENCES_MIGRATION("migration of preferences"),
3333
SHARE_TO_NEWPIPE("share to newpipe"),
3434
CHECK_FOR_NEW_APP_VERSION("check for new app version"),
35-
OPEN_INFO_ITEM_DIALOG("open info item dialog");
35+
OPEN_INFO_ITEM_DIALOG("open info item dialog"),
36+
GETTING_MAIN_SCREEN_TAB("getting main screen tab");
3637

3738
private final String message;
3839

app/src/main/java/org/schabi/newpipe/fragments/BlankFragment.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,52 @@
99

1010
import org.schabi.newpipe.BaseFragment;
1111
import org.schabi.newpipe.R;
12+
import org.schabi.newpipe.error.ErrorInfo;
13+
import org.schabi.newpipe.error.ErrorPanelHelper;
1214

1315
public class BlankFragment extends BaseFragment {
16+
17+
@Nullable
18+
final ErrorInfo errorInfo;
19+
@Nullable
20+
ErrorPanelHelper errorPanel = null;
21+
22+
/**
23+
* Builds a blank fragment that just says the app name and suggests clicking on search.
24+
*/
25+
public BlankFragment() {
26+
this(null);
27+
}
28+
29+
/**
30+
* @param errorInfo if null acts like {@link BlankFragment}, else shows an error panel.
31+
*/
32+
public BlankFragment(@Nullable final ErrorInfo errorInfo) {
33+
this.errorInfo = errorInfo;
34+
}
35+
1436
@Nullable
1537
@Override
1638
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container,
1739
final Bundle savedInstanceState) {
1840
setTitle("NewPipe");
19-
return inflater.inflate(R.layout.fragment_blank, container, false);
41+
final View view = inflater.inflate(R.layout.fragment_blank, container, false);
42+
if (errorInfo != null) {
43+
errorPanel = new ErrorPanelHelper(this, view, null);
44+
errorPanel.showError(errorInfo);
45+
view.findViewById(R.id.blank_page_content).setVisibility(View.GONE);
46+
}
47+
return view;
48+
}
49+
50+
@Override
51+
public void onDestroyView() {
52+
super.onDestroyView();
53+
54+
if (errorPanel != null) {
55+
errorPanel.dispose();
56+
errorPanel = null;
57+
}
2058
}
2159

2260
@Override

app/src/main/java/org/schabi/newpipe/fragments/MainFragment.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
import org.schabi.newpipe.BaseFragment;
3737
import org.schabi.newpipe.R;
3838
import org.schabi.newpipe.databinding.FragmentMainBinding;
39+
import org.schabi.newpipe.error.ErrorInfo;
3940
import org.schabi.newpipe.error.ErrorUtil;
41+
import org.schabi.newpipe.error.UserAction;
4042
import org.schabi.newpipe.local.playlist.LocalPlaylistFragment;
4143
import org.schabi.newpipe.settings.tabs.Tab;
4244
import org.schabi.newpipe.settings.tabs.TabsManager;
@@ -302,10 +304,9 @@ public Fragment getItem(final int position) {
302304
final Fragment fragment;
303305
try {
304306
fragment = tab.getFragment(context);
305-
} catch (final Exception e) {
306-
ErrorUtil.showUiErrorSnackbar(context, "Getting fragment item", e);
307-
// TODO: show an error fragment instead
308-
return new BlankFragment();
307+
} catch (final Throwable t) {
308+
return new BlankFragment(new ErrorInfo(t, UserAction.GETTING_MAIN_SCREEN_TAB,
309+
"Tab " + tab.getClass().getSimpleName() + ":" + tab.getTabName(context)));
309310
}
310311

311312
if (fragment instanceof BaseFragment) {

app/src/main/res/layout/fragment_blank.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
android:layout_width="match_parent"
55
android:layout_height="match_parent">
66

7-
<include layout="@layout/main_bg" />
7+
<include
8+
android:id="@+id/blank_page_content"
9+
layout="@layout/main_bg" />
810

911
<include
1012
android:id="@+id/error_panel"

0 commit comments

Comments
 (0)