From 6a538508c00466c63d6debfb08f956437980dcc4 Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Wed, 26 Jul 2023 22:39:37 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BF=AE=E5=A4=8D=E9=A2=84=E8=A7=88?= =?UTF-8?q?=E4=B8=8D=E5=8F=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/huanchengfly/tieba/post/App.kt | 35 +++++++++++ .../post/ui/common/theme/compose/Theme.kt | 59 ++++++++++--------- app/src/main/res/values/colors.xml | 2 + 3 files changed, 69 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/App.kt b/app/src/main/java/com/huanchengfly/tieba/post/App.kt index 656f6ec6..1ec7ed16 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/App.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/App.kt @@ -302,8 +302,43 @@ class App : Application(), IApp, SketchFactory { } object ThemeDelegate : ThemeSwitcher { + private fun getDefaultColorResId(attrId: Int): Int { + return when (attrId) { + R.attr.colorPrimary -> R.color.default_color_primary + R.attr.colorNewPrimary -> R.color.default_color_primary + R.attr.colorAccent -> R.color.default_color_accent + R.attr.colorOnAccent -> R.color.default_color_on_accent + R.attr.colorToolbar -> R.color.default_color_toolbar + R.attr.colorToolbarItem -> R.color.default_color_toolbar_item + R.attr.colorToolbarItemSecondary -> R.color.default_color_toolbar_item_secondary + R.attr.colorToolbarItemActive -> R.color.default_color_toolbar_item_active + R.attr.colorToolbarSurface -> R.color.default_color_toolbar_bar + R.attr.colorOnToolbarSurface -> R.color.default_color_on_toolbar_bar + R.attr.colorText -> R.color.default_color_text + R.attr.colorTextSecondary -> R.color.default_color_text_secondary + R.attr.colorTextOnPrimary -> R.color.default_color_text_on_primary + R.attr.color_text_disabled -> R.color.default_color_text_disabled + R.attr.colorBackground -> R.color.default_color_background + R.attr.colorWindowBackground -> R.color.default_color_window_background + R.attr.colorChip -> R.color.default_color_chip + R.attr.colorOnChip -> R.color.default_color_on_chip + R.attr.colorUnselected -> R.color.default_color_unselected + R.attr.colorNavBar -> R.color.default_color_nav + R.attr.colorNavBarSurface -> R.color.default_color_nav_bar_surface + R.attr.colorOnNavBarSurface -> R.color.default_color_on_nav_bar_surface + R.attr.colorCard -> R.color.default_color_card + R.attr.colorFloorCard -> R.color.default_color_floor_card + R.attr.colorDivider -> R.color.default_color_divider + R.attr.shadow_color -> R.color.default_color_shadow + R.attr.colorIndicator -> R.color.default_color_swipe_refresh_view_background + R.attr.colorPlaceholder -> R.color.default_color_placeholder + else -> R.color.transparent + } + } + @SuppressLint("DiscouragedApi") fun getColorByAttr(context: Context, attrId: Int, theme: String): Int { + if (!isInitialized) return context.getColorCompat(getDefaultColorResId(attrId)) val resources = context.resources when (attrId) { R.attr.colorPrimary -> { diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/common/theme/compose/Theme.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/common/theme/compose/Theme.kt index 8b6da84b..ecf1831b 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/common/theme/compose/Theme.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/common/theme/compose/Theme.kt @@ -14,6 +14,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext import com.huanchengfly.tieba.post.App import com.huanchengfly.tieba.post.R import com.huanchengfly.tieba.post.utils.ThemeUtil @@ -53,7 +54,10 @@ data class ExtendedColors( ) val LocalExtendedColors = staticCompositionLocalOf { - ExtendedColors("", false) + ExtendedColors( + ThemeUtil.THEME_DEFAULT, + false, + ) } @SuppressLint("ConflictingOnColor") @@ -90,71 +94,72 @@ fun getColorPalette( @Composable private fun getThemeColorForTheme(theme: String): ExtendedColors { + val context = LocalContext.current val nowTheme = ThemeUtil.getThemeTranslucent(theme) val textColor = - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorText, nowTheme)) + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorText, nowTheme)) val bottomBarColor = - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorNavBar, nowTheme)) + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorNavBar, nowTheme)) return ExtendedColors( nowTheme, ThemeUtil.isNightMode(nowTheme), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorNewPrimary, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorPrimary, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorOnAccent, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorToolbar, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorToolbarItem, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorNewPrimary, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorPrimary, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorOnAccent, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorToolbar, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorToolbarItem, nowTheme)), Color( App.ThemeDelegate.getColorByAttr( - App.INSTANCE, + context, R.attr.colorToolbarItemSecondary, nowTheme ) ), Color( App.ThemeDelegate.getColorByAttr( - App.INSTANCE, + context, R.attr.colorToolbarItemActive, nowTheme ) ), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorToolbarSurface, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorToolbarSurface, nowTheme)), Color( App.ThemeDelegate.getColorByAttr( - App.INSTANCE, + context, R.attr.colorOnToolbarSurface, nowTheme ) ), bottomBarColor, - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorNavBarSurface, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorNavBarSurface, nowTheme)), Color( App.ThemeDelegate.getColorByAttr( - App.INSTANCE, + context, R.attr.colorOnNavBarSurface, nowTheme ) ), textColor.copy(alpha = ContentAlpha.high), textColor.copy(alpha = ContentAlpha.medium), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorOnAccent, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.color_text_disabled, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorBackground, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorChip, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorOnChip, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorUnselected, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorCard, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorFloorCard, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorDivider, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.shadow_color, nowTheme)), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorIndicator, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorOnAccent, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.color_text_disabled, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorBackground, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorChip, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorOnChip, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorUnselected, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorCard, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorFloorCard, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorDivider, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.shadow_color, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorIndicator, nowTheme)), Color( App.ThemeDelegate.getColorByAttr( - App.INSTANCE, + context, R.attr.colorWindowBackground, nowTheme ) ), - Color(App.ThemeDelegate.getColorByAttr(App.INSTANCE, R.attr.colorPlaceholder, nowTheme)), + Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorPlaceholder, nowTheme)), ) } diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index ebeea165..e0ffc1fb 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -70,6 +70,8 @@ #FEE9E9E9 #FFFFFFFF #FFF8F8F8 + #FF808080 + #FFF0F0F0 @color/tieba @color/tieba