Skip to content

Commit

Permalink
Handle youtube url scheme from web view.
Browse files Browse the repository at this point in the history
  • Loading branch information
ychescale9 committed Feb 29, 2024
1 parent c0041f2 commit 72b481c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ private fun ContentUi(
it.settings.javaScriptEnabled = true
it.settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
},
client = remember { SchemeAwareWebViewClient() },
)

var showProgressIndicator by remember { mutableStateOf(false) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.github.reactivecircus.kstreamlined.android.feature.contentviewer

import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.webkit.WebResourceRequest
import android.webkit.WebView
import co.touchlab.kermit.Logger

internal class SchemeAwareWebViewClient : AccompanistWebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
request?.let {
var url = it.url.toString()
if (url.startsWith("http://") || url.startsWith("https://")) {
return false
}
if (url.contains("youtube.com")) {
url = url.replaceFirst("intent://", "vnd.youtube:")
}
try {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
view?.context?.startActivity(intent)
} catch (e: ActivityNotFoundException) {
Logger.e(e) { "No Activity was found to handle the Intent." }
}
}
return true
}
}

0 comments on commit 72b481c

Please sign in to comment.