Skip to content

Commit

Permalink
Add Pokemon Info - Evolution
Browse files Browse the repository at this point in the history
  • Loading branch information
naufalprakoso committed May 14, 2020
1 parent 0108d4c commit a4fe667
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Also available in Play Store
- [x] Pokemon Info
- [x] Pokemon Info - About
- [x] Pokemon Info - Base Stats
- [ ] Pokemon Info - Evolution
- [x] Pokemon Info - Evolution
- [x] News Detail

## Thanks
Expand All @@ -105,8 +105,8 @@ Also available in Play Store

## Contributors

| [<img src="https://avatars3.githubusercontent.com/u/12054216?s=115&v=4" width="48"><br><sub>@zsmb13</sub>](https://github.com/zsmb13) | [<img src="https://avatars0.githubusercontent.com/u/8435541?s=115&v=4" width="48"><br><sub>@aesean</sub>](https://github.com/aesean) | [<img src="https://avatars1.githubusercontent.com/u/988405?s=115&v=4" width="48"><br><sub>@STFBEE</sub>](https://github.com/STFBEE) | [<img src="https://avatars1.githubusercontent.com/u/25616312?s=115&v=4" width="48"><br><sub>@CorneilleEdi</sub>](https://github.com/CorneilleEdi) | [<img src="https://user-images.githubusercontent.com/95717/60592969-5da81680-9d67-11e9-92a3-8664ee0e2eda.png" width="48"><br><sub>You</sub>](https://github.com/mrcsxsiq/Kotlin-Pokedex/pulls) |
| :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars3.githubusercontent.com/u/12054216?s=115&v=4" width="48"><br><sub>@zsmb13</sub>](https://github.com/zsmb13) | [<img src="https://avatars0.githubusercontent.com/u/8435541?s=115&v=4" width="48"><br><sub>@aesean</sub>](https://github.com/aesean) | [<img src="https://avatars1.githubusercontent.com/u/988405?s=115&v=4" width="48"><br><sub>@STFBEE</sub>](https://github.com/STFBEE) | [<img src="https://avatars1.githubusercontent.com/u/25616312?s=115&v=4" width="48"><br><sub>@CorneilleEdi</sub>](https://github.com/CorneilleEdi) | [<img src="https://avatars2.githubusercontent.com/u/15768474?s=115&v=4" width="48"><br><sub>@naufalprakoso</sub>](https://github.com/naufalprakoso) | [<img src="https://user-images.githubusercontent.com/95717/60592969-5da81680-9d67-11e9-92a3-8664ee0e2eda.png" width="48"><br><sub>You</sub>](https://github.com/mrcsxsiq/Kotlin-Pokedex/pulls) |
| :---: | :---: | :---: | :---: | :---: | :---: |

## Author

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ interface PokemonDAO {
@Query("SELECT * FROM pokemon WHERE id = :id")
fun getById(id: String?): LiveData<Pokemon>

@Query("SELECT * FROM pokemon WHERE id IN(:evolutionIds)")
fun getEvolutionsByIds(evolutionIds: List<String>): LiveData<List<Pokemon>>

@Query("SELECT * FROM pokemon")
fun all(): LiveData<List<Pokemon>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ class DashboardViewModel(private val pokemonDAO: PokemonDAO) : ViewModel() {
fun getPokemonById(id: String?): LiveData<Pokemon> {
return pokemonDAO.getById(id)
}

fun getPokemonEvolutionsByIds(ids: List<String>): LiveData<List<Pokemon>> {
return pokemonDAO.getEvolutionsByIds(ids)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ViewPagerAdapter(
),
Page(
context.getString(R.string.dashboard_tab_3),
{ EvolutionFragment() }
{ EvolutionFragment.newInstance(pokemonId) }
),
Page(
context.getString(R.string.dashboard_tab_4),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package dev.marcosfarias.pokedex.ui.dashboard.evolution

import android.content.Context
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
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.utils.PokemonColorUtil
import kotlinx.android.synthetic.main.item_pokemon.view.*

class EvolutionAdapter(
private val context: Context
) : RecyclerView.Adapter<EvolutionAdapter.ViewHolder>() {

private val list = arrayListOf<Pokemon>()

fun setList(list: List<Pokemon>) {
this.list.clear()
this.list.addAll(list)
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindView(item: Pokemon) {
itemView.textViewName.text = item.name
itemView.textViewID.text = item.id

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

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

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

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

Glide.with(itemView.context)
.load(item.imageurl)
.placeholder(android.R.color.transparent)
.into(itemView.imageView)
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_pokemon, parent, false)
return ViewHolder(view)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = list[position]
holder.bindView(item)
}

override fun getItemCount(): Int {
return list.size
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import dev.marcosfarias.pokedex.R
import dev.marcosfarias.pokedex.model.Pokemon
import dev.marcosfarias.pokedex.ui.dashboard.DashboardViewModel
import kotlinx.android.synthetic.main.fragment_evolution.*
import org.koin.androidx.viewmodel.ext.android.viewModel

class EvolutionFragment : Fragment() {

companion object {
@JvmStatic
fun newInstance(id: String?) = EvolutionFragment().apply {
arguments = Bundle().apply {
putString("id", id)
}
}
}

private val dashboardViewModel: DashboardViewModel by viewModel()

override fun onCreateView(
Expand All @@ -20,4 +33,29 @@ class EvolutionFragment : Fragment() {
): View? {
return inflater.inflate(R.layout.fragment_evolution, container, false)
}

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

val id = checkNotNull(arguments?.getString("id"))
val recyclerView = recyclerViewEvolvingPokemon
val layoutManager = LinearLayoutManager(context)
recyclerView.layoutManager = layoutManager
val adapter = EvolutionAdapter(view.context)
recyclerView.adapter = adapter

dashboardViewModel.getPokemonById(id).observe(viewLifecycleOwner, Observer { pokemonValue ->
pokemonValue?.let { pokemon ->
val evolutions = pokemon.evolutions ?: emptyList()
dashboardViewModel.getPokemonEvolutionsByIds(evolutions).observe(viewLifecycleOwner, Observer {
val pokemons: List<Pokemon> = it
adapter.setList(pokemons)
adapter.notifyDataSetChanged()

if (pokemons.isEmpty())
textNonEvolving.visibility = View.VISIBLE
})
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import com.google.gson.reflect.TypeToken
import java.lang.reflect.Type

class ListStringConverter {
val gson = Gson()
val type: Type = object : TypeToken<List<String?>?>() {}.type
private val gson = Gson()
private val type: Type = object : TypeToken<List<String>>() {}.type

@TypeConverter
fun fromString(json: String?): List<String> {
Expand Down
18 changes: 13 additions & 5 deletions app/src/main/res/layout/fragment_evolution.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textNonEvolving"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="@string/in_progress"
android:text="@string/non_evolving_pokemon"
android:textAlignment="center"
android:textSize="20sp" />
</LinearLayout>
android:textSize="20sp"
android:visibility="gone" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewEvolvingPokemon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin" />
</RelativeLayout>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<string name="app_name">Pokedex</string>
<string name="title_home">Home</string>
<string name="title_dashboard">Dashboard</string>
<string name="in_progress">In progress...</string>
<string name="in_progress">In progress…</string>
<string name="non_evolving_pokemon">Non Evolving Pokemon</string>
<string name="menu_item_1">Pokedex</string>
<string name="menu_item_2">Moves</string>
<string name="menu_item_3">Abilities</string>
Expand Down

0 comments on commit a4fe667

Please sign in to comment.