airbnb / epoxy

Epoxy is an Android library for building complex screens in a RecyclerView
https://goo.gl/eIK82p
Apache License 2.0
8.46k stars 730 forks source link

PagingDataEpoxyController never calls addModels() when PagingData is empty #1291

Open abdul-hasib opened 2 years ago

abdul-hasib commented 2 years ago

PagingDataEpoxyController is easy to set up and works great however I am facing some issues in displaying a model when the PagingData is empty. I have tried to override the addModels() method but seems it never gets triggered when data is empty. Not sure if I am missing something here.

PS: I am new to Kotlin and Android

Paging model

class PagingModel : ViewModel() {
    var repository = PagingRepository()
    fun query(query: Query): Flow<PagingData<DocumentSnapshot>> =
        repository.query(query).cachedIn(viewModelScope)
}

Paging Repository

class PagingRepository {

    fun query(query: Query): Flow<PagingData<DocumentSnapshot>> {
        return Pager(
            config = PagingConfig(
                pageSize = NETWORK_PAGE_SIZE,
                prefetchDistance = 1,
                enablePlaceholders = false
            ),
            pagingSourceFactory = {
                FirestorePagingSource(query, Source.DEFAULT)
            }
        ).flow
    }

    companion object {
        const val NETWORK_PAGE_SIZE = 30
    }
}

I am loading data from onCreateView() of fragment

lifecycleScope.launchWhenCreated {
      pagingModel.query(query).collectLatest {
          Timber.d("before submit data")
          recyclerView.setController(controller)
          controller.submitData(it)
          Timber.d("after submit data")  // ==============> THIS IS NEVER PRINTED
      }
 }

My Controller class

inner class Controller : PagingDataEpoxyController<DocumentSnapshot>() {

        override fun addModels(models: List<EpoxyModel<*>>) {
            add(photo()) // =============> THIS NEVER GETS CALLED WHEN DATA IS EMPTY
            super.addModels(models)
        }

        override fun buildItemModel(
            currentPosition: Int,
            item: DocumentSnapshot?
        ): EpoxyModel<*> {
              // my model here
        }
}