Skip to content

Commit faa7a91

Browse files
authored
Merge pull request #7004 from litetex/fix-showTextError-and-rework-ErrorPanel
Reworked the ErrorPanel
2 parents f629a4d + d931d05 commit faa7a91

2 files changed

Lines changed: 88 additions & 52 deletions

File tree

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

Lines changed: 72 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import android.util.Log
66
import android.view.View
77
import android.widget.Button
88
import android.widget.TextView
9+
import androidx.annotation.Nullable
10+
import androidx.annotation.StringRes
911
import androidx.core.view.isVisible
1012
import androidx.fragment.app.Fragment
1113
import com.jakewharton.rxbinding4.view.clicks
@@ -37,22 +39,39 @@ class ErrorPanelHelper(
3739
onRetry: Runnable
3840
) {
3941
private val context: Context = rootView.context!!
42+
4043
private val errorPanelRoot: View = rootView.findViewById(R.id.error_panel)
41-
private val errorTextView: TextView = errorPanelRoot.findViewById(R.id.error_message_view)
42-
private val errorServiceInfoTextView: TextView = errorPanelRoot.findViewById(R.id.error_message_service_info_view)
43-
private val errorServiceExplenationTextView: TextView = errorPanelRoot.findViewById(R.id.error_message_service_explenation_view)
44-
private val errorButtonAction: Button = errorPanelRoot.findViewById(R.id.error_button_action)
45-
private val errorButtonRetry: Button = errorPanelRoot.findViewById(R.id.error_button_retry)
44+
45+
// the only element that is visible by default
46+
private val errorTextView: TextView =
47+
errorPanelRoot.findViewById(R.id.error_message_view)
48+
private val errorServiceInfoTextView: TextView =
49+
errorPanelRoot.findViewById(R.id.error_message_service_info_view)
50+
private val errorServiceExplanationTextView: TextView =
51+
errorPanelRoot.findViewById(R.id.error_message_service_explanation_view)
52+
private val errorActionButton: Button =
53+
errorPanelRoot.findViewById(R.id.error_action_button)
54+
private val errorRetryButton: Button =
55+
errorPanelRoot.findViewById(R.id.error_retry_button)
4656

4757
private var errorDisposable: Disposable? = null
4858

4959
init {
50-
errorDisposable = errorButtonRetry.clicks()
60+
errorDisposable = errorRetryButton.clicks()
5161
.debounce(300, TimeUnit.MILLISECONDS)
5262
.observeOn(AndroidSchedulers.mainThread())
5363
.subscribe { onRetry.run() }
5464
}
5565

66+
private fun ensureDefaultVisibility() {
67+
errorTextView.isVisible = true
68+
69+
errorServiceInfoTextView.isVisible = false
70+
errorServiceExplanationTextView.isVisible = false
71+
errorActionButton.isVisible = false
72+
errorRetryButton.isVisible = false
73+
}
74+
5675
fun showError(errorInfo: ErrorInfo) {
5776

5877
if (errorInfo.throwable != null && errorInfo.throwable!!.isInterruptedCaused) {
@@ -62,56 +81,46 @@ class ErrorPanelHelper(
6281
return
6382
}
6483

65-
errorButtonAction.isVisible = true
84+
ensureDefaultVisibility()
85+
6686
if (errorInfo.throwable is ReCaptchaException) {
67-
errorButtonAction.setText(R.string.recaptcha_solve)
68-
errorButtonAction.setOnClickListener {
87+
errorTextView.setText(R.string.recaptcha_request_toast)
88+
89+
showAndSetErrorButtonAction(
90+
R.string.recaptcha_solve
91+
) {
6992
// Starting ReCaptcha Challenge Activity
7093
val intent = Intent(context, ReCaptchaActivity::class.java)
7194
intent.putExtra(
7295
ReCaptchaActivity.RECAPTCHA_URL_EXTRA,
7396
(errorInfo.throwable as ReCaptchaException).url
7497
)
7598
fragment.startActivityForResult(intent, ReCaptchaActivity.RECAPTCHA_REQUEST)
76-
errorButtonAction.setOnClickListener(null)
99+
errorActionButton.setOnClickListener(null)
77100
}
78-
errorTextView.setText(R.string.recaptcha_request_toast)
79-
// additional info is only provided by AccountTerminatedException
80-
errorServiceInfoTextView.isVisible = false
81-
errorServiceExplenationTextView.isVisible = false
82-
errorButtonRetry.isVisible = true
101+
102+
errorRetryButton.isVisible = true
83103
} else if (errorInfo.throwable is AccountTerminatedException) {
84-
errorButtonRetry.isVisible = false
85-
errorButtonAction.isVisible = false
86104
errorTextView.setText(R.string.account_terminated)
105+
87106
if (!isNullOrEmpty((errorInfo.throwable as AccountTerminatedException).message)) {
88-
errorServiceInfoTextView.setText(
89-
context.resources.getString(
90-
R.string.service_provides_reason,
91-
NewPipe.getNameOfService(ServiceHelper.getSelectedServiceId(context))
92-
)
93-
)
94-
errorServiceExplenationTextView.setText(
95-
(errorInfo.throwable as AccountTerminatedException).message
107+
errorServiceInfoTextView.text = context.resources.getString(
108+
R.string.service_provides_reason,
109+
NewPipe.getNameOfService(ServiceHelper.getSelectedServiceId(context))
96110
)
97111
errorServiceInfoTextView.isVisible = true
98-
errorServiceExplenationTextView.isVisible = true
99-
} else {
100-
errorServiceInfoTextView.isVisible = false
101-
errorServiceExplenationTextView.isVisible = false
112+
113+
errorServiceExplanationTextView.text =
114+
(errorInfo.throwable as AccountTerminatedException).message
115+
errorServiceExplanationTextView.isVisible = true
102116
}
103117
} else {
104-
errorButtonAction.setText(R.string.error_snackbar_action)
105-
errorButtonAction.setOnClickListener {
118+
showAndSetErrorButtonAction(
119+
R.string.error_snackbar_action
120+
) {
106121
ErrorActivity.reportError(context, errorInfo)
107122
}
108123

109-
// additional info is only provided by AccountTerminatedException
110-
errorServiceInfoTextView.isVisible = false
111-
errorServiceExplenationTextView.isVisible = false
112-
113-
// hide retry button by default, then show only if not unavailable/unsupported content
114-
errorButtonRetry.isVisible = false
115124
errorTextView.setText(
116125
when (errorInfo.throwable) {
117126
is AgeRestrictedContentException -> R.string.restricted_video_no_stream
@@ -124,7 +133,7 @@ class ErrorPanelHelper(
124133
is ContentNotSupportedException -> R.string.content_not_supported
125134
else -> {
126135
// show retry button only for content which is not unavailable or unsupported
127-
errorButtonRetry.isVisible = true
136+
errorRetryButton.isVisible = true
128137
if (errorInfo.throwable != null && errorInfo.throwable!!.isNetworkRelated) {
129138
R.string.network_error
130139
} else {
@@ -134,17 +143,36 @@ class ErrorPanelHelper(
134143
}
135144
)
136145
}
137-
errorPanelRoot.animate(true, 300)
146+
147+
setRootVisible()
148+
}
149+
150+
/**
151+
* Shows the errorButtonAction, sets a text into it and sets the click listener.
152+
*/
153+
private fun showAndSetErrorButtonAction(
154+
@StringRes resid: Int,
155+
@Nullable listener: View.OnClickListener
156+
) {
157+
errorActionButton.isVisible = true
158+
errorActionButton.setText(resid)
159+
errorActionButton.setOnClickListener(listener)
138160
}
139161

140162
fun showTextError(errorString: String) {
141-
errorButtonAction.isVisible = false
142-
errorButtonRetry.isVisible = false
163+
ensureDefaultVisibility()
164+
143165
errorTextView.text = errorString
166+
167+
setRootVisible()
168+
}
169+
170+
private fun setRootVisible() {
171+
errorPanelRoot.animate(true, 300)
144172
}
145173

146174
fun hide() {
147-
errorButtonAction.setOnClickListener(null)
175+
errorActionButton.setOnClickListener(null)
148176
errorPanelRoot.animate(false, 150)
149177
}
150178

@@ -153,8 +181,8 @@ class ErrorPanelHelper(
153181
}
154182

155183
fun dispose() {
156-
errorButtonAction.setOnClickListener(null)
157-
errorButtonRetry.setOnClickListener(null)
184+
errorActionButton.setOnClickListener(null)
185+
errorRetryButton.setOnClickListener(null)
158186
errorDisposable?.dispose()
159187
}
160188

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,30 @@
2121
android:id="@+id/error_message_service_info_view"
2222
android:layout_width="wrap_content"
2323
android:layout_height="wrap_content"
24-
android:gravity="center"
2524
android:layout_marginTop="6dp"
25+
android:gravity="center"
2626
android:text="@string/general_error"
2727
android:textSize="16sp"
28-
tools:text="YouTube provides this reason:" />
28+
android:visibility="gone"
29+
tools:text="YouTube provides this reason:"
30+
tools:visibility="visible" />
2931

3032
<TextView
31-
android:id="@+id/error_message_service_explenation_view"
33+
android:id="@+id/error_message_service_explanation_view"
3234
android:layout_width="wrap_content"
3335
android:layout_height="wrap_content"
3436
android:gravity="center"
3537
android:layout_marginTop="4dp"
3638
android:text="@string/general_error"
3739
android:textSize="16sp"
3840
android:textStyle="italic"
39-
tools:text="This account has been terminated because we received multiple third-party claims of copyright infringement regarding material that the user posted." />
41+
android:visibility="gone"
42+
tools:text="This account has been terminated because we received multiple third-party claims of copyright infringement regarding material that the user posted."
43+
tools:visibility="visible" />
4044

4145

4246
<Button
43-
android:id="@+id/error_button_action"
47+
android:id="@+id/error_action_button"
4448
android:layout_width="wrap_content"
4549
android:layout_height="wrap_content"
4650
android:layout_marginTop="8dp"
@@ -49,10 +53,12 @@
4953
android:textAllCaps="true"
5054
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
5155
android:textSize="16sp"
52-
android:theme="@style/ServiceColoredButton" />
56+
android:theme="@style/ServiceColoredButton"
57+
android:visibility="gone"
58+
tools:visibility="visible" />
5359

5460
<Button
55-
android:id="@+id/error_button_retry"
61+
android:id="@+id/error_retry_button"
5662
android:layout_width="wrap_content"
5763
android:layout_height="wrap_content"
5864
android:layout_marginTop="4dp"
@@ -62,6 +68,8 @@
6268
android:textAllCaps="true"
6369
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
6470
android:textSize="16sp"
65-
android:theme="@style/ServiceColoredButton" />
71+
android:theme="@style/ServiceColoredButton"
72+
android:visibility="gone"
73+
tools:visibility="visible" />
6674

6775
</LinearLayout>

0 commit comments

Comments
 (0)