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

added databinding and safeargs implementations #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ Also available in Play Store

- :white_check_mark: Kotlin
- :white_check_mark: Live data
- :white_check_mark: DataBinding
- :white_check_mark: Navigation
- :white_check_mark: SafeArgs
- :white_check_mark: ViewModel
- :white_check_mark: Rest with retrofit
- :white_check_mark: Offline Persistence with Room
Expand Down
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

dataBinding {
enabled = true
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class MenuAdapter(
itemView.setOnClickListener {
it.findNavController().navigate(R.id.action_navigation_home_to_navigation_pokedex)
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import android.graphics.PorterDuffColorFilter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import dev.marcosfarias.pokedex.R
import dev.marcosfarias.pokedex.model.Pokemon
import dev.marcosfarias.pokedex.ui.pokedex.PokedexFragmentDirections
import dev.marcosfarias.pokedex.utils.PokemonColorUtil
import kotlinx.android.synthetic.main.item_pokemon.view.*

Expand All @@ -26,7 +26,7 @@ class PokemonAdapter(
itemView.textViewID.text = item.id

val color = PokemonColorUtil(itemView.context).getPokemonColor(item.typeofpokemon)
itemView.relativeLayoutBackground.background.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP )
itemView.relativeLayoutBackground.background.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)

item.typeofpokemon?.elementAtOrNull(0).let {
itemView.textViewType3.text = it
Expand Down Expand Up @@ -55,8 +55,7 @@ class PokemonAdapter(
.into(itemView.imageView)

itemView.setOnClickListener {
var bundle = bundleOf("id" to item.id)
it.findNavController().navigate(R.id.action_navigation_pokedex_to_navigation_dashboard, bundle)
it.findNavController().navigate(PokedexFragmentDirections.actionNavigationPokedexToNavigationDashboard(item.id!!))
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import dev.marcosfarias.pokedex.model.Pokemon
interface PokemonDAO {

@Query("SELECT * FROM pokemon WHERE id = :id")
fun getById(id: String?): LiveData<List<Pokemon?>?>
fun getById(id: String?): LiveData<Pokemon?>

@Query("SELECT * FROM pokemon")
fun all(): LiveData<List<Pokemon>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,91 +6,84 @@ 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.Observer
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.Glide
import dev.marcosfarias.pokedex.R
import dev.marcosfarias.pokedex.adapter.ViewPagerAdapter
import dev.marcosfarias.pokedex.databinding.FragmentDashboardBinding
import dev.marcosfarias.pokedex.ui.dashboard.about.AboutFragment
import dev.marcosfarias.pokedex.ui.dashboard.evolution.EvolutionFragment
import dev.marcosfarias.pokedex.ui.dashboard.moves.MovesFragment
import dev.marcosfarias.pokedex.ui.dashboard.stats.StatsFragment
import dev.marcosfarias.pokedex.utils.PokemonColorUtil
import kotlinx.android.synthetic.main.fragment_dashboard.view.*

class DashboardFragment : Fragment() {

private lateinit var dashboardViewModel: DashboardViewModel
private lateinit var binding: FragmentDashboardBinding

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val id = DashboardFragmentArgs.fromBundle(arguments!!).pokemonId

dashboardViewModel = ViewModelProviders.of(this, DashboardViewModelFactory(id)).get(DashboardViewModel::class.java)
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_dashboard, container, false)
binding.vm = dashboardViewModel
binding.lifecycleOwner = viewLifecycleOwner

dashboardViewModel.pokemon.observe(this, Observer { pokemon ->
val color = PokemonColorUtil(requireContext()).getPokemonColor(pokemon?.typeofpokemon)
val colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)

binding.appBar.background.colorFilter = colorFilter
binding.toolbarLayout.contentScrim?.colorFilter = colorFilter
activity?.window?.statusBarColor = color

pokemon?.typeofpokemon?.elementAtOrNull(0).let {
binding.textViewType3.text = it
if (it == null) {
binding.textViewType3.visibility = View.GONE
}
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ) : View? {
dashboardViewModel = ViewModelProviders.of(this).get(DashboardViewModel::class.java)

val root = inflater.inflate(R.layout.fragment_dashboard, container, false)

arguments?.getString("id").let {

dashboardViewModel.getPokemonById(it).observe(this, Observer { list ->
list?.get(0).let { pokemon ->
root.textViewID.text = pokemon?.id
root.textViewName.text = pokemon?.name

val color = PokemonColorUtil(root.context).getPokemonColor(pokemon?.typeofpokemon)
root.app_bar.background.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP )
root.toolbar_layout.contentScrim?.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP )
activity?.window?.statusBarColor = PokemonColorUtil(root.context).getPokemonColor(pokemon?.typeofpokemon)

pokemon?.typeofpokemon?.elementAtOrNull(0).let {
root.textViewType3.text = it
if (it == null) {
root.textViewType3.visibility = View.GONE
}
}

pokemon?.typeofpokemon?.elementAtOrNull(1).let {
root.textViewType2.text = it
if (it == null) {
root.textViewType2.visibility = View.GONE
}
}

pokemon?.typeofpokemon?.elementAtOrNull(2).let {
root.textViewType1.text = it
if (it == null) {
root.textViewType1.visibility = View.GONE
}
}

Glide.with(root.context)
.load(pokemon?.imageurl)
.placeholder(android.R.color.transparent)
.into(root.imageView)

val pager = root.viewPager
val tabs = root.tabs


val adapter = ViewPagerAdapter(fragmentManager!!)
adapter.addFragment(AboutFragment.newInstance(pokemon?.id), getString(R.string.dashboard_tab_1))
adapter.addFragment(StatsFragment.newInstance(pokemon?.id), getString(R.string.dashboard_tab_2))
adapter.addFragment(EvolutionFragment(), getString(R.string.dashboard_tab_3))
adapter.addFragment(MovesFragment(), getString(R.string.dashboard_tab_4))

pager.adapter = adapter

tabs.setupWithViewPager(pager)
pokemon?.typeofpokemon?.elementAtOrNull(1).let {
binding.textViewType2.text = it
if (it == null) {
binding.textViewType2.visibility = View.GONE
}
}

pokemon?.typeofpokemon?.elementAtOrNull(2).let {
binding.textViewType1.text = it
if (it == null) {
binding.textViewType1.visibility = View.GONE
}
}

})
Glide.with(requireContext())
.load(pokemon?.imageurl)
.placeholder(android.R.color.transparent)
.into(binding.imageView)

val pager = binding.viewPager
val tabs = binding.tabs

}
pager.adapter = ViewPagerAdapter(fragmentManager!!).apply {
addFragment(AboutFragment.newInstance(id), getString(R.string.dashboard_tab_1))
addFragment(StatsFragment.newInstance(id), getString(R.string.dashboard_tab_2))
addFragment(EvolutionFragment(), getString(R.string.dashboard_tab_3))
addFragment(MovesFragment(), getString(R.string.dashboard_tab_4))
}

tabs.setupWithViewPager(pager)
})

return root
return binding.root
}



}
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package dev.marcosfarias.pokedex.ui.dashboard

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import dev.marcosfarias.pokedex.App
import dev.marcosfarias.pokedex.database.dao.PokemonDAO
import dev.marcosfarias.pokedex.model.Pokemon
import dev.marcosfarias.pokedex.repository.APIService
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class DashboardViewModel : ViewModel() {
class DashboardViewModel(pokemonId: String) : ViewModel() {

private val pokemonDAO : PokemonDAO = App.database!!.pokemonDAO()
private val pokemonDAO: PokemonDAO = App.database!!.pokemonDAO()
var pokemon: LiveData<Pokemon?>

fun getPokemonById(id: String?): LiveData<List<Pokemon?>?> {
return pokemonDAO.getById(id)
init {
pokemon = pokemonDAO.getById(pokemonId)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.marcosfarias.pokedex.ui.dashboard

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class DashboardViewModelFactory(private val id: String) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(DashboardViewModel::class.java)) {
return DashboardViewModel(id) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,35 @@ 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.Observer
import androidx.lifecycle.ViewModelProviders
import dev.marcosfarias.pokedex.R
import dev.marcosfarias.pokedex.databinding.FragmentAboutBinding
import dev.marcosfarias.pokedex.ui.dashboard.DashboardViewModel
import kotlinx.android.synthetic.main.fragment_about.view.*
import dev.marcosfarias.pokedex.ui.dashboard.DashboardViewModelFactory

class AboutFragment : Fragment() {

private lateinit var dashboardViewModel: DashboardViewModel
private lateinit var binding: FragmentAboutBinding

companion object {
@JvmStatic
fun newInstance(id: String?) = AboutFragment().apply {
arguments = Bundle().apply {
putString("id", id)
}
fun newInstance(id: String) = AboutFragment().apply {
arguments = AboutFragmentArgs.Builder(id).build().toBundle()
}
}

private lateinit var dashboardViewModel: DashboardViewModel

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
dashboardViewModel = ViewModelProviders.of(this).get(DashboardViewModel::class.java)

val root = inflater.inflate(R.layout.fragment_about, container, false)

arguments?.getString("id").let {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val id = AboutFragmentArgs.fromBundle(arguments!!).pokemonId
dashboardViewModel = ViewModelProviders.of(this, DashboardViewModelFactory(id)).get(DashboardViewModel::class.java)
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_about, container, false)

dashboardViewModel.getPokemonById(it).observe(this, Observer { list ->
list?.get(0).let { pokemon ->
binding.vm = dashboardViewModel
binding.lifecycleOwner = viewLifecycleOwner

root.textViewDescription.text = pokemon?.xdescription
root.textViewHeight.text = pokemon?.height
root.textViewWeight.text = pokemon?.weight
root.textViewEggCycle.text = pokemon?.cycles
root.textViewEggGroups.text = pokemon?.egg_groups
root.textViewBaseEXP.text = pokemon?.base_exp
}
})
}


return root
return binding.root
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,9 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import dev.marcosfarias.pokedex.R
import dev.marcosfarias.pokedex.ui.dashboard.DashboardViewModel

class EvolutionFragment : Fragment() {

private lateinit var dashboardViewModel: DashboardViewModel

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ) : View? {
dashboardViewModel = ViewModelProviders.of(this).get(DashboardViewModel::class.java)

val root = inflater.inflate(R.layout.fragment_evolution, container, false)


return root
}



override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_evolution, container, false)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,5 @@ import dev.marcosfarias.pokedex.R

class MovesFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ) : View? {
val root = inflater.inflate(R.layout.fragment_moves, container, false)
return root
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = inflater.inflate(R.layout.fragment_moves, container, false)
}
Loading