How to add paging with the paging library in my structure
I'm looking to put a pagination on my product listings. How do I add paging with paging library 3 in jetpack compose. I took a look at the documentation but I didn't understand. Anyone who can help I appreciate it
My Repository:
package com.example.quitanda.reposittory
import com.example.quitanda.classes.Retrofit
class SearchRepository {
private val retrofit = Retrofit()
private val search = retrofit.frutasServices
// ...
suspend fun getSearch(userId: String) =
// This isn't an optimal implementation because it doesn't take into
// account caching. We'll look at how to improve upon this in the next
// sections.
search.getSearch(userId)
}
My viewModel:
package com.example.quitanda.models
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.quitanda.reposittory.SearchRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
class SearchViewModel:ViewModel() {
private val searchRepository= SearchRepository()
private val _searche: MutableStateFlow<List<ShoppingCart>> = MutableStateFlow(listOf())
val searche: StateFlow<List<ShoppingCart>>
get() = _searche
fun buscar(title:String){
viewModelScope.launch {
try {
_searche.value = searchRepository.getSearch(title)
}catch (e:Exception){
Log.d("Service error",e.toString())
}
}
}
}
My Service:
package com.example.quitanda.models
import retrofit2.http.GET
import retrofit2.http.Path
interface Services {
@GET("category/")
suspend fun getFruta(): List<ShoppingCart>
@GET("category/{id}")
suspend fun getFrutas1(@Path("id") id: Int): List<ShoppingCart>
@GET("category/{id}")
suspend fun getVerduras(@Path("id") id:Int): List<ShoppingCart>
@GET("category/{id}")
suspend fun getTemperos(@Path("id") id:Int): List<ShoppingCart>
@GET("category/busca/{title}")
suspend fun getSearch(@Path("title") title: String): List<ShoppingCart>
@GET("product/{id}")
suspend fun getSingle(@Path("id") id: Int): List<ShoppingCart>
}