Skip to content
Permalink
Browse files

Clean up null handling with ?.let calls

  • Loading branch information
zsmb13 committed Jan 7, 2020
1 parent 595f1e4 commit af08dabe618dee42db5862f4388cb3bc9d3cf747
@@ -36,42 +36,42 @@ class DashboardFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)
val id = checkNotNull(arguments?.getString("id"))
dashboardViewModel.getPokemonById(id).observe(viewLifecycleOwner, Observer { pokemonValue ->
pokemonValue.let { pokemon ->
textViewID.text = pokemon?.id
textViewName.text = pokemon?.name
pokemonValue?.let { pokemon ->
textViewID.text = pokemon.id
textViewName.text = pokemon.name

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

pokemon?.typeofpokemon?.getOrNull(0).let { firstType ->
pokemon.typeofpokemon?.getOrNull(0).let { firstType ->
textViewType3.text = firstType
textViewType3.isVisible = firstType != null
}

pokemon?.typeofpokemon?.getOrNull(1).let { secondType ->
pokemon.typeofpokemon?.getOrNull(1).let { secondType ->
textViewType2.text = secondType
textViewType2.isVisible = secondType != null
}

pokemon?.typeofpokemon?.getOrNull(2).let { thirdType ->
pokemon.typeofpokemon?.getOrNull(2).let { thirdType ->
textViewType1.text = thirdType
textViewType1.isVisible = thirdType != null
}

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

val pager = viewPager
val tabs = tabs
pager.adapter = ViewPagerAdapter(fragmentManager!!, context!!, pokemon?.id!!)
pager.adapter = ViewPagerAdapter(fragmentManager!!, context!!, pokemon.id!!)
tabs.setupWithViewPager(pager)
}
})
@@ -41,13 +41,13 @@ class AboutFragment : Fragment() {
super.onViewCreated(view, savedInstanceState)
val id = checkNotNull(arguments?.getString("id"))
dashboardViewModel.getPokemonById(id).observe(viewLifecycleOwner, Observer { pokemonValue ->
pokemonValue.let { pokemon ->
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
pokemonValue?.let { pokemon ->
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
}
})
}
@@ -42,24 +42,24 @@ class StatsFragment : Fragment() {

val id = checkNotNull(arguments?.getString("id"))
dashboardViewModel.getPokemonById(id).observe(viewLifecycleOwner, Observer { pokemonValue ->
pokemonValue.let { pokemon ->
textViewTypeDefenses.text = pokemon?.ydescription
pokemonValue?.let { pokemon ->
textViewTypeDefenses.text = pokemon.ydescription

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()
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()

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
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
}
})
}

0 comments on commit af08dab

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