Add custom condition to IgnoredErrorsHolder

This commit is contained in:
Grigorii 2022-12-28 14:27:05 +04:00
parent 49037036b4
commit 35ad69239c
4 changed files with 36 additions and 23 deletions

View File

@ -15,6 +15,7 @@ import ru.touchin.extensions.setOnRippleClickListener
import ru.touchin.roboswag.views.widget.Switcher
import ru.touchin.roboswag.webview.R
import ru.touchin.roboswag.webview.databinding.BaseWebViewBinding
import ru.touchin.roboswag.webview.web_view.redirection.IgnoredErrorsHolder
import ru.touchin.roboswag.webview.web_view.redirection.RedirectionController
open class BaseWebView @JvmOverloads constructor(
@ -80,8 +81,11 @@ open class BaseWebView @JvmOverloads constructor(
binding.linearProgressBar.progress = progress
}
fun setBaseWebViewClient(callback: WebViewCallback = this) {
binding.webView.webViewClient = BaseWebViewClient(callback)
fun setBaseWebViewClient(
callback: WebViewCallback = this,
ignoredErrorsHolder: IgnoredErrorsHolder = IgnoredErrorsHolder()
) {
binding.webView.webViewClient = BaseWebViewClient(callback, ignoredErrorsHolder)
binding.webView.webChromeClient = BaseChromeWebViewClient(callback)
}
@ -119,7 +123,7 @@ open class BaseWebView @JvmOverloads constructor(
) {
val indexOfHead = htmlString
.indexOf("</head>", ignoreCase = true)
.takeIf { it != -1 } ?: 0
.takeIf { it >= 0 } ?: 0
val styledHtml = StringBuilder(htmlString)
.insert(indexOfHead, styleDeps)
@ -147,8 +151,6 @@ open class BaseWebView @JvmOverloads constructor(
scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
setLayerType(View.LAYER_TYPE_HARDWARE, null)
with(settings) {
allowContentAccess = true
allowFileAccess = true
loadsImagesAutomatically = true
javaScriptEnabled = true
domStorageEnabled = true

View File

@ -11,11 +11,11 @@ import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.core.os.postDelayed
import ru.touchin.roboswag.webview.web_view.redirection.IgnoredUrlsHolder
import ru.touchin.roboswag.webview.web_view.redirection.IgnoredErrorsHolder
open class BaseWebViewClient(
private val callback: WebViewCallback,
private val ignoredUrlsHolder: IgnoredUrlsHolder = IgnoredUrlsHolder(),
private val ignoredErrorsHolder: IgnoredErrorsHolder = IgnoredErrorsHolder(),
private val isSslPinningEnable: Boolean = true
) : WebViewClient() {
@ -83,7 +83,7 @@ open class BaseWebViewClient(
* onReceivedError isn't called when url is "about:blank" (url string isBlank)
*/
override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
if (ignoredUrlsHolder.shouldIgnoreError(request.url.toString())) return
if (ignoredErrorsHolder.shouldIgnoreError(request, error)) return
isError = true
}

View File

@ -0,0 +1,26 @@
package ru.touchin.roboswag.webview.web_view.redirection
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
/**
* Define some urls here which errors should be ignored while redirection to prevent show error screen
* Additionally you can add custom error ignoring condition
*/
class IgnoredErrorsHolder(
private val ignoredUrlsList: List<String> = listOf(
"https://stats.g.doubleclick.net/"
),
private val ignoreCondition: ((WebResourceRequest, WebResourceError) -> Boolean)? = null
) {
constructor(vararg urls: String) : this(urls.toList())
fun shouldIgnoreError(request: WebResourceRequest, error: WebResourceError): Boolean {
if (ignoreCondition != null) return ignoreCondition.invoke(request, error)
val url = request.url.toString()
return ignoredUrlsList.any { url in it }
}
}

View File

@ -1,15 +0,0 @@
package ru.touchin.roboswag.webview.web_view.redirection
/**
* Define some urls here which errors should be ignored while redirection to prevent show error screen
*/
class IgnoredUrlsHolder(
private val ignoredUrlsList: List<String> = listOf(
"https://stats.g.doubleclick.net/"
)
) {
constructor(vararg urls: String) : this(urls.toList())
fun shouldIgnoreError(url: String) = ignoredUrlsList.any { url in it }
}