From 996a7ed22cfed8798c55fe37a3cbac1d1d99fd01 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Sun, 30 Apr 2023 18:41:15 +0200 Subject: [PATCH] fix: Sometimes url isn't even defined. --- .../android/fragments/ArticleFragment.kt | 45 +++++++++++-------- .../readerforselfossv2/model/MercuryModel.kt | 5 ++- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt index cd95a5d..4e3ddf3 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt @@ -31,7 +31,6 @@ import bou.amine.apps.readerforselfossv2.android.utils.openInBrowserAsNewTask import bou.amine.apps.readerforselfossv2.android.utils.shareLink import bou.amine.apps.readerforselfossv2.model.MercuryModel import bou.amine.apps.readerforselfossv2.model.SelfossModel -import bou.amine.apps.readerforselfossv2.model.StatusAndData import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.rest.MercuryApi import bou.amine.apps.readerforselfossv2.service.AppSettingsService @@ -260,21 +259,8 @@ class ArticleFragment : Fragment(), DIAware { CoroutineScope(Dispatchers.Main).launch { try { val response = mercuryApi.query(url) - if (response.success && response.data != null && !response.data?.content.isNullOrEmpty()) { - binding.titleView.text = response.data!!.title.orEmpty() - if (typeface != null) { - binding.titleView.typeface = typeface - } - URL(response.data!!.url) - url = response.data!!.url - - contentText = response.data!!.content.orEmpty() - htmlToWebview() - - handleLeadImage(response) - - binding.nestedScrollView.scrollTo(0, 0) - binding.progressBar.visibility = View.GONE + if (response.success && response.data != null) { + handleMercuryData(response.data!!) } else { openInBrowserAfterFailing() } @@ -287,14 +273,35 @@ class ArticleFragment : Fragment(), DIAware { } } - private fun handleLeadImage(response: StatusAndData) { - if (!response.data?.lead_image_url.isNullOrEmpty() && context != null) { + private fun handleMercuryData(data: MercuryModel.ParsedContent) { + if (data.error == true || data.failed == true) { + openInBrowserAfterFailing() + } else { + binding.titleView.text = data.title.orEmpty() + if (typeface != null) { + binding.titleView.typeface = typeface + } + URL(data.url) + url = data.url!! + + contentText = data.content.orEmpty() + htmlToWebview() + + handleLeadImage(data?.lead_image_url) + + binding.nestedScrollView.scrollTo(0, 0) + binding.progressBar.visibility = View.GONE + } + } + + private fun handleLeadImage(lead_image_url: String?) { + if (!lead_image_url.isNullOrEmpty() && context != null) { binding.imageView.visibility = View.VISIBLE Glide .with(requireContext()) .asBitmap() .load( - response.data!!.lead_image_url.orEmpty() + lead_image_url ) .apply(RequestOptions.fitCenterTransform()) .into(binding.imageView) diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/MercuryModel.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/MercuryModel.kt index 7915058..2dd1da5 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/MercuryModel.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/MercuryModel.kt @@ -9,6 +9,9 @@ class MercuryModel { val title: String?, val content: String?, val lead_image_url: String?, // NOSONAR - val url: String + val url: String?, + val error: Boolean?, + val message: String?, + val failed: Boolean? ) }