From ab2d0c4036ce7d4da74a8f04909b6afb068ab1ea Mon Sep 17 00:00:00 2001 From: Amine Date: Wed, 31 Oct 2018 20:14:20 +0100 Subject: [PATCH] Closes #230. --- .../adapters/ItemCardAdapter.kt | 2 + .../adapters/ItemListAdapter.kt | 7 ++++ .../bou/readerforselfoss/utils/LinksUtils.kt | 42 +++++++++++++++++++ app/src/main/res/layout/card_item.xml | 1 - app/src/main/res/layout/list_item.xml | 1 - 5 files changed, 51 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt index 09e5967..6af265e 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt @@ -16,6 +16,7 @@ import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase import apps.amine.bou.readerforselfoss.themes.AppColors +import apps.amine.bou.readerforselfoss.utils.LinkOnTouchListener import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper import apps.amine.bou.readerforselfoss.utils.glide.bitmapCenterCrop @@ -65,6 +66,7 @@ class ItemCardAdapter( holder.mView.favButton.isLiked = itm.starred holder.mView.title.text = Html.fromHtml(itm.title) + holder.mView.title.setOnTouchListener(LinkOnTouchListener()) holder.mView.title.setLinkTextColor(appColors.colorAccent) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt index 291aff7..0a99e78 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt @@ -5,10 +5,14 @@ import android.content.Context import androidx.constraintlayout.widget.ConstraintLayout import androidx.recyclerview.widget.RecyclerView import android.text.Html +import android.text.Spannable +import android.text.style.ClickableSpan import android.util.TypedValue import android.view.LayoutInflater +import android.view.MotionEvent import android.view.View import android.view.ViewGroup +import android.widget.TextView import android.widget.Toast import apps.amine.bou.readerforselfoss.R import apps.amine.bou.readerforselfoss.api.selfoss.Item @@ -16,6 +20,7 @@ import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.persistence.database.AppDatabase import apps.amine.bou.readerforselfoss.themes.AppColors +import apps.amine.bou.readerforselfoss.utils.LinkOnTouchListener import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper import apps.amine.bou.readerforselfoss.utils.glide.bitmapCenterCrop @@ -67,6 +72,8 @@ class ItemListAdapter( holder.mView.title.text = Html.fromHtml(itm.title) + holder.mView.title.setOnTouchListener(LinkOnTouchListener()) + holder.mView.title.setLinkTextColor(appColors.colorAccent) holder.mView.sourceTitleAndDate.text = itm.sourceAndDateText() diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/LinksUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/LinksUtils.kt index b6d2e3f..804dedc 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/LinksUtils.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/LinksUtils.kt @@ -6,8 +6,13 @@ import android.content.Context import android.content.Intent import android.graphics.BitmapFactory import android.net.Uri +import android.text.Spannable +import android.text.style.ClickableSpan import androidx.browser.customtabs.CustomTabsIntent import android.util.Patterns +import android.view.MotionEvent +import android.view.View +import android.widget.TextView import android.widget.Toast import apps.amine.bou.readerforselfoss.R import apps.amine.bou.readerforselfoss.ReaderActivity @@ -146,3 +151,40 @@ fun Context.openInBrowserAsNewTask(i: Item) { intent.data = Uri.parse(i.getLinkDecoded().toStringUriWithHttp()) startActivity(intent) } + +class LinkOnTouchListener: View.OnTouchListener { + override fun onTouch(v: View?, event: MotionEvent?): Boolean { + var ret = false + val widget: TextView = v as TextView + val text: CharSequence = widget.text + val stext = Spannable.Factory.getInstance().newSpannable(text) + + val action = event!!.action + + if (action == MotionEvent.ACTION_UP || + action == MotionEvent.ACTION_DOWN) { + var x: Float = event.x + var y: Float = event.y + + x -= widget.totalPaddingLeft + y -= widget.totalPaddingTop + + x += widget.scrollX + y += widget.scrollY + + val layout = widget.layout + val line = layout.getLineForVertical(y.toInt()) + val off = layout.getOffsetForHorizontal(line, x) + + val link = stext.getSpans(off, off, ClickableSpan::class.java) + + if (link.isNotEmpty()) { + if (action == MotionEvent.ACTION_UP) { + link[0].onClick(widget) + } + ret = true + } + } + return ret + } +} diff --git a/app/src/main/res/layout/card_item.xml b/app/src/main/res/layout/card_item.xml index f6f1de5..d8dcccb 100644 --- a/app/src/main/res/layout/card_item.xml +++ b/app/src/main/res/layout/card_item.xml @@ -69,7 +69,6 @@ app:layout_constraintLeft_toRightOf="@+id/sourceImage" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="@+id/sourceImage" - android:autoLink="web" tools:text="Titre" />