Skip to content

Commit

Permalink
Refactor SearchResponse mapping function
Browse files Browse the repository at this point in the history
  • Loading branch information
etonotieno committed Sep 5, 2023
1 parent 6233fbb commit 7d3e239
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import okhttp3.RequestBody.Companion.asRequestBody
import sample.aniwave.data.source.AnimeDataSource
import sample.aniwave.data.source.network.model.NetworkAnime
import sample.aniwave.data.source.network.model.getAnilistId
import sample.aniwave.data.source.network.model.getTopResult
import sample.aniwave.data.source.network.model.toNetworkAnime
import java.io.File
import javax.inject.Inject
Expand All @@ -27,10 +26,9 @@ class NetworkAnimeDataSource @Inject constructor(

suspend fun searchAnime(photo: File): NetworkAnime? {
val result =
searchApi.searchAnime(image = createFileMultipart(photo)).getTopResult()
return animeApi.getAnime(result.getAnilistId()).result?.toNetworkAnime()
// Override the Episode & imageUrl to return the data from AnimeSearchApi
?.copy(episode = result?.episode ?: 0, imageUrl = result?.image.orEmpty())
searchApi.searchAnime(image = createFileMultipart(photo))
val item = animeApi.getAnime(result.getAnilistId()).result
return result.toNetworkAnime(item)
}

private fun createFileMultipart(photo: File): MultipartBody.Part {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,28 @@ data class NetworkAnimePhotoSearchResponse(

fun NetworkAnimePhotoSearchResponse.Result?.getAnilistId(): Int = this?.anilist ?: 0

fun NetworkAnimePhotoSearchResponse.getAnilistId(): Int = getTopResult().getAnilistId()

/**
* Get the Top Result of the most similar Anime
*/
fun NetworkAnimePhotoSearchResponse?.getTopResult(): NetworkAnimePhotoSearchResponse.Result? {
return this?.result?.sortedByDescending { it.similarity }?.first()
fun NetworkAnimePhotoSearchResponse.getTopResult(): NetworkAnimePhotoSearchResponse.Result? {
return this.result?.sortedByDescending { it.similarity }?.first()
}

fun NetworkAnimePhotoSearchResponse.toNetworkAnime(
response: NetworkAnimeResponse.AnimeResponse?
): NetworkAnime? {
// Override the Episode & imageUrl to return the data from AnimeSearchApi
val result = getTopResult() ?: return null
val item = response ?: return null
return NetworkAnime(
id = result.getAnilistId(),
imageUrl = result.image.orEmpty(),
episode = result.episode ?: 0,
title = item.getEnglishTitle(),
score = item.score ?: 0.0,
releaseYear = "${item.getReleaseYear()}",
rank = item.rank ?: 0,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,12 @@ fun NetworkAnimeResponse.AnimeResponse.toNetworkAnime(): NetworkAnime {
)
}

private fun NetworkAnimeResponse.AnimeResponse.getReleaseYear(): Int {
fun NetworkAnimeResponse.AnimeResponse.getReleaseYear(): Int {
return this.year ?: this.aired.prop?.from?.year ?: 0
}

private fun NetworkAnimeResponse.AnimeResponse?.getEnglishTitle(): String =
this?.titles?.find { title -> title.type == "English" }?.title.orEmpty()
fun NetworkAnimeResponse.AnimeResponse.getEnglishTitle(): String =
this.titles?.find { title -> title.type == "English" }?.title.orEmpty()

fun List<NetworkAnimeResponse.AnimeResponse>.toNetworkAnime(): List<NetworkAnime> =
map { it.toNetworkAnime() }
2 changes: 1 addition & 1 deletion detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ style:
active: false
ReturnCount:
active: true
max: 2
max: 4
excludedFunctions:
- 'equals'
excludeLabeled: false
Expand Down

0 comments on commit 7d3e239

Please sign in to comment.