From bfa7e3d83165ff9839b720ff4d554b6cf2be789a Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 13 Apr 2019 10:41:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/kunfei/bookshelf/MApplication.java | 16 +++++++++++++++- .../com/kunfei/bookshelf/base/MBaseActivity.java | 9 ++------- .../kunfei/bookshelf/help/ReadBookControl.java | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/kunfei/bookshelf/MApplication.java b/app/src/main/java/com/kunfei/bookshelf/MApplication.java index f2ddb0c63b..ff6344f2bf 100644 --- a/app/src/main/java/com/kunfei/bookshelf/MApplication.java +++ b/app/src/main/java/com/kunfei/bookshelf/MApplication.java @@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit; import androidx.annotation.RequiresApi; +import androidx.appcompat.app.AppCompatDelegate; import androidx.multidex.MultiDex; import io.reactivex.internal.functions.Functions; import io.reactivex.plugins.RxJavaPlugins; @@ -78,6 +79,7 @@ public void onCreate() { if (TextUtils.isEmpty(downloadPath) | Objects.equals(downloadPath, FileHelp.getCachePath())) { setDownloadPath(null); } + initNightTheme(); if (!ThemeStore.isConfigured(this, versionCode)) { upThemeStore(); } @@ -103,11 +105,19 @@ protected void attachBaseContext(Context base) { MultiDex.install(this); } + public void initNightTheme() { + if (isNightTheme()) { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); + } else { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); + } + } + /** * 初始化主题 */ public void upThemeStore() { - if (configPreferences.getBoolean("nightTheme", false)) { + if (isNightTheme()) { ThemeStore.editTheme(this) .primaryColor(configPreferences.getInt("colorPrimaryNight", getResources().getColor(R.color.md_grey_800))) .accentColor(configPreferences.getInt("colorAccentNight", getResources().getColor(R.color.md_pink_800))) @@ -122,6 +132,10 @@ public void upThemeStore() { } } + public boolean isNightTheme() { + return configPreferences.getBoolean("nightTheme", false); + } + /** * 设置下载地址 */ diff --git a/app/src/main/java/com/kunfei/bookshelf/base/MBaseActivity.java b/app/src/main/java/com/kunfei/bookshelf/base/MBaseActivity.java index cb599114cd..9f839de5e2 100644 --- a/app/src/main/java/com/kunfei/bookshelf/base/MBaseActivity.java +++ b/app/src/main/java/com/kunfei/bookshelf/base/MBaseActivity.java @@ -30,7 +30,6 @@ import java.util.ArrayList; import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatDelegate; public abstract class MBaseActivity extends BaseActivity { private static final String TAG = MBaseActivity.class.getSimpleName(); @@ -202,23 +201,19 @@ public void setOrientation(int screenDirection) { * @return 是否夜间模式 */ public boolean isNightTheme() { - return preferences.getBoolean("nightTheme", false); + return MApplication.getInstance().isNightTheme(); } protected void setNightTheme(boolean isNightTheme) { preferences.edit() .putBoolean("nightTheme", isNightTheme) .apply(); + MApplication.getInstance().initNightTheme(); MApplication.getInstance().upThemeStore(); RxBus.get().post(RxBusTag.RECREATE, true); } protected void initTheme() { - if (isNightTheme()) { - getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES); - } else { - getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO); - } if (ColorUtil.isColorLight(ThemeStore.primaryColor(this))) { setTheme(R.style.CAppTheme); } else { diff --git a/app/src/main/java/com/kunfei/bookshelf/help/ReadBookControl.java b/app/src/main/java/com/kunfei/bookshelf/help/ReadBookControl.java index 72ea58608b..ce6dc61e8e 100644 --- a/app/src/main/java/com/kunfei/bookshelf/help/ReadBookControl.java +++ b/app/src/main/java/com/kunfei/bookshelf/help/ReadBookControl.java @@ -300,7 +300,7 @@ public void setBgColor(int index, int bgColor) { } private boolean getIsNightTheme() { - return preferences.getBoolean("nightTheme", false); + return MApplication.getInstance().isNightTheme(); } public boolean getImmersionStatusBar() {