diff options
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt | 73 |
1 files changed, 39 insertions, 34 deletions
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 e7bc1542..13ce2920 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Utils.kt @@ -35,6 +35,7 @@ import ca.allanwang.kau.email.sendEmail import ca.allanwang.kau.mediapicker.createMediaFile import ca.allanwang.kau.mediapicker.createPrivateMediaFile import ca.allanwang.kau.utils.colorToForeground +import ca.allanwang.kau.utils.ctxCoroutine import ca.allanwang.kau.utils.darken import ca.allanwang.kau.utils.isColorDark import ca.allanwang.kau.utils.navigationBarColor @@ -64,11 +65,12 @@ import com.pitchedapps.frost.facebook.FBCDN_NET import com.pitchedapps.frost.facebook.FbCookie import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.facebook.FbUrlFormatter.Companion.VIDEO_REDIRECT +import com.pitchedapps.frost.facebook.MESSENGER_COM import com.pitchedapps.frost.facebook.USER_AGENT 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 com.pitchedapps.frost.injectors.ThemeProvider import com.pitchedapps.frost.prefs.Prefs import java.io.File import java.io.IOException @@ -76,14 +78,14 @@ import java.net.URLEncoder import java.nio.charset.StandardCharsets import java.util.ArrayList import java.util.Locale -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.launch import org.apache.commons.text.StringEscapeUtils import org.jsoup.Jsoup import org.jsoup.nodes.Document import org.jsoup.nodes.Element +import org.koin.core.component.KoinComponent +import org.koin.core.component.inject /** * Created by Allan Wang on 2017-06-03. @@ -95,14 +97,6 @@ const val ARG_IMAGE_URL = "arg_image_url" const val ARG_TEXT = "arg_text" const val ARG_COOKIE = "arg_cookie" -/** - * Most context items implement [CoroutineScope] by default. - * We will add a fallback just in case. - * It is expected that the scope returned always has the Android main dispatcher as part of the context. - */ -internal inline val Context.ctxCoroutine: CoroutineScope - get() = this as? CoroutineScope ?: GlobalScope - inline fun <reified T : Activity> Context.launchNewTask( cookieList: ArrayList<CookieEntity> = arrayListOf(), clearStack: Boolean = false @@ -181,17 +175,22 @@ fun WebOverlayActivity.url(): String { return intent.getStringExtra(ARG_URL) ?: FbItem.FEED.url } -fun Activity.setFrostTheme(prefs: Prefs, forceTransparent: Boolean = false) { +fun Activity.setFrostTheme(themeProvider: ThemeProvider, forceTransparent: Boolean = false) { val isTransparent = - forceTransparent || (Color.alpha(prefs.bgColor) != 255) || (Color.alpha(prefs.headerColor) != 255) - if (prefs.bgColor.isColorDark) { + forceTransparent || (Color.alpha(themeProvider.bgColor) != 255) || (Color.alpha( + themeProvider.headerColor + ) != 255) + if (themeProvider.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(val prefs: Prefs) { +class ActivityThemeUtils : KoinComponent { + + private val prefs: Prefs by inject() + private val themeProvider: ThemeProvider by inject() private var toolbar: Toolbar? = null var themeWindow = true @@ -217,21 +216,21 @@ class ActivityThemeUtils(val prefs: Prefs) { 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) } @@ -258,19 +257,19 @@ 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() + val themeProvider = ThemeProvider.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(themeProvider.textColor) + actionView.setTextColor(themeProvider.accentColor) // only set if previous text colors are set - view.setBackgroundColor(prefs.bgColor.withAlpha(255).colorToForeground(0.1f)) + view.setBackgroundColor(themeProvider.bgColor.withAlpha(255).colorToForeground(0.1f)) } } -fun Activity.frostNavigationBar(prefs: Prefs) { - navigationBarColor = if (prefs.tintNavBar) prefs.headerColor else Color.BLACK +fun Activity.frostNavigationBar(prefs: Prefs, themeProvider: ThemeProvider) { + navigationBarColor = if (prefs.tintNavBar) themeProvider.headerColor else Color.BLACK } @Throws(IOException::class) @@ -306,6 +305,12 @@ fun Context.resolveActivityForUri(uri: Uri): Boolean { inline val String?.isFacebookUrl get() = this != null && (contains(FACEBOOK_COM) || contains(FBCDN_NET)) +inline val String?.isMessengerUrl + get() = this != null && contains(MESSENGER_COM) + +inline val String?.isFbCookie + get() = this != null && contains("c_user") + /** * [true] if url is a video and can be accepted by VideoViewer */ @@ -453,7 +458,7 @@ fun String.unescapeHtml(): String = .replace("\\u003C", "<") .replace("\\\"", "\"") -suspend fun Context.loadAssets(prefs: Prefs): Unit = coroutineScope { - CssAssets.load(this@loadAssets, prefs) +suspend fun Context.loadAssets(themeProvider: ThemeProvider): Unit = coroutineScope { + themeProvider.preload() JsAssets.load(this@loadAssets) } |