1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-09 20:42:34 +01:00

Create themeprovider

This commit is contained in:
Allan Wang 2021-01-12 20:46:02 -08:00
parent 82fa4c5d6c
commit 0e5c1ab2dd
No known key found for this signature in database
GPG Key ID: 69D90B885D405BDB
14 changed files with 52 additions and 42 deletions

View File

@ -37,6 +37,7 @@ import com.pitchedapps.frost.db.CookieEntity
import com.pitchedapps.frost.db.GenericDao
import com.pitchedapps.frost.db.selectAll
import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.utils.BiometricUtils
import com.pitchedapps.frost.utils.EXTRA_COOKIES
@ -54,6 +55,7 @@ class StartActivity : KauBaseActivity() {
private val fbCookie: FbCookie by inject()
private val prefs: Prefs by inject()
private val themeProvider: ThemeProvider by inject()
private val cookieDao: CookieDao by inject()
private val genericDao: GenericDao by inject()
@ -85,7 +87,7 @@ class StartActivity : KauBaseActivity() {
transform = CookieEntity::toSensitiveString
)}"
}
loadAssets(prefs)
loadAssets(themeProvider)
authDefer.await()
when {
cookies.isEmpty() -> launchNewTask<LoginActivity>()

View File

@ -39,6 +39,7 @@ import ca.allanwang.kau.utils.statusBarColor
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.pitchedapps.frost.R
import com.pitchedapps.frost.databinding.ActivityIntroBinding
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.intro.BaseIntroFragment
import com.pitchedapps.frost.intro.IntroAccountFragment
import com.pitchedapps.frost.intro.IntroFragmentEnd
@ -66,6 +67,7 @@ class IntroActivity : KauBaseActivity(), ViewPager.PageTransformer,
ViewPager.OnPageChangeListener {
private val prefs: Prefs by inject()
private val themeProvider: ThemeProvider by inject()
lateinit var binding: ActivityIntroBinding
private var barHasNext = true
@ -176,7 +178,7 @@ class IntroActivity : KauBaseActivity(), ViewPager.PageTransformer,
override fun finish() {
launch(NonCancellable) {
loadAssets(prefs)
loadAssets(themeProvider)
NotificationWidget.forceUpdate(this@IntroActivity)
launchNewTask<MainActivity>(cookies(), false)
super.finish()

View File

@ -236,7 +236,7 @@ class SettingsActivity : KPrefActivity() {
if (!super.backPress()) {
setResult(resultFlag)
launch(NonCancellable) {
loadAssets(prefs)
loadAssets(themeProvider)
finishSlideOut()
}
}

View File

@ -88,7 +88,7 @@ enum class Theme(
{ it.customIconColor });
@VisibleForTesting
internal val file = file?.let { "${it}.css" }
internal val file = file?.let { "$it.css" }
companion object {
val values = values() // save one instance

View File

@ -21,10 +21,12 @@ package com.pitchedapps.frost.facebook
*/
const val FACEBOOK_COM = "facebook.com"
const val MESSENGER_COM = "messenger.com"
const val FBCDN_NET = "fbcdn.net"
const val WWW_FACEBOOK_COM = "www.$FACEBOOK_COM"
const val WWW_MESSENGER_COM = "www.$MESSENGER_COM"
const val HTTPS_FACEBOOK_COM = "https://$WWW_FACEBOOK_COM"
const val HTTPS_MESSENGER_COM = "https://www.messenger.com"
const val HTTPS_MESSENGER_COM = "https://$WWW_MESSENGER_COM"
const val FACEBOOK_BASE_COM = "m.$FACEBOOK_COM"
const val FB_URL_BASE = "https://$FACEBOOK_BASE_COM/"
const val FACEBOOK_MBASIC_COM = "mbasic.$FACEBOOK_COM"

View File

@ -28,9 +28,9 @@ import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.cookies
import com.pitchedapps.frost.utils.launchLogin
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll

View File

@ -49,33 +49,33 @@ class ThemeProvider(private val context: Context, private val prefs: Prefs) {
val textColor: Int
get() = theme.textColorGetter(prefs)
val accentColor: Int
val accentColor: Int
get() = theme.accentColorGetter(prefs)
val accentColorForWhite: Int
val accentColorForWhite: Int
get() = when {
accentColor.isColorVisibleOn(Color.WHITE) -> accentColor
textColor.isColorVisibleOn(Color.WHITE) -> textColor
else -> FACEBOOK_BLUE
}
val nativeBgColor: Int
val nativeBgColor: Int
get() = bgColor.withAlpha(30)
fun nativeBgColor(unread: Boolean) = bgColor
fun nativeBgColor(unread: Boolean) = bgColor
.colorToForeground(if (unread) 0.7f else 0.0f)
.withAlpha(30)
val bgColor: Int
val bgColor: Int
get() = theme.backgroundColorGetter(prefs)
val headerColor: Int
val headerColor: Int
get() = theme.headerColorGetter(prefs)
val iconColor: Int
val iconColor: Int
get() = theme.iconColorGetter(prefs)
val isCustomTheme: Boolean
val isCustomTheme: Boolean
get() = theme == Theme.CUSTOM
/**
@ -91,7 +91,7 @@ class ThemeProvider(private val context: Context, private val prefs: Prefs) {
val file = theme.file ?: return JsActions.EMPTY
try {
var content =
context.assets.open("css/${category.folder}/theme/${file}").bufferedReader()
context.assets.open("css/${category.folder}/themes/$file").bufferedReader()
.use(BufferedReader::readText)
if (theme == Theme.CUSTOM) {
val bt = if (Color.alpha(prefs.bgColor) == 255)
@ -143,4 +143,4 @@ class ThemeProvider(private val context: Context, private val prefs: Prefs) {
single { ThemeProvider(get(), get()) }
}
}
}
}

View File

@ -26,7 +26,6 @@ import ca.allanwang.kau.utils.withAlpha
import com.pitchedapps.frost.BuildConfig
import com.pitchedapps.frost.enums.FACEBOOK_BLUE
import com.pitchedapps.frost.enums.Theme
import com.pitchedapps.frost.injectors.InjectorContract
import com.pitchedapps.frost.prefs.OldPrefs
import com.pitchedapps.frost.prefs.PrefsBase
import org.koin.core.component.KoinComponent
@ -61,8 +60,6 @@ interface ThemePrefs : PrefsBase {
val iconColor: Int
val themeInjector: InjectorContract
val isCustomTheme: Boolean
var tintNavBar: Boolean
@ -137,9 +134,6 @@ class ThemePrefsImpl(
override val iconColor: Int
get() = t.iconColorGetter(this)
override val themeInjector: InjectorContract
get() = t.injector
override val isCustomTheme: Boolean
get() = t == Theme.CUSTOM

View File

@ -27,7 +27,6 @@ import com.pitchedapps.frost.R
import com.pitchedapps.frost.activities.SettingsActivity
import com.pitchedapps.frost.enums.MainActivityLayout
import com.pitchedapps.frost.enums.Theme
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.utils.REQUEST_NAV
import com.pitchedapps.frost.utils.REQUEST_TEXT_ZOOM
import com.pitchedapps.frost.utils.frostEvent

View File

@ -67,8 +67,8 @@ import com.pitchedapps.frost.facebook.FbUrlFormatter.Companion.VIDEO_REDIRECT
import com.pitchedapps.frost.facebook.USER_AGENT
import com.pitchedapps.frost.facebook.formattedFbUri
import com.pitchedapps.frost.facebook.formattedFbUrl
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.injectors.JsAssets
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.prefs.Prefs
import java.io.File
import java.io.IOException
@ -222,21 +222,21 @@ class ActivityThemeUtils : KoinComponent {
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 = themeProvider.headerColor.darken(0.1f).withAlpha(255)
if (prefs.tintNavBar) navigationBarColor = themeProvider.headerColor
if (themeWindow) window.setBackgroundDrawable(ColorDrawable(themeProvider.bgColor))
toolbar?.setBackgroundColor(themeProvider.headerColor)
toolbar?.setTitleTextColor(themeProvider.iconColor)
toolbar?.overflowIcon?.setTint(themeProvider.iconColor)
texts.forEach { it.setTextColor(themeProvider.textColor) }
headers.forEach { it.setBackgroundColor(themeProvider.headerColor) }
backgrounds.forEach { it.setBackgroundColor(themeProvider.bgColor) }
}
}
}
inline fun Activity.setFrostColors(prefs: Prefs, builder: ActivityThemeUtils.() -> Unit) {
val themer = ActivityThemeUtils(prefs)
inline fun Activity.setFrostColors(builder: ActivityThemeUtils.() -> Unit) {
val themer = ActivityThemeUtils()
themer.builder()
themer.theme(this)
}
@ -458,7 +458,7 @@ fun String.unescapeHtml(): String =
.replace("\\u003C", "<")
.replace("\\\"", "\"")
suspend fun Context.loadAssets(prefs: Prefs): Unit = coroutineScope {
ThemeProvider.load(this@loadAssets, prefs)
suspend fun Context.loadAssets(themeProvider: ThemeProvider): Unit = coroutineScope {
themeProvider.preload()
JsAssets.load(this@loadAssets)
}

View File

@ -34,6 +34,7 @@ import com.pitchedapps.frost.facebook.FB_HOME_URL
import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.facebook.USER_AGENT
import com.pitchedapps.frost.fragments.WebFragment
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.ctxCoroutine
@ -62,6 +63,7 @@ class FrostWebView @JvmOverloads constructor(
val fbCookie: FbCookie by inject()
val prefs: Prefs by inject()
val themeProvider: ThemeProvider by inject()
val cookieDao: CookieDao by inject()
override fun reload(animate: Boolean) {

View File

@ -24,9 +24,11 @@ import android.util.AttributeSet
import android.view.View
import android.webkit.WebView
import ca.allanwang.kau.utils.withAlpha
import com.pitchedapps.frost.enums.ThemeCategory
import com.pitchedapps.frost.facebook.USER_AGENT
import com.pitchedapps.frost.injectors.CssHider
import com.pitchedapps.frost.injectors.CssAsset
import com.pitchedapps.frost.injectors.CssHider
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.injectors.jsInject
import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.utils.L
@ -50,6 +52,7 @@ class DebugWebView @JvmOverloads constructor(
) : WebView(context, attrs, defStyleAttr), KoinComponent {
private val prefs: Prefs by inject()
private val themeProvider: ThemeProvider by inject()
var onPageFinished: (String?) -> Unit = {}
init {
@ -112,7 +115,7 @@ class DebugWebView @JvmOverloads constructor(
CssHider.STORIES.maybe(!prefs.showStories),
CssHider.PEOPLE_YOU_MAY_KNOW.maybe(!prefs.showSuggestedFriends),
CssHider.SUGGESTED_GROUPS.maybe(!prefs.showSuggestedGroups),
prefs.themeInjector,
themeProvider.injector(ThemeCategory.FACEBOOK),
CssHider.NON_RECENT.maybe(
(url?.contains("?sk=h_chr") ?: false) &&
prefs.aggressiveRecents

View File

@ -23,15 +23,17 @@ import android.webkit.WebResourceResponse
import android.webkit.WebView
import android.webkit.WebViewClient
import ca.allanwang.kau.utils.withAlpha
import com.pitchedapps.frost.enums.ThemeCategory
import com.pitchedapps.frost.facebook.FACEBOOK_BASE_COM
import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.facebook.WWW_FACEBOOK_COM
import com.pitchedapps.frost.facebook.formattedFbUrl
import com.pitchedapps.frost.injectors.CssHider
import com.pitchedapps.frost.injectors.CssAsset
import com.pitchedapps.frost.injectors.CssHider
import com.pitchedapps.frost.injectors.JsActions
import com.pitchedapps.frost.injectors.JsAssets
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.injectors.jsInject
import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.utils.L
@ -70,6 +72,7 @@ open class FrostWebViewClient(val web: FrostWebView) : BaseWebViewClient() {
private val fbCookie: FbCookie get() = web.fbCookie
private val prefs: Prefs get() = web.prefs
private val themeProvider: ThemeProvider get() = web.themeProvider
private val refresh: SendChannel<Boolean> = web.parent.refreshChannel
private val isMain = web.parent.baseEnum != null
@ -110,7 +113,7 @@ open class FrostWebViewClient(val web: FrostWebView) : BaseWebViewClient() {
CssHider.STORIES.maybe(!prefs.showStories),
CssHider.PEOPLE_YOU_MAY_KNOW.maybe(!prefs.showSuggestedFriends),
CssHider.SUGGESTED_GROUPS.maybe(!prefs.showSuggestedGroups),
prefs.themeInjector,
themeProvider.injector(ThemeCategory.FACEBOOK),
CssHider.NON_RECENT.maybe(
(web.url?.contains("?sk=h_chr") ?: false) &&
prefs.aggressiveRecents

View File

@ -30,12 +30,14 @@ import ca.allanwang.kau.utils.fadeIn
import ca.allanwang.kau.utils.isVisible
import ca.allanwang.kau.utils.launchMain
import com.pitchedapps.frost.db.CookieEntity
import com.pitchedapps.frost.enums.ThemeCategory
import com.pitchedapps.frost.facebook.FB_LOGIN_URL
import com.pitchedapps.frost.facebook.FB_USER_MATCHER
import com.pitchedapps.frost.facebook.FbCookie
import com.pitchedapps.frost.facebook.USER_AGENT
import com.pitchedapps.frost.facebook.get
import com.pitchedapps.frost.injectors.CssHider
import com.pitchedapps.frost.injectors.ThemeProvider
import com.pitchedapps.frost.injectors.jsInject
import com.pitchedapps.frost.prefs.Prefs
import com.pitchedapps.frost.utils.L
@ -56,6 +58,7 @@ class LoginWebView @JvmOverloads constructor(
private val fbCookie: FbCookie by inject()
private val prefs: Prefs by inject()
private val themeProvider: ThemeProvider by inject()
private val completable: CompletableDeferred<CookieEntity> = CompletableDeferred()
private lateinit var progressCallback: (Int) -> Unit
@ -105,7 +108,7 @@ class LoginWebView @JvmOverloads constructor(
if (url.isFacebookUrl)
view.jsInject(
CssHider.CORE,
prefs.themeInjector,
themeProvider.injector(ThemeCategory.FACEBOOK),
prefs = prefs
)
}