Skip to content

Commit

Permalink
Open webView urls in external navigator
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinokuni committed Apr 13, 2024
1 parent 02a3f82 commit 7b644cb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 })
},
Expand All @@ -145,7 +147,8 @@ class ItemScreen(
context = context,
onGlobalLayoutListener = { viewHeight, contentHeight ->
isScrollable = viewHeight - contentHeight < 0
}
},
onUrlClick = { url -> openUrl(url) }
) {
if (item.imageLink != null) {
BackgroundTitle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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)
}
Expand All @@ -53,7 +57,7 @@ class ItemNestedScrollView(
}
)

viewTreeObserver.addOnGlobalLayoutListener {
viewTreeObserver.addOnGlobalLayoutListener {
val viewHeight = this.measuredHeight
val contentHeight = getChildAt(0).height

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 7b644cb

Please sign in to comment.