From 40ecdad81cdb96d0cce60ca3f92f56a0c91aad9e Mon Sep 17 00:00:00 2001 From: Grigorii Date: Wed, 28 Dec 2022 16:41:08 +0400 Subject: [PATCH] Refactor RedirectionController --- .../redirection/RedirectionController.kt | 71 ++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/webview/src/main/java/ru/touchin/roboswag/webview/web_view/redirection/RedirectionController.kt b/webview/src/main/java/ru/touchin/roboswag/webview/web_view/redirection/RedirectionController.kt index 951ca2d..fce22ce 100644 --- a/webview/src/main/java/ru/touchin/roboswag/webview/web_view/redirection/RedirectionController.kt +++ b/webview/src/main/java/ru/touchin/roboswag/webview/web_view/redirection/RedirectionController.kt @@ -6,36 +6,59 @@ import java.net.URL /** * Set [RedirectionController] to [BaseWebView] to handle url redirections * - * By default all redirections are dismissed. For example: + * By default all redirections are dismissed. Any matched condition will perform redirect. + * If you need multiple conditions -> use [RedirectionCondition.CompositeCondition] * - * baseWebView.redirectionController = RedirectionController( - * hosts = listOf("www.petshop.ru"), - * paths = listOf("catalog") + * webView.redirectionController = RedirectionController( + * CompositeCondition(ByHost("www.petshop.ru") + ByPath("catalog")), + * ByPredicate { url -> checkUrl(url) } * ) */ -class RedirectionController( - private val regex: List? = null, - private val hosts: List? = null, - private val paths: List? = null, - private val queries: List? = null, - private val checkCondition: ((url: String) -> Boolean)? = null -) { +class RedirectionController(private val conditions: List = emptyList()) { + + constructor(vararg redirectionConditions: RedirectionCondition) + : this(redirectionConditions.toList()) fun shouldRedirectToUrl(rawUrl: String?): Boolean { - if (rawUrl == null) return false - - if (checkCondition != null) return checkCondition.invoke(rawUrl) - val url = URL(rawUrl) - if (!regex.isNullOrEmpty()) return regex.any { rawUrl.matches(it) } - - if (!queries.isNullOrEmpty()) return queries.any { it in url.query } - - if (!paths.isNullOrEmpty()) return paths.any { it in url.path } - - if (!hosts.isNullOrEmpty()) return hosts.any { it == url.host } - - return false + return conditions.any { it.shouldRedirect(url) } + } +} + +/** + * Class with base redirection conditions + */ +sealed class RedirectionCondition { + + operator fun plus(other: RedirectionCondition) = listOf(this, other) + + abstract fun shouldRedirect(url: URL): Boolean + + class ByRegex(private val regex: Regex) : RedirectionCondition() { + override fun shouldRedirect(url: URL) = url.toString().matches(regex) + } + + class ByHost(private val host: String) : RedirectionCondition() { + override fun shouldRedirect(url: URL) = url.host == host + } + + class ByPath(private val path: String) : RedirectionCondition() { + override fun shouldRedirect(url: URL) = path in url.path + } + + class ByQuery(private val query: String) : RedirectionCondition() { + 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 + */ + class CompositeCondition(private val conditions: List) : RedirectionCondition() { + override fun shouldRedirect(url: URL): Boolean = conditions.all { it.shouldRedirect(url) } } }