0

I'm trying to api call via paging but it's not calling. Here is my code

but main things is I tried to print logs while until repository log printed and from pagingsource not any log printed that means API not calling.

and when I tried to call this api as simple where I'm calling another APIs without Paging it works fine the only issue is with Paging.

NetworkApi.kt

    interface NetworkApi {
    
        companion object {
            const val BASE_URL = ""
        }
    
        @POST("v1/products")
        @JvmSuppressWildcards
        suspend fun getProductList(
            @Body requestBody: Map<String, @JvmSuppressWildcards Any>
        ): List<CustomSection>
    
    }

ProductPagingSource.kt


    class ProductPagingSource(private val networkApi: NetworkApi) : PagingSource<Int, CustomSection>() {
    
        override fun getRefreshKey(state: PagingState<Int, CustomSection>): Int? {
            val intKey = state.anchorPosition?.let { anchorPosition ->
                state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1)
                    ?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)
            }
            Timber.d("PRODUCT RESPOSNSE SOURCE $intKey")
            return intKey
        }
    
        override suspend fun load(params: LoadParams<Int>): LoadResult<Int, CustomSection> {
    
            val position = params.key ?: STARTING_PAGE_INDEX
    
    //         Create JSON body for request with given key
            val body = mapOf(
                "category" to "115",
                "user_id" to "",
                "page" to 1,
                "order_by" to "null"
            )
    
    //        Timber.d("PRODUCT RESPOSNSE SUCCESS $body")
    
            return try {
    //            val response = networkApi.getProductList(Model())
                val response = networkApi.getProductList(body)
    
                Timber.d("PRODUCT RESPOSNSE SUCCESS $response")
    
                LoadResult.Page(
                    data = response,
                    prevKey = if (position == STARTING_PAGE_INDEX) null else position - 1,
                    nextKey = if (response.isEmpty()) null else position + 1
                )
    
            } catch (exception: Exception) {
                Timber.d("PRODUCT RESPOSNSE ERROR ${exception.localizedMessage}")
                LoadResult.Error(exception)
            }
    
        }
    
    }

ProductRepository.kt

    class ProductRepository @Inject constructor(val networkApi: NetworkApi) : BaseApiResponse(){
    
        fun getProductList(): Flow<PagingData<CustomSection>> {
    
            Timber.d("PRODUCT RESPOSNSE Repository")
    
            return Pager(config = PagingConfig(pageSize = 10, prefetchDistance = 10,maxSize = 30, enablePlaceholders = false),
                pagingSourceFactory = { ProductPagingSource(networkApi) }).flow
        }
    
    }

Logcat

13233-13233 ProductViewModel com.theyoungindians.android2 D PRODUCT RESPOSNSE ViewModel

com.theyoungindians.android2 D PRODUCT RESPOSNSE Repository13233-13233 ProductRepository

EDIT 1

Here is my viewModel where I'm collecting data

    @HiltViewModel
    class ProductViewModel
    @Inject constructor(private val productRepository: ProductRepository) : ViewModel() {
    
        fun getProductList(): Flow<PagingData<CustomSection>> {
    
            Timber.d("PRODUCT RESPOSNSE ViewModel")
            return productRepository.getProductList()
                .cachedIn(viewModelScope)
        }
    
    }
Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
Pratik Fagadiya
  • 1,195
  • 7
  • 21

0 Answers0