Skip to content

Commit

Permalink
Significantly simplify ThemeController.java
Browse files Browse the repository at this point in the history
- Slight change in accent color preference title;
  • Loading branch information
BlackyHawky committed Nov 16, 2024
1 parent 1a3615c commit 76d2914
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package com.best.deskclock.settings;

import static com.best.deskclock.settings.ThemeController.Setting.CHANGED;

import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
Expand Down Expand Up @@ -132,85 +134,49 @@ public boolean onPreferenceChange(Preference pref, Object newValue) {
case 1 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
case 2 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
mAutoNightAccentColorPref.setChecked(DataModel.getDataModel().isAutoNightAccentColorEnabled());
mAutoNightAccentColorPref.setVisible(index != 1);
mNightAccentColorPref.setVisible(index != 1 && !mAutoNightAccentColorPref.isChecked());
}

case KEY_DARK_MODE -> {
final ListPreference amoledPref = (ListPreference) pref;
final int darkModeIndex = amoledPref.findIndexOfValue((String) newValue);
amoledPref.setSummary(amoledPref.getEntries()[darkModeIndex]);
if (Utils.isNight(requireActivity().getResources())) {
switch (darkModeIndex) {
case 0 -> ThemeController.applyDarkMode(ThemeController.DarkMode.DEFAULT_DARK_MODE);
case 1 -> ThemeController.applyDarkMode(ThemeController.DarkMode.AMOLED);
}
ThemeController.setNewSetting(CHANGED);
}
}

case KEY_ACCENT_COLOR -> {
final ListPreference accentColorPref = (ListPreference) pref;
final int index = accentColorPref.findIndexOfValue((String) newValue);
accentColorPref.setSummary(accentColorPref.getEntries()[index]);
switch (index) {
case 0 -> ThemeController.applyAccentColor(ThemeController.AccentColor.DEFAULT);
case 1 -> ThemeController.applyAccentColor(ThemeController.AccentColor.BLUE_GRAY);
case 2 -> ThemeController.applyAccentColor(ThemeController.AccentColor.BROWN);
case 3 -> ThemeController.applyAccentColor(ThemeController.AccentColor.GREEN);
case 4 -> ThemeController.applyAccentColor(ThemeController.AccentColor.INDIGO);
case 5 -> ThemeController.applyAccentColor(ThemeController.AccentColor.ORANGE);
case 6 -> ThemeController.applyAccentColor(ThemeController.AccentColor.PINK);
case 7 -> ThemeController.applyAccentColor(ThemeController.AccentColor.RED);
case 8 -> ThemeController.applyAccentColor(ThemeController.AccentColor.BLACK);
case 9 -> ThemeController.applyAccentColor(ThemeController.AccentColor.PURPLE);
case 10 -> ThemeController.applyAccentColor(ThemeController.AccentColor.YELLOW);
}
ThemeController.setNewSetting(CHANGED);
}

case KEY_AUTO_NIGHT_ACCENT_COLOR -> {
ThemeController.recreateActivityForAutoNightAccentColor(ThemeController.AutoNightAccentColor.TOGGLED);
ThemeController.setNewSettingWithDelay(CHANGED);
Utils.setVibrationTime(requireContext(), 50);
}

case KEY_NIGHT_ACCENT_COLOR -> {
final ListPreference nightAccentColorPref = (ListPreference) pref;
final int index = nightAccentColorPref.findIndexOfValue((String) newValue);
nightAccentColorPref.setSummary(nightAccentColorPref.getEntries()[index]);
switch (index) {
case 0 -> ThemeController.applyNightAccentColor(ThemeController.NightAccentColor.DEFAULT_NIGHT);
case 1 -> ThemeController.applyNightAccentColor(ThemeController.NightAccentColor.NIGHT_BLUE_GRAY);
case 2 -> ThemeController.applyNightAccentColor(ThemeController.NightAccentColor.NIGHT_BROWN);
case 3 -> ThemeController.applyNightAccentColor(ThemeController.NightAccentColor.NIGHT_GREEN);
case 4 -> ThemeController.applyNightAccentColor(ThemeController.NightAccentColor.NIGHT_INDIGO);
case 5 -> ThemeController.applyNightAccentColor(ThemeController.NightAccentColor.NIGHT_ORANGE);
case 6 -> ThemeController.applyNightAccentColor(ThemeController.NightAccentColor.NIGHT_PINK);
case 7 -> ThemeController.applyNightAccentColor(ThemeController.NightAccentColor.NIGHT_RED);
case 8 -> ThemeController.applyNightAccentColor(ThemeController.NightAccentColor.NIGHT_BLACK);
case 9 -> ThemeController.applyNightAccentColor(ThemeController.NightAccentColor.NIGHT_PURPLE);
case 10 -> ThemeController.applyNightAccentColor(ThemeController.NightAccentColor.NIGHT_YELLOW);
if (Utils.isNight(requireContext().getResources())) {
ThemeController.setNewSetting(CHANGED);
}
}

case KEY_CARD_BACKGROUND -> {
final TwoStatePreference cardBackgroundPref = (TwoStatePreference) pref;
cardBackgroundPref.setChecked(DataModel.getDataModel().isCardBackgroundDisplayed());
if (cardBackgroundPref.isChecked()) {
ThemeController.applyLayoutBackground(ThemeController.LayoutBackground.TRANSPARENT);
} else {
ThemeController.applyLayoutBackground(ThemeController.LayoutBackground.DEFAULT);
}
ThemeController.setNewSettingWithDelay(CHANGED);
Utils.setVibrationTime(requireContext(), 50);
}

case KEY_CARD_BORDER -> {
final TwoStatePreference cardBorderPref = (TwoStatePreference) pref;
cardBorderPref.setChecked(DataModel.getDataModel().isCardBorderDisplayed());
if (cardBorderPref.isChecked()) {
ThemeController.applyLayoutBorderedSettings(ThemeController.LayoutBorder.BORDERED);
} else {
ThemeController.applyLayoutBorderedSettings(ThemeController.LayoutBorder.DEFAULT);
}
ThemeController.setNewSettingWithDelay(CHANGED);
Utils.setVibrationTime(requireContext(), 50);
}

Expand All @@ -223,18 +189,15 @@ public boolean onPreferenceChange(Preference pref, Object newValue) {
case KEY_TAB_INDICATOR -> {
final TwoStatePreference tabIndicatorPref = (TwoStatePreference) pref;
tabIndicatorPref.setChecked(DataModel.getDataModel().isTabIndicatorDisplayed());
ThemeController.setNewSettingWithDelay(CHANGED);
Utils.setVibrationTime(requireContext(), 50);
requireActivity().setResult(RESULT_OK);
}

case KEY_FADE_TRANSITIONS -> {
final TwoStatePreference fadeTransitionsPref = (TwoStatePreference) pref;
fadeTransitionsPref.setChecked(DataModel.getDataModel().isFadeTransitionsEnabled());
if (fadeTransitionsPref.isChecked()) {
ThemeController.enableFadeTransitions(ThemeController.FadeTransitions.ENABLED);
} else {
ThemeController.enableFadeTransitions(ThemeController.FadeTransitions.DISABLED);
}
ThemeController.setNewSettingWithDelay(CHANGED);
Utils.setVibrationTime(requireContext(), 50);
}
}
Expand All @@ -246,11 +209,15 @@ private void setupPreferences() {
final Vibrator vibrator = (Vibrator) requireContext().getSystemService(Context.VIBRATOR_SERVICE);
mVibrationPref.setVisible(vibrator.hasVibrator());

final int themeIndex = mThemePref.findIndexOfValue(DataModel.getDataModel().getTheme().toLowerCase());
mAutoNightAccentColorPref.setChecked(DataModel.getDataModel().isAutoNightAccentColorEnabled());
// themeIndex != 1 --> System or Dark
mAutoNightAccentColorPref.setVisible(themeIndex != 1);
mNightAccentColorPref.setVisible(themeIndex != 1 && !mAutoNightAccentColorPref.isChecked());
mNightAccentColorPref.setVisible(!mAutoNightAccentColorPref.isChecked());
if (mAutoNightAccentColorPref.isChecked()) {
mAccentColorPref.setTitle(requireContext().getString(R.string.title_accent_color));
mAccentColorPref.setDialogTitle(requireContext().getString(R.string.title_accent_color));
} else {
mAccentColorPref.setTitle(requireContext().getString(R.string.day_accent_color_title));
mAccentColorPref.setDialogTitle(requireContext().getString(R.string.day_accent_color_title));
}
}

private void refresh() {
Expand All @@ -266,8 +233,6 @@ private void refresh() {
mAutoNightAccentColorPref.setChecked(DataModel.getDataModel().isAutoNightAccentColorEnabled());
mAutoNightAccentColorPref.setOnPreferenceChangeListener(this);

final int themeIndex = mThemePref.findIndexOfValue(DataModel.getDataModel().getTheme());
mNightAccentColorPref.setVisible(!mAutoNightAccentColorPref.isChecked() && themeIndex != 1);
mNightAccentColorPref.setSummary(mNightAccentColorPref.getEntry());
mNightAccentColorPref.setOnPreferenceChangeListener(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static com.best.deskclock.settings.InterfaceCustomizationActivity.DARK_THEME;
import static com.best.deskclock.settings.InterfaceCustomizationActivity.LIGHT_THEME;
import static com.best.deskclock.settings.InterfaceCustomizationActivity.SYSTEM_THEME;
import static com.best.deskclock.settings.ThemeController.Setting.CHANGED;

import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -163,7 +164,7 @@ public static class PrefsFragment extends ScreenFragment implements Preference.O
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
requireContext().sendBroadcast(new Intent(ACTION_UPDATE_WIDGETS_AFTER_RESTORE));
ThemeController.recreateActivityAfterRestoringSettings(ThemeController.RestoreSettings.DONE);
ThemeController.setNewSetting(CHANGED);
Toast.makeText(requireContext(),
requireContext().getString(R.string.backup_restore_toast_message_for_restore),
Toast.LENGTH_SHORT)
Expand Down
Loading

0 comments on commit 76d2914

Please sign in to comment.