refactor(android): decouple LoadError from AndroidX Paging

This commit is contained in:
Harsh Shandilya 2022-09-30 10:38:15 +05:30
parent 1ba9cefda6
commit 24afea0412
No known key found for this signature in database
2 changed files with 10 additions and 6 deletions

View file

@ -17,17 +17,17 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.unit.dp
import androidx.paging.LoadState
@Composable
fun LoadError(
data: LoadState.Error,
label: String,
error: Throwable,
modifier: Modifier = Modifier,
) {
var showDialog by remember { mutableStateOf(false) }
Column(verticalArrangement = Arrangement.spacedBy(4.dp), modifier = modifier) {
Text(
text = "Failed to load posts",
text = label,
style = MaterialTheme.typography.bodyLarge,
modifier = Modifier.align(Alignment.CenterHorizontally),
)
@ -47,14 +47,14 @@ fun LoadError(
text = "Copy stacktrace",
modifier =
Modifier.clickable {
clipboard.setText(AnnotatedString(data.error.stackTraceToString()))
clipboard.setText(AnnotatedString(error.stackTraceToString()))
showDialog = false
}
)
},
text = {
Text(
text = "${data.error.message}",
text = "${error.message}",
style = MaterialTheme.typography.bodyLarge,
)
}

View file

@ -49,7 +49,11 @@ fun NetworkPosts(
if (items.itemCount == 0) {
Box(modifier = Modifier.fillMaxSize()) {
if (loadState is LoadState.Error) {
LoadError(data = loadState, modifier = Modifier.align(Alignment.Center))
LoadError(
label = "Failed to load posts",
error = loadState.error,
modifier = Modifier.align(Alignment.Center),
)
}
}
} else {