From 7b644cbc97a55862a43d023cc122d013916b5ae6 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Sat, 13 Apr 2024 14:24:06 +0200 Subject: [PATCH] Open webView urls in external navigator --- .../com/readrops/app/compose/item/ItemScreen.kt | 13 ++++++++----- .../compose/item/view/ItemNestedScrollView.kt | 10 +++++++--- .../app/compose/item/view/ItemWebView.kt | 16 ++++++++++------ 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/appcompose/src/main/java/com/readrops/app/compose/item/ItemScreen.kt b/appcompose/src/main/java/com/readrops/app/compose/item/ItemScreen.kt index ff9e8c7b..5b357a5d 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/item/ItemScreen.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/item/ItemScreen.kt @@ -103,6 +103,11 @@ class ItemScreen( primaryColor } + fun openUrl(url: String) { + val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) + context.startActivity(intent) + } + Scaffold( modifier = Modifier .nestedScroll(nestedScrollConnection), @@ -123,10 +128,7 @@ class ItemScreen( } }, onShare = { screenModel.shareItem(item, context) }, - onOpenUrl = { - val intent = Intent(Intent.ACTION_VIEW, Uri.parse(item.link)) - context.startActivity(intent) - }, + onOpenUrl = { openUrl(item.link!!) }, onChangeReadState = { screenModel.setItemReadState(item.apply { isRead = it }) }, @@ -145,7 +147,8 @@ class ItemScreen( context = context, onGlobalLayoutListener = { viewHeight, contentHeight -> isScrollable = viewHeight - contentHeight < 0 - } + }, + onUrlClick = { url -> openUrl(url) } ) { if (item.imageLink != null) { BackgroundTitle( diff --git a/appcompose/src/main/java/com/readrops/app/compose/item/view/ItemNestedScrollView.kt b/appcompose/src/main/java/com/readrops/app/compose/item/view/ItemNestedScrollView.kt index 3b7095f5..0a4453db 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/item/view/ItemNestedScrollView.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/item/view/ItemNestedScrollView.kt @@ -8,10 +8,11 @@ import androidx.compose.ui.platform.ComposeView import androidx.core.view.ViewCompat import androidx.core.widget.NestedScrollView -@SuppressLint("ResourceType") +@SuppressLint("ResourceType", "ViewConstructor") class ItemNestedScrollView( context: Context, onGlobalLayoutListener: (viewHeight: Int, contentHeight: Int) -> Unit, + onUrlClick: (String) -> Unit, composeViewContent: @Composable () -> Unit ) : NestedScrollView(context) { @@ -35,7 +36,10 @@ class ItemNestedScrollView( composeViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL) composeView.layoutParams = composeViewParams - val webView = ItemWebView(context).apply { + val webView = ItemWebView( + context = context, + onUrlClick = onUrlClick + ).apply { id = 2 ViewCompat.setNestedScrollingEnabled(this, true) } @@ -53,7 +57,7 @@ class ItemNestedScrollView( } ) - viewTreeObserver.addOnGlobalLayoutListener { + viewTreeObserver.addOnGlobalLayoutListener { val viewHeight = this.measuredHeight val contentHeight = getChildAt(0).height diff --git a/appcompose/src/main/java/com/readrops/app/compose/item/view/ItemWebView.kt b/appcompose/src/main/java/com/readrops/app/compose/item/view/ItemWebView.kt index 82f70732..622ce12c 100644 --- a/appcompose/src/main/java/com/readrops/app/compose/item/view/ItemWebView.kt +++ b/appcompose/src/main/java/com/readrops/app/compose/item/view/ItemWebView.kt @@ -14,22 +14,26 @@ import com.readrops.db.pojo.ItemWithFeed import org.jsoup.Jsoup import org.jsoup.parser.Parser -@SuppressLint("SetJavaScriptEnabled") +@SuppressLint("SetJavaScriptEnabled", "ViewConstructor") class ItemWebView( context: Context, - attrs: AttributeSet?, + onUrlClick: (String) -> Unit, + attrs: AttributeSet? = null, ) : WebView(context, attrs) { - constructor(context: Context): this(context, null) - init { settings.javaScriptEnabled = true settings.builtInZoomControls = true settings.displayZoomControls = false settings.setSupportZoom(false) - - webViewClient = WebViewClient() isVerticalScrollBarEnabled = false + + webViewClient = object : WebViewClient() { + override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { + url?.let { onUrlClick(it) } + return true + } + } } fun loadText(