fix: 动态取色时主题预览异常

This commit is contained in:
HuanCheng65 2023-10-06 14:50:16 +08:00
parent d231519e24
commit 0f1e2b450f
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
11 changed files with 53 additions and 34 deletions

View File

@ -126,9 +126,6 @@
<meta-data <meta-data
android:name="is_self_build" android:name="is_self_build"
android:value="${is_self_build}" /> android:value="${is_self_build}" />
<meta-data
android:name="enable_new_ui"
android:value="true" />
<activity <activity
android:name=".activities.MainActivity" android:name=".activities.MainActivity"

View File

@ -111,7 +111,7 @@ class App : Application(), IApp, SketchFactory {
} }
fun setIcon( fun setIcon(
enableNewUi: Boolean = applicationMetaData.getBoolean("enable_new_ui") || appPreferences.enableNewUi, enableNewUi: Boolean = appPreferences.enableNewUi,
) { ) {
setOldMainActivityEnabled(!enableNewUi) setOldMainActivityEnabled(!enableNewUi)
if (enableNewUi) AppIconUtil.setIcon() if (enableNewUi) AppIconUtil.setIcon()
@ -305,7 +305,7 @@ class App : Application(), IApp, SketchFactory {
get() = INSTANCE.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK get() = INSTANCE.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
private val isNewUi: Boolean private val isNewUi: Boolean
get() = INSTANCE.applicationMetaData.getBoolean("enable_new_ui") || INSTANCE.appPreferences.enableNewUi get() = INSTANCE.appPreferences.enableNewUi
} }
object ThemeDelegate : ThemeSwitcher { object ThemeDelegate : ThemeSwitcher {
@ -349,7 +349,14 @@ class App : Application(), IApp, SketchFactory {
val resources = context.resources val resources = context.resources
when (attrId) { when (attrId) {
R.attr.colorPrimary -> { R.attr.colorPrimary -> {
if (ThemeUtil.THEME_CUSTOM == theme) { if (ThemeUtil.isDynamicTheme(theme) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val dynamicTonalPalette = dynamicTonalPalette(context)
return if (ThemeUtil.isNightMode(theme)) {
dynamicTonalPalette.primary80.toArgb()
} else {
dynamicTonalPalette.primary40.toArgb()
}
} else if (ThemeUtil.THEME_CUSTOM == theme) {
val customPrimaryColorStr = context.appPreferences.customPrimaryColor val customPrimaryColorStr = context.appPreferences.customPrimaryColor
return if (customPrimaryColorStr != null) { return if (customPrimaryColorStr != null) {
Color.parseColor(customPrimaryColorStr) Color.parseColor(customPrimaryColorStr)
@ -370,7 +377,7 @@ class App : Application(), IApp, SketchFactory {
} }
R.attr.colorNewPrimary -> { R.attr.colorNewPrimary -> {
return if (ThemeUtil.isUsingDynamicTheme() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { return if (ThemeUtil.isDynamicTheme(theme) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val dynamicTonalPalette = dynamicTonalPalette(context) val dynamicTonalPalette = dynamicTonalPalette(context)
if (ThemeUtil.isNightMode(theme)) { if (ThemeUtil.isNightMode(theme)) {
dynamicTonalPalette.primary80.toArgb() dynamicTonalPalette.primary80.toArgb()
@ -389,7 +396,14 @@ class App : Application(), IApp, SketchFactory {
} }
R.attr.colorAccent -> { R.attr.colorAccent -> {
return if (ThemeUtil.THEME_CUSTOM == theme || ThemeUtil.isTranslucentTheme(theme)) { return if (ThemeUtil.isDynamicTheme(theme) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val dynamicTonalPalette = dynamicTonalPalette(context)
if (ThemeUtil.isNightMode(theme)) {
dynamicTonalPalette.secondary80.toArgb()
} else {
dynamicTonalPalette.secondary40.toArgb()
}
} else if (ThemeUtil.THEME_CUSTOM == theme || ThemeUtil.isTranslucentTheme(theme)) {
getColorByAttr(context, R.attr.colorPrimary, theme) getColorByAttr(context, R.attr.colorPrimary, theme)
} else { } else {
context.getColorCompat( context.getColorCompat(
@ -729,7 +743,7 @@ class App : Application(), IApp, SketchFactory {
} }
override fun getColorByAttr(context: Context, attrId: Int): Int { override fun getColorByAttr(context: Context, attrId: Int): Int {
return getColorByAttr(context, attrId, ThemeUtil.getThemeTranslucent()) return getColorByAttr(context, attrId, ThemeUtil.getCurrentTheme())
} }
override fun getColorById(context: Context, colorId: Int): Int { override fun getColorById(context: Context, colorId: Int): Int {

View File

@ -115,7 +115,7 @@ abstract class BaseActivity : AppCompatActivity(), ExtraRefreshable, CoroutineSc
getDeviceDensity() getDeviceDensity()
INSTANCE.addActivity(this) INSTANCE.addActivity(this)
if (isNeedSetTheme) ThemeUtil.setTheme(this) if (isNeedSetTheme) ThemeUtil.setTheme(this)
oldTheme = ThemeUtil.getTheme() oldTheme = ThemeUtil.getRawTheme()
if (isNeedImmersionBar) { if (isNeedImmersionBar) {
refreshStatusBarColor() refreshStatusBarColor()
} }
@ -137,8 +137,8 @@ abstract class BaseActivity : AppCompatActivity(), ExtraRefreshable, CoroutineSc
} }
fun refreshUIIfNeed() { fun refreshUIIfNeed() {
if (TextUtils.equals(oldTheme, ThemeUtil.getTheme()) && if (TextUtils.equals(oldTheme, ThemeUtil.getRawTheme()) &&
ThemeUtil.THEME_CUSTOM != ThemeUtil.getTheme() && ThemeUtil.THEME_CUSTOM != ThemeUtil.getRawTheme() &&
!ThemeUtil.isTranslucentTheme() !ThemeUtil.isTranslucentTheme()
) { ) {
return return
@ -299,7 +299,7 @@ abstract class BaseActivity : AppCompatActivity(), ExtraRefreshable, CoroutineSc
if (isNeedImmersionBar) { if (isNeedImmersionBar) {
refreshStatusBarColor() refreshStatusBarColor()
} }
oldTheme = ThemeUtil.getTheme() oldTheme = ThemeUtil.getRawTheme()
} }
private fun recreateIfNeed(): Boolean { private fun recreateIfNeed(): Boolean {

View File

@ -44,12 +44,12 @@ public class ThemeAdapter extends RecyclerView.Adapter<MyViewHolder> implements
themes = mContext.getResources().getStringArray(R.array.theme_values); themes = mContext.getResources().getStringArray(R.array.theme_values);
themeNames = mContext.getResources().getStringArray(R.array.themeNames); themeNames = mContext.getResources().getStringArray(R.array.themeNames);
List<String> themeList = Arrays.asList(themes); List<String> themeList = Arrays.asList(themes);
selectedPosition = themeList.indexOf(ThemeUtil.getTheme()); selectedPosition = themeList.indexOf(ThemeUtil.getRawTheme());
} }
public void refresh() { public void refresh() {
List<String> themeList = Arrays.asList(themes); List<String> themeList = Arrays.asList(themes);
selectedPosition = themeList.indexOf(ThemeUtil.getTheme()); selectedPosition = themeList.indexOf(ThemeUtil.getRawTheme());
notifyDataSetChanged(); notifyDataSetChanged();
} }
@ -98,7 +98,7 @@ public class ThemeAdapter extends RecyclerView.Adapter<MyViewHolder> implements
} }
previewView.setBackgroundTintList(ColorStateList.valueOf(toolbarColor)); previewView.setBackgroundTintList(ColorStateList.valueOf(toolbarColor));
holder.setItemOnClickListener(v -> { holder.setItemOnClickListener(v -> {
int oldPosition = selectedPosition + 0; int oldPosition = selectedPosition;
selectedPosition = position; selectedPosition = position;
notifyItemChanged(oldPosition); notifyItemChanged(oldPosition);
notifyItemChanged(position); notifyItemChanged(position);

View File

@ -267,7 +267,7 @@ public class WebViewFragment extends BaseFragment implements DownloadListener, B
private void injectJavaScript() { private void injectJavaScript() {
if (mWebView == null) return; if (mWebView == null) return;
mWebView.evaluateJavascript(clipboardGuardJs, null); mWebView.evaluateJavascript(clipboardGuardJs, null);
String nowTheme = ThemeUtil.getTheme(); String nowTheme = ThemeUtil.getRawTheme();
String url = mWebView.getUrl(); String url = mWebView.getUrl();
if (url == null || nowTheme == null) { if (url == null || nowTheme == null) {
return; return;

View File

@ -314,7 +314,7 @@ private fun getBlackDarkDynamicColor(tonalPalette: TonalPalette): ExtendedColors
@Composable @Composable
private fun getThemeColorForTheme(theme: String): ExtendedColors { private fun getThemeColorForTheme(theme: String): ExtendedColors {
val context = LocalContext.current val context = LocalContext.current
val nowTheme = ThemeUtil.getThemeTranslucent(theme) val nowTheme = ThemeUtil.getCurrentTheme(theme)
val textColor = val textColor =
Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorText, nowTheme)) Color(App.ThemeDelegate.getColorByAttr(context, R.attr.colorText, nowTheme))
val bottomBarColor = val bottomBarColor =

View File

@ -423,7 +423,7 @@ fun AppThemePage(
Color( Color(
App.ThemeDelegate.getColorByAttr( App.ThemeDelegate.getColorByAttr(
context, context,
R.attr.colorOnBackground, R.attr.colorText,
item item
) )
) )

View File

@ -26,14 +26,14 @@ public class PopupUtil {
Field contextField = ListPopupWindow.class.getDeclaredField("mContext"); Field contextField = ListPopupWindow.class.getDeclaredField("mContext");
contextField.setAccessible(true); contextField.setAccessible(true);
Context context = (Context) contextField.get(listPopupWindow); Context context = (Context) contextField.get(listPopupWindow);
if (ThemeUtil.INSTANCE.getThemeTranslucent().equals(ThemeUtil.THEME_TRANSLUCENT_LIGHT)) { if (ThemeUtil.INSTANCE.getCurrentTheme().equals(ThemeUtil.THEME_TRANSLUCENT_LIGHT)) {
listPopupWindow.setBackgroundDrawable( listPopupWindow.setBackgroundDrawable(
ThemeUtils.tintDrawable( ThemeUtils.tintDrawable(
AppCompatResources.getDrawable(context, R.drawable.bg_popup), AppCompatResources.getDrawable(context, R.drawable.bg_popup),
context.getResources().getColor(R.color.theme_color_background_light) context.getResources().getColor(R.color.theme_color_background_light)
) )
); );
} else if (ThemeUtil.INSTANCE.getThemeTranslucent().equals(ThemeUtil.THEME_TRANSLUCENT_DARK)) { } else if (ThemeUtil.INSTANCE.getCurrentTheme().equals(ThemeUtil.THEME_TRANSLUCENT_DARK)) {
listPopupWindow.setBackgroundDrawable( listPopupWindow.setBackgroundDrawable(
ThemeUtils.tintDrawable( ThemeUtils.tintDrawable(
AppCompatResources.getDrawable(context, R.drawable.bg_popup), AppCompatResources.getDrawable(context, R.drawable.bg_popup),
@ -65,13 +65,13 @@ public class PopupUtil {
Field popupField = obj.getClass().getDeclaredField("mPopup"); Field popupField = obj.getClass().getDeclaredField("mPopup");
popupField.setAccessible(true); popupField.setAccessible(true);
MenuPopupWindow menuPopupWindow = (MenuPopupWindow) popupField.get(obj); MenuPopupWindow menuPopupWindow = (MenuPopupWindow) popupField.get(obj);
Log.i("Theme", ThemeUtil.INSTANCE.getThemeTranslucent()); Log.i("Theme", ThemeUtil.INSTANCE.getCurrentTheme());
if (ThemeUtil.INSTANCE.getThemeTranslucent().equals(ThemeUtil.THEME_TRANSLUCENT_LIGHT)) { if (ThemeUtil.INSTANCE.getCurrentTheme().equals(ThemeUtil.THEME_TRANSLUCENT_LIGHT)) {
menuPopupWindow.setBackgroundDrawable( menuPopupWindow.setBackgroundDrawable(
ThemeUtils.tintDrawable(context.getDrawable(R.drawable.bg_popup), ThemeUtils.tintDrawable(context.getDrawable(R.drawable.bg_popup),
context.getResources().getColor(R.color.theme_color_background_light)) context.getResources().getColor(R.color.theme_color_background_light))
); );
} else if (ThemeUtil.INSTANCE.getThemeTranslucent().equals(ThemeUtil.THEME_TRANSLUCENT_DARK)) { } else if (ThemeUtil.INSTANCE.getCurrentTheme().equals(ThemeUtil.THEME_TRANSLUCENT_DARK)) {
menuPopupWindow.setBackgroundDrawable( menuPopupWindow.setBackgroundDrawable(
ThemeUtils.tintDrawable(context.getDrawable(R.drawable.bg_popup), ThemeUtils.tintDrawable(context.getDrawable(R.drawable.bg_popup),
context.getResources().getColor(R.color.theme_color_background_dark)) context.getResources().getColor(R.color.theme_color_background_dark))

View File

@ -6,6 +6,7 @@ import android.content.res.ColorStateList
import android.graphics.Color import android.graphics.Color
import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.os.Build
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.webkit.WebView import android.webkit.WebView
@ -118,7 +119,7 @@ object ThemeUtil {
fun switchTheme(newTheme: String, recordOldTheme: Boolean = true) { fun switchTheme(newTheme: String, recordOldTheme: Boolean = true) {
if (recordOldTheme) { if (recordOldTheme) {
val oldTheme = getTheme() val oldTheme = getRawTheme()
if (!isNightMode(oldTheme)) { if (!isNightMode(oldTheme)) {
dataStore.putString(KEY_OLD_THEME, oldTheme) dataStore.putString(KEY_OLD_THEME, oldTheme)
} }
@ -224,7 +225,7 @@ object ThemeUtil {
@JvmStatic @JvmStatic
fun isNightMode(): Boolean { fun isNightMode(): Boolean {
return isNightMode(getTheme()) return isNightMode(getRawTheme())
} }
@JvmStatic @JvmStatic
@ -236,7 +237,7 @@ object ThemeUtil {
} }
fun isTranslucentTheme(): Boolean { fun isTranslucentTheme(): Boolean {
return isTranslucentTheme(getTheme()) return isTranslucentTheme(getRawTheme())
} }
@JvmStatic @JvmStatic
@ -250,8 +251,12 @@ object ThemeUtil {
) )
} }
fun isDynamicTheme(theme: String): Boolean {
return theme.endsWith("_dynamic")
}
fun isStatusBarFontDark(): Boolean { fun isStatusBarFontDark(): Boolean {
val theme = getTheme() val theme = getRawTheme()
val isToolbarPrimaryColor: Boolean = INSTANCE.appPreferences.toolbarPrimaryColor val isToolbarPrimaryColor: Boolean = INSTANCE.appPreferences.toolbarPrimaryColor
return if (theme == THEME_CUSTOM) { return if (theme == THEME_CUSTOM) {
INSTANCE.appPreferences.customStatusBarFontDark INSTANCE.appPreferences.customStatusBarFontDark
@ -269,12 +274,12 @@ object ThemeUtil {
} }
fun setTheme(context: Activity) { fun setTheme(context: Activity) {
val nowTheme = getThemeTranslucent() val nowTheme = getCurrentTheme()
context.setTheme(getThemeByName(nowTheme)) context.setTheme(getThemeByName(nowTheme))
} }
@JvmOverloads @JvmOverloads
fun getThemeTranslucent(theme: String = getTheme()): String { fun getCurrentTheme(theme: String = getRawTheme()): String {
var nowTheme = theme var nowTheme = theme
if (isTranslucentTheme(nowTheme)) { if (isTranslucentTheme(nowTheme)) {
val colorTheme = val colorTheme =
@ -284,6 +289,8 @@ object ThemeUtil {
} else { } else {
THEME_TRANSLUCENT_LIGHT THEME_TRANSLUCENT_LIGHT
} }
} else if (isUsingDynamicTheme() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
nowTheme = "${nowTheme}_dynamic"
} }
return nowTheme return nowTheme
} }
@ -442,7 +449,7 @@ object ThemeUtil {
} }
@JvmStatic @JvmStatic
fun getTheme(): String { fun getRawTheme(): String {
val theme = themeState.value val theme = themeState.value
return when (theme.lowercase(Locale.getDefault())) { return when (theme.lowercase(Locale.getDefault())) {
THEME_TRANSLUCENT, THEME_TRANSLUCENT,
@ -456,7 +463,8 @@ object ThemeUtil {
THEME_RED, THEME_RED,
THEME_BLUE_DARK, THEME_BLUE_DARK,
THEME_GREY_DARK, THEME_GREY_DARK,
THEME_AMOLED_DARK -> theme.lowercase(Locale.getDefault()) THEME_AMOLED_DARK,
-> theme.lowercase(Locale.getDefault())
else -> THEME_DEFAULT else -> THEME_DEFAULT
} }

View File

@ -33,7 +33,7 @@ public class TiebaLiteJavaScript {
@JavascriptInterface @JavascriptInterface
public String getTheme() { public String getTheme() {
return ThemeUtil.getTheme(); return ThemeUtil.getRawTheme();
} }
@JavascriptInterface @JavascriptInterface

View File

@ -93,7 +93,7 @@ public class Util {
Button mButton = mView.findViewById(R.id.snackbar_action); Button mButton = mView.findViewById(R.id.snackbar_action);
TextView mTextView = mView.findViewById(R.id.snackbar_text); TextView mTextView = mView.findViewById(R.id.snackbar_text);
mButton.setTextAppearance(view.getContext(), R.style.TextAppearance_Bold); mButton.setTextAppearance(view.getContext(), R.style.TextAppearance_Bold);
if (ThemeUtil.THEME_TRANSLUCENT.equals(ThemeUtil.getTheme())) { if (ThemeUtil.THEME_TRANSLUCENT.equals(ThemeUtil.getRawTheme())) {
mView.setBackgroundTintList(ColorStateList.valueOf(view.getResources().getColor(R.color.white))); mView.setBackgroundTintList(ColorStateList.valueOf(view.getResources().getColor(R.color.white)));
mTextView.setTextColor(view.getResources().getColor(R.color.color_text)); mTextView.setTextColor(view.getResources().getColor(R.color.color_text));
} else { } else {