1
0
Fork 0

added 'automatic dark theme' option

This commit is contained in:
sspanak 2023-08-01 17:46:47 +03:00 committed by Dimo Karaivanov
parent 0190e84053
commit 9f65cb5ce8
19 changed files with 178 additions and 40 deletions

View file

@ -69,4 +69,7 @@
<string name="key_volume_up">Усилване на звук</string>
<string name="dictionary_truncating">Изтриване…</string>
<string name="error">Грешка</string>
<string name="pref_dark_theme_yes">Да</string>
<string name="pref_dark_theme_no">Не</string>
<string name="pref_dark_theme_auto">Автоматично</string>
</resources>

View file

@ -23,4 +23,7 @@
<string name="char_newline">Neue Zeile</string>
<string name="pref_category_setup">Ersteinrichtung</string>
<string name="error">Fehler</string>
<string name="pref_dark_theme_yes">Ja</string>
<string name="pref_dark_theme_no">Nein</string>
<string name="pref_dark_theme_auto">Automatisch</string>
</resources>

View file

@ -69,4 +69,7 @@
<string name="key_call">Llamar</string>
<string name="key_volume_down">Bajar volumen</string>
<string name="key_volume_up">Subir Volumen</string>
<string name="pref_dark_theme_yes"></string>
<string name="pref_dark_theme_no">No</string>
<string name="pref_dark_theme_auto">Automática</string>
</resources>

View file

@ -64,4 +64,7 @@
<string name="pref_show_soft_numpad">Pavé numérique à l\'écran</string>
<string name="dictionary_truncating">Suppression…</string>
<string name="error">Erreur</string>
<string name="pref_dark_theme_yes">Oui</string>
<string name="pref_dark_theme_no">Non</string>
<string name="pref_dark_theme_auto">Automatique</string>
</resources>

View file

@ -32,5 +32,8 @@
<string name="pref_upside_down_keys_summary">Abilita l\'impostazione se ci sono 789 sulla prima riga, invece di 123.</string>
<string name="pref_category_setup">Configurazione iniziale</string>
<string name="error">Errore</string>
<string name="pref_dark_theme_yes">Si</string>
<string name="pref_dark_theme_no">No</string>
<string name="pref_dark_theme_auto">Automatica</string>
</resources>

View file

@ -55,4 +55,7 @@
<string name="char_newline">שורה חדשה</string>
<string name="char_space">רווח</string>
<string name="error">טעות</string>
<string name="pref_dark_theme_yes">כן</string>
<string name="pref_dark_theme_no">לא</string>
<string name="pref_dark_theme_auto">אוטומטי</string>
</resources>

View file

@ -23,4 +23,7 @@
<string name="pref_category_setup">Initiële setup</string>
<string name="dictionary_truncating">Verwijderen…</string>
<string name="error">Fout</string>
<string name="pref_dark_theme_yes">Ja</string>
<string name="pref_dark_theme_no">Nee</string>
<string name="pref_dark_theme_auto">Automatisch</string>
</resources>

View file

@ -58,4 +58,7 @@
<string name="pref_upside_down_keys">Inverter Ordem das Teclas</string>
<string name="dictionary_truncate_unselected">Limpar Não Selecionados</string>
<string name="error">Erro</string>
<string name="pref_dark_theme_yes">Sim</string>
<string name="pref_dark_theme_no">Não</string>
<string name="pref_dark_theme_auto">Automático</string>
</resources>

View file

@ -69,4 +69,7 @@
<string name="key_volume_up">Увеличить громкости</string>
<string name="dictionary_truncating">Удаление…</string>
<string name="error">Ошибка</string>
<string name="pref_dark_theme_yes">Да</string>
<string name="pref_dark_theme_no">Нет</string>
<string name="pref_dark_theme_auto">Автоматически</string>
</resources>

View file

@ -69,4 +69,7 @@
<string name="key_volume_down">Зменшення гучності</string>
<string name="dictionary_truncating">Видалення…</string>
<string name="error">Помилка</string>
<string name="pref_dark_theme_yes">Так</string>
<string name="pref_dark_theme_no">Ні</string>
<string name="pref_dark_theme_auto">Автоматично</string>
</resources>

View file

@ -35,6 +35,9 @@
<string name="pref_auto_text_case_summary">Automatically start sentences with a capital letter.</string>
<string name="pref_choose_languages">Languages</string>
<string name="pref_dark_theme">Dark Theme</string>
<string name="pref_dark_theme_yes">Yes</string>
<string name="pref_dark_theme_no">No</string>
<string name="pref_dark_theme_auto">Auto</string>
<string name="pref_double_zero_char">Character for Double 0-key Press</string>
<string name="pref_show_soft_function_keys">Show On-Screen Keys</string>
<string name="pref_show_soft_numpad">Show On-Screen Numpad</string>

View file

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto" app:orderingFromXml="true">
<SwitchPreferenceCompat
app:defaultValue="true"
app:key="pref_dark_theme"
app:layout="@layout/pref_switch"
<DropDownPreference
app:defaultValue="-1"
app:iconSpaceReserved="false"
app:key="pref_theme"
app:layout="@layout/pref_dropdown"
app:title="@string/pref_dark_theme" />
<SwitchPreferenceCompat

View file

@ -136,9 +136,7 @@ public class PreferencesActivity extends AppCompatActivity implements Preference
private void applyTheme() {
AppCompatDelegate.setDefaultNightMode(
settings.getDarkTheme() ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO
);
AppCompatDelegate.setDefaultNightMode(settings.getTheme());
}

View file

@ -2,7 +2,9 @@ package io.github.sspanak.tt9.preferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.PreferenceManager;
import java.util.ArrayList;
@ -226,7 +228,22 @@ public class SettingsStore {
/************* UI settings *************/
public boolean getDarkTheme() { return prefs.getBoolean("pref_dark_theme", true); }
public boolean getDarkTheme() {
int theme = getTheme();
if (theme == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) {
return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
} else {
return theme == AppCompatDelegate.MODE_NIGHT_YES;
}
}
public int getTheme() {
try {
return Integer.parseInt(prefs.getString("pref_theme", String.valueOf(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)));
} catch (NumberFormatException e) {
return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
}
}
public boolean getShowSoftKeys() { return prefs.getBoolean("pref_show_soft_keys", true); }

View file

@ -0,0 +1,72 @@
package io.github.sspanak.tt9.preferences.items;
import androidx.preference.DropDownPreference;
import androidx.preference.Preference;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import io.github.sspanak.tt9.Logger;
public class ItemDropDown {
private final DropDownPreference item;
private LinkedHashMap<Integer, String> values;
public ItemDropDown(DropDownPreference item) {
this.item = item;
}
protected void populate(LinkedHashMap<Integer, String> values) {
if (item == null) {
Logger.w("tt9/ItemDropDown.populate", "Cannot populate a NULL item. Ignoring.");
return;
}
this.values = values != null ? values : new LinkedHashMap<>();
ArrayList<String> keys = new ArrayList<>();
for (int key : this.values.keySet()) {
keys.add(String.valueOf(key));
}
item.setEntryValues(keys.toArray(new CharSequence[0]));
item.setEntries(this.values.values().toArray(new CharSequence[0]));
}
public ItemDropDown enableClickHandler() {
if (item == null) {
Logger.w("tt9/SectionKeymap.populateItem", "Cannot set a click listener a NULL item. Ignoring.");
return this;
}
item.setOnPreferenceChangeListener(this::onClick);
return this;
}
protected boolean onClick(Preference preference, Object newKey) {
try {
String previewValue = values.get(Integer.parseInt(newKey.toString()));
((DropDownPreference) preference).setValue(newKey.toString());
setPreview(previewValue);
return true;
} catch (NumberFormatException e) {
return false;
}
}
private void setPreview(String value) {
if (item != null) {
item.setSummary(value);
}
}
public ItemDropDown preview() {
try {
setPreview(values.get(Integer.parseInt(item.getValue())));
} catch (NumberFormatException e) {
setPreview("");
}
return this;
}
}

View file

@ -0,0 +1,43 @@
package io.github.sspanak.tt9.preferences.items;
import android.content.Context;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.DropDownPreference;
import androidx.preference.Preference;
import java.util.LinkedHashMap;
import io.github.sspanak.tt9.R;
public class ItemSelectTheme extends ItemDropDown {
public static final String NAME = "pref_theme";
private final Context context;
public ItemSelectTheme(Context context, DropDownPreference item) {
super(item);
this.context = context;
}
public ItemDropDown populate() {
LinkedHashMap<Integer, String> themes = new LinkedHashMap<>();
themes.put(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM, context.getString(R.string.pref_dark_theme_auto));
themes.put(AppCompatDelegate.MODE_NIGHT_NO, context.getString(R.string.pref_dark_theme_no));
themes.put(AppCompatDelegate.MODE_NIGHT_YES, context.getString(R.string.pref_dark_theme_yes));
super.populate(themes);
return this;
}
@Override
protected boolean onClick(Preference preference, Object newKey) {
if (super.onClick(preference, newKey)) {
AppCompatDelegate.setDefaultNightMode(Integer.parseInt(newKey.toString()));
return true;
}
return false;
}
}

View file

@ -1,27 +0,0 @@
package io.github.sspanak.tt9.preferences.items;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.Preference;
import androidx.preference.SwitchPreferenceCompat;
public class ItemToggleDarkTheme {
public static final String NAME = "pref_dark_theme";
private final SwitchPreferenceCompat themeToggle;
public ItemToggleDarkTheme(SwitchPreferenceCompat item) {
themeToggle = item;
}
public void enableToggleHandler() {
themeToggle.setOnPreferenceChangeListener(this::onChange);
}
private boolean onChange(Preference p, Object newValue) {
AppCompatDelegate.setDefaultNightMode(
((boolean) newValue) ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO
);
return true;
}
}

View file

@ -2,7 +2,7 @@ package io.github.sspanak.tt9.preferences.screens;
import io.github.sspanak.tt9.R;
import io.github.sspanak.tt9.preferences.PreferencesActivity;
import io.github.sspanak.tt9.preferences.items.ItemToggleDarkTheme;
import io.github.sspanak.tt9.preferences.items.ItemSelectTheme;
public class AppearanceScreen extends BaseScreenFragment {
public AppearanceScreen() { init(); }
@ -13,6 +13,9 @@ public class AppearanceScreen extends BaseScreenFragment {
@Override
protected void onCreate() {
(new ItemToggleDarkTheme(findPreference(ItemToggleDarkTheme.NAME))).enableToggleHandler();
(new ItemSelectTheme(activity, findPreference(ItemSelectTheme.NAME)))
.populate()
.enableClickHandler()
.preview();
}
}

View file

@ -27,9 +27,7 @@ public class AddWordAct extends AppCompatActivity {
protected void onCreate(Bundle savedData) {
settings = new SettingsStore(this);
AppCompatDelegate.setDefaultNightMode(
settings.getDarkTheme() ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO
);
AppCompatDelegate.setDefaultNightMode(settings.getTheme());
super.onCreate(savedData);