diff options
author | Allan Wang <me@allanwang.ca> | 2021-04-17 17:49:18 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2021-04-17 17:49:18 -0700 |
commit | d96d1d06a7c1581b97c042f2a74e7cd5b1c2546e (patch) | |
tree | 60ad049189d448132d1b99ea041a8f80a83ffe22 /app/src/main/kotlin/com/pitchedapps/frost/injectors | |
parent | cb2adc75d8e105adfdf0d5646c1a9b6d39c292f8 (diff) | |
download | frost-d96d1d06a7c1581b97c042f2a74e7cd5b1c2546e.tar.gz frost-d96d1d06a7c1581b97c042f2a74e7cd5b1c2546e.tar.bz2 frost-d96d1d06a7c1581b97c042f2a74e7cd5b1c2546e.zip |
Update theme providers and readd koin modules
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/injectors')
3 files changed, 78 insertions, 32 deletions
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 55eb198e..bceb9b3e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsAssets.kt @@ -22,11 +22,11 @@ import androidx.annotation.VisibleForTesting import ca.allanwang.kau.kotlin.lazyContext import com.pitchedapps.frost.prefs.Prefs import com.pitchedapps.frost.utils.L +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. 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 2959974f..7c52fac7 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/JsInjector.kt @@ -21,8 +21,8 @@ import androidx.annotation.VisibleForTesting import com.pitchedapps.frost.prefs.Prefs import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.web.FrostWebViewClient -import kotlin.random.Random import org.apache.commons.text.StringEscapeUtils +import kotlin.random.Random class JsBuilder { private val css = StringBuilder() diff --git a/app/src/main/kotlin/com/pitchedapps/frost/injectors/ThemeProvider.kt b/app/src/main/kotlin/com/pitchedapps/frost/injectors/ThemeProvider.kt index f57b3a51..5c721389 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/injectors/ThemeProvider.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/injectors/ThemeProvider.kt @@ -30,63 +30,109 @@ import com.pitchedapps.frost.enums.Theme import com.pitchedapps.frost.enums.ThemeCategory import com.pitchedapps.frost.prefs.Prefs import com.pitchedapps.frost.utils.L +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn import dagger.hilt.android.qualifiers.ApplicationContext -import java.io.BufferedReader -import java.io.FileNotFoundException -import javax.inject.Inject +import dagger.hilt.components.SingletonComponent import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.koin.core.context.GlobalContext +import java.io.BufferedReader +import java.io.FileNotFoundException +import javax.inject.Inject +import javax.inject.Singleton + +interface ThemeProvider { + val textColor: Int + + val accentColor: Int + + val accentColorForWhite: Int + + val nativeBgColor: Int + + fun nativeBgColor(unread: Boolean): Int + + val bgColor: Int + + val headerColor: Int + + val iconColor: Int + + val isCustomTheme: Boolean + + /** + * Note that while this can be loaded from any thread, it is typically done through [preload]] + */ + fun injector(category: ThemeCategory): InjectorContract + + fun setTheme(id: Int) + + fun reset() + + suspend fun preload() + + companion object { + + fun get(): ThemeProvider = GlobalContext.get().get() + + fun module() = org.koin.dsl.module { + single<ThemeProvider> { ThemeProviderImpl(get(), get()) } + } + } +} /** * Provides [InjectorContract] for each [ThemeCategory]. * Can be reloaded to take in changes from [Prefs] */ -class ThemeProvider @Inject internal constructor( +class ThemeProviderImpl @Inject internal constructor( @ApplicationContext private val context: Context, private val prefs: Prefs -) { +) : ThemeProvider { private var theme: Theme = Theme.values[prefs.theme] + set(value) { + field = value + prefs.theme = value.ordinal + } private val injectors: MutableMap<ThemeCategory, InjectorContract> = mutableMapOf() - val textColor: Int + override val textColor: Int get() = theme.textColorGetter(prefs) - val accentColor: Int + override val accentColor: Int get() = theme.accentColorGetter(prefs) - val accentColorForWhite: Int + override val accentColorForWhite: Int get() = when { accentColor.isColorVisibleOn(Color.WHITE) -> accentColor textColor.isColorVisibleOn(Color.WHITE) -> textColor else -> FACEBOOK_BLUE } - val nativeBgColor: Int + override val nativeBgColor: Int get() = bgColor.withAlpha(30) - fun nativeBgColor(unread: Boolean) = bgColor + override fun nativeBgColor(unread: Boolean) = bgColor .colorToForeground(if (unread) 0.7f else 0.0f) .withAlpha(30) - val bgColor: Int + override val bgColor: Int get() = theme.backgroundColorGetter(prefs) - val headerColor: Int + override val headerColor: Int get() = theme.headerColorGetter(prefs) - val iconColor: Int + override val iconColor: Int get() = theme.iconColorGetter(prefs) - val isCustomTheme: Boolean + override val isCustomTheme: Boolean get() = theme == Theme.CUSTOM - /** - * Note that while this can be loaded from any thread, it is typically done through [preload]] - */ - fun injector(category: ThemeCategory): InjectorContract = + override fun injector(category: ThemeCategory): InjectorContract = injectors.getOrPut(category) { createInjector(category) } /** @@ -128,28 +174,28 @@ class ThemeProvider @Inject internal constructor( } } - fun setTheme(id: Int) { + override fun setTheme(id: Int) { + if (theme.ordinal == id) return theme = Theme.values[id] reset() } - fun reset() { + override fun reset() { injectors.clear() } - suspend fun preload() { + override suspend fun preload() { withContext(Dispatchers.IO) { reset() ThemeCategory.values().forEach { injector(it) } } } +} - companion object { - - fun get(): ThemeProvider = GlobalContext.get().get() - - fun module() = org.koin.dsl.module { - single { ThemeProvider(get(), get()) } - } - } +@Module +@InstallIn(SingletonComponent::class) +interface ThemeProviderModule { + @Binds + @Singleton + fun themeProvider(to: ThemeProviderImpl): ThemeProvider } |