Skip to content
Merged
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 app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
alias(libs.plugins.detekt)
alias(libs.plugins.ksp)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.junit5)
alias(libs.plugins.jetbrains.kotlin.serialization)
}

Expand Down Expand Up @@ -115,6 +116,7 @@ dependencies {
testImplementation(libs.androidx.arch.core)

androidTestImplementation(composeBom)
androidTestImplementation(libs.androidx.test.rules)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.arch.core)
androidTestImplementation(libs.androidx.compose.ui.test)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class RemoteDataSource(
suspend fun getPokemonInfoRemoteBySpeciesName(name: String): ApiResponse<PokemonInfoRemote> =
withContext(dispatcher) {
return@withContext try {
val pokemonWithSpeciesAndTypesRemote = getSpeciesWithPokemonTypeAndVarieties(name)
val initialPokemonInfo = getSpeciesWithPokemonTypeAndVarieties(name)
ApiResponse.success(
getPokemonInfoRemoteByName(pokemonWithSpeciesAndTypesRemote)
fetchAndProcessEvolutionChain(initialPokemonInfo)
)
} catch (exception: RemoteDataSourceException) {
ApiResponse.error(
Expand All @@ -53,9 +53,9 @@ class RemoteDataSource(
suspend fun getPokemonInfoRemoteByName(name: String): ApiResponse<PokemonInfoRemote> =
withContext(dispatcher) {
return@withContext try {
val pokemonWithSpeciesAndTypesRemote = getPokemonWithSpeciesAndType(name)
val initialPokemonInfo = getPokemonWithSpeciesAndType(name)
ApiResponse.success(
getPokemonInfoRemoteByName(pokemonWithSpeciesAndTypesRemote)
fetchAndProcessEvolutionChain(initialPokemonInfo)
)
} catch (exception: RemoteDataSourceException) {
ApiResponse.error(
Expand All @@ -68,9 +68,9 @@ class RemoteDataSource(
suspend fun getPokemonInfoRemoteById(id: Int): ApiResponse<PokemonInfoRemote> =
withContext(dispatcher) {
return@withContext try {
val pokemonWithSpeciesAndTypesRemote = getPokemonWithSpeciesAndType(id)
val initialPokemonInfo = getPokemonWithSpeciesAndType(id)
ApiResponse.success(
getPokemonInfoRemoteByName(pokemonWithSpeciesAndTypesRemote)
fetchAndProcessEvolutionChain(initialPokemonInfo)
)
} catch (exception: RemoteDataSourceException) {
ApiResponse.error(ERROR_POKEMON_ID_NOT_FOUND.format(id), exception)
Expand Down Expand Up @@ -175,7 +175,7 @@ class RemoteDataSource(
return PokemonInfoRemote(pokemon, species, types)
}

private suspend fun getPokemonInfoRemoteByName(
private suspend fun fetchAndProcessEvolutionChain(
pokemonWithSpeciesAndTypesRemote: PokemonInfoRemote
): PokemonInfoRemote {
var chain: EvolutionChainRemote? = null
Expand All @@ -186,7 +186,32 @@ class RemoteDataSource(
)
}.getSuccessOrThrow()
}
return pokemonWithSpeciesAndTypesRemote.copy(evolutionChain = processChain(chain = chain?.chain))
return pokemonWithSpeciesAndTypesRemote.copy(evolutionChain = processEvolutionChain(chain = chain?.chain))
}

private fun processChain(
currentChainRemote: ChainRemote?,
currentRank: Int,
evolutionMap: MutableMap<Int, MutableList<ChainLink>>
) {
if (currentChainRemote == null) return

val chainLink = ChainLink(
currentChainRemote.species.name,
getUrlPath(currentChainRemote.species.url),
currentChainRemote.isBaby
)
evolutionMap.computeIfAbsent(currentRank) { mutableListOf() }.add(chainLink)

for (nextEvolution in currentChainRemote.evolvesTo) {
processChain(nextEvolution, currentRank + 1, evolutionMap)
}
}

private fun processEvolutionChain(chain: ChainRemote?): Map<Int, List<ChainLink>> {
val evolutionMap = mutableMapOf<Int, MutableList<ChainLink>>()
processChain(chain, 0, evolutionMap)
return evolutionMap.mapValues { it.value.toList() }
}

companion object {
Expand All @@ -197,22 +222,5 @@ class RemoteDataSource(
const val ERROR_GENERATIONS = "Could not fetch generations"
const val ERROR_GENERATION_NOT_FOUND = "Generation with name %s not found"
const val ERROR_GENERATION_ID_NOT_FOUND = "Generation with id %s not found"

private fun processChain(
map: Map<Int, List<ChainLink>> = mutableMapOf(),
chain: ChainRemote?,
rank: Int = 0
): Map<Int, List<ChainLink>> {
val chainList = map.toMutableMap()
if (chain == null) return chainList
val oldList = chainList[rank].orEmpty().toMutableList().apply {
add(ChainLink(chain.species.name, getUrlPath(chain.species.url), chain.isBaby))
}
chainList[rank] = oldList
for (link in chain.evolvesTo) {
chainList.putAll(processChain(chainList, link, rank + 1))
}
return chainList
}
}
}
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ targetSdk = "36"
minSdk = "24"

# Dependency versions
androidGradlePlugin = "8.12.0"
androidGradlePlugin = "8.12.1"
androidxComposeBom = "2025.08.00"
androidxTest = "1.7.0"
androidxTestRunner = "1.7.0"
Expand Down Expand Up @@ -78,6 +78,7 @@ retrofit-converter-moshi = { group = "com.squareup.retrofit2", name = "converter
timber = { group = "com.jakewharton.timber", name = "timber", version.ref = "timber" }

# Development Environment Dependencies
androidx-test-rules = { group = "androidx.test", name = "rules", version.ref = "androidxTest" }
androidx-test-runner = { group = "androidx.test", name = "runner", version.ref = "androidxTestRunner" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-arch-core = { group = "androidx.arch.core", name = "core-testing", version.ref = "archTesting" }
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-9.0.0-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.