Skip to content

Commit

Permalink
fix: App theme swatch
Browse files Browse the repository at this point in the history
Signed-off-by: Hu Shenghao <[email protected]>
  • Loading branch information
hushenghao committed Dec 5, 2024
1 parent 12f5922 commit 0fa150e
Show file tree
Hide file tree
Showing 23 changed files with 465 additions and 124 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### v3.2.1

- Optimize the setting of theme logic
- Optimize APK file size

### v3.2.0 (2024-12-03)
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### v3.2.1

- 优化设置主题逻辑
- 优化 APK 文件大小

### v3.2.0 (2024-12-03)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import android.app.Application
import android.content.Context
import androidx.startup.Initializer
import com.dede.android_eggs.util.ActivityActionDispatcher
import com.dede.android_eggs.views.settings.compose.prefs.ThemePrefUtil
import com.dede.basic.GlobalContext

class ApplicationInitializer : Initializer<Unit> {

override fun create(context: Context) {
val application = context.applicationContext as Application
// apply compat style
ThemePrefUtil.apply(application)
ActivityActionDispatcher.register(application)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ fun rememberThemedHashImageBitmap(
height: Int = 32
): ImageBitmap {
val context = LocalContext.current
return remember(hash, ThemeUtils.isDarkMode(context.resources)) {
return remember(hash, ThemeUtils.isDarkMode(context)) {
var bitmap = checkNotNull(BlurHashDecoder.decode(hash, width, height)) {
"BlurHash decode error! hash: ".format(hash)
}
if (ThemeUtils.isDarkMode(context.resources)) {
if (ThemeUtils.isDarkMode(context)) {
val nightMode =
Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.dede.android_eggs.util.actions

import android.app.Activity
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.appcompat.widget.AppCompatTextView
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
Expand Down Expand Up @@ -35,7 +35,7 @@ import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import com.dede.android_eggs.R
import com.dede.android_eggs.util.ActivityActionDispatcher
import com.dede.android_eggs.views.theme.AppTheme
import com.dede.android_eggs.views.theme.EasterEggsTheme
import com.dede.basic.androidLifecycleOwner
import com.dede.basic.androidSavedStateOwner
import com.dede.basic.getBoolean
Expand Down Expand Up @@ -83,7 +83,7 @@ class WarningDialogAction : ActivityActionDispatcher.ActivityAction {
)
}
composeView.setContent {
AppTheme {
EasterEggsTheme {
val context = LocalContext.current
WarningDialog(
info.title,
Expand Down Expand Up @@ -143,15 +143,17 @@ private fun WarningDialog(
val messageSpanned = remember(messageText) {
HtmlCompat.fromHtml(messageText, HtmlCompat.FROM_HTML_MODE_COMPACT)
}
val textColor = MaterialTheme.colorScheme.onPrimaryContainer.toArgb()
val textStyle = MaterialTheme.typography.bodyMedium
val textColor = MaterialTheme.colorScheme.onSurfaceVariant
AndroidView(
modifier = Modifier.fillMaxWidth(),
factory = {
TextView(it)
AppCompatTextView(it)
},
update = {
it.text = messageSpanned
it.setTextColor(textColor)
it.setTextColor(textColor.toArgb())
it.textSize = textStyle.fontSize.value
}
)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.WindowInsets
Expand All @@ -33,7 +34,6 @@ import com.dede.android_eggs.inject.FlavorFeatures
import com.dede.android_eggs.ui.composes.ReverseModalNavigationDrawer
import com.dede.android_eggs.util.LocalEvent
import com.dede.android_eggs.util.OrientationAngleSensor
import com.dede.android_eggs.util.ThemeUtils
import com.dede.android_eggs.util.compose.end
import com.dede.android_eggs.views.main.compose.AnimatorDisabledAlertDialog
import com.dede.android_eggs.views.main.compose.BottomSearchBar
Expand All @@ -51,7 +51,7 @@ import com.dede.android_eggs.views.main.util.IntentHandler
import com.dede.android_eggs.views.settings.SettingsScreen
import com.dede.android_eggs.views.settings.compose.basic.SettingPrefUtil
import com.dede.android_eggs.views.settings.compose.prefs.IconVisualEffectsPrefUtil
import com.dede.android_eggs.views.theme.AppTheme
import com.dede.android_eggs.views.theme.EasterEggsTheme
import com.dede.basic.Utils
import com.dede.basic.provider.BaseEasterEgg
import com.dede.basic.provider.EasterEgg
Expand All @@ -78,7 +78,7 @@ class EasterEggsActivity : AppCompatActivity() {
lateinit var sensor: EasterEggLogoSensorMatrixConvert

override fun onCreate(savedInstanceState: Bundle?) {
ThemeUtils.enableEdgeToEdge(this)
enableEdgeToEdge()
super.onCreate(savedInstanceState)

setContent {
Expand All @@ -87,7 +87,7 @@ class EasterEggsActivity : AppCompatActivity() {
LocalEasterEggLogoSensor provides sensor,
LocalKonfettiState provides konfettiState
) {
AppTheme {
EasterEggsTheme {
val drawerState = rememberDrawerState(DrawerValue.Closed)
ReverseModalNavigationDrawer(
drawerContent = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dede.android_eggs.views.settings.compose.prefs

import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.material.icons.Icons
Expand Down Expand Up @@ -37,8 +38,16 @@ import com.dede.android_eggs.views.theme.themeMode
fun ThemePref() {
var themeModeValue by rememberPrefIntState(KEY_NIGHT_MODE, FOLLOW_SYSTEM)
val onOptionClick = click@{ mode: Int ->
if (themeModeValue == mode) {
return@click
}
themeModeValue = mode
themeMode = themeModeValue
var appCompatMode = mode
if (appCompatMode == AMOLED) {
appCompatMode = DARK
}
AppCompatDelegate.setDefaultNightMode(appCompatMode)
LocalEvent.poster().post(ACTION_NIGHT_MODE_CHANGED)
}

Expand Down
1 change: 1 addition & 0 deletions fastlane/metadata/android/en-US/changelogs/56.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Optimize the setting of theme logic
- Optimize APK file size
1 change: 1 addition & 0 deletions fastlane/metadata/android/zh-CN/changelogs/56.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- 优化设置主题逻辑
- 优化 APK 文件大小
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -58,8 +59,7 @@ import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.dede.android_eggs.util.AGPUtils
import com.dede.android_eggs.util.ThemeUtils
import com.dede.android_eggs.views.theme.AppTheme
import com.dede.android_eggs.views.theme.EasterEggsTheme
import com.dede.basic.Utils
import com.dede.basic.copy
import kotlin.system.exitProcess
Expand All @@ -70,7 +70,7 @@ import kotlin.system.exitProcess
class CrashActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
ThemeUtils.enableEdgeToEdge(this)
enableEdgeToEdge()
super.onCreate(savedInstanceState)

val tr: Throwable? = GlobalExceptionHandler.getUncaughtException(intent)
Expand All @@ -80,7 +80,7 @@ class CrashActivity : AppCompatActivity() {
}

setContent {
AppTheme {
EasterEggsTheme {
Surface {
CrashScreen(tr)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object CustomTabsBrowser {
}

val params = CustomTabColorSchemeParams.Builder()
.setToolbarColor(ThemeUtils.getThemedSurfaceColor(context))
.setToolbarColor(ThemeUtils.getThemedSurfaceColor())
.build()

val builder = CustomTabsIntent.Builder()
Expand Down
2 changes: 1 addition & 1 deletion feature/embedding-splits/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies {
implementation(project(":theme"))

implementation(libs.androidx.core)
implementation(libs.androidx.activity)
implementation(libs.androidx.appcompat)
implementation(libs.androidx.window)
implementation(libs.androidx.startup)
implementation(libs.androidx.shapes)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.dede.android_eggs.embedding_splits

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationVector1D
import androidx.compose.animation.core.spring
Expand Down Expand Up @@ -38,29 +39,34 @@ import androidx.graphics.shapes.pillStar
import androidx.graphics.shapes.rectangle
import androidx.graphics.shapes.star
import androidx.graphics.shapes.toPath
import com.dede.android_eggs.util.ThemeUtils
import com.dede.android_eggs.views.theme.AppTheme
import com.dede.android_eggs.util.LocalEvent
import com.dede.android_eggs.views.settings.compose.prefs.ThemePrefUtil
import com.dede.android_eggs.views.theme.EasterEggsTheme
import kotlinx.coroutines.launch
import kotlin.math.max
import kotlin.random.Random

/**
* Placeholder for embedding splits
*/
class PlaceholderActivity : ComponentActivity() {
class PlaceholderActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
ThemeUtils.enableEdgeToEdge(this)
enableEdgeToEdge()
super.onCreate(savedInstanceState)

setContent {
AppTheme {
EasterEggsTheme {
@Suppress("UnusedMaterial3ScaffoldPaddingParameter")
Scaffold {
Placeholder()
}
}
}

LocalEvent.receiver(this).register(ThemePrefUtil.ACTION_NIGHT_MODE_CHANGED) {
recreate()
}
}

init {
Expand Down
4 changes: 0 additions & 4 deletions feature/widget/src/main/res/values-night/colors.xml

This file was deleted.

2 changes: 1 addition & 1 deletion feature/widget/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="appwidget_background_color">#DDE5DB</color>
<color name="appwidget_background_color">?attr/colorSurfaceVariant</color>
</resources>
1 change: 1 addition & 0 deletions theme/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ android {
dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.core)
implementation(libs.google.material)

implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui)
Expand Down
65 changes: 8 additions & 57 deletions theme/src/main/java/com/dede/android_eggs/util/ThemeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,10 @@ package com.dede.android_eggs.util
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Color
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.enableEdgeToEdge
import androidx.annotation.ColorInt
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.ui.graphics.toArgb
import com.dede.android_eggs.views.settings.compose.prefs.DynamicColorPrefUtil
import com.dede.android_eggs.views.settings.compose.prefs.ThemePrefUtil
import com.dede.android_eggs.views.theme.isDynamicColorEnable
import com.dede.android_eggs.views.theme.surfaceDark
import com.dede.android_eggs.views.theme.surfaceLight
import com.dede.android_eggs.views.theme.themeMode
import com.dede.android_eggs.views.theme.currentColorScheme


object ThemeUtils {
Expand All @@ -26,54 +16,15 @@ object ThemeUtils {
Configuration.UI_MODE_NIGHT_YES
}

fun isDarkMode(resources: Resources): Boolean {
val currentThemeMode = themeMode
return currentThemeMode == ThemePrefUtil.DARK ||
currentThemeMode == ThemePrefUtil.AMOLED ||
(currentThemeMode == ThemePrefUtil.FOLLOW_SYSTEM && isSystemNightMode(resources))
}

fun enableEdgeToEdge(activity: ComponentActivity) {
activity.enableEdgeToEdge(
statusBarStyle = SystemBarStyle.auto(
Color.TRANSPARENT, Color.TRANSPARENT
) { resources -> isDarkMode(resources) },
)

LocalEvent.receiver(activity).register(ThemePrefUtil.ACTION_NIGHT_MODE_CHANGED) {
enableEdgeToEdge(activity)
}
fun isDarkMode(context: Context): Boolean {
val themeMode = ThemePrefUtil.getThemeModeValue(context)
return themeMode == ThemePrefUtil.DARK ||
themeMode == ThemePrefUtil.AMOLED ||
(themeMode == ThemePrefUtil.FOLLOW_SYSTEM && isSystemNightMode(context.resources))
}

@ColorInt
fun getThemedSurfaceColor(context: Context): Int {
val themeModeValue = ThemePrefUtil.getThemeModeValue(context)
if (DynamicColorPrefUtil.isSupported() && isDynamicColorEnable) {
return when (themeModeValue) {
ThemePrefUtil.LIGHT -> dynamicLightColorScheme(context).surface.toArgb()
ThemePrefUtil.DARK -> dynamicDarkColorScheme(context).surface.toArgb()
ThemePrefUtil.FOLLOW_SYSTEM -> {
if (isSystemNightMode(context.resources)) {
dynamicDarkColorScheme(context).surface.toArgb()
} else {
dynamicLightColorScheme(context).surface.toArgb()
}
}
else -> Color.WHITE
}
}

return when (themeModeValue) {
ThemePrefUtil.LIGHT -> surfaceLight.toArgb()
ThemePrefUtil.DARK -> surfaceDark.toArgb()
ThemePrefUtil.FOLLOW_SYSTEM -> {
if (isSystemNightMode(context.resources)) {
surfaceDark.toArgb()
} else {
surfaceLight.toArgb()
}
}
else -> Color.WHITE
}
fun getThemedSurfaceColor(): Int {
return currentColorScheme.surface.toArgb()
}
}
Loading

0 comments on commit 0fa150e

Please sign in to comment.