Guest User

Untitled

a guest
Jul 15th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. class MainActivity : AppCompatActivity() {
  2.  
  3. private lateinit var listAdapter : ItemsAdapter
  4.  
  5. override fun onCreate(savedInstanceState: Bundle?) {
  6. super.onCreate(savedInstanceState)
  7. setContentView(R.layout.activity_main)
  8.  
  9. //create stuff for the ViewModel
  10. val database = ItemDatabase.getInstance(this)
  11. val viewModelFactory = ViewModelFactory(ItemRepository(database.itemDao(), Executors.newSingleThreadExecutor()))
  12. val viewModel = ViewModelProviders.of(this, viewModelFactory).get(MainViewModel::class.java)
  13.  
  14. //observe the result and adap UI accordingly
  15. viewModel.getItems().observe(this, Observer {
  16. it?.let { res : Resource<List<Item>> ->
  17. //if there is data, always display it
  18. it.data?.let {
  19. listAdapter.submitList(it)
  20. }
  21.  
  22. when (res.status) {
  23. STATUS_LOADING -> {
  24. progressBar.visibility = VISIBLE
  25. error.visibility = GONE
  26. }
  27. STATUS_ERROR -> {
  28. progressBar.visibility = GONE
  29. error.visibility = VISIBLE
  30. }
  31. STATUS_SUCCESS -> {
  32. progressBar.visibility = GONE
  33. error.visibility = GONE
  34. }
  35. }
  36. }
  37. })
  38.  
  39. listAdapter = ItemsAdapter()
  40. val recyclerView : RecyclerView = findViewById(R.id.recyclerView)
  41. recyclerView.apply {
  42. setHasFixedSize(true)
  43. adapter = listAdapter
  44. layoutManager = LinearLayoutManager(context)
  45. }
  46. }
  47. }
Add Comment
Please, Sign In to add comment