I want to do endless scrolling so that after scrolling 20 more products and so on endlessly. I have a model and the api itself from there I take everything that is required. Everything is working out. But I need another 20 products to appear after scrolling to the end, like in aliexpress. How to do it?
@Composable
fun FeedProductsScreen(viewModel: ProductViewModel){
val products by viewModel.productsLiveData.observeAsState(emptyList())
LaunchedEffect(Unit) {
viewModel.fetchProducts()
}
LazyVerticalGrid(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 10.dp, vertical = 5.dp),
columns = GridCells.Fixed(2),
content = {
items(products) {product ->
ProductCard(product)
}
}
)
}
api code
interface MarketplaceApi {
@GET("products")
suspend fun getProducts(
@Query("skip") skip: Int,
@Query("limit") limit: Int
): ProductResponse
companion object {
fun create(): MarketplaceApi {
val retrofit = Retrofit.Builder()
.baseUrl("https://dummyjson.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit.create(MarketplaceApi::class.java)
}
}
}
viewmodel code
class ProductViewModel : ViewModel() {
private val repository = ProductRepository()
val productsLiveData = MutableLiveData<List<Product>>()
private var currentPage = 0
private val pageSize = 20
fun fetchProducts() {
viewModelScope.launch {
try {
val response = repository.fetchProducts(currentPage, pageSize)
val products = response.products
productsLiveData.value = products
} catch (e: Exception) {
println("ERROR: ${e.message}")
}
}
}
I tried a lot of options but it didn't work out.I tried a lot of options but nothing came out. I tried through page numbering or through viewmodel management, but it still didn't work out.
Thank you!