Skip to content

Commit 5a6dbfd

Browse files
committed
refactor error activity and tiny tweaks
1 parent 818e55d commit 5a6dbfd

File tree

3 files changed

+85
-63
lines changed

3 files changed

+85
-63
lines changed

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

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,9 @@ class ErrorActivity : BaseActivity() {
6565
// print stack trace once again for debugging:
6666
errorInfo.stackTraces.forEach { Log.e(TAG, it) }
6767

68-
val sorryMessage = getString(R.string.sorry_string)
69-
val errorMessage = errorInfo.getMessage(this).toString()
70-
val infoLabels = getString(R.string.info_labels)
71-
val infoValues = buildInfoString()
72-
val errorDetails = formErrorText(errorInfo.stackTraces)
73-
7468
composeSetContent {
7569
ErrorReportScreen(
76-
sorryMessage = sorryMessage,
77-
errorMessage = errorMessage,
78-
infoLabels = infoLabels,
79-
infoValues = infoValues,
80-
errorDetails = errorDetails,
70+
errorInfo = errorInfo,
8171
onBackClick = { finish() },
8272
onReportViaEmail = { comment -> sendErrorEmail(comment) },
8373
onCopyForGitHub = { comment ->
@@ -109,24 +99,6 @@ class ErrorActivity : BaseActivity() {
10999
ShareUtils.openIntentInApp(this, intent)
110100
}
111101

112-
private fun formErrorText(stacktrace: Array<String>): String {
113-
val separator = "-------------------------------------"
114-
return stacktrace.joinToString(separator + "\n", separator + "\n", separator)
115-
}
116-
117-
private fun buildInfoString(): String {
118-
return errorInfo.userAction.message + "\n" +
119-
errorInfo.request + "\n" +
120-
contentLanguageString + "\n" +
121-
contentCountryString + "\n" +
122-
appLanguage + "\n" +
123-
errorInfo.getServiceName() + "\n" +
124-
currentTimeStamp + "\n" +
125-
packageName + "\n" +
126-
BuildConfig.VERSION_NAME + "\n" +
127-
osString
128-
}
129-
130102
private fun buildJson(comment: String): String {
131103
try {
132104
return JsonWriter.string()

app/src/main/java/org/schabi/newpipe/ui/components/common/PrivacyPolicyDialog.kt

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,26 @@ fun PrivacyPolicyDialog(
3838
},
3939
title = {
4040
Text(
41-
text = stringResource(R.string.privacy_policy_title),
42-
color = MaterialTheme.colorScheme.onSurface
41+
text = stringResource(R.string.privacy_policy_title)
4342
)
4443
},
4544
text = {
4645
Column {
4746
Text(
48-
text = stringResource(R.string.start_accept_privacy_policy),
49-
color = MaterialTheme.colorScheme.onSurfaceVariant
47+
text = stringResource(R.string.start_accept_privacy_policy)
5048
)
5149
TextButton(onClick = onReadPrivacyPolicy) {
5250
Text(
5351
text = stringResource(R.string.read_privacy_policy),
54-
color = MaterialTheme.colorScheme.primary
52+
color = MaterialTheme.colorScheme.secondary
5553
)
5654
}
5755
}
5856
},
5957
confirmButton = {
6058
TextButton(onClick = onAccept) {
6159
Text(
62-
text = stringResource(R.string.accept),
63-
color = MaterialTheme.colorScheme.primary
60+
text = stringResource(R.string.accept)
6461
)
6562
}
6663
},
@@ -78,17 +75,15 @@ fun PrivacyPolicyDialog(
7875
)
7976
}
8077

81-
@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO)
82-
@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES)
78+
@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO, showBackground = true)
79+
@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
8380
@Composable
8481
private fun PrivacyPolicyDialogPreview() {
8582
AppTheme {
86-
Surface(color = MaterialTheme.colorScheme.background) {
87-
PrivacyPolicyDialog(
88-
onAccept = {},
89-
onDecline = {},
90-
onReadPrivacyPolicy = {}
91-
)
92-
}
83+
PrivacyPolicyDialog(
84+
onAccept = {},
85+
onDecline = {},
86+
onReadPrivacyPolicy = {}
87+
)
9388
}
9489
}

app/src/main/java/org/schabi/newpipe/ui/screens/ErrorReportScreen.kt

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
package org.schabi.newpipe.ui.screens
77

8+
import android.content.Context
89
import android.content.res.Configuration
10+
import android.os.Build
911
import androidx.compose.foundation.horizontalScroll
1012
import androidx.compose.foundation.layout.Column
1113
import androidx.compose.foundation.layout.Row
@@ -21,32 +23,62 @@ import androidx.compose.material3.Icon
2123
import androidx.compose.material3.IconButton
2224
import androidx.compose.material3.MaterialTheme
2325
import androidx.compose.material3.OutlinedTextField
24-
import androidx.compose.material3.Surface
2526
import androidx.compose.material3.Text
2627
import androidx.compose.runtime.Composable
2728
import androidx.compose.runtime.getValue
2829
import androidx.compose.runtime.mutableStateOf
2930
import androidx.compose.runtime.saveable.rememberSaveable
3031
import androidx.compose.runtime.setValue
3132
import androidx.compose.ui.Modifier
33+
import androidx.compose.ui.platform.LocalContext
3234
import androidx.compose.ui.res.painterResource
3335
import androidx.compose.ui.res.stringResource
3436
import androidx.compose.ui.text.font.FontFamily
3537
import androidx.compose.ui.text.font.FontWeight
3638
import androidx.compose.ui.text.style.TextAlign
3739
import androidx.compose.ui.tooling.preview.Preview
3840
import androidx.compose.ui.unit.dp
41+
import java.time.ZonedDateTime
42+
import java.time.format.DateTimeFormatter
43+
import org.schabi.newpipe.BuildConfig
3944
import org.schabi.newpipe.R
45+
import org.schabi.newpipe.error.ErrorInfo
4046
import org.schabi.newpipe.ui.components.common.PrivacyPolicyDialog
4147
import org.schabi.newpipe.ui.components.common.ScaffoldWithToolbar
4248
import org.schabi.newpipe.ui.theme.AppTheme
49+
import org.schabi.newpipe.util.Localization
4350

4451
private const val ACTION_EMAIL = "EMAIL"
4552
private const val ACTION_GITHUB = "GITHUB"
4653

4754
@Composable
4855
fun ErrorReportScreen(
49-
sorryMessage: String,
56+
errorInfo: ErrorInfo,
57+
onBackClick: () -> Unit,
58+
onReportViaEmail: (comment: String) -> Unit,
59+
onCopyForGitHub: (comment: String) -> Unit,
60+
onReportOnGitHub: () -> Unit,
61+
onReadPrivacyPolicy: () -> Unit = {},
62+
onShareError: (comment: String) -> Unit = {}
63+
) {
64+
val context = LocalContext.current
65+
66+
ErrorReportContent(
67+
errorMessage = errorInfo.getMessage(context).toString(),
68+
infoLabels = stringResource(R.string.info_labels),
69+
infoValues = buildInfoString(context, errorInfo),
70+
errorDetails = formErrorText(errorInfo.stackTraces),
71+
onBackClick = onBackClick,
72+
onReportViaEmail = onReportViaEmail,
73+
onCopyForGitHub = onCopyForGitHub,
74+
onReportOnGitHub = onReportOnGitHub,
75+
onReadPrivacyPolicy = onReadPrivacyPolicy,
76+
onShareError = onShareError
77+
)
78+
}
79+
80+
@Composable
81+
private fun ErrorReportContent(
5082
errorMessage: String,
5183
infoLabels: String,
5284
infoValues: String,
@@ -95,7 +127,7 @@ fun ErrorReportScreen(
95127
) {
96128
// Sorry header
97129
Text(
98-
text = sorryMessage,
130+
text = stringResource(R.string.sorry_string),
99131
style = MaterialTheme.typography.titleLarge,
100132
fontWeight = FontWeight.Bold,
101133
textAlign = TextAlign.Center,
@@ -193,23 +225,46 @@ fun ErrorReportScreen(
193225
}
194226
}
195227

196-
@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO)
197-
@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES)
228+
private fun buildInfoString(context: Context, errorInfo: ErrorInfo): String {
229+
val contentLanguage = Localization.getPreferredLocalization(context).localizationCode
230+
val contentCountry = Localization.getPreferredContentCountry(context).countryCode
231+
val appLanguage = Localization.getAppLocale().toString()
232+
val osName = System.getProperty("os.name")!!
233+
val osBase = Build.VERSION.BASE_OS.ifEmpty { "Android" }
234+
val osString = "$osName $osBase ${Build.VERSION.RELEASE} - ${Build.VERSION.SDK_INT}"
235+
val timestamp = ZonedDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)
236+
237+
return errorInfo.userAction.message + "\n" +
238+
errorInfo.request + "\n" +
239+
contentLanguage + "\n" +
240+
contentCountry + "\n" +
241+
appLanguage + "\n" +
242+
errorInfo.getServiceName() + "\n" +
243+
timestamp + "\n" +
244+
context.packageName + "\n" +
245+
BuildConfig.VERSION_NAME + "\n" +
246+
osString
247+
}
248+
249+
private fun formErrorText(stackTraces: Array<String>): String {
250+
val separator = "-------------------------------------"
251+
return stackTraces.joinToString(separator + "\n", separator + "\n", separator)
252+
}
253+
254+
@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO, showBackground = true)
255+
@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
198256
@Composable
199257
private fun ErrorReportScreenPreview() {
200258
AppTheme {
201-
Surface(color = MaterialTheme.colorScheme.background) {
202-
ErrorReportScreen(
203-
sorryMessage = "Sorry, that should not have happened.",
204-
errorMessage = "Requested list not handled",
205-
infoLabels = "What:\nRequest:\nContent Language:\nContent Country:\nApp Language:\nService:\nTimestamp:\nPackage:\nVersion:\nOS version:",
206-
infoValues = "Requested list\nnone\nen\nUS\nen_US\nYouTube\n2026-04-17T12:00:00Z\norg.schabi.newpipe\n0.27.5\nAndroid 14 - 34",
207-
errorDetails = "java.lang.IllegalArgumentException: ...\n\tat org.schabi.newpipe.SomeClass.method(SomeClass.kt:42)",
208-
onBackClick = {},
209-
onReportViaEmail = {},
210-
onCopyForGitHub = {},
211-
onReportOnGitHub = {}
212-
)
213-
}
259+
ErrorReportContent(
260+
errorMessage = "Requested list not handled",
261+
infoLabels = "What:\nRequest:\nContent Language:\nContent Country:\nApp Language:\nService:\nTimestamp:\nPackage:\nVersion:\nOS version:",
262+
infoValues = "Requested list\nnone\nen\nUS\nen_US\nYouTube\n2026-04-17T12:00:00Z\norg.schabi.newpipe\n0.27.5\nAndroid 14 - 34",
263+
errorDetails = "-------------------------------------\njava.lang.IllegalArgumentException: ...\n\tat org.schabi.newpipe.SomeClass.method(SomeClass.kt:42)\n-------------------------------------",
264+
onBackClick = {},
265+
onReportViaEmail = {},
266+
onCopyForGitHub = {},
267+
onReportOnGitHub = {}
268+
)
214269
}
215270
}

0 commit comments

Comments
 (0)