Skip to content
This repository has been archived by the owner on Feb 17, 2024. It is now read-only.

Commit

Permalink
chore: 修复预览不可用
Browse files Browse the repository at this point in the history
  • Loading branch information
HuanCheng65 committed Jul 26, 2023
1 parent 895e6a8 commit 6a53850
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 27 deletions.
35 changes: 35 additions & 0 deletions app/src/main/java/com/huanchengfly/tieba/post/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,7 +54,10 @@ data class ExtendedColors(
)

val LocalExtendedColors = staticCompositionLocalOf {
ExtendedColors("", false)
ExtendedColors(
ThemeUtil.THEME_DEFAULT,
false,
)
}

@SuppressLint("ConflictingOnColor")
Expand Down Expand Up @@ -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)),
)
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
<color name="default_color_shadow">#FEE9E9E9</color>
<color name="default_color_swipe_refresh_view_background">#FFFFFFFF</color>
<color name="default_color_chip">#FFF8F8F8</color>
<color name="default_color_on_chip">#FF808080</color>
<color name="default_color_placeholder">#FFF0F0F0</color>

<color name="theme_color_primary_white">@color/tieba</color>
<color name="theme_color_primary_tieba">@color/tieba</color>
Expand Down

0 comments on commit 6a53850

Please sign in to comment.