Use firstOrNull

This commit is contained in:
Luna712 2023-10-23 13:13:30 -06:00
parent 69a4b3c318
commit a43bc28f92

View file

@ -273,46 +273,21 @@ open class TmdbProvider : MainAPI() {
return if (isTvSeries) {
val contentRatings = tmdb.tvService().content_ratings(id).awaitResponse().body()
contentRatings?.results
?.find { it.iso_3166_1 == country }
?.rating
?.find { it: ContentRating ->
it.iso_3166_1 == country
}?.rating
} else {
val releaseDates = tmdb.moviesService().releaseDates(id).awaitResponse().body()?.results
val certification = findFirstNonEmptyCertificationWithCountry(releaseDates, country)
if (!certification.isNullOrEmpty()) {
certification
} else {
null
}
val certification = releaseDates?.firstOrNull { it: ReleaseDatesResult ->
it.iso_3166_1 == country
}?.release_dates?.firstOrNull { it: ReleaseDate ->
!it.certification.isNullOrBlank()
}?.certification
certification
}
}
private fun findFirstNonEmptyCertificationWithCountry(
releaseDatesResults: List<ReleaseDatesResult>?,
country: String
): String? {
for (releaseDateResult in releaseDatesResults.orEmpty()) {
if (releaseDateResult.iso_3166_1 == country) {
val certification = findFirstNonEmptyCertification(releaseDateResult.release_dates)
if (!certification.isNullOrEmpty()) {
return certification
}
}
}
return null
}
private fun findFirstNonEmptyCertification(releaseDates: List<ReleaseDate>?): String? {
for (releaseDate in releaseDates.orEmpty()) {
val certification = releaseDate.certification
if (!certification.isNullOrBlank()) {
return certification
}
}
return null
}
// Possible to add recommendations and such here.
override suspend fun load(url: String): LoadResponse? {
// https://www.themoviedb.org/movie/7445-brothers