@@ -6,6 +6,8 @@ import android.util.Log
66import android.view.View
77import android.widget.Button
88import android.widget.TextView
9+ import androidx.annotation.Nullable
10+ import androidx.annotation.StringRes
911import androidx.core.view.isVisible
1012import androidx.fragment.app.Fragment
1113import 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
0 commit comments