Skip to content

Instantly share code, notes, and snippets.

@MrNtlu
Created December 16, 2022 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrNtlu/34eada1c1d6988a68799a18762693894 to your computer and use it in GitHub Desktop.
Save MrNtlu/34eada1c1d6988a68799a18762693894 to your computer and use it in GitHub Desktop.
@Composable
fun PagingListScreen() {
val viewModel = hiltViewModel<NewsViewModel>()
val articles = viewModel.getBreakingNews().collectAsLazyPagingItems()
LazyColumn {
items(
items = articles,
key = { it.url }
) { article ->
Text(
modifier = Modifier
.height(75.dp),
text = article?.title ?: "",
)
Divider()
}
when (val state = articles.loadState.refresh) { //FIRST LOAD
is LoadState.Error -> {
//TODO Error Item
//state.error to get error message
}
is LoadState.Loading -> { // Loading UI
item {
Column(
modifier = Modifier
.fillParentMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
Text(
modifier = Modifier
.padding(8.dp),
text = "Refresh Loading"
)
CircularProgressIndicator(color = Color.Black)
}
}
}
else -> {}
}
when (val state = articles.loadState.append) { // Pagination
is LoadState.Error -> {
//TODO Pagination Error Item
//state.error to get error message
}
is LoadState.Loading -> { // Pagination Loading UI
item {
Column(
modifier = Modifier
.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
Text(text = "Pagination Loading")
CircularProgressIndicator(color = Color.Black)
}
}
}
else -> {}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment