1

Just after calling loadInitial method it automatically starts calling loadAfter method until and unless all pages are loaded and it gets an empty array from response. I am attaching my DataSource and DataSourceFactory code along with ViewModel function and PagedListAdapter.

PassbookDataSource.kt

    package co.indiagold.gold.buy.loan.home

import androidx.paging.PageKeyedDataSource
import co.indiagold.gold.buy.loan.helper.GenericMethods.handleServerError
import co.indiagold.gold.buy.loan.helper.GenericResponse
import co.indiagold.gold.buy.loan.network.Repository
import co.indiagold.gold.buy.loan.network.ResultWrapper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class PassbookDataSource(private val accessToken: String, private val assetName:String,private val viewModelScope: CoroutineScope) : PageKeyedDataSource<Int, PassbookResponseModel>() {
    private val PAGE_SIZE = 20

    override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, PassbookResponseModel>) {
        var result: ResultWrapper<GenericResponse<ArrayList<PassbookResponseModel>?>?>? = null
        viewModelScope.launch {
            result = Repository.getPassbookDetail(accessToken, assetName,1, PAGE_SIZE)
        }.invokeOnCompletion {
            if (result is ResultWrapper.Success) {
                val successResult = result as ResultWrapper.Success
                callback.onResult(successResult.value!!.result as MutableList<PassbookResponseModel>, null, 2)
            } else {
                it.handleServerError("vault/passbook")
            }
        }
    }

    override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, PassbookResponseModel>) {
    }

    override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, PassbookResponseModel>) {
        var result: ResultWrapper<GenericResponse<ArrayList<PassbookResponseModel>?>?>? = null
        viewModelScope.launch {
            result = Repository.getPassbookDetail(accessToken,assetName, params.key, PAGE_SIZE)
        }.invokeOnCompletion {
            if (result is ResultWrapper.Success) {
                val successResult = result as ResultWrapper.Success
                callback.onResult(successResult.value!!.result as MutableList<PassbookResponseModel>, params.key+1)
            } else {
                it.handleServerError("vault/passbook")
            }
        }
    }
}

PassbookDataSourceFactory.kt

class PassbookDataSourceFactory(private val accessToken:String,private val assetName:String,private val viewModelScope:CoroutineScope) : DataSource.Factory<Int,PassbookResponseModel>(){

    val orderListMLD = MutableLiveData<PassbookDataSource>(null)
    var orderDataSource : PassbookDataSource? = null

    override fun create(): DataSource<Int, PassbookResponseModel> {
        orderDataSource = PassbookDataSource(accessToken,assetName,viewModelScope)
        orderListMLD.postValue(orderDataSource)
        return orderDataSource!!
    }
}

Viewmodel Function

fun getPassbookDetail(assetName: String) {
        val accessToken = GenericMethods.getDataFromPreferences(Constants.SHARED_PREFS.LOGIN_PREFERENCES, Constants.SHARED_PREFS.ACCESS_TOKEN)
        if (accessToken.isNullOrEmpty()) {
            handleNullAccessTokenCase()
        } else {
            val factory = PassbookDataSourceFactory(accessToken, assetName, viewModelScope)

            val config = PagedList.Config.Builder()
                .setEnablePlaceholders(true)
                .setInitialLoadSizeHint(10)
                .setPrefetchDistance(4)
                .build()

            val executor = Executors.newFixedThreadPool(5)

            passbookResponseMLD = LivePagedListBuilder(factory, config)
                .setFetchExecutor(executor)
                .build()
        }
    }

PassbookAdapter.kt

package co.indiagold.gold.buy.loan.home

import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagedList
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import co.indiagold.gold.buy.loan.databinding.CardPassbookBinding
import co.indiagold.gold.buy.loan.helper.Constants
import co.indiagold.gold.buy.loan.orders.OrdersActivity
import kotlinx.android.synthetic.main.card_passbook.view.*

class PassbookAdapter(private val activity: Activity,private val emptyListListener: EmptyListListener) : PagedListAdapter<PassbookResponseModel,PassbookAdapter.MyHolder>(PASSBOOK_DIFF_CALLBACK) {

    private lateinit var binding: CardPassbookBinding
    class MyHolder(itemView : View) : RecyclerView.ViewHolder(itemView)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
        binding = CardPassbookBinding.inflate(LayoutInflater.from(parent.context))
        emptyListListener.onListLoad(currentList?.size)
        return MyHolder(binding.root)
    }

    override fun onCurrentListChanged(previousList: PagedList<PassbookResponseModel>?, currentList: PagedList<PassbookResponseModel>?) {
        super.onCurrentListChanged(previousList, currentList)
        emptyListListener.onListLoad(currentList?.size)
    }

    override fun onBindViewHolder(holder: MyHolder, position: Int) {
        binding.passbookEntry = getItem(position)

        holder.itemView.passbookRootLayout.setOnClickListener {
            val intent = Intent(activity,OrdersActivity::class.java)
            intent.putExtra(Constants.BUNDLE_STRINGS.ORDER_ID,getItem(position)?.referenceId)
            activity.startActivity(intent)
        }
    }

    override fun getItemId(position: Int): Long {
        return getItem(position)!!.createdAt
    }

    /*override fun getItemViewType(position: Int): Int {
        return position
    }*/
}



private val PASSBOOK_DIFF_CALLBACK = object  : DiffUtil.ItemCallback<PassbookResponseModel>(){
    override fun areItemsTheSame(oldItem: PassbookResponseModel, newItem: PassbookResponseModel): Boolean {
        return oldItem.createdAt == newItem.createdAt
    }

    override fun areContentsTheSame(oldItem: PassbookResponseModel, newItem: PassbookResponseModel): Boolean {
        return oldItem == newItem
    }
}

Proof of multiple calls

1
  • Did you figure out what the issue was?
    – 11m0
    Dec 5, 2020 at 21:16

1 Answer 1

0

The issue is, that you may put recycler view inside NextedScrollview or ScrollView, by removing scrollview it will work fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.