Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question:Paging with Room Datasource #36

Open
thexaib opened this issue Feb 27, 2021 · 2 comments
Open

Question:Paging with Room Datasource #36

thexaib opened this issue Feb 27, 2021 · 2 comments
Labels
question Further information is requested

Comments

@thexaib
Copy link

thexaib commented Feb 27, 2021

Hi, this is more a question than an issue, related to Room Datasource+LiveData PagedList.
I want to use selectable item feature of the OneAdapter, so i am trying to change currently working RecyclerView with PagedListAdapter.
But for OneAdapter, i need to ask PagedList from Room datasource to load next items , which i could not find how can it be done. As i understand PagedListAdapter requests next loading of items automatically by calculating Item size and recyclerview viewport size. and calls pagedlist.loadAround(Int) . I am not sure about the Int value passed here, its probably the internally maintained enumerated index of current list. I could not find how can i request next page to load in viewmodel. Did anyone can direct me how to use Room Paging Datasource with OneAdapter.?

OneAdapter Impl

object OneAdapters {

    fun create(recyclerView: RecyclerView, onClick:(ModelEntity)->Unit,onCurrentBind:(Int)->Unit,loadMoreCall:(Int)->Unit) =OneAdapter(recyclerView){
        itemModules+=ExperimentModelItem(onClick,onCurrentBind)
        pagingModule=PagingModuleImpl(loadMoreCall)
    }


    internal class ExperimentModelItem(onClick: (ModelEntity) -> Unit,onCurrentBind: (Int) -> Unit) : ItemModule<ModelEntity>() {

        init {
            config {
                layoutResource = R.layout.item_tile_list
            }
            onBind { model, viewBinder, metadata ->

                viewBinder.bindings(ItemTileListBinding::bind).run {
                    onCurrentBind(metadata.position)
                    title.text = model.modelname
                    model.modified_at?.let { d ->
                        subtitle.text = "modified ${DateFormatHelper.readable(d)}"
                    }
                }
            }
            onUnbind { model, viewBinder, _ ->
            }

            eventHooks += ClickEventHook<ModelEntity>().apply {
                onClick { model, viewBinder, _ ->
                    onClick(model)
                }
            }
        }

    }
    internal class PagingModuleImpl(loadMoreCall:(Int)->Unit) : PagingModule() {
        init {
            config {
                layoutResource = R.layout.item_loading_progress// can be some spinner animation
                visibleThreshold = 0 // invoke onLoadMore 3 items before the end
            }
            onLoadMore { currentPage ->
                // place your load more logic here, like asking the ViewModel to load the next page of data.
                loadMoreCall(currentPage)
            }
        }
    }
}

Activity

private var mListMaxPosition: Int = 0
    private val mRecyclerAdapter: OneAdapter by lazy {
        OneAdapters.create(binding.explistActivityRecylerview, onClick = {
            // navigate
        },
            onCurrentBind = {
                if (it > mListMaxPosition)
                    mListMaxPosition = it 
            },
            loadMoreCall = {
                viewmodel.add(VMEvent.LoadMore(mListMaxPosition))
            })
    }

viewmodel

init{
 val factory =repo.getAllModelsFactory()
        val config = PagedList.Config.Builder()
            .setEnablePlaceholders(false) // when true, the oneadapter's internal adapter crash on validating list having null models   
            .setInitialLoadSizeHint(15)
            .setPageSize(5).build()

        val pagedListBuilder: LivePagedListBuilder<Int, ModelEntity>  = LivePagedListBuilder<Int,ModelEntity>(
            factory,
            config
            )
models=pagedListBuilder.build() // observed LiveData in activity
}
fun    onLoadMore(currentPage:Int) // called from activity, 
{
   models.value?.let {
     // it.loadAround(currentPage) // max intPosition comming from activity
    //it.loadAround(it.lastKey as Int)

   }
 
}
@idanatz
Copy link
Owner

idanatz commented Mar 6, 2021

Hi,
In order to work with Paging3, you will need your adapter to inherited from PagingDataAdapter. (source)

OneAdapter is based on RecyclerView.Adapter and not PagingDataAdapter because I didn't want to rely on a brand new library (when it just came out) as a base for my library.

I wanted to add some kind of support back in the day for Paging2 but the paging library keeps changing and v2 is no longer that attractive.
When Paging3 will be out of alpha and will prove its maturity I will try to add a way OneAdapter can integrate with it.

@thexaib
Copy link
Author

thexaib commented Mar 7, 2021

i also played around with paging 3, but i did not see much control on manually fetching next page and paging 2 looked like it could be done. but i failed :P. looking forward to paging adapter support.

@idanatz idanatz added the question Further information is requested label Oct 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants