diff options
author | Allan Wang <me@allanwang.ca> | 2020-02-23 16:06:45 -0800 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2020-02-23 16:06:45 -0800 |
commit | c8b54fd10a08ed53eb7d21578a4fe990fe14e3bc (patch) | |
tree | 0841d112c8c00504ce10ca72ef39e403b69c595c /app/src/main/kotlin/com/pitchedapps/frost/injectors | |
parent | 4d5aaf541dbfa7d521ebbc5f011a642c83c4b9c5 (diff) | |
download | frost-c8b54fd10a08ed53eb7d21578a4fe990fe14e3bc.tar.gz frost-c8b54fd10a08ed53eb7d21578a4fe990fe14e3bc.tar.bz2 frost-c8b54fd10a08ed53eb7d21578a4fe990fe14e3bc.zip |
Move prefs to service locator
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/injectors')
5 files changed, 47 insertions, 35 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt index 1b210251..bbfc7a02 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssAssets.kt @@ -20,7 +20,6 @@ import android.content.Context import android.graphics.Color import android.webkit.WebView import androidx.annotation.VisibleForTesting -import ca.allanwang.kau.kotlin.lazyContext import ca.allanwang.kau.utils.adjustAlpha import ca.allanwang.kau.utils.colorToBackground import ca.allanwang.kau.utils.colorToForeground @@ -29,11 +28,11 @@ import ca.allanwang.kau.utils.use import ca.allanwang.kau.utils.withAlpha import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import java.io.BufferedReader import java.io.FileNotFoundException import java.util.Locale -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.withContext /** * Created by Allan Wang on 2017-05-31. @@ -50,29 +49,38 @@ enum class CssAssets(val folder: String = THEME_FOLDER) : InjectorContract { /** * Note that while this can be loaded from any thread, it is typically done through [load] */ - private val injector = lazyContext { + private var injector: JsInjector? = null + + private fun injector(context: Context, prefs: Prefs): JsInjector = + injector ?: createInjector(context, prefs).also { injector = it } + + /** + * Note that while this can be loaded from any thread, it is typically done through [load] + */ + private fun createInjector(context: Context, prefs: Prefs): JsInjector = try { var content = - it.assets.open("css/$folder/$file").bufferedReader().use(BufferedReader::readText) + context.assets.open("css/$folder/$file").bufferedReader() + .use(BufferedReader::readText) if (this == CUSTOM) { - val bt = if (Color.alpha(Prefs.bgColor) == 255) - Prefs.bgColor.toRgbaString() + val bt = if (Color.alpha(prefs.bgColor) == 255) + prefs.bgColor.toRgbaString() else "transparent" - val bb = Prefs.bgColor.colorToForeground(0.35f) + val bb = prefs.bgColor.colorToForeground(0.35f) content = content - .replace("\$T\$", Prefs.textColor.toRgbaString()) - .replace("\$TT\$", Prefs.textColor.colorToBackground(0.05f).toRgbaString()) - .replace("\$A\$", Prefs.accentColor.toRgbaString()) - .replace("\$AT\$", Prefs.iconColor.toRgbaString()) - .replace("\$B\$", Prefs.bgColor.toRgbaString()) + .replace("\$T\$", prefs.textColor.toRgbaString()) + .replace("\$TT\$", prefs.textColor.colorToBackground(0.05f).toRgbaString()) + .replace("\$A\$", prefs.accentColor.toRgbaString()) + .replace("\$AT\$", prefs.iconColor.toRgbaString()) + .replace("\$B\$", prefs.bgColor.toRgbaString()) .replace("\$BT\$", bt) .replace("\$BBT\$", bb.withAlpha(51).toRgbaString()) - .replace("\$O\$", Prefs.bgColor.withAlpha(255).toRgbaString()) + .replace("\$O\$", prefs.bgColor.withAlpha(255).toRgbaString()) .replace("\$OO\$", bb.withAlpha(255).toRgbaString()) - .replace("\$D\$", Prefs.textColor.adjustAlpha(0.3f).toRgbaString()) + .replace("\$D\$", prefs.textColor.adjustAlpha(0.3f).toRgbaString()) .replace("\$TI\$", bb.withAlpha(60).toRgbaString()) .replace("\$C\$", bt) } @@ -81,24 +89,24 @@ enum class CssAssets(val folder: String = THEME_FOLDER) : InjectorContract { L.e(e) { "CssAssets file not found" } JsInjector(JsActions.EMPTY.function) } - } - override fun inject(webView: WebView) = - injector(webView.context).inject(webView) + override fun inject(webView: WebView, prefs: Prefs) = + injector(webView.context, prefs).inject(webView, prefs) fun reset() { - injector.invalidate() + injector = null } companion object { + // Ensures that all non themes and the selected theme are loaded - suspend fun load(context: Context) { + suspend fun load(context: Context, prefs: Prefs) { withContext(Dispatchers.IO) { - val currentTheme = Prefs.t.injector as? CssAssets + val currentTheme = prefs.t.injector as? CssAssets val (themes, others) = CssAssets.values().partition { it.folder == THEME_FOLDER } themes.filter { it != currentTheme }.forEach { it.reset() } - currentTheme?.injector?.invoke(context) - others.forEach { it.injector.invoke(context) } + currentTheme?.injector(context, prefs) + others.forEach { it.injector(context, prefs) } } } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssHider.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssHider.kt index 4c53dcda..94367853 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssHider.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/CssHider.kt @@ -17,6 +17,7 @@ package com.pitchedapps.frost.injectors import android.webkit.WebView +import com.pitchedapps.frost.utils.Prefs /** * Created by Allan Wang on 2017-05-31. @@ -53,6 +54,6 @@ enum class CssHider(vararg val items: String) : InjectorContract { .single(name).build() } - override fun inject(webView: WebView) = - injector.inject(webView) + override fun inject(webView: WebView, prefs: Prefs) = + injector.inject(webView, prefs) } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsActions.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsActions.kt index 175837ca..72138f5b 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsActions.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsActions.kt @@ -18,6 +18,7 @@ package com.pitchedapps.frost.injectors import android.webkit.WebView import com.pitchedapps.frost.facebook.FB_URL_BASE +import com.pitchedapps.frost.utils.Prefs /** * Created by Allan Wang on 2017-05-31. @@ -42,8 +43,8 @@ enum class JsActions(body: String) : InjectorContract { val function = "(function(){$body})();" - override fun inject(webView: WebView) = - JsInjector(function).inject(webView) + override fun inject(webView: WebView, prefs: Prefs) = + JsInjector(function).inject(webView, prefs) } @Suppress("NOTHING_TO_INLINE") diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt index f5a0713f..03ab6e96 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt @@ -21,6 +21,7 @@ import android.webkit.WebView import androidx.annotation.VisibleForTesting import ca.allanwang.kau.kotlin.lazyContext import com.pitchedapps.frost.utils.L +import com.pitchedapps.frost.utils.Prefs import java.io.BufferedReader import java.io.FileNotFoundException import java.util.Locale @@ -49,14 +50,14 @@ enum class JsAssets : InjectorContract { } } - override fun inject(webView: WebView) = - injector(webView.context).inject(webView) + override fun inject(webView: WebView, prefs: Prefs) = + injector(webView.context).inject(webView, prefs) companion object { // Ensures that all non themes and the selected theme are loaded suspend fun load(context: Context) { withContext(Dispatchers.IO) { - JsAssets.values().forEach { it.injector.invoke(context) } + values().forEach { it.injector.invoke(context) } } } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt index 586ff393..14e475e3 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt @@ -19,6 +19,7 @@ package com.pitchedapps.frost.injectors import android.webkit.WebView import androidx.annotation.VisibleForTesting import com.pitchedapps.frost.utils.L +import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.web.FrostWebViewClient import kotlin.random.Random import org.apache.commons.text.StringEscapeUtils @@ -83,7 +84,7 @@ class JsBuilder { * Contract for all injectors to allow it to interact properly with a webview */ interface InjectorContract { - fun inject(webView: WebView) + fun inject(webView: WebView, prefs: Prefs) /** * Toggle the injector (usually through Prefs * If false, will fallback to an empty action @@ -94,19 +95,19 @@ interface InjectorContract { /** * Helper method to inject multiple functions simultaneously with a single callback */ -fun WebView.jsInject(vararg injectors: InjectorContract) { +fun WebView.jsInject(vararg injectors: InjectorContract, prefs: Prefs) { injectors.filter { it != JsActions.EMPTY }.forEach { - it.inject(this) + it.inject(this,prefs) } } -fun FrostWebViewClient.jsInject(vararg injectors: InjectorContract) = web.jsInject(*injectors) +fun FrostWebViewClient.jsInject(vararg injectors: InjectorContract, prefs: Prefs) = web.jsInject(*injectors, prefs = prefs) /** * Wrapper class to convert a function into an injector */ class JsInjector(val function: String) : InjectorContract { - override fun inject(webView: WebView) = + override fun inject(webView: WebView, prefs: Prefs) = webView.evaluateJavascript(function, null) } |