3
\$\begingroup\$

This is the code of my function to endless scrolling. It's a good practice to bind new disposable in onscroll listener? What do you think about my code?

   private fun searchData(srsearch: String) {
    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(et_search_querry.windowToken, 0)

    rv_results.layoutManager = LinearLayoutManager(this)
    val manager = rv_results.layoutManager as LinearLayoutManager
    pb_fetch.visibility = View.VISIBLE
    disposable =
            wikiApiServe.getRecordsByQuerry("query", "json", "search", srsearch)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            { result ->
                                var continueS = result._continue._continue
                                var offset = result._continue.sroffset
                                var searchS = srsearch

                                pb_fetch.visibility = View.GONE
                                rv_results.adapter = ResultsAdapter(result.query.search, this)
                                tv_search_results.text =
                                        String.format(resources.getQuantityText(R.plurals.results_found,
                                                result.query.searchinfo.totalhits).toString(), result.query.searchinfo.totalhits)
                                rv_results.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                                    override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                                        super.onScrolled(recyclerView, dx, dy)
                                        if (!isLoading) {
                                            if (manager.findLastCompletelyVisibleItemPosition()
                                                    >= rv_results.adapter.itemCount - 1) {
                                                isLoading = true
                                                pb_fetch.visibility = View.VISIBLE
                                                disposable?.dispose()
                                                disposable = wikiApiServe.getMoreRecords("query", "json", "search", continueS, offset, searchS)
                                                        .subscribeOn(Schedulers.io())
                                                        .observeOn(AndroidSchedulers.mainThread())
                                                        .subscribe({ result ->
                                                            isLoading = false
                                                                    continueS = result._continue._continue
                                                                    offset = result._continue.sroffset
                                                                    pb_fetch.visibility = View.GONE
                                                                    (rv_results.adapter as ResultsAdapter).addMore(result.query.search)
                                                                }, { _ ->
                                                            isLoading = false
                                                            pb_fetch.visibility = View.GONE
                                                            Toast.makeText(this@MainActivity,
                                                                    "Something went wrong try again",
                                                                    Toast.LENGTH_SHORT).show()
                                                        })
                                            }
                                        }
                                    }
                                })
                            },
                            { _ ->
                                pb_fetch.visibility = View.GONE
                                Toast.makeText(this,
                                        "Something went wrong try again",
                                        Toast.LENGTH_SHORT).show()
                            }
                    )
}
\$\endgroup\$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.