Skip to content

Instantly share code, notes, and snippets.

@rubenquadros
Last active August 19, 2021 19:33
Show Gist options
  • Save rubenquadros/dbff28d1e5abf2b6ecc8de2466cd8e11 to your computer and use it in GitHub Desktop.
Save rubenquadros/dbff28d1e5abf2b6ecc8de2466cd8e11 to your computer and use it in GitHub Desktop.
Game listing with loading
@Composable
fun GameListing(
games: Flow<PagingData<GameResultsEntity>>
) {
val lazyGameItems = games.collectAsLazyPagingItems()
LazyVerticalGrid(
cells = GridCells.Fixed(2),
content = {
items(lazyGameItems.itemCount) { index ->
lazyGameItems[index]?.let { GameItem(it) }
}
lazyGameItems.apply {
when {
loadState.refresh is
LoadState.Loading -> {
item { LoadingItem() }
item { LoadingItem() }
}
loadState.append is
LoadState.Loading -> {
item { LoadingItem() }
item { LoadingItem() }
}
loadState.refresh is
LoadState.Error -> {}
loadState.append is
LoadState.Error -> {}
}
}
}
)
}
@Composable
fun LoadingItem() {
CircularProgressIndicator(
modifier =
Modifier.testTag("ProgressBarItem")
.fillMaxWidth()
.padding(16.dp)
.wrapContentWidth(
Alignment.CenterHorizontally
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment