Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Jan 10, 2019
1 parent b98948f commit 81aaba1
Showing 1 changed file with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.kunfei.bookshelf.MApplication;
import com.kunfei.bookshelf.R;
import com.kunfei.bookshelf.help.RxBusTag;
import com.kunfei.bookshelf.utils.ColorUtil;
import com.kunfei.bookshelf.utils.Theme.ATH;
import com.kunfei.bookshelf.view.activity.ThemeSettingActivity;

Expand Down Expand Up @@ -72,6 +73,7 @@ public void onPause() {

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
AlertDialog alertDialog;
switch (key) {
case "immersionStatusBar":
case "navigationBarColorChange":
Expand All @@ -81,24 +83,55 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
case "colorPrimary":
case "colorAccent":
case "colorBackground":
MApplication.getInstance().upThemeStore();
RxBus.get().post(RxBusTag.RECREATE, true);
if (!settingActivity.isNightTheme()) {
new Handler().postDelayed(() -> getActivity().recreate(), 200);
if (!ColorUtil.isColorLight(sharedPreferences.getInt("colorBackground", settingActivity.getResources().getColor(R.color.md_grey_100)))) {
alertDialog = new AlertDialog.Builder(getActivity())
.setTitle("白天背景太暗")
.setMessage("将会恢复默认背景?")
.setPositiveButton(R.string.ok, (dialog, which) -> {
settingActivity.preferences.edit().putInt("colorBackground", settingActivity.getResources().getColor(R.color.md_grey_100)).apply();
upTheme(false);
})
.setNegativeButton(R.string.cancel, (dialogInterface, i) -> {
upTheme(false);
})
.show();
ATH.setAlertDialogTint(alertDialog);
} else {
upTheme(false);
}
break;
case "colorPrimaryNight":
case "colorAccentNight":
case "colorBackgroundNight":
MApplication.getInstance().upThemeStore();
RxBus.get().post(RxBusTag.RECREATE, true);
if (settingActivity.isNightTheme()) {
new Handler().postDelayed(() -> getActivity().recreate(), 200);
if (ColorUtil.isColorLight(sharedPreferences.getInt("colorBackgroundNight", settingActivity.getResources().getColor(R.color.md_grey_800)))) {
alertDialog = new AlertDialog.Builder(getActivity())
.setTitle("夜间背景太亮")
.setMessage("将会恢复默认背景?")
.setPositiveButton(R.string.ok, (dialog, which) -> {
settingActivity.preferences.edit().putInt("colorBackgroundNight", settingActivity.getResources().getColor(R.color.md_grey_800)).apply();
upTheme(true);
})
.setNegativeButton(R.string.cancel, (dialogInterface, i) -> {
upTheme(true);
})
.show();
ATH.setAlertDialogTint(alertDialog);

} else {
upTheme(true);
}
break;
}
}

private void upTheme(boolean isNightTheme) {
if (settingActivity.isNightTheme() == isNightTheme) {
MApplication.getInstance().upThemeStore();
RxBus.get().post(RxBusTag.RECREATE, true);
new Handler().postDelayed(() -> getActivity().recreate(), 200);
}
}

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (Objects.equals(preference.getKey(), "defaultTheme")) {
Expand Down

0 comments on commit 81aaba1

Please sign in to comment.