SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.alhaz.tutorial.architecturecomponent.views.movies.list
  2.  
  3. import android.content.Intent
  4. import androidx.appcompat.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.view.View
  7. import androidx.lifecycle.Observer
  8. import androidx.lifecycle.ViewModelProviders
  9. import androidx.recyclerview.widget.LinearLayoutManager
  10. import kotlinx.android.synthetic.main.activity_movie_list.*
  11. import me.alhaz.tutorial.architecturecomponent.R
  12. import me.alhaz.tutorial.architecturecomponent.helper.valueobject.Status.SUCCESS
  13. import me.alhaz.tutorial.architecturecomponent.helper.valueobject.Status.ERROR
  14. import me.alhaz.tutorial.architecturecomponent.helper.valueobject.Status.EMPTY
  15. import me.alhaz.tutorial.architecturecomponent.viewmodels.movies.factory.MovieViewModelFactory
  16. import me.alhaz.tutorial.architecturecomponent.viewmodels.movies.list.MovieListViewModel
  17. import me.alhaz.tutorial.architecturecomponent.views.movies.detail.MovieDetailActivity
  18.  
  19. class MovieListActivity : AppCompatActivity() {
  20.  
  21.     private lateinit var viewModel: MovieListViewModel
  22.     private lateinit var movieListAdapter: MovieListAdapter
  23.  
  24.     override fun onCreate(savedInstanceState: Bundle?) {
  25.         super.onCreate(savedInstanceState)
  26.         setContentView(R.layout.activity_movie_list)
  27.  
  28.         setupLayout()
  29.         setupAdapter()
  30.         setupViewModel()
  31.     }
  32.  
  33.     fun setupLayout() {
  34.         rvMovies.setHasFixedSize(true)
  35.         rvMovies.layoutManager = LinearLayoutManager(this)
  36.     }
  37.  
  38.     fun setupAdapter() {
  39.         movieListAdapter = MovieListAdapter(this, clickListener = { movie ->
  40.             openDetailMoviePage(movie.id)
  41.         })
  42.         rvMovies.adapter = movieListAdapter
  43.     }
  44.  
  45.     private fun obtainViewModel(): MovieListViewModel {
  46.         val factory = MovieViewModelFactory.getInstance(this.application)
  47.         return ViewModelProviders.of(this, factory).get(MovieListViewModel::class.java)
  48.     }
  49.  
  50.     private fun setupViewModel() {
  51.         viewModel = obtainViewModel()
  52.         viewModel.getListMovie().observe(this, Observer { data ->
  53.             data?.let { response ->
  54.                 when (response.status) {
  55.                     SUCCESS -> {
  56.                         response.data?.let { movies ->
  57.                             progressBar.visibility = View.GONE
  58.                             rvMovies.visibility = View.VISIBLE
  59.                             movieListAdapter.submitList(movies)
  60.                         }
  61.                     }
  62.                     EMPTY -> {
  63.                         progressBar.visibility = View.GONE
  64.                         rvMovies.visibility = View.GONE
  65.                     }
  66.                     ERROR -> {
  67.                         progressBar.visibility = View.GONE
  68.                         rvMovies.visibility = View.GONE
  69.                     }
  70.                 }
  71.             }
  72.         })
  73.     }
  74.  
  75.  
  76.     fun openDetailMoviePage(movieID: Long) {
  77.         val intent = Intent(this, MovieDetailActivity::class.java)
  78.         intent.putExtra("movie_id", movieID)
  79.         startActivity(intent)
  80.     }
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top