Skip to content

Commit

Permalink
feat:Android switch to shouldInterceptRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinnZou committed Dec 2, 2023
1 parent 2d0df30 commit 34e4c6c
Showing 1 changed file with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
Expand Down Expand Up @@ -210,6 +211,7 @@ open class AccompanistWebViewClient : WebViewClient() {
internal set
open lateinit var navigator: WebViewNavigator
internal set
private var isRedirect = false

override fun onPageStarted(
view: WebView,
Expand Down Expand Up @@ -272,12 +274,52 @@ open class AccompanistWebViewClient : WebViewClient() {
}
}

override fun shouldOverrideUrlLoading(
// override fun shouldOverrideUrlLoading(
// view: WebView?,
// request: WebResourceRequest?,
// ): Boolean {
// KLogger.d {
// "shouldOverrideUrlLoading: ${request?.url}"
// }
// navigator.requestInterceptor?.apply {
// val webRequest =
// WebRequest(
// request?.url.toString(),
// request?.requestHeaders?.toMutableMap() ?: mutableMapOf(),
// )
// val interceptResult =
// this.beforeRequest(
// webRequest,
// navigator,
// )
// return when (interceptResult) {
// is WebRequestInterceptResult.Allow -> {
// super.shouldOverrideUrlLoading(view, request)
// }
//
// is WebRequestInterceptResult.Reject -> {
// true
// }
//
// is WebRequestInterceptResult.Redirect -> {
// interceptResult.request.apply {
// navigator.loadUrl(this.url, this.headers)
// }
// true
// }
// }
// }
// return super.shouldOverrideUrlLoading(view, request)
// }

override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest?,
): Boolean {
KLogger.d {
"shouldOverrideUrlLoading: ${request?.url}"
): WebResourceResponse? {
KLogger.d { "shouldInterceptRequest: ${request?.url} ${request?.isForMainFrame} ${request?.isRedirect} ${request?.method}" }
if (request?.isForMainFrame == false || isRedirect) {
isRedirect = false
return super.shouldInterceptRequest(view, request)
}
navigator.requestInterceptor?.apply {
val webRequest =
Expand All @@ -292,22 +334,25 @@ open class AccompanistWebViewClient : WebViewClient() {
)
return when (interceptResult) {
is WebRequestInterceptResult.Allow -> {
super.shouldOverrideUrlLoading(view, request)
super.shouldInterceptRequest(view, request)
}

is WebRequestInterceptResult.Reject -> {
true
navigator.stopLoading()
super.shouldInterceptRequest(view, request)
}

is WebRequestInterceptResult.Redirect -> {
isRedirect = true
interceptResult.request.apply {
navigator.loadUrl(this.url, this.headers)
}
true
null
}
}
}
return super.shouldOverrideUrlLoading(view, request)
isRedirect = false
return super.shouldInterceptRequest(view, request)
}
}

Expand Down

0 comments on commit 34e4c6c

Please sign in to comment.