WooDroid icon indicating copy to clipboard operation
WooDroid copied to clipboard

What if we want to retrieve values one by one?

Open SharifRafid opened this issue 3 years ago • 0 comments

Instead of loading all the values (Products for example) together and then showing them in the list view or recycler ,what if I want to show each value after it's loaded ? Currently I can accomplish this by running the enque function multiple times keeping the filter as per_page=1 and increasing the page number for each product and then showing it. But this is not that much efficient as you can understand. So please suggest how I can load values (Products for example) one by one and show them in the recycler view. And currently this is what I am doing


  val allProducts : MutableLiveData<Product> by lazy {
        woocommerce = Woocommerce.Builder().setSiteUrl(baseURL)
            .setApiVersion(Woocommerce.API_V2)
            .setConsumerKey(consumerKey)
            .setConsumerSecret(consumerSecret)
            .build()
        preLoadProducts()
        MutableLiveData<Product>()
    }

    private fun preLoadProducts() {
        val productFilter = ProductFilter()
        productFilter.page = pageNum
        productFilter.per_page = 1
        loadProducts(productFilter)
    }

    private fun loadProducts(productFilter: ProductFilter){
        woocommerce.ProductRepository().products(productFilter).enqueue(object :
            Callback<List<Product>> {
            override fun onResponse(call: Call<List<Product>>, response: Response<List<Product>>) {
                try{
                    val productsResponse = response.body()
                    allProducts.value = productsResponse?.get(0)
                    pageNum ++
                    preLoadProducts()
                }catch (e : Exception){
                    e.printStackTrace()
                    allProducts.value = null
                }

            }
            override fun onFailure(call: Call<List<Product>>, t: Throwable) {
                t.printStackTrace()
                allProducts.value = null
            }
        })
    }

I'm not that much of an expert, but I hope you get the idea. Thanks

SharifRafid avatar Sep 04 '20 16:09 SharifRafid