Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Apr 13, 2019
1 parent a0de31d commit bfa7e3d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
16 changes: 15 additions & 1 deletion app/src/main/java/com/kunfei/bookshelf/MApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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)))
Expand All @@ -122,6 +132,10 @@ public void upThemeStore() {
}
}

public boolean isNightTheme() {
return configPreferences.getBoolean("nightTheme", false);
}

/**
* 设置下载地址
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDelegate;

public abstract class MBaseActivity<T extends IPresenter> extends BaseActivity<T> {
private static final String TAG = MBaseActivity.class.getSimpleName();
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit bfa7e3d

Please sign in to comment.