Skip to content

Instantly share code, notes, and snippets.

@cdmunoz
Created March 21, 2018 01:08
Show Gist options
  • Select an option

  • Save cdmunoz/0799aac442edc3243efa1131b9ee46b3 to your computer and use it in GitHub Desktop.

Select an option

Save cdmunoz/0799aac442edc3243efa1131b9ee46b3 to your computer and use it in GitHub Desktop.
class CryptocurrenciesActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(layout.activity_cryptocurrencies)
initializeRecycler()
progressBar.visibility = View.VISIBLE
loadData()
cryptocurrenciesViewModel.cryptocurrenciesResult().observe(this,
Observer<List<Cryptocurrency>> {
if (it != null) {
val position = cryptocurrenciesAdapter.itemCount
cryptocurrenciesAdapter.addCryptocurrencies(it)
recycler.adapter = cryptocurrenciesAdapter
recycler.scrollToPosition(position - Constants.LIST_SCROLLING)
}
})
cryptocurrenciesViewModel.cryptocurrenciesError().observe(this, Observer<String> {
if (it != null) {
Toast.makeText(this, resources.getString(R.string.cryptocurrency_error_message) + it,
Toast.LENGTH_SHORT).show()
}
})
cryptocurrenciesViewModel.cryptocurrenciesLoader().observe(this, Observer<Boolean> {
if (it == false) progressBar.visibility = View.GONE
})
}
private fun initializeRecycler() {
val gridLayoutManager = GridLayoutManager(this, 1)
gridLayoutManager.orientation = LinearLayoutManager.VERTICAL
recycler.apply {
setHasFixedSize(true)
layoutManager = gridLayoutManager
addOnScrollListener(InfiniteScrollListener({ loadData() }, gridLayoutManager))
}
}
fun loadData() {
cryptocurrenciesViewModel.loadCryptocurrencies(Constants.LIMIT, currentPage * Constants.OFFSET)
currentPage++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment