LibreTube/app/src/main/java/com/github/libretube/util/ThemeHelper.kt

142 lines
4.5 KiB
Kotlin
Raw Normal View History

2022-06-07 13:05:49 +05:30
package com.github.libretube.util
2022-08-25 14:56:10 +05:30
import android.app.Activity
2022-06-07 13:05:49 +05:30
import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
2022-07-04 12:25:52 +05:30
import android.text.Spanned
import android.util.TypedValue
2022-06-07 13:05:49 +05:30
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
2022-07-04 12:25:52 +05:30
import androidx.core.text.HtmlCompat
2022-06-07 13:05:49 +05:30
import com.github.libretube.R
2022-09-08 21:59:00 +05:30
import com.github.libretube.constants.PreferenceKeys
2022-11-21 20:42:21 +05:30
import com.github.libretube.ui.adapters.IconsSheetAdapter
2022-07-12 21:09:00 +05:30
import com.google.android.material.color.DynamicColors
2022-06-07 13:05:49 +05:30
2022-06-26 15:56:19 +05:30
object ThemeHelper {
2022-06-07 13:05:49 +05:30
2022-08-20 13:12:24 +05:30
/**
* Set the theme, including accent color and night mode
*/
2022-07-12 21:09:00 +05:30
fun updateTheme(activity: AppCompatActivity) {
2022-08-12 18:30:07 +05:30
val themeMode = PreferenceHelper.getString(PreferenceKeys.THEME_MODE, "A")
2022-07-12 22:29:21 +05:30
2022-08-25 14:56:10 +05:30
updateAccentColor(activity)
2022-08-25 14:41:31 +05:30
applyDynamicColorsIfEnabled(activity)
2022-08-25 14:56:10 +05:30
applyPureThemeIfEnabled(activity)
2022-07-12 22:29:21 +05:30
updateThemeMode(themeMode)
2022-06-07 13:05:49 +05:30
}
2022-08-20 13:12:24 +05:30
/**
* Update the accent color of the app
*/
2022-07-12 22:29:21 +05:30
private fun updateAccentColor(
2022-08-25 14:56:10 +05:30
activity: AppCompatActivity
2022-07-12 22:29:21 +05:30
) {
2022-07-12 21:09:00 +05:30
val theme = when (
PreferenceHelper.getString(
2022-07-17 21:48:39 +05:30
PreferenceKeys.ACCENT_COLOR,
2022-07-12 21:09:00 +05:30
"purple"
)
) {
2022-08-25 14:41:31 +05:30
// set the accent color, use the pure black/white theme if enabled
2022-08-25 14:56:10 +05:30
"my" -> R.style.BaseTheme
"red" -> R.style.Theme_Red
"blue" -> R.style.Theme_Blue
"yellow" -> R.style.Theme_Yellow
"green" -> R.style.Theme_Green
"purple" -> R.style.Theme_Purple
"monochrome" -> R.style.Theme_Monochrome
2022-12-09 21:37:30 +05:30
"violet" -> R.style.Theme_Violet
2022-08-25 14:56:10 +05:30
else -> R.style.Theme_Purple
2022-06-07 13:05:49 +05:30
}
2022-07-12 21:09:00 +05:30
activity.setTheme(theme)
}
2022-08-20 13:12:24 +05:30
/**
* apply dynamic colors to the activity
*/
2022-08-25 14:41:31 +05:30
private fun applyDynamicColorsIfEnabled(activity: AppCompatActivity) {
if (
PreferenceHelper.getString(
PreferenceKeys.ACCENT_COLOR,
"purple"
) == "my"
2022-08-25 14:56:10 +05:30
) {
DynamicColors.applyToActivityIfAvailable(activity)
}
}
/**
* apply the pure black/white theme
*/
private fun applyPureThemeIfEnabled(activity: Activity) {
val pureThemeEnabled = PreferenceHelper.getBoolean(
PreferenceKeys.PURE_THEME,
false
)
if (pureThemeEnabled) activity.theme.applyStyle(R.style.Pure, true)
2022-06-07 13:05:49 +05:30
}
2022-08-20 13:12:24 +05:30
/**
* set the theme mode (light, dark, auto)
*/
2022-07-12 22:29:21 +05:30
private fun updateThemeMode(themeMode: String) {
val mode = when (themeMode) {
"A" -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
"L" -> AppCompatDelegate.MODE_NIGHT_NO
"D" -> AppCompatDelegate.MODE_NIGHT_YES
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
2022-06-07 13:05:49 +05:30
}
2022-07-12 22:29:21 +05:30
AppCompatDelegate.setDefaultNightMode(mode)
2022-06-07 13:05:49 +05:30
}
2022-08-20 13:12:24 +05:30
/**
* change the app icon
*/
2022-06-07 13:05:49 +05:30
fun changeIcon(context: Context, newLogoActivityAlias: String) {
// Disable Old Icon(s)
2022-11-21 20:42:21 +05:30
for (appIcon in IconsSheetAdapter.availableIcons) {
val activityClass = "com.github.libretube." + appIcon.activityAlias
2022-07-17 20:59:37 +05:30
2022-07-17 21:05:36 +05:30
// remove old icons
2022-06-07 13:05:49 +05:30
context.packageManager.setComponentEnabledSetting(
2022-07-17 20:59:37 +05:30
ComponentName(context.packageName, activityClass),
2022-06-07 13:05:49 +05:30
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
}
2022-07-17 20:59:37 +05:30
// set the class name for the activity alias
2022-11-21 20:42:21 +05:30
val newLogoActivityClass = "com.github.libretube." + newLogoActivityAlias
2022-06-07 13:05:49 +05:30
// Enable New Icon
context.packageManager.setComponentEnabledSetting(
2022-07-17 20:59:37 +05:30
ComponentName(context.packageName, newLogoActivityClass),
2022-06-07 13:05:49 +05:30
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)
}
2022-08-20 13:12:24 +05:30
/**
* Get a color by a resource code
*/
2022-07-23 03:28:23 +05:30
fun getThemeColor(context: Context, colorCode: Int): Int {
2022-07-04 12:25:52 +05:30
val value = TypedValue()
context.theme.resolveAttribute(colorCode, value, true)
return value.data
}
2022-08-20 13:12:24 +05:30
/**
* Get the styled app name
*/
2022-07-04 12:25:52 +05:30
fun getStyledAppName(context: Context): Spanned {
val colorPrimary = getThemeColor(context, R.attr.colorPrimaryDark)
val hexColor = String.format("#%06X", (0xFFFFFF and colorPrimary))
return HtmlCompat.fromHtml(
"Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT
)
}
2022-06-07 13:05:49 +05:30
}