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
android:name="is_self_build"
android:value="${is_self_build}" />
<meta-data
android:name="enable_new_ui"
android:value="true" />
<activity
android:name=".activities.MainActivity"

View File

@ -111,7 +111,7 @@ class App : Application(), IApp, SketchFactory {
}
fun setIcon(
enableNewUi: Boolean = applicationMetaData.getBoolean("enable_new_ui") || appPreferences.enableNewUi,
enableNewUi: Boolean = appPreferences.enableNewUi,
) {
setOldMainActivityEnabled(!enableNewUi)
if (enableNewUi) AppIconUtil.setIcon()
@ -305,7 +305,7 @@ class App : Application(), IApp, SketchFactory {
get() = INSTANCE.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
private val isNewUi: Boolean
get() = INSTANCE.applicationMetaData.getBoolean("enable_new_ui") || INSTANCE.appPreferences.enableNewUi
get() = INSTANCE.appPreferences.enableNewUi
}
object ThemeDelegate : ThemeSwitcher {
@ -349,7 +349,14 @@ class App : Application(), IApp, SketchFactory {
val resources = context.resources
when (attrId) {
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
return if (customPrimaryColorStr != null) {
Color.parseColor(customPrimaryColorStr)
@ -370,7 +377,7 @@ class App : Application(), IApp, SketchFactory {
}
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)
if (ThemeUtil.isNightMode(theme)) {
dynamicTonalPalette.primary80.toArgb()
@ -389,7 +396,14 @@ class App : Application(), IApp, SketchFactory {
}
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)
} else {
context.getColorCompat(
@ -729,7 +743,7 @@ class App : Application(), IApp, SketchFactory {
}
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 {

View File

@ -115,7 +115,7 @@ abstract class BaseActivity : AppCompatActivity(), ExtraRefreshable, CoroutineSc
getDeviceDensity()
INSTANCE.addActivity(this)
if (isNeedSetTheme) ThemeUtil.setTheme(this)
oldTheme = ThemeUtil.getTheme()
oldTheme = ThemeUtil.getRawTheme()
if (isNeedImmersionBar) {
refreshStatusBarColor()
}
@ -137,8 +137,8 @@ abstract class BaseActivity : AppCompatActivity(), ExtraRefreshable, CoroutineSc
}
fun refreshUIIfNeed() {
if (TextUtils.equals(oldTheme, ThemeUtil.getTheme()) &&
ThemeUtil.THEME_CUSTOM != ThemeUtil.getTheme() &&
if (TextUtils.equals(oldTheme, ThemeUtil.getRawTheme()) &&
ThemeUtil.THEME_CUSTOM != ThemeUtil.getRawTheme() &&
!ThemeUtil.isTranslucentTheme()
) {
return
@ -299,7 +299,7 @@ abstract class BaseActivity : AppCompatActivity(), ExtraRefreshable, CoroutineSc
if (isNeedImmersionBar) {
refreshStatusBarColor()
}
oldTheme = ThemeUtil.getTheme()
oldTheme = ThemeUtil.getRawTheme()
}
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);
themeNames = mContext.getResources().getStringArray(R.array.themeNames);
List<String> themeList = Arrays.asList(themes);
selectedPosition = themeList.indexOf(ThemeUtil.getTheme());
selectedPosition = themeList.indexOf(ThemeUtil.getRawTheme());
}
public void refresh() {
List<String> themeList = Arrays.asList(themes);
selectedPosition = themeList.indexOf(ThemeUtil.getTheme());
selectedPosition = themeList.indexOf(ThemeUtil.getRawTheme());
notifyDataSetChanged();
}
@ -98,7 +98,7 @@ public class ThemeAdapter extends RecyclerView.Adapter<MyViewHolder> implements
}
previewView.setBackgroundTintList(ColorStateList.valueOf(toolbarColor));
holder.setItemOnClickListener(v -> {
int oldPosition = selectedPosition + 0;
int oldPosition = selectedPosition;
selectedPosition = position;
notifyItemChanged(oldPosition);
notifyItemChanged(position);

View File

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

View File

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

View File

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

View File

@ -26,14 +26,14 @@ public class PopupUtil {
Field contextField = ListPopupWindow.class.getDeclaredField("mContext");
contextField.setAccessible(true);
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(
ThemeUtils.tintDrawable(
AppCompatResources.getDrawable(context, R.drawable.bg_popup),
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(
ThemeUtils.tintDrawable(
AppCompatResources.getDrawable(context, R.drawable.bg_popup),
@ -65,13 +65,13 @@ public class PopupUtil {
Field popupField = obj.getClass().getDeclaredField("mPopup");
popupField.setAccessible(true);
MenuPopupWindow menuPopupWindow = (MenuPopupWindow) popupField.get(obj);
Log.i("Theme", ThemeUtil.INSTANCE.getThemeTranslucent());
if (ThemeUtil.INSTANCE.getThemeTranslucent().equals(ThemeUtil.THEME_TRANSLUCENT_LIGHT)) {
Log.i("Theme", ThemeUtil.INSTANCE.getCurrentTheme());
if (ThemeUtil.INSTANCE.getCurrentTheme().equals(ThemeUtil.THEME_TRANSLUCENT_LIGHT)) {
menuPopupWindow.setBackgroundDrawable(
ThemeUtils.tintDrawable(context.getDrawable(R.drawable.bg_popup),
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(
ThemeUtils.tintDrawable(context.getDrawable(R.drawable.bg_popup),
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.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
@ -118,7 +119,7 @@ object ThemeUtil {
fun switchTheme(newTheme: String, recordOldTheme: Boolean = true) {
if (recordOldTheme) {
val oldTheme = getTheme()
val oldTheme = getRawTheme()
if (!isNightMode(oldTheme)) {
dataStore.putString(KEY_OLD_THEME, oldTheme)
}
@ -224,7 +225,7 @@ object ThemeUtil {
@JvmStatic
fun isNightMode(): Boolean {
return isNightMode(getTheme())
return isNightMode(getRawTheme())
}
@JvmStatic
@ -236,7 +237,7 @@ object ThemeUtil {
}
fun isTranslucentTheme(): Boolean {
return isTranslucentTheme(getTheme())
return isTranslucentTheme(getRawTheme())
}
@JvmStatic
@ -250,8 +251,12 @@ object ThemeUtil {
)
}
fun isDynamicTheme(theme: String): Boolean {
return theme.endsWith("_dynamic")
}
fun isStatusBarFontDark(): Boolean {
val theme = getTheme()
val theme = getRawTheme()
val isToolbarPrimaryColor: Boolean = INSTANCE.appPreferences.toolbarPrimaryColor
return if (theme == THEME_CUSTOM) {
INSTANCE.appPreferences.customStatusBarFontDark
@ -269,12 +274,12 @@ object ThemeUtil {
}
fun setTheme(context: Activity) {
val nowTheme = getThemeTranslucent()
val nowTheme = getCurrentTheme()
context.setTheme(getThemeByName(nowTheme))
}
@JvmOverloads
fun getThemeTranslucent(theme: String = getTheme()): String {
fun getCurrentTheme(theme: String = getRawTheme()): String {
var nowTheme = theme
if (isTranslucentTheme(nowTheme)) {
val colorTheme =
@ -284,6 +289,8 @@ object ThemeUtil {
} else {
THEME_TRANSLUCENT_LIGHT
}
} else if (isUsingDynamicTheme() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
nowTheme = "${nowTheme}_dynamic"
}
return nowTheme
}
@ -442,7 +449,7 @@ object ThemeUtil {
}
@JvmStatic
fun getTheme(): String {
fun getRawTheme(): String {
val theme = themeState.value
return when (theme.lowercase(Locale.getDefault())) {
THEME_TRANSLUCENT,
@ -456,7 +463,8 @@ object ThemeUtil {
THEME_RED,
THEME_BLUE_DARK,
THEME_GREY_DARK,
THEME_AMOLED_DARK -> theme.lowercase(Locale.getDefault())
THEME_AMOLED_DARK,
-> theme.lowercase(Locale.getDefault())
else -> THEME_DEFAULT
}

View File

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

View File

@ -93,7 +93,7 @@ public class Util {
Button mButton = mView.findViewById(R.id.snackbar_action);
TextView mTextView = mView.findViewById(R.id.snackbar_text);
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)));
mTextView.setTextColor(view.getResources().getColor(R.color.color_text));
} else {