Neeraj Verma
Neeraj Verma

Reputation: 1

How to detect Data loads in Paging 3 Room Compose

How to detect when new data has been added 2nd time into LazyColumn.?

Ignore: Initally it should be loaded from database.

Observe: 2nd Time Api call should be add data into local and room should re-load/append the new data

[Note]: I am using Key attribute from items() of LazyColumn which will prevent auto prepend scrolling when new data gets added into ScrollItemStack.Therefore I want to display a button whenever the new data from API get loaded into UI Stack

When new data is loaded we just want to display a button and by clicking it screen will be scrolled to top position.

Find sample image here:

find sample image here

val lazyArticleItems = viewModel.articlesFlows.collectAsLazyPagingItems()

LaunchedEffect(key1 = lazyArticleItems.loadState, block = {
        //visible only if new data from API has been loaded
        if(lazyArticleItems.loadState.source.prepend is LoadState.NotLoading) {
            isVisible.value = true
            DisplayDataHasBeenUpdated()
        }
    })


if (lazyArticleItems.loadState.refresh is LoadState.Loading && lazyArticleItems.itemCount < 1) {
//            Log.d("GoogleIO", "LazyArticleIsLoading::Refresh")
        } else {
            LazyColumn(modifier = Modifier.fillMaxSize()) {
                items(
                    contentType = lazyArticleItems.itemContentType { "Article" },
                    key = lazyArticleItems.itemKey { it.id },
                    count = lazyArticleItems.itemCount
                ) { index: Int ->
                    ArticleItem(lazyArticleItems[index]!!)
                    if (index < lazyArticleItems.itemCount - 1) Divider()
                }
            }
        }

We tried detechted loadstate.prepend, append but not able to achieve this implemention

Upvotes: 0

Views: 214

Answers (0)

Related Questions