0

I am trying to make an api call with paging 3 and retrofit. But for some reason the api call is not getting triggered. I checked my code multiple times to find an issue but couldn't understand why am I doing wrong. When I make that api call seperately then its getting called. But not with paging 3. Can someone give some idea why it might be the case? And how can I find the problem here ?

My Api Service Class

    interface ApiService {
    companion object{
        const val BASE_URL = "https://picsum.photos/v2/"
    }

    @GET("list")
    suspend fun getImageList(
        @Query("page") pageNo:Int,
        @Query("limit") limit: Int=20
    ):Response<ImageListResponse>
}

My Paging Source

class ImagePagingSource(private val api:ApiService): PagingSource<Int, ImageItem>(){

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ImageItem> {
        try {
            Log.d("ImagePagingSource","Load is Called")
            val position = params.key ?: 1
            val previousKey = if (position == 1) null else position - 1
            /*
             * We don't have the total page key in this api response.
             * So restricting the call after page 10 statically.
             * */
            val nextKey = if (position == 10) null else position + 1
            val response = api.getImageList(position)
            if (!response.isSuccessful) return LoadResult.Error(Exception("Api Error : ${response.message()}"))
            if (response.body().isNullOrEmpty()) return LoadResult.Error(Exception("No Image Found"))
            Log.d("ImagePagingSource",response.body().toString())
            return LoadResult.Page(
                data = response.body()!!,
                prevKey = previousKey,
                nextKey = nextKey
            )
        } catch (e: Exception) {
            return LoadResult.Error(e)
        }
    }

    override fun getRefreshKey(state: PagingState<Int, ImageItem>): Int? {
        return state.anchorPosition?.let { anchorPosition ->
            val anchorPage = state.closestPageToPosition(anchorPosition)
            anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
        }
    }

}

My Repository Class

class HomePageRepo
@Inject
constructor(
    private val apiService: ApiService
)
{
    fun getImages() = Pager(
        config = PagingConfig(pageSize = 20, maxSize = 100),
        pagingSourceFactory = { ImagePagingSource(apiService) }
    ).liveData

    suspend fun callApi(){
        apiService.getImageList(1)
    }

}

View Model

fun getSearchResult():LiveData<PagingData<ImageItem>> = repo.getImages().cachedIn(viewModelScope)

Paging Adapter For Recycler View

class ImageShowPagingAdapter(private val context:Context) : PagingDataAdapter<
        ImageItem,
        ImageShowPagingAdapter.ImageShowViewHolder
        >(COMPARATOR) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImageShowViewHolder {
        val inflater = LayoutInflater.from(context)
        val view = RecViewItemImagesBinding.inflate(inflater, parent, false)
        return ImageShowViewHolder(view)
    }

    override fun onBindViewHolder(holder: ImageShowViewHolder, position: Int) {
        val item = getItem(position)
        Glide.with(context)
            .load(item?.download_url)
            .apply(
                RequestOptions()
                    .placeholder(R.drawable.all_type_content_default)
                    .error(R.drawable.all_type_content_default)
            )
            .into(holder.binding.ivImage)
    }

    inner class ImageShowViewHolder(val binding: RecViewItemImagesBinding) : RecyclerView.ViewHolder(binding.root)
    companion object {
        private val COMPARATOR = object : DiffUtil.ItemCallback<ImageItem>() {
            override fun areItemsTheSame(oldItem: ImageItem, newItem: ImageItem): Boolean {
                return oldItem.id == newItem.id
            }

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

Here is the complete url that I am trying to hit

https://picsum.photos/v2/list?page=1&limit=20

Can someone please help me out to understand why it is happening or how to find the problem here. Thank you in advance

It seems the problem was inside of my recycler view paging adapter what was causing the problem. I have solved the problem now.

samriddha
  • 35
  • 6
  • What do you mean by "the api call is not getting triggered"? What happens when you run your code? What do you want it to do instead? – Code-Apprentice Dec 24 '22 at 06:50
  • "the api call is not getting triggered" means that the api is not getting called at all. I have logged to check if it is getting called or not.But for some reason the api doesn't gets called. Hope I am able to make you understand the problem here. – samriddha Dec 24 '22 at 06:52
  • Thanks for the clarification. Where do you expect the api to be called? – Code-Apprentice Dec 24 '22 at 06:56
  • 1
    From my activity. I am observing the live data inside of my view model. – samriddha Dec 24 '22 at 07:00
  • 1
    can you try putting a debug on load method – pseudoankit Dec 24 '22 at 07:03
  • 1
    Yes. I already have put 2 logs inside of load() method to check if load is getting called or not.. But none of them gets executed sadly. – samriddha Dec 24 '22 at 07:11
  • You did it right by posting an answer. Undelete your answer and remove the edit from your question. Feel free to give more details with some code if you think it will be helpful to others. – Code-Apprentice Dec 24 '22 at 08:26

1 Answers1

0

It seems the problem was inside of my recycler view paging adapter what was causing the problem. I was instanciating the paging adapter but when setting it with recycler view there were some error in my code. Hence resulting the api not getting called at all.If anyone facing the same issue please check your paging adapter and see if it is getting assigned properly with your recyclerview.

samriddha
  • 35
  • 6