11package org.schabi.newpipe.ui.components.common
22
33import android.content.Intent
4- import androidx.annotation.StringRes
54import androidx.compose.foundation.layout.Column
65import androidx.compose.foundation.layout.Spacer
76import androidx.compose.foundation.layout.height
@@ -19,42 +18,18 @@ import org.schabi.newpipe.R
1918import org.schabi.newpipe.error.ErrorInfo
2019import org.schabi.newpipe.error.ErrorUtil
2120import org.schabi.newpipe.error.ReCaptchaActivity
22- import org.schabi.newpipe.extractor.exceptions.AccountTerminatedException
23- import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
2421import org.schabi.newpipe.ui.theme.AppTheme
2522import org.schabi.newpipe.ui.theme.SizeTokens.SpacingExtraLarge
2623import org.schabi.newpipe.ui.theme.SizeTokens.SpacingMedium
27- import org.schabi.newpipe.ui.theme.SizeTokens.SpacingSmall
2824import org.schabi.newpipe.util.external_communication.ShareUtils
2925
30- enum class ErrorAction (@StringRes val actionStringId : Int ) {
31- REPORT (R .string.error_snackbar_action),
32- SOLVE_CAPTCHA (R .string.recaptcha_solve)
33- }
34-
35- /* *
36- * Determines the error action type based on the throwable in ErrorInfo
37- *
38- */
39- fun determineErrorAction (errorInfo : ErrorInfo ): ErrorAction {
40- return when (errorInfo.throwable) {
41- is ReCaptchaException -> ErrorAction .SOLVE_CAPTCHA
42- is AccountTerminatedException -> ErrorAction .REPORT
43- else -> ErrorAction .REPORT
44- }
45- }
46-
4726@Composable
4827fun ErrorPanel (
4928 errorInfo : ErrorInfo ,
5029 modifier : Modifier = Modifier ,
5130 onRetry : (() -> Unit )? = null,
5231
5332) {
54- val explanation = errorInfo.getExplanation()
55- val canOpenInBrowser = errorInfo.openInBrowserUrl != null
56- val errorActionType = determineErrorAction(errorInfo)
57-
5833 val context = LocalContext .current
5934
6035 Column (
@@ -68,37 +43,26 @@ fun ErrorPanel(
6843 textAlign = TextAlign .Center
6944 )
7045
71- if (explanation.isNotBlank()) {
72- Spacer (Modifier .height(SpacingSmall ))
73- Text (
74- text = explanation,
75- style = MaterialTheme .typography.bodyMedium,
76- textAlign = TextAlign .Center
77- )
78- }
79-
8046 Spacer (Modifier .height(SpacingMedium ))
81- when (errorActionType) {
82- ErrorAction .REPORT -> {
83- ServiceColoredButton (onClick = {
84- ErrorUtil .openActivity(context, errorInfo)
85- }) {
86- Text (stringResource(errorActionType.actionStringId).uppercase())
87- }
47+
48+ if (errorInfo.recaptchaUrl != null ) {
49+ ServiceColoredButton (onClick = {
50+ // Starting ReCaptcha Challenge Activity
51+ val intent = Intent (context, ReCaptchaActivity ::class .java)
52+ .putExtra(
53+ ReCaptchaActivity .RECAPTCHA_URL_EXTRA ,
54+ errorInfo.recaptchaUrl
55+ )
56+ .addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
57+ context.startActivity(intent)
58+ }) {
59+ Text (stringResource(R .string.recaptcha_solve).uppercase())
8860 }
89- ErrorAction .SOLVE_CAPTCHA -> {
90- ServiceColoredButton (onClick = {
91- // Starting ReCaptcha Challenge Activity
92- val intent = Intent (context, ReCaptchaActivity ::class .java)
93- .putExtra(
94- ReCaptchaActivity .RECAPTCHA_URL_EXTRA ,
95- (errorInfo.throwable as ReCaptchaException ).url
96- )
97- .addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
98- context.startActivity(intent)
99- }) {
100- Text (stringResource(errorActionType.actionStringId).uppercase())
101- }
61+ } else if (errorInfo.isReportable) {
62+ ServiceColoredButton (onClick = {
63+ ErrorUtil .openActivity(context, errorInfo)
64+ }) {
65+ Text (stringResource(R .string.error_snackbar_action).uppercase())
10266 }
10367 }
10468
@@ -107,9 +71,9 @@ fun ErrorPanel(
10771 Text (stringResource(R .string.retry).uppercase())
10872 }
10973 }
110- if (canOpenInBrowser ) {
74+ if (errorInfo.openInBrowserUrl != null ) {
11175 ServiceColoredButton (onClick = {
112- errorInfo.openInBrowserUrl? .let { url ->
76+ errorInfo.openInBrowserUrl.let { url ->
11377 ShareUtils .openUrlInBrowser(context, url)
11478 }
11579 }) {
0 commit comments