Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enhance WebViewJavascriptBridge with customizable WebViewClient #6

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/src/main/java/com/ding1ding/jsbridge/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class MainActivity :
allowUniversalAccessFromFileURLs = true
}

webViewClient = createWebViewClient()
webViewClient = mWebViewClient
}

webViewContainer.addView(webView)
Expand All @@ -102,7 +102,7 @@ class MainActivity :
context = this,
webView = webView,
lifecycle = lifecycle,
webViewClient = createWebViewClient(),
userWebViewClient = mWebViewClient,
).apply {
consolePipe = object : ConsolePipe {
override fun post(message: String) {
Expand All @@ -121,7 +121,7 @@ class MainActivity :
}
}

private fun createWebViewClient() = object : WebViewClient() {
private val mWebViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: android.graphics.Bitmap?) {
super.onPageStarted(view, url, favicon)
Logger.d(TAG) { "onPageStarted" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ class WebViewJavascriptBridge private constructor(

private val scope = CoroutineScope(Dispatchers.Main + SupervisorJob())

private var userWebViewClient: WebViewClient? = null

init {
setupBridge()
setupWebViewClient()
}

@SuppressLint("SetJavaScriptEnabled")
Expand All @@ -50,14 +53,14 @@ class WebViewJavascriptBridge private constructor(
Logger.d { "Bridge setup completed" }
}

fun setWebViewClient(client: WebViewClient?) {
private fun setupWebViewClient() {
webView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
isWebViewReady.set(true)
Logger.d { "WebView page finished loading" }
injectJavascriptIfNeeded()
client?.onPageFinished(view, url)
userWebViewClient?.onPageFinished(view, url)
}

@Deprecated(
Expand All @@ -68,12 +71,17 @@ class WebViewJavascriptBridge private constructor(
),
)
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean =
client?.shouldOverrideUrlLoading(view, url) ?: super.shouldOverrideUrlLoading(view, url)
userWebViewClient?.shouldOverrideUrlLoading(view, url)
?: super.shouldOverrideUrlLoading(view, url)

// Add other WebViewClient methods as needed, delegating to the client if it's not null
}
}

fun setUserWebViewClient(client: WebViewClient?) {
this.userWebViewClient = client
}

@MainThread
fun injectJavascriptIfNeeded() {
if (isInjected.get() || !isWebViewReady.get()) {
Expand Down Expand Up @@ -230,10 +238,10 @@ class WebViewJavascriptBridge private constructor(
context: Context,
webView: WebView,
lifecycle: Lifecycle? = null,
webViewClient: WebViewClient? = null,
userWebViewClient: WebViewClient? = null,
): WebViewJavascriptBridge = WebViewJavascriptBridge(context, webView).apply {
lifecycle?.addObserver(this)
setWebViewClient(webViewClient)
setUserWebViewClient(userWebViewClient)
Logger.d { "Bridge created and lifecycle observer added" }
}

Expand Down
Loading