-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,577 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package fr.delcey.mvctomvvm.ui | ||
|
||
import androidx.annotation.ColorRes | ||
import fr.delcey.mvctomvvm.R | ||
import fr.delcey.mvctomvvm.data.pokemon.PokemonResponse | ||
import fr.delcey.mvctomvvm.data.pokemon.TypesItem | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class PokemonUtils @Inject constructor() { | ||
fun getType(types: List<TypesItem?>?, typeNumber: Int): String? = types?.firstOrNull { | ||
it?.slot == typeNumber | ||
}?.type?.name | ||
|
||
@ColorRes | ||
fun getTypeColorRes(type: String?): Int? = when (type) { | ||
"normal" -> R.color.type_normal | ||
"fire" -> R.color.type_fire | ||
"water" -> R.color.type_water | ||
"electric" -> R.color.type_electric | ||
"grass" -> R.color.type_grass | ||
"ice" -> R.color.type_ice | ||
"fighting" -> R.color.type_fighting | ||
"poison" -> R.color.type_poison | ||
"ground" -> R.color.type_ground | ||
"flying" -> R.color.type_flying | ||
"psychic" -> R.color.type_psychic | ||
"bug" -> R.color.type_bug | ||
"rock" -> R.color.type_rock | ||
"ghost" -> R.color.type_ghost | ||
"dragon" -> R.color.type_dragon | ||
"dark" -> R.color.type_dark | ||
"steel" -> R.color.type_steel | ||
"fairy" -> R.color.type_fairy | ||
else -> null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
app/src/main/java/fr/delcey/mvctomvvm/ui/detail/PokemonDetailViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package fr.delcey.mvctomvvm.ui.detail | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MediatorLiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.Transformations | ||
import androidx.lifecycle.ViewModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import fr.delcey.mvctomvvm.data.PokemonRepository | ||
import fr.delcey.mvctomvvm.ui.PokemonUtils | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class PokemonDetailViewModel @Inject constructor( | ||
pokemonRepository: PokemonRepository, | ||
pokemonUtils: PokemonUtils | ||
) : ViewModel() { | ||
|
||
private val pokemonIdMutableLiveData = MutableLiveData<String>() | ||
private val pokemonResponseLiveData = Transformations.switchMap(pokemonIdMutableLiveData) { id -> | ||
pokemonRepository.getPokemonByIdLiveData(id) | ||
} | ||
|
||
private val pokemonDetailViewStateMediatorLiveData = MediatorLiveData<PokemonDetailViewState>() | ||
|
||
init { | ||
pokemonDetailViewStateMediatorLiveData.addSource(pokemonResponseLiveData) { pokemonResponse -> | ||
val number = pokemonResponse.id?.toString() ?: return@addSource | ||
|
||
val type1: String = pokemonUtils.getType(pokemonResponse.types, 1)?: return@addSource | ||
val type2: String? = pokemonUtils.getType(pokemonResponse.types, 2) | ||
val type2Color: Int? = pokemonUtils.getTypeColorRes(type2) | ||
|
||
pokemonDetailViewStateMediatorLiveData.value = PokemonDetailViewState( | ||
number = "#$number", | ||
name = pokemonResponse.name?.capitalize() ?: return@addSource, | ||
imageUrl = pokemonResponse.sprites?.frontDefault ?: return@addSource, | ||
type1Name = type1, | ||
type1Color = pokemonUtils.getTypeColorRes(type1) ?: return@addSource, | ||
type2Name = type2, | ||
type2Color = type2Color, | ||
isType2Visible = type2 != null && type2Color != null | ||
) | ||
} | ||
} | ||
|
||
fun getPokemonDetailViewStateLiveData(): LiveData<PokemonDetailViewState> = pokemonDetailViewStateMediatorLiveData | ||
|
||
fun setPokemonId(pokemonId: String) { | ||
pokemonIdMutableLiveData.value = pokemonId | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
app/src/main/java/fr/delcey/mvctomvvm/ui/detail/PokemonDetailViewState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package fr.delcey.mvctomvvm.ui.detail | ||
|
||
import androidx.annotation.ColorRes | ||
|
||
data class PokemonDetailViewState( | ||
val number: String, | ||
val name: String, | ||
val imageUrl: String, | ||
val type1Name: String, | ||
@ColorRes | ||
val type1Color: Int, | ||
val type2Name: String?, | ||
@ColorRes | ||
val type2Color: Int?, | ||
val isType2Visible: Boolean | ||
) |
Oops, something went wrong.