Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/databinding #9

Merged
merged 7 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
implements fragments logics via databinding
  • Loading branch information
skydoves committed Jun 7, 2019
commit ee82281b3827f26e8c2ef3e495421ae3f4431aa7
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.skydoves.themovies.binding

import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.skydoves.themovies.extension.bindResource
import com.skydoves.themovies.models.Resource
import com.skydoves.themovies.models.entity.Movie
import com.skydoves.themovies.models.entity.Person
import com.skydoves.themovies.models.entity.Tv
import com.skydoves.themovies.view.adapter.MovieListAdapter
import com.skydoves.themovies.view.adapter.PeopleAdapter
import com.skydoves.themovies.view.adapter.TvListAdapter

@BindingAdapter("adapterMovieList")
fun bindAdapterMovieList(view: RecyclerView, resource: Resource<List<Movie>>?) {
view.bindResource(resource) {
if (resource != null) {
val adapter = view.adapter as? MovieListAdapter
adapter?.addMovieList(resource)
}
}
}

@BindingAdapter("adapterPersonList")
fun bindAdapterPersonList(view: RecyclerView, resource: Resource<List<Person>>?) {
view.bindResource(resource) {
if (resource != null) {
val adapter = view.adapter as? PeopleAdapter
adapter?.addPeople(resource)
}
}
}

@BindingAdapter("adapterTvList")
fun bindAdapterTvList(view: RecyclerView, resource: Resource<List<Tv>>?) {
view.bindResource(resource) {
if (resource != null) {
val adapter = view.adapter as? TvListAdapter
adapter?.addTvList(resource)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.skydoves.themovies.extension

import androidx.recyclerview.widget.RecyclerView
import com.skydoves.themovies.models.Resource
import com.skydoves.themovies.models.Status
import org.jetbrains.anko.toast

fun RecyclerView.bindResource(resource: Resource<Any>?, onSuccess: () -> Unit) {
if (resource != null) {
when (resource.status) {
Status.LOADING -> Unit
Status.SUCCESS -> onSuccess()
Status.ERROR -> this.context.toast(resource.errorEnvelope?.status_message.toString())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,18 @@ class PersonDetailActivity : AppCompatActivity() {
const val intent_requestCode = 1000
const val personId = "person"

fun startActivity(fragment: Fragment, activity: FragmentActivity, person: Person, view: View) {
if (activity.checkIsMaterialVersion()) {
val intent = Intent(activity, PersonDetailActivity::class.java)
ViewCompat.getTransitionName(view)?.let {
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, view, it)
intent.putExtra(personId, person)
activity.startActivityFromFragment(fragment, intent, intent_requestCode, options.toBundle())
fun startActivity(fragment: Fragment, activity: FragmentActivity?, person: Person, view: View) {
if (activity != null) {
if (activity.checkIsMaterialVersion()) {
val intent = Intent(activity, PersonDetailActivity::class.java)
ViewCompat.getTransitionName(view)?.let {
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, view, it)
intent.putExtra(personId, person)
activity.startActivityFromFragment(fragment, intent, intent_requestCode, options.toBundle())
}
} else {
activity.startActivityForResult<PersonDetailActivity>(intent_requestCode, personId to person)
}
} else {
activity.startActivityForResult<PersonDetailActivity>(intent_requestCode, personId to person)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ constructor(
) : ViewModel() {

private var moviePageLiveData: MutableLiveData<Int> = MutableLiveData()
private val movieListLiveData: LiveData<Resource<List<Movie>>>
val movieListLiveData: LiveData<Resource<List<Movie>>>

private var tvPageLiveData: MutableLiveData<Int> = MutableLiveData()
private val tvListLiveData: LiveData<Resource<List<Tv>>>
val tvListLiveData: LiveData<Resource<List<Tv>>>

private var peoplePageLiveData: MutableLiveData<Int> = MutableLiveData()
private val peopleLiveData: LiveData<Resource<List<Person>>>
val peopleLiveData: LiveData<Resource<List<Person>>>

init {
Timber.d("injection MainActivityViewModel")
Expand All @@ -52,15 +52,12 @@ constructor(
}
}

fun getMovieListObservable() = movieListLiveData
fun getMovieListValues() = getMovieListObservable().value
fun getMovieListValues() = movieListLiveData.value
fun postMoviePage(page: Int) = moviePageLiveData.postValue(page)

fun getTvListObservable() = tvListLiveData
fun getTvListValues() = getTvListObservable().value
fun getTvListValues() = tvListLiveData.value
fun postTvPage(page: Int) = tvPageLiveData.postValue(page)

fun getPeopleObservable() = peopleLiveData
fun getPeopleValues() = getPeopleObservable().value
fun getPeopleValues() = peopleLiveData.value
fun postPeoplePage(page: Int) = peoplePageLiveData.postValue(page)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.skydoves.baserecyclerviewadapter.RecyclerViewPaginator
import com.skydoves.themovies.R
import com.skydoves.themovies.extension.observeLiveData
import com.skydoves.themovies.databinding.MainFragmentMovieBinding
import com.skydoves.themovies.extension.vm
import com.skydoves.themovies.models.Resource
import com.skydoves.themovies.models.Status
import com.skydoves.themovies.models.entity.Movie
import com.skydoves.themovies.view.adapter.MovieListAdapter
import com.skydoves.themovies.view.ui.details.movie.MovieDetailActivity
import com.skydoves.themovies.view.viewholder.MovieListViewHolder
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.main_fragment_movie.*
import org.jetbrains.anko.support.v4.toast
import javax.inject.Inject

/**
Expand All @@ -34,12 +33,14 @@ class MovieListFragment : Fragment(), MovieListViewHolder.Delegate {
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
private val viewModel by lazy { vm(viewModelFactory, MainActivityViewModel::class) }

private val adapter = MovieListAdapter(this)
private lateinit var binding: MainFragmentMovieBinding
private lateinit var paginator: RecyclerViewPaginator

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.main_fragment_movie, container, false)
binding = DataBindingUtil.inflate(inflater, R.layout.main_fragment_movie, container, false)
binding.viewModel = viewModel
binding.lifecycleOwner = this
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -50,11 +51,11 @@ class MovieListFragment : Fragment(), MovieListViewHolder.Delegate {
override fun onAttach(context: Context) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
observeViewModel()
loadMore(page = 1)
}

private fun initializeUI() {
recyclerView.adapter = adapter
recyclerView.adapter = MovieListAdapter(this)
recyclerView.layoutManager = GridLayoutManager(context, 2)
paginator = RecyclerViewPaginator(
recyclerView = recyclerView,
Expand All @@ -65,19 +66,6 @@ class MovieListFragment : Fragment(), MovieListViewHolder.Delegate {
paginator.currentPage = 1
}

private fun observeViewModel() {
observeLiveData(viewModel.getMovieListObservable()) { updateMovieList(it) }
viewModel.postMoviePage(1)
}

private fun updateMovieList(resource: Resource<List<Movie>>) {
when (resource.status) {
Status.LOADING -> Unit
Status.SUCCESS -> adapter.addMovieList(resource)
Status.ERROR -> toast(resource.errorEnvelope?.status_message.toString())
}
}

private fun loadMore(page: Int) {
viewModel.postMoviePage(page)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.skydoves.baserecyclerviewadapter.RecyclerViewPaginator
import com.skydoves.themovies.R
import com.skydoves.themovies.extension.observeLiveData
import com.skydoves.themovies.databinding.MainFragmentStarBinding
import com.skydoves.themovies.extension.vm
import com.skydoves.themovies.models.Resource
import com.skydoves.themovies.models.Status
import com.skydoves.themovies.models.entity.Person
import com.skydoves.themovies.view.adapter.PeopleAdapter
import com.skydoves.themovies.view.ui.details.person.PersonDetailActivity
import com.skydoves.themovies.view.viewholder.PeopleViewHolder
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.main_fragment_movie.*
import org.jetbrains.anko.support.v4.startActivity
import org.jetbrains.anko.support.v4.toast
import javax.inject.Inject

/**
Expand All @@ -35,12 +33,14 @@ class PersonListFragment : Fragment(), PeopleViewHolder.Delegate {
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
private val viewModel by lazy { vm(viewModelFactory, MainActivityViewModel::class) }

private val adapter = PeopleAdapter(this)
private lateinit var binding: MainFragmentStarBinding
private lateinit var paginator: RecyclerViewPaginator

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.main_fragment_star, container, false)
binding = DataBindingUtil.inflate(inflater, R.layout.main_fragment_star, container, false)
binding.viewModel = viewModel
binding.lifecycleOwner = this
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -51,11 +51,11 @@ class PersonListFragment : Fragment(), PeopleViewHolder.Delegate {
override fun onAttach(context: Context) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
observeViewModel()
loadMore(page = 1)
}

private fun initializeUI() {
recyclerView.adapter = adapter
recyclerView.adapter = PeopleAdapter(this)
recyclerView.layoutManager = GridLayoutManager(context, 2)
paginator = RecyclerViewPaginator(
recyclerView = recyclerView,
Expand All @@ -64,26 +64,11 @@ class PersonListFragment : Fragment(), PeopleViewHolder.Delegate {
onLast = { viewModel.getPeopleValues()?.onLastPage!! })
}

private fun observeViewModel() {
observeLiveData(viewModel.getPeopleObservable()) { updatePeople(it) }
viewModel.postPeoplePage(1)
}

private fun updatePeople(resource: Resource<List<Person>>) {
when (resource.status) {
Status.LOADING -> Unit
Status.SUCCESS -> adapter.addPeople(resource)
Status.ERROR -> toast(resource.errorEnvelope?.status_message.toString())
}
}

private fun loadMore(page: Int) {
viewModel.postPeoplePage(page)
}

override fun onItemClick(person: Person, view: View) {
activity?.let {
PersonDetailActivity.startActivity(this, it, person, view)
} ?: startActivity<PersonDetailActivity>("person" to person)
PersonDetailActivity.startActivity(this, activity, person, view)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.skydoves.baserecyclerviewadapter.RecyclerViewPaginator
import com.skydoves.themovies.R
import com.skydoves.themovies.extension.observeLiveData
import com.skydoves.themovies.databinding.MainFragmentTvBinding
import com.skydoves.themovies.extension.vm
import com.skydoves.themovies.models.Resource
import com.skydoves.themovies.models.Status
import com.skydoves.themovies.models.entity.Tv
import com.skydoves.themovies.view.adapter.TvListAdapter
import com.skydoves.themovies.view.ui.details.tv.TvDetailActivity
import com.skydoves.themovies.view.viewholder.TvListViewHolder
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.main_fragment_movie.*
import org.jetbrains.anko.support.v4.toast
import javax.inject.Inject

/**
Expand All @@ -34,12 +33,14 @@ class TvListFragment : Fragment(), TvListViewHolder.Delegate {
@Inject
lateinit var viewModelFactory: ViewModelProvider.Factory
private val viewModel by lazy { vm(viewModelFactory, MainActivityViewModel::class) }

private val adapter = TvListAdapter(this)
private lateinit var binding: MainFragmentTvBinding
private lateinit var paginator: RecyclerViewPaginator

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.main_fragment_tv, container, false)
binding = DataBindingUtil.inflate(inflater, R.layout.main_fragment_tv, container, false)
binding.viewModel = viewModel
binding.lifecycleOwner = this
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -50,11 +51,11 @@ class TvListFragment : Fragment(), TvListViewHolder.Delegate {
override fun onAttach(context: Context) {
AndroidSupportInjection.inject(this)
super.onAttach(context)
observeViewModel()
loadMore(page = 1)
}

private fun initializeUI() {
recyclerView.adapter = adapter
recyclerView.adapter = TvListAdapter(this)
recyclerView.layoutManager = GridLayoutManager(context, 2)
paginator = RecyclerViewPaginator(
recyclerView = recyclerView,
Expand All @@ -65,19 +66,6 @@ class TvListFragment : Fragment(), TvListViewHolder.Delegate {
paginator.currentPage = 1
}

private fun observeViewModel() {
observeLiveData(viewModel.getTvListObservable()) { updateTvList(it) }
viewModel.postTvPage(1)
}

private fun updateTvList(resource: Resource<List<Tv>>) {
when (resource.status) {
Status.LOADING -> Unit
Status.SUCCESS -> adapter.addTvList(resource)
Status.ERROR -> toast(resource.errorEnvelope?.status_message.toString())
}
}

private fun loadMore(page: Int) {
viewModel.postTvPage(page)
}
Expand Down
Loading