Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What if we want to retrieve values one by one? #27

Open
SharifRafid opened this issue Sep 4, 2020 · 0 comments
Open

What if we want to retrieve values one by one? #27

SharifRafid opened this issue Sep 4, 2020 · 0 comments

Comments

@SharifRafid
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant