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/utils | |
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/utils')
5 files changed, 72 insertions, 37 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/BiometricUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/BiometricUtils.kt index c5b336bf..5a157a36 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/BiometricUtils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/BiometricUtils.kt @@ -30,6 +30,8 @@ import java.util.concurrent.Executor import java.util.concurrent.ExecutorService import java.util.concurrent.Executors import kotlinx.coroutines.CompletableDeferred +import org.koin.core.KoinComponent +import org.koin.core.inject typealias BiometricDeferred = CompletableDeferred<BiometricPrompt.CryptoObject?> @@ -37,11 +39,13 @@ typealias BiometricDeferred = CompletableDeferred<BiometricPrompt.CryptoObject?> * Container for [BiometricPrompt] * Inspired by coroutine's CommonPool */ -object BiometricUtils { +object BiometricUtils : KoinComponent { private val executor: Executor get() = pool ?: getOrCreatePoolSync() + private val prefs: Prefs by inject() + @Volatile private var pool: ExecutorService? = null @@ -65,7 +69,7 @@ object BiometricUtils { pool ?: Executors.newSingleThreadExecutor().also { pool = it } private fun shouldPrompt(context: Context): Boolean { - return Prefs.biometricsEnabled && System.currentTimeMillis() - lastUnlockTime > UNLOCK_TIME_INTERVAL + return prefs.biometricsEnabled && System.currentTimeMillis() - lastUnlockTime > UNLOCK_TIME_INTERVAL } /** diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt index 389cb6b7..393df763 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/L.kt @@ -21,6 +21,8 @@ import ca.allanwang.kau.logging.KauLogger import ca.allanwang.kau.logging.KauLoggerExtension import com.bugsnag.android.Bugsnag import com.pitchedapps.frost.BuildConfig +import org.koin.core.KoinComponent +import org.koin.core.inject /** * Created by Allan Wang on 2017-05-28. @@ -31,9 +33,11 @@ object L : KauLogger("Frost", { when (it) { Log.VERBOSE -> BuildConfig.DEBUG Log.INFO, Log.ERROR -> true - else -> BuildConfig.DEBUG || Prefs.verboseLogging + else -> BuildConfig.DEBUG || L.prefs.verboseLogging } -}) { +}), KoinComponent { + + private val prefs: Prefs by inject() inline fun test(message: () -> Any?) { _d { @@ -67,7 +71,7 @@ object L : KauLogger("Frost", { * bugsnagInit is changed per application and helps prevent crashes (if calling pre init) * analytics is changed by the user, and may be toggled throughout the app */ - if (BuildConfig.DEBUG || !bugsnagInit || !Prefs.analytics) { + if (BuildConfig.DEBUG || !bugsnagInit || !prefs.analytics) { super.logImpl(priority, message, t) } else { if (message != null) { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt index eb8e4b35..15d7ba9e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt @@ -19,6 +19,7 @@ package com.pitchedapps.frost.utils import android.graphics.Color import ca.allanwang.kau.kotlin.lazyResettable import ca.allanwang.kau.kpref.KPref +import ca.allanwang.kau.kpref.KPrefFactory import ca.allanwang.kau.utils.colorToForeground import ca.allanwang.kau.utils.isColorVisibleOn import ca.allanwang.kau.utils.withAlpha @@ -29,13 +30,15 @@ import com.pitchedapps.frost.enums.FeedSort import com.pitchedapps.frost.enums.MainActivityLayout import com.pitchedapps.frost.enums.Theme import com.pitchedapps.frost.injectors.InjectorContract +import org.koin.core.context.GlobalContext +import org.koin.dsl.module /** * Created by Allan Wang on 2017-05-28. * * Shared Preference object with lazy cached retrievals */ -object Prefs : KPref() { +class Prefs(factory: KPrefFactory) : KPref("${BuildConfig.APPLICATION_ID}.prefs", factory) { var lastLaunch: Long by kpref("last_launch", -1L) @@ -69,7 +72,7 @@ object Prefs : KPref() { var identifier: Int by kpref("identifier", -1) - private val loader = lazyResettable { Theme.values[Prefs.theme] } + private val loader = lazyResettable { Theme.values[theme] } val t: Theme by loader @@ -87,9 +90,9 @@ object Prefs : KPref() { } inline val nativeBgColor: Int - get() = Prefs.bgColor.withAlpha(30) + get() = bgColor.withAlpha(30) - fun nativeBgColor(unread: Boolean) = Prefs.bgColor + fun nativeBgColor(unread: Boolean) = bgColor .colorToForeground(if (unread) 0.7f else 0.0f) .withAlpha(30) @@ -194,5 +197,11 @@ object Prefs : KPref() { inline val mainActivityLayout: MainActivityLayout get() = MainActivityLayout(mainActivityLayoutType) - override fun deleteKeys() = arrayOf("search_bar") + companion object { + fun get(): Prefs = GlobalContext.get().koin.get() + + fun module() = module { + single { Prefs(get()) } + } + } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Showcase.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Showcase.kt index edbe7682..423eda97 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Showcase.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Showcase.kt @@ -17,18 +17,25 @@ package com.pitchedapps.frost.utils import ca.allanwang.kau.kpref.KPref +import ca.allanwang.kau.kpref.KPrefFactory +import com.pitchedapps.frost.BuildConfig +import org.koin.dsl.module /** * Created by Allan Wang on 2017-07-03. * * Showcase prefs that offer one time helpers to guide new users */ -object Showcase : KPref() { +class Showcase(factory: KPrefFactory) : KPref("${BuildConfig.APPLICATION_ID}.showcase", factory) { // check if this is the first time launching the web overlay; show snackbar if true val firstWebOverlay: Boolean by kprefSingle("first_web_overlay") val intro: Boolean by kprefSingle("intro_pages") - override fun deleteKeys() = arrayOf("shown_release", "experimental_by_default") + companion object { + fun module() = module { + single { Showcase(get()) } + } + } } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt index 66cc6a9e..cf5392cb 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt @@ -50,6 +50,7 @@ import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.SnackbarContentLayout import com.pitchedapps.frost.BuildConfig import com.pitchedapps.frost.R +import com.pitchedapps.frost.activities.BaseActivity import com.pitchedapps.frost.activities.ImageActivity import com.pitchedapps.frost.activities.LoginActivity import com.pitchedapps.frost.activities.SelectorActivity @@ -69,10 +70,6 @@ import com.pitchedapps.frost.facebook.formattedFbUri import com.pitchedapps.frost.facebook.formattedFbUrl import com.pitchedapps.frost.injectors.CssAssets import com.pitchedapps.frost.injectors.JsAssets -import java.io.File -import java.io.IOException -import java.util.ArrayList -import java.util.Locale import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.coroutineScope @@ -81,6 +78,13 @@ import org.apache.commons.text.StringEscapeUtils import org.jsoup.Jsoup import org.jsoup.nodes.Document import org.jsoup.nodes.Element +import org.koin.core.KoinComponent +import org.koin.core.context.GlobalContext +import org.koin.core.inject +import java.io.File +import java.io.IOException +import java.util.ArrayList +import java.util.Locale /** * Created by Allan Wang on 2017-06-03. @@ -124,6 +128,7 @@ fun Activity.cookies(): ArrayList<CookieEntity> { * See [requestWebOverlay] to verify the launch */ private inline fun <reified T : WebOverlayActivityBase> Context.launchWebOverlayImpl(url: String) { + val prefs = Prefs.get() val argUrl = url.formattedFbUrl L.v { "Launch received: $url\nLaunch web overlay: $argUrl" } if (argUrl.isFacebookUrl && argUrl.contains("/logout.php")) { @@ -131,10 +136,11 @@ private inline fun <reified T : WebOverlayActivityBase> Context.launchWebOverlay ctxCoroutine.launch { FbCookie.logout(this@launchWebOverlayImpl) } - } else if (!(Prefs.linksInDefaultApp && resolveActivityForUri(Uri.parse(argUrl)))) + } else if (!(prefs.linksInDefaultApp && resolveActivityForUri(Uri.parse(argUrl)))) { startActivity<T>(false, intentBuilder = { putExtra(ARG_URL, argUrl) }) + } } fun Context.launchWebOverlay(url: String) = launchWebOverlayImpl<WebOverlayActivity>(url) @@ -172,22 +178,24 @@ fun WebOverlayActivity.url(): String { } fun Activity.setFrostTheme(forceTransparent: Boolean = false) { + val prefs = Prefs.get() val isTransparent = - forceTransparent || (Color.alpha(Prefs.bgColor) != 255) || (Color.alpha(Prefs.headerColor) != 255) - if (Prefs.bgColor.isColorDark) { + forceTransparent || (Color.alpha(prefs.bgColor) != 255) || (Color.alpha(prefs.headerColor) != 255) + if (prefs.bgColor.isColorDark) { setTheme(if (isTransparent) R.style.FrostTheme_Transparent else R.style.FrostTheme) } else { setTheme(if (isTransparent) R.style.FrostTheme_Light_Transparent else R.style.FrostTheme_Light) } } -class ActivityThemeUtils { +class ActivityThemeUtils : KoinComponent { private var toolbar: Toolbar? = null var themeWindow = true private var texts = mutableListOf<TextView>() private var headers = mutableListOf<View>() private var backgrounds = mutableListOf<View>() + private val prefs: Prefs by inject() fun toolbar(toolbar: Toolbar) { this.toolbar = toolbar @@ -207,15 +215,15 @@ class ActivityThemeUtils { fun theme(activity: Activity) { with(activity) { - statusBarColor = Prefs.headerColor.darken(0.1f).withAlpha(255) - if (Prefs.tintNavBar) navigationBarColor = Prefs.headerColor - if (themeWindow) window.setBackgroundDrawable(ColorDrawable(Prefs.bgColor)) - toolbar?.setBackgroundColor(Prefs.headerColor) - toolbar?.setTitleTextColor(Prefs.iconColor) - toolbar?.overflowIcon?.setTint(Prefs.iconColor) - texts.forEach { it.setTextColor(Prefs.textColor) } - headers.forEach { it.setBackgroundColor(Prefs.headerColor) } - backgrounds.forEach { it.setBackgroundColor(Prefs.bgColor) } + statusBarColor = prefs.headerColor.darken(0.1f).withAlpha(255) + if (prefs.tintNavBar) navigationBarColor = prefs.headerColor + if (themeWindow) window.setBackgroundDrawable(ColorDrawable(prefs.bgColor)) + toolbar?.setBackgroundColor(prefs.headerColor) + toolbar?.setTitleTextColor(prefs.iconColor) + toolbar?.overflowIcon?.setTint(prefs.iconColor) + texts.forEach { it.setTextColor(prefs.textColor) } + headers.forEach { it.setBackgroundColor(prefs.headerColor) } + backgrounds.forEach { it.setBackgroundColor(prefs.bgColor) } } } } @@ -248,18 +256,20 @@ fun View.frostSnackbar(@StringRes text: Int, builder: Snackbar.() -> Unit = {}) @SuppressLint("RestrictedApi") private inline fun frostSnackbar(crossinline builder: Snackbar.() -> Unit): Snackbar.() -> Unit = { + val prefs = Prefs.get() builder() // hacky workaround, but it has proper checks and shouldn't crash ((view as? FrameLayout)?.getChildAt(0) as? SnackbarContentLayout)?.apply { - messageView.setTextColor(Prefs.textColor) - actionView.setTextColor(Prefs.accentColor) + messageView.setTextColor(prefs.textColor) + actionView.setTextColor(prefs.accentColor) // only set if previous text colors are set - view.setBackgroundColor(Prefs.bgColor.withAlpha(255).colorToForeground(0.1f)) + view.setBackgroundColor(prefs.bgColor.withAlpha(255).colorToForeground(0.1f)) } } fun Activity.frostNavigationBar() { - navigationBarColor = if (Prefs.tintNavBar) Prefs.headerColor else Color.BLACK + val prefs = Prefs.get() + navigationBarColor = if (prefs.tintNavBar) prefs.headerColor else Color.BLACK } @Throws(IOException::class) @@ -393,10 +403,11 @@ inline fun Context.sendFrostEmail(subjectId: String, crossinline builder: EmailB } fun EmailBuilder.addFrostDetails() { - addItem("Prev version", Prefs.prevVersionCode.toString()) + val prefs = Prefs.get() + addItem("Prev version", prefs.prevVersionCode.toString()) val proTag = "FO" // if (IS_FROST_PRO) "TY" else "FP" - addItem("Random Frost ID", "${Prefs.frostId}-$proTag") + addItem("Random Frost ID", "${prefs.frostId}-$proTag") addItem("Locale", Locale.getDefault().displayName) } @@ -438,7 +449,7 @@ fun String.unescapeHtml(): String = .replace("\\u003C", "<") .replace("\\\"", "\"") -suspend fun Context.loadAssets(): Unit = coroutineScope { - CssAssets.load(this@loadAssets) +suspend fun Context.loadAssets(prefs: Prefs): Unit = coroutineScope { + CssAssets.load(this@loadAssets, prefs) JsAssets.load(this@loadAssets) } |