From d1481a1db61ce2995ed1e0e9ceb9721dd94d24f7 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Thu, 18 Aug 2022 14:48:27 +0200 Subject: [PATCH] Reintroduce network checks where required --- .../android/background/background.kt | 2 +- .../android/fragments/ArticleFragment.kt | 139 +++++++++--------- .../repository/RepositoryImpl.kt | 2 +- 3 files changed, 74 insertions(+), 69 deletions(-) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt index 2bf2fa5..3fb4d84 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/background/background.kt @@ -43,7 +43,7 @@ class LoadingWorker(val context: Context, params: WorkerParameters) : Worker(con override fun doWork(): Result { val settings = Settings() val periodicRefresh = settings.getBoolean("periodic_refresh", false) - if (periodicRefresh) { + if (periodicRefresh && repository.isNetworkAvailable()) { CoroutineScope(Dispatchers.IO).launch { val notificationManager = 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 3fca8d8..17c441e 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 @@ -278,84 +278,89 @@ class ArticleFragment : Fragment(), DIAware { binding.progressBar.visibility = View.VISIBLE val parser = MercuryApi() - parser.parseUrl(url).enqueue( - object : Callback { - override fun onResponse( - call: Call, - response: Response - ) { - // TODO: clean all the following after finding the mercury content issue - try { - if (response.body() != null && response.body()!!.content != null && !response.body()!!.content.isNullOrEmpty()) { - try { - binding.titleView.text = response.body()!!.title - if (typeface != null) { - binding.titleView.typeface = typeface - } + if (repository.isNetworkAvailable()) { + parser.parseUrl(url).enqueue( + object : Callback { + override fun onResponse( + call: Call, + response: Response + ) { + // TODO: clean all the following after finding the mercury content issue + try { + if (response.body() != null && response.body()!!.content != null && !response.body()!!.content.isNullOrEmpty()) { try { - // Note: Mercury may return relative urls... If it does the url val will not be changed. - URL(response.body()!!.url) - url = response.body()!!.url - } catch (e: MalformedURLException) { - // Mercury returned a relative url. We do nothing. - } - } catch (e: Exception) { - } - - try { - contentText = response.body()!!.content.orEmpty() - htmlToWebview() - } catch (e: Exception) { - } - - try { - if (response.body()!!.lead_image_url != null && !response.body()!!.lead_image_url.isNullOrEmpty() && context != null) { - binding.imageView.visibility = View.VISIBLE - try { - Glide - .with(requireContext()) - .asBitmap() - .loadMaybeBasicAuth(config, response.body()!!.lead_image_url.orEmpty()) - .apply(RequestOptions.fitCenterTransform()) - .into(binding.imageView) - } catch (e: Exception) { + binding.titleView.text = response.body()!!.title + if (typeface != null) { + binding.titleView.typeface = typeface } - } else { - binding.imageView.visibility = View.GONE + try { + // Note: Mercury may return relative urls... If it does the url val will not be changed. + URL(response.body()!!.url) + url = response.body()!!.url + } catch (e: MalformedURLException) { + // Mercury returned a relative url. We do nothing. + } + } catch (e: Exception) { } - } catch (e: Exception) { - if (context != null) { - } - } - try { - binding.nestedScrollView.scrollTo(0, 0) + try { + contentText = response.body()!!.content.orEmpty() + htmlToWebview() + } catch (e: Exception) { + } - binding.progressBar.visibility = View.GONE - } catch (e: Exception) { - if (context != null) { + try { + if (response.body()!!.lead_image_url != null && !response.body()!!.lead_image_url.isNullOrEmpty() && context != null) { + binding.imageView.visibility = View.VISIBLE + try { + Glide + .with(requireContext()) + .asBitmap() + .loadMaybeBasicAuth( + config, + response.body()!!.lead_image_url.orEmpty() + ) + .apply(RequestOptions.fitCenterTransform()) + .into(binding.imageView) + } catch (e: Exception) { + } + } else { + binding.imageView.visibility = View.GONE + } + } catch (e: Exception) { + if (context != null) { + } + } + + try { + binding.nestedScrollView.scrollTo(0, 0) + + binding.progressBar.visibility = View.GONE + } catch (e: Exception) { + if (context != null) { + } + } + } else { + try { + openInBrowserAfterFailing(customTabsIntent) + } catch (e: Exception) { + if (context != null) { + } } } - } else { - try { - openInBrowserAfterFailing(customTabsIntent) - } catch (e: Exception) { - if (context != null) { - } + } catch (e: Exception) { + if (context != null) { } } - } catch (e: Exception) { - if (context != null) { - } } - } - override fun onFailure( - call: Call, - t: Throwable - ) = openInBrowserAfterFailing(customTabsIntent) - } - ) + override fun onFailure( + call: Call, + t: Throwable + ) = openInBrowserAfterFailing(customTabsIntent) + } + ) + } } private fun htmlToWebview() { diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt index ff83fac..078e206 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/repository/RepositoryImpl.kt @@ -378,7 +378,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails } } - private fun isNetworkAvailable() = isConnectionAvailable.value && !offlineOverride + fun isNetworkAvailable() = isConnectionAvailable.value && !offlineOverride // TODO: Handle offline actions } \ No newline at end of file