fix: url can be empty ?
All checks were successful
Check master code / build (push) Successful in 14m37s
Create tag / build (push) Successful in 7m25s
Create tag / createTagAndChangelog (push) Successful in 44s
Create tag / release (push) Successful in 5m12s

This commit is contained in:
Amine Bouabdallaoui 2025-02-27 21:40:06 +01:00
parent 2564b19726
commit a90ccec707
4 changed files with 38 additions and 37 deletions

View File

@ -75,7 +75,7 @@ class ArticleFragment :
private var colorSurface: Int = 0 private var colorSurface: Int = 0
private var fontSize: Int = DEFAULT_FONT_SIZE private var fontSize: Int = DEFAULT_FONT_SIZE
private lateinit var item: SelfossModel.Item private lateinit var item: SelfossModel.Item
private lateinit var url: String private var url: String? = null
private lateinit var contentText: String private lateinit var contentText: String
private lateinit var contentSource: String private lateinit var contentSource: String
private lateinit var contentImage: String private lateinit var contentImage: String
@ -168,8 +168,8 @@ class ArticleFragment :
private fun handleContent() { private fun handleContent() {
if (contentText.isEmptyOrNullOrNullString()) { if (contentText.isEmptyOrNullOrNullString()) {
if (repository.isNetworkAvailable()) { if (repository.isNetworkAvailable() && url.isUrlValid()) {
getContentFromMercury() getContentFromMercury(url!!)
} }
} else { } else {
binding.titleView.text = contentTitle binding.titleView.text = contentTitle
@ -271,7 +271,7 @@ class ArticleFragment :
} }
@Suppress("detekt:SwallowedException") @Suppress("detekt:SwallowedException")
private fun getContentFromMercury() { private fun getContentFromMercury(url: String) {
binding.progressBar.visibility = View.VISIBLE binding.progressBar.visibility = View.VISIBLE
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
@ -424,10 +424,10 @@ class ArticleFragment :
var baseUrl: String? = null var baseUrl: String? = null
try { try {
val itemUrl = URL(url) val itemUrl = URL(url.orEmpty())
baseUrl = itemUrl.protocol + "://" + itemUrl.host baseUrl = itemUrl.protocol + "://" + itemUrl.host
} catch (e: MalformedURLException) { } catch (e: MalformedURLException) {
e.sendSilentlyWithAcraWithName("htmlToWebview > $url") e.sendSilentlyWithAcraWithName("htmlToWebview > ${url.orEmpty()}")
} }
val fontName: String = val fontName: String =

View File

@ -11,22 +11,24 @@ import bou.amine.apps.readerforselfossv2.android.utils.acra.sendSilentlyWithAcra
import bou.amine.apps.readerforselfossv2.utils.toStringUriWithHttp import bou.amine.apps.readerforselfossv2.utils.toStringUriWithHttp
fun Context.shareLink( fun Context.shareLink(
itemUrl: String, itemUrl: String?,
itemTitle: String, itemTitle: String,
) { ) {
val sendIntent = Intent() if (itemUrl.isUrlValid()) {
sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
sendIntent.putExtra(Intent.EXTRA_TEXT, itemUrl.toStringUriWithHttp()) sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_SUBJECT, itemTitle) sendIntent.putExtra(Intent.EXTRA_TEXT, itemUrl!!.toStringUriWithHttp())
sendIntent.type = "text/plain" sendIntent.putExtra(Intent.EXTRA_SUBJECT, itemTitle)
startActivity( sendIntent.type = "text/plain"
Intent startActivity(
.createChooser( Intent
sendIntent, .createChooser(
getString(R.string.share), sendIntent,
).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), getString(R.string.share),
) ).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
)
}
} }
@ColorInt @ColorInt

View File

@ -15,12 +15,12 @@ import android.widget.Toast
import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.R
import bou.amine.apps.readerforselfossv2.android.ReaderActivity import bou.amine.apps.readerforselfossv2.android.ReaderActivity
import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.model.SelfossModel
import bou.amine.apps.readerforselfossv2.utils.toStringUriWithHttp import bou.amine.apps.readerforselfossv2.utils.isEmptyOrNullOrNullString
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
fun Context.openItemUrl( fun Context.openItemUrl(
currentItem: Int, currentItem: Int,
linkDecoded: String, linkDecoded: String?,
articleViewer: Boolean, articleViewer: Boolean,
app: Activity, app: Activity,
) { ) {
@ -37,12 +37,13 @@ fun Context.openItemUrl(
intent.putExtra("currentItem", currentItem) intent.putExtra("currentItem", currentItem)
app.startActivity(intent) app.startActivity(intent)
} else { } else {
this.openUrlInBrowserAsNewTask(linkDecoded) this.openUrlInBrowserAsNewTask(linkDecoded!!)
} }
} }
} }
fun String.isUrlValid(): Boolean = this.toHttpUrlOrNull() != null && Patterns.WEB_URL.matcher(this).matches() fun String?.isUrlValid(): Boolean =
!this.isEmptyOrNullOrNullString() && this!!.toHttpUrlOrNull() != null && Patterns.WEB_URL.matcher(this).matches()
fun String.isBaseUrlInvalid(): Boolean { fun String.isBaseUrlInvalid(): Boolean {
val baseUrl = this.toHttpUrlOrNull() val baseUrl = this.toHttpUrlOrNull()
@ -56,14 +57,16 @@ fun String.isBaseUrlInvalid(): Boolean {
} }
fun Context.openItemUrlInBrowserAsNewTask(i: SelfossModel.Item) { fun Context.openItemUrlInBrowserAsNewTask(i: SelfossModel.Item) {
this.openUrlInBrowserAsNewTask(i.getLinkDecoded().toStringUriWithHttp()) this.openUrlInBrowserAsNewTask(i.getLinkDecoded())
} }
fun Context.openUrlInBrowserAsNewTask(url: String) { fun Context.openUrlInBrowserAsNewTask(url: String?) {
val intent = Intent(Intent.ACTION_VIEW) if (url.isUrlValid()) {
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(url) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
this.mayBeStartActivity(intent) intent.data = Uri.parse(url)
this.mayBeStartActivity(intent)
}
} }
fun Context.openUrlInBrowser(url: String) { fun Context.openUrlInBrowser(url: String) {

View File

@ -127,8 +127,8 @@ class SelfossModel {
val tags: List<String>, val tags: List<String>,
val author: String? = null, val author: String? = null,
) { ) {
fun getLinkDecoded(): String { fun getLinkDecoded(): String? {
var stringUrl: String var stringUrl: String?
stringUrl = stringUrl =
if (link.contains("//news.google.com/news/") && link.contains("&amp;url=")) { if (link.contains("//news.google.com/news/") && link.contains("&amp;url=")) {
link.substringAfter("&amp;url=") link.substringAfter("&amp;url=")
@ -146,11 +146,7 @@ class SelfossModel {
stringUrl = "http:$stringUrl" stringUrl = "http:$stringUrl"
} }
if (stringUrl.isEmptyOrNullOrNullString()) { return if (stringUrl.isEmptyOrNullOrNullString()) null else stringUrl
throw ModelException("Link $link was translated to $stringUrl, but was empty. Handle this.")
}
return stringUrl
} }
fun sourceAuthorAndDate(): String { fun sourceAuthorAndDate(): String {