Created
December 16, 2022 18:04
-
-
Save MrNtlu/34eada1c1d6988a68799a18762693894 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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