From ef2241e0cb451447843c45665fe8014c6dc2faba Mon Sep 17 00:00:00 2001 From: she11sh0cked Date: Tue, 19 May 2020 22:01:48 +0200 Subject: [PATCH] Fix recommendations throwing to early (#16) * Fix recommendations throwing to early Co-authored-by: she11sh0cked --- .../browse/source/browse/RecommendsPager.kt | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/RecommendsPager.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/RecommendsPager.kt index 3ac900748..5b531ccc5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/RecommendsPager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/RecommendsPager.kt @@ -38,7 +38,7 @@ open class RecommendsPager( } private fun myAnimeList(): Observable>? { - fun getId(): Observable { + fun getId(): Observable { val endpoint = myAnimeListEndpoint.toHttpUrlOrNull() ?: throw Exception("Could not convert endpoint url") @@ -69,17 +69,16 @@ open class RecommendsPager( val result = results.last() val title = result["title"].string if (!title.contains(manga.title, true)) { - throw Exception("Not found") + return@map null } - val id = result["mal_id"].string - if (id.isEmpty()) { - throw Exception("Not found") - } - id + result["mal_id"].string } } return getId().map { id -> + if (id == null) { + return@map listOf() + } val endpoint = myAnimeListEndpoint.toHttpUrlOrNull() ?: throw Exception("Could not convert endpoint url") @@ -184,7 +183,7 @@ open class RecommendsPager( title["native"].nullString?.contains("", true) != true && countOccurrence(synonyms, manga.title) <= 0 ) { - throw Exception("Not found") + return@map listOf() } val recommendations = result["recommendations"].obj val edges = recommendations["edges"].array @@ -215,19 +214,24 @@ open class RecommendsPager( var recommendations: Observable>? = null for (api in apiList) { - recommendations = when (api) { + val currentRecommendations = when (api) { API.MYANIMELIST -> myAnimeList() API.ANILIST -> anilist() } - ?: throw Exception("Could not get recommendations") - val recommendationsBlocking = recommendations.toBlocking().first() - if (recommendationsBlocking.isNotEmpty()) { + if (currentRecommendations != null && + currentRecommendations.toBlocking().first().isNotEmpty() + ) { + recommendations = currentRecommendations break } } - return recommendations!!.map { + if (recommendations == null) { + throw Exception("No recommendations found") + } + + return recommendations.map { MangasPage(it, false) }.doOnNext { onPageReceived(it)