Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MainActivity : AppCompatActivity() {
- private lateinit var listAdapter : ItemsAdapter
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- //create stuff for the ViewModel
- val database = ItemDatabase.getInstance(this)
- val viewModelFactory = ViewModelFactory(ItemRepository(database.itemDao(), Executors.newSingleThreadExecutor()))
- val viewModel = ViewModelProviders.of(this, viewModelFactory).get(MainViewModel::class.java)
- //observe the result and adap UI accordingly
- viewModel.getItems().observe(this, Observer {
- it?.let { res : Resource<List<Item>> ->
- //if there is data, always display it
- it.data?.let {
- listAdapter.submitList(it)
- }
- when (res.status) {
- STATUS_LOADING -> {
- progressBar.visibility = VISIBLE
- error.visibility = GONE
- }
- STATUS_ERROR -> {
- progressBar.visibility = GONE
- error.visibility = VISIBLE
- }
- STATUS_SUCCESS -> {
- progressBar.visibility = GONE
- error.visibility = GONE
- }
- }
- }
- })
- listAdapter = ItemsAdapter()
- val recyclerView : RecyclerView = findViewById(R.id.recyclerView)
- recyclerView.apply {
- setHasFixedSize(true)
- adapter = listAdapter
- layoutManager = LinearLayoutManager(context)
- }
- }
- }
Add Comment
Please, Sign In to add comment