Fix review comments
This commit is contained in:
parent
40ecdad81c
commit
06c9ff8a1f
|
|
@ -3,10 +3,10 @@ package ru.touchin.extensions
|
||||||
import android.content.res.TypedArray
|
import android.content.res.TypedArray
|
||||||
import androidx.annotation.StyleableRes
|
import androidx.annotation.StyleableRes
|
||||||
|
|
||||||
private const val DEFAULT_VALUE = -1
|
private const val NOT_FOUND_VALUE = -1
|
||||||
|
|
||||||
fun TypedArray.getResourceIdOrNull(@StyleableRes index: Int) = getResourceId(index, DEFAULT_VALUE)
|
fun TypedArray.getResourceIdOrNull(@StyleableRes index: Int) = getResourceId(index, NOT_FOUND_VALUE)
|
||||||
.takeIf { it != DEFAULT_VALUE }
|
.takeIf { it != NOT_FOUND_VALUE }
|
||||||
|
|
||||||
fun TypedArray.getColorOrNull(@StyleableRes index: Int) = getColor(index, DEFAULT_VALUE)
|
fun TypedArray.getColorOrNull(@StyleableRes index: Int) = getColor(index, NOT_FOUND_VALUE)
|
||||||
.takeIf { it != DEFAULT_VALUE }
|
.takeIf { it != NOT_FOUND_VALUE }
|
||||||
|
|
|
||||||
|
|
@ -55,16 +55,16 @@ open class BaseWebView @JvmOverloads constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStateChanged(newState: WebViewLoadingState) {
|
override fun onStateChanged(newState: WebViewState) {
|
||||||
when {
|
when {
|
||||||
newState == WebViewLoadingState.LOADED -> {
|
newState == WebViewState.SUCCESS -> {
|
||||||
binding.pullToRefresh.isRefreshing = false
|
binding.pullToRefresh.isRefreshing = false
|
||||||
showChild(R.id.pull_to_refresh)
|
showChild(R.id.pull_to_refresh)
|
||||||
}
|
}
|
||||||
newState == WebViewLoadingState.LOADING && !binding.pullToRefresh.isRefreshing -> {
|
newState == WebViewState.LOADING && !binding.pullToRefresh.isRefreshing -> {
|
||||||
showChild(if (isCircleProgressBar) R.id.progress_bar else R.id.linear_progress_bar)
|
showChild(if (isCircleProgressBar) R.id.progress_bar else R.id.linear_progress_bar)
|
||||||
}
|
}
|
||||||
newState == WebViewLoadingState.ERROR -> {
|
newState == WebViewState.ERROR -> {
|
||||||
showChild(R.id.error_layout)
|
showChild(R.id.error_layout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -186,7 +186,7 @@ open class BaseWebView @JvmOverloads constructor(
|
||||||
isCircleProgressBar = getBoolean(R.styleable.BaseWebView_isCircleProgressBar, true)
|
isCircleProgressBar = getBoolean(R.styleable.BaseWebView_isCircleProgressBar, true)
|
||||||
isPullToRefreshEnabled = getBoolean(R.styleable.BaseWebView_isPullToRefreshEnabled, false)
|
isPullToRefreshEnabled = getBoolean(R.styleable.BaseWebView_isPullToRefreshEnabled, false)
|
||||||
|
|
||||||
onStateChanged(WebViewLoadingState.LOADING)
|
onStateChanged(WebViewState.LOADING)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ open class BaseWebViewClient(
|
||||||
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
|
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
|
||||||
super.onPageStarted(view, url, favicon)
|
super.onPageStarted(view, url, favicon)
|
||||||
isError = false
|
isError = false
|
||||||
callback.onStateChanged(WebViewLoadingState.LOADING)
|
callback.onStateChanged(WebViewState.LOADING)
|
||||||
|
|
||||||
Looper.myLooper()?.let { looper ->
|
Looper.myLooper()?.let { looper ->
|
||||||
val handler = Handler(looper)
|
val handler = Handler(looper)
|
||||||
|
|
@ -73,7 +73,7 @@ open class BaseWebViewClient(
|
||||||
if (isSslPinningEnable) {
|
if (isSslPinningEnable) {
|
||||||
super.onReceivedSslError(view, handler, error)
|
super.onReceivedSslError(view, handler, error)
|
||||||
isError = true
|
isError = true
|
||||||
callback.onStateChanged(WebViewLoadingState.ERROR)
|
callback.onStateChanged(WebViewState.ERROR)
|
||||||
} else {
|
} else {
|
||||||
handler.proceed()
|
handler.proceed()
|
||||||
}
|
}
|
||||||
|
|
@ -88,7 +88,7 @@ open class BaseWebViewClient(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun pageFinished() {
|
private fun pageFinished() {
|
||||||
callback.onStateChanged(if (isError) WebViewLoadingState.ERROR else WebViewLoadingState.LOADED)
|
callback.onStateChanged(if (isError) WebViewState.ERROR else WebViewState.SUCCESS)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun String?.processCookies(): Map<String, String> {
|
private fun String?.processCookies(): Map<String, String> {
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import android.webkit.WebView
|
||||||
|
|
||||||
interface WebViewCallback {
|
interface WebViewCallback {
|
||||||
|
|
||||||
fun onStateChanged(newState: WebViewLoadingState)
|
fun onStateChanged(newState: WebViewState)
|
||||||
|
|
||||||
fun onOverrideUrlLoading(url: String?): Boolean
|
fun onOverrideUrlLoading(url: String?): Boolean
|
||||||
|
|
||||||
|
|
@ -27,9 +27,3 @@ interface WebViewCallback {
|
||||||
|
|
||||||
fun onProgressChanged(progress: Int) = Unit
|
fun onProgressChanged(progress: Int) = Unit
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class WebViewLoadingState {
|
|
||||||
LOADING,
|
|
||||||
ERROR,
|
|
||||||
LOADED
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package ru.touchin.roboswag.webview.web_view
|
||||||
|
|
||||||
|
enum class WebViewState {
|
||||||
|
LOADING,
|
||||||
|
SUCCESS,
|
||||||
|
ERROR
|
||||||
|
}
|
||||||
|
|
@ -11,7 +11,7 @@ import java.net.URL
|
||||||
*
|
*
|
||||||
* webView.redirectionController = RedirectionController(
|
* webView.redirectionController = RedirectionController(
|
||||||
* CompositeCondition(ByHost("www.petshop.ru") + ByPath("catalog")),
|
* CompositeCondition(ByHost("www.petshop.ru") + ByPath("catalog")),
|
||||||
* ByPredicate { url -> checkUrl(url) }
|
* RedirectionCondition { url -> checkUrl(url) }
|
||||||
* )
|
* )
|
||||||
*/
|
*/
|
||||||
class RedirectionController(private val conditions: List<RedirectionCondition> = emptyList()) {
|
class RedirectionController(private val conditions: List<RedirectionCondition> = emptyList()) {
|
||||||
|
|
@ -29,36 +29,32 @@ class RedirectionController(private val conditions: List<RedirectionCondition> =
|
||||||
/**
|
/**
|
||||||
* Class with base redirection conditions
|
* Class with base redirection conditions
|
||||||
*/
|
*/
|
||||||
sealed class RedirectionCondition {
|
fun interface RedirectionCondition {
|
||||||
|
|
||||||
|
fun shouldRedirect(url: URL): Boolean
|
||||||
|
|
||||||
operator fun plus(other: RedirectionCondition) = listOf(this, other)
|
operator fun plus(other: RedirectionCondition) = listOf(this, other)
|
||||||
|
|
||||||
abstract fun shouldRedirect(url: URL): Boolean
|
class ByRegex(private val regex: Regex) : RedirectionCondition {
|
||||||
|
|
||||||
class ByRegex(private val regex: Regex) : RedirectionCondition() {
|
|
||||||
override fun shouldRedirect(url: URL) = url.toString().matches(regex)
|
override fun shouldRedirect(url: URL) = url.toString().matches(regex)
|
||||||
}
|
}
|
||||||
|
|
||||||
class ByHost(private val host: String) : RedirectionCondition() {
|
class ByHost(private val host: String) : RedirectionCondition {
|
||||||
override fun shouldRedirect(url: URL) = url.host == host
|
override fun shouldRedirect(url: URL) = url.host == host
|
||||||
}
|
}
|
||||||
|
|
||||||
class ByPath(private val path: String) : RedirectionCondition() {
|
class ByPath(private val path: String) : RedirectionCondition {
|
||||||
override fun shouldRedirect(url: URL) = path in url.path
|
override fun shouldRedirect(url: URL) = path in url.path
|
||||||
}
|
}
|
||||||
|
|
||||||
class ByQuery(private val query: String) : RedirectionCondition() {
|
class ByQuery(private val query: String) : RedirectionCondition {
|
||||||
override fun shouldRedirect(url: URL) = query in url.query
|
override fun shouldRedirect(url: URL) = query in url.query
|
||||||
}
|
}
|
||||||
|
|
||||||
class ByPredicate(private val predicate: (URL) -> Boolean) : RedirectionCondition() {
|
|
||||||
override fun shouldRedirect(url: URL) = predicate.invoke(url)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All of the conditions should be matched to perform redirect
|
* All of the conditions should be matched to perform redirect
|
||||||
*/
|
*/
|
||||||
class CompositeCondition(private val conditions: List<RedirectionCondition>) : RedirectionCondition() {
|
class CompositeCondition(private val conditions: List<RedirectionCondition>) : RedirectionCondition {
|
||||||
override fun shouldRedirect(url: URL): Boolean = conditions.all { it.shouldRedirect(url) }
|
override fun shouldRedirect(url: URL): Boolean = conditions.all { it.shouldRedirect(url) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue