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
@@ -11,6 +10,7 @@ import androidx.compose.runtime.Composable
1110import androidx.compose.ui.Alignment
1211import androidx.compose.ui.Modifier
1312import androidx.compose.ui.platform.LocalContext
13+ import androidx.compose.ui.platform.LocalInspectionMode
1414import androidx.compose.ui.res.stringResource
1515import androidx.compose.ui.text.font.FontWeight
1616import androidx.compose.ui.text.style.TextAlign
@@ -19,105 +19,73 @@ import org.schabi.newpipe.R
1919import org.schabi.newpipe.error.ErrorInfo
2020import org.schabi.newpipe.error.ErrorUtil
2121import org.schabi.newpipe.error.ReCaptchaActivity
22- import org.schabi.newpipe.extractor.exceptions.AccountTerminatedException
23- import org.schabi.newpipe.extractor.exceptions.ReCaptchaException
2422import org.schabi.newpipe.ui.theme.AppTheme
25- import org.schabi.newpipe.ui.theme.SizeTokens.SpacingExtraLarge
26- import org.schabi.newpipe.ui.theme.SizeTokens.SpacingMedium
27- import org.schabi.newpipe.ui.theme.SizeTokens.SpacingSmall
23+ import org.schabi.newpipe.ui.theme.SizeTokens
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
34+ val isPreview = LocalInspectionMode .current
35+ val messageText = if (isPreview) {
36+ stringResource(R .string.error_snackbar_message)
37+ } else {
38+ errorInfo.getMessage(context)
39+ }
5940
6041 Column (
6142 horizontalAlignment = Alignment .CenterHorizontally ,
6243 modifier = modifier
6344 ) {
6445
6546 Text (
66- text = stringResource(errorInfo.messageStringId) ,
47+ text = messageText ,
6748 style = MaterialTheme .typography.titleMedium.copy(fontWeight = FontWeight .Bold ),
6849 textAlign = TextAlign .Center
6950 )
7051
71- if (explanation.isNotBlank()) {
72- Spacer ( Modifier .height( SpacingSmall ))
73- Text (
74- text = explanation,
75- style = MaterialTheme .typography.bodyMedium,
76- textAlign = TextAlign . Center
77- )
52+ Spacer ( Modifier .height( SizeTokens . SpacingMedium ))
53+ if (errorInfo.isReportable) {
54+ ServiceColoredButton (onClick = {
55+ ErrorUtil .openActivity(context, errorInfo)
56+ }) {
57+ Text (stringResource( R .string.error_snackbar_action).uppercase())
58+ }
7859 }
7960
80- 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- }
88- }
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+ errorInfo.recaptchaUrl?.let { recaptchaUrl ->
62+ ServiceColoredButton (onClick = {
63+ val intent = Intent (context, ReCaptchaActivity ::class .java)
64+ .putExtra(ReCaptchaActivity .RECAPTCHA_URL_EXTRA , recaptchaUrl)
65+ .addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
66+ context.startActivity(intent)
67+ }) {
68+ Text (stringResource(R .string.recaptcha_solve).uppercase())
10269 }
10370 }
10471
105- onRetry?.let {
106- ServiceColoredButton (onClick = it) {
107- Text (stringResource(R .string.retry).uppercase())
72+ if (errorInfo.isRetryable) {
73+ onRetry?.let {
74+ ServiceColoredButton (onClick = it) {
75+ Text (stringResource(R .string.retry).uppercase())
76+ }
10877 }
10978 }
110- if (canOpenInBrowser) {
79+
80+ errorInfo.openInBrowserUrl?.let { url ->
11181 ServiceColoredButton (onClick = {
112- errorInfo.openInBrowserUrl?.let { url ->
113- ShareUtils .openUrlInBrowser(context, url)
114- }
82+ ShareUtils .openUrlInBrowser(context, url)
11583 }) {
11684 Text (stringResource(R .string.open_in_browser).uppercase())
11785 }
11886 }
11987
120- Spacer (Modifier .height(SpacingExtraLarge ))
88+ Spacer (Modifier .height(SizeTokens . SpacingExtraLarge ))
12189 }
12290}
12391
0 commit comments