Created
March 21, 2018 01:08
-
-
Save cdmunoz/0799aac442edc3243efa1131b9ee46b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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