Skip to content
Permalink
Browse files

Move View initialization code to onViewCreated

  • Loading branch information
zsmb13 committed Jan 7, 2020
1 parent 95ebf91 commit 7385d6a24420f33a13b03f2afd5bb2db5f85a95e
@@ -16,7 +16,7 @@ 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.*
import kotlinx.android.synthetic.main.fragment_dashboard.*

class DashboardFragment : Fragment() {

@@ -32,51 +32,54 @@ class DashboardFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
return inflater.inflate(R.layout.fragment_dashboard, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
arguments?.getString("id").let {
dashboardViewModel.getPokemonById(it).observe(viewLifecycleOwner, Observer { list ->
list?.get(0).let { pokemon ->
root.textViewID.text = pokemon?.id
root.textViewName.text = pokemon?.name
textViewID.text = pokemon?.id
textViewName.text = pokemon?.name

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

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

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

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

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

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


val adapter = ViewPagerAdapter(fragmentManager!!)
@@ -101,10 +104,6 @@ class DashboardFragment : Fragment() {


}


return root
}


}
@@ -9,7 +9,7 @@ import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import dev.marcosfarias.pokedex.R
import dev.marcosfarias.pokedex.ui.dashboard.DashboardViewModel
import kotlinx.android.synthetic.main.fragment_about.view.*
import kotlinx.android.synthetic.main.fragment_about.*

class AboutFragment : Fragment() {

@@ -34,26 +34,22 @@ class AboutFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_about, container, false)
return inflater.inflate(R.layout.fragment_about, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
arguments?.getString("id").let {

dashboardViewModel.getPokemonById(it).observe(viewLifecycleOwner, Observer { list ->
list?.get(0).let { pokemon ->

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
textViewDescription.text = pokemon?.xdescription
textViewHeight.text = pokemon?.height
textViewWeight.text = pokemon?.weight
textViewEggCycle.text = pokemon?.cycles
textViewEggGroups.text = pokemon?.egg_groups
textViewBaseEXP.text = pokemon?.base_exp
}
})
}


return root
}


}
@@ -23,10 +23,7 @@ class EvolutionFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_evolution, container, false)


return root
return inflater.inflate(R.layout.fragment_evolution, container, false)
}


@@ -14,8 +14,7 @@ class MovesFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_moves, container, false)
return root
return inflater.inflate(R.layout.fragment_moves, container, false)
}

}
@@ -9,7 +9,7 @@ import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import dev.marcosfarias.pokedex.R
import dev.marcosfarias.pokedex.ui.dashboard.DashboardViewModel
import kotlinx.android.synthetic.main.fragment_stats.view.*
import kotlinx.android.synthetic.main.fragment_stats.*

class StatsFragment : Fragment() {

@@ -34,36 +34,35 @@ class StatsFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_stats, container, false)
return inflater.inflate(R.layout.fragment_stats, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

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

dashboardViewModel.getPokemonById(it).observe(viewLifecycleOwner, Observer { list ->
list?.get(0).let { pokemon ->
textViewTypeDefenses.text = pokemon?.ydescription

root.textViewTypeDefenses.text = pokemon?.ydescription

root.textViewHP.text = pokemon?.hp.toString()
root.textViewAttack.text = pokemon?.attack.toString()
root.textViewDefense.text = pokemon?.defense.toString()
root.textViewSpAtk.text = pokemon?.special_attack.toString()
root.textViewSpDef.text = pokemon?.special_defense.toString()
root.textViewSpeed.text = pokemon?.speed.toString()
root.textViewTotal.text = pokemon?.total.toString()
textViewHP.text = pokemon?.hp.toString()
textViewAttack.text = pokemon?.attack.toString()
textViewDefense.text = pokemon?.defense.toString()
textViewSpAtk.text = pokemon?.special_attack.toString()
textViewSpDef.text = pokemon?.special_defense.toString()
textViewSpeed.text = pokemon?.speed.toString()
textViewTotal.text = pokemon?.total.toString()

root.progressBarHP.progress = pokemon?.hp ?: 0
root.progressBarAttack.progress = pokemon?.attack ?: 0
root.progressBarDefense.progress = pokemon?.defense ?: 0
root.progressBarSpAtk.progress = pokemon?.special_attack ?: 0
root.progressBarSpDef.progress = pokemon?.special_defense ?: 0
root.progressBarSpeed.progress = pokemon?.speed ?: 0
root.progressBarTotal.progress = pokemon?.total ?: 0
progressBarHP.progress = pokemon?.hp ?: 0
progressBarAttack.progress = pokemon?.attack ?: 0
progressBarDefense.progress = pokemon?.defense ?: 0
progressBarSpAtk.progress = pokemon?.special_attack ?: 0
progressBarSpDef.progress = pokemon?.special_defense ?: 0
progressBarSpeed.progress = pokemon?.speed ?: 0
progressBarTotal.progress = pokemon?.total ?: 0
}
})
}

return root
}


}
@@ -10,7 +10,7 @@ import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dev.marcosfarias.pokedex.R
import dev.marcosfarias.pokedex.model.Generation
import kotlinx.android.synthetic.main.fragment_generation.view.*
import kotlinx.android.synthetic.main.fragment_generation.*

class GenerationFragment : BottomSheetDialogFragment() {

@@ -26,16 +26,20 @@ class GenerationFragment : BottomSheetDialogFragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_generation, container, false)
val recyclerView = root.recyclerView
return inflater.inflate(R.layout.fragment_generation, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val recyclerView = recyclerView
val layoutManager = GridLayoutManager(context, 2)
recyclerView.layoutManager = layoutManager

generationViewModel.getListGeneration().observe(viewLifecycleOwner, Observer {
val pokemons: List<Generation> = it
recyclerView.adapter = GenerationAdapter(pokemons, root.context)
recyclerView.adapter = GenerationAdapter(pokemons, view.context)
})
return root
}

}
@@ -13,7 +13,7 @@ import dev.marcosfarias.pokedex.R
import dev.marcosfarias.pokedex.model.Menu
import dev.marcosfarias.pokedex.model.News
import dev.marcosfarias.pokedex.utils.PokemonColorUtil
import kotlinx.android.synthetic.main.fragment_home.view.*
import kotlinx.android.synthetic.main.fragment_home.*

class HomeFragment : Fragment() {

@@ -29,12 +29,15 @@ class HomeFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
}

val root = inflater.inflate(R.layout.fragment_home, container, false)
activity?.window?.statusBarColor = PokemonColorUtil(root.context).covertColor(R.color.red)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.window?.statusBarColor = PokemonColorUtil(view.context).covertColor(R.color.red)

val recyclerViewMenu = root.recyclerViewMenu
val recyclerViewNews = root.recyclerViewNews
val recyclerViewMenu = recyclerViewMenu
val recyclerViewNews = recyclerViewNews

recyclerViewMenu.layoutManager = GridLayoutManager(context, 2)

@@ -48,15 +51,13 @@ class HomeFragment : Fragment() {

homeViewModel.getListMenu().observe(viewLifecycleOwner, Observer {
val items: List<Menu> = it
recyclerViewMenu.adapter = MenuAdapter(items, root.context)
recyclerViewMenu.adapter = MenuAdapter(items, view.context)
})

homeViewModel.getListNews().observe(viewLifecycleOwner, Observer {
val items: List<News> = it
recyclerViewNews.adapter = NewsAdapter(items, root.context)
recyclerViewNews.adapter = NewsAdapter(items, view.context)
})
return root
}


}
@@ -16,12 +16,12 @@ class NewsDetailFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_news_detail, container, false)
}

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

activity?.window?.statusBarColor = PokemonColorUtil(root.context).covertColor(R.color.white)

return root
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.window?.statusBarColor = PokemonColorUtil(view.context).covertColor(R.color.white)
}

}
@@ -14,7 +14,7 @@ import dev.marcosfarias.pokedex.model.Pokemon
import dev.marcosfarias.pokedex.ui.generation.GenerationFragment
import dev.marcosfarias.pokedex.ui.search.SearchFragment
import dev.marcosfarias.pokedex.utils.PokemonColorUtil
import kotlinx.android.synthetic.main.fragment_pokedex.view.*
import kotlinx.android.synthetic.main.fragment_pokedex.*

class PokedexFragment : Fragment() {

@@ -30,24 +30,27 @@ class PokedexFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_pokedex, container, false)
return inflater.inflate(R.layout.fragment_pokedex, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.window?.statusBarColor =
PokemonColorUtil(root.context).covertColor(R.color.background)
PokemonColorUtil(view.context).covertColor(R.color.background)

val progressBar = root.progressBar
val recyclerView = root.recyclerView
val progressBar = progressBar
val recyclerView = recyclerView
val layoutManager = GridLayoutManager(context, 2)
recyclerView.layoutManager = layoutManager

pokedexViewModel.getListPokemon().observe(viewLifecycleOwner, Observer {
val pokemons: List<Pokemon> = it
recyclerView.adapter = PokemonAdapter(pokemons, root.context)
recyclerView.adapter = PokemonAdapter(pokemons, view.context)
if (pokemons.isNotEmpty())
progressBar.visibility = View.GONE
})

val speedDialView = root.speedDial
val speedDialView = speedDial
speedDialView.inflate(R.menu.menu_pokedex)
speedDialView.setOnActionSelectedListener(SpeedDialView.OnActionSelectedListener { actionItem ->
when (actionItem.id) {
@@ -67,8 +70,6 @@ class PokedexFragment : Fragment() {
}
}
})

return root
}

private fun showAllGen() {
@@ -14,10 +14,7 @@ class SearchFragment : BottomSheetDialogFragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

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

return root
return inflater.inflate(R.layout.fragment_search, container, false)
}

}

0 comments on commit 7385d6a

Please sign in to comment.
You can’t perform that action at this time.