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(WebViewJavascriptBridge): Add WebViewClient option and improve i… #4

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
4 changes: 4 additions & 0 deletions app/src/main/java/com/ding1ding/jsbridge/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class MainActivity :
javaScriptEnabled = true
allowUniversalAccessFromFileURLs = true
}

webViewClient = createWebViewClient()
}

Expand Down Expand Up @@ -117,10 +118,13 @@ class MainActivity :

private fun createWebViewClient() = object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: android.graphics.Bitmap?) {
super.onPageStarted(view, url, favicon)
Logger.d(TAG) { "onPageStarted" }
}

override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
bridge.injectJavascriptIfNeeded()
Logger.d(TAG) { "onPageFinished" }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class WebViewJavascriptBridge private constructor(

init {
setupBridge()
setupWebViewClient()
}

@SuppressLint("SetJavaScriptEnabled")
Expand All @@ -43,19 +42,33 @@ class WebViewJavascriptBridge private constructor(
Logger.d { "Bridge setup completed" }
}

private fun setupWebViewClient() {
fun setWebViewClient(client: WebViewClient?) {
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)
}

@Deprecated(
"Deprecated in Java",
ReplaceWith(
"client?.shouldOverrideUrlLoading(view, url) ?: super.shouldOverrideUrlLoading(view, url)",
"android.webkit.WebViewClient",
),
)
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean =
client?.shouldOverrideUrlLoading(view, url)
?: super.shouldOverrideUrlLoading(view, url)

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

@MainThread
private fun injectJavascriptIfNeeded() {
fun injectJavascriptIfNeeded() {
if (isInjected.get() || !isWebViewReady.get()) {
Logger.d {
"JavaScript injection skipped. Injected: ${isInjected.get()}, WebView ready: ${isWebViewReady.get()}"
Expand Down Expand Up @@ -179,7 +192,6 @@ class WebViewJavascriptBridge private constructor(
fun reinitialize() {
release()
setupBridge()
setupWebViewClient()
Logger.d { "Bridge reinitialized" }
}

Expand Down Expand Up @@ -214,8 +226,10 @@ class WebViewJavascriptBridge private constructor(
context: Context,
webView: WebView,
lifecycle: Lifecycle? = null,
webViewClient: WebViewClient? = null,
): WebViewJavascriptBridge = WebViewJavascriptBridge(context, webView).also { bridge ->
lifecycle?.addObserver(bridge)
bridge.setWebViewClient(webViewClient)
Logger.d { "Bridge created and lifecycle observer added" }
}

Expand Down
Loading