From 9ede16c8648e1e2a81be6c3acd2cfc645e9361a3 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Sun, 8 Dec 2019 18:55:42 +0100 Subject: [PATCH] Initial work on dark theme --- .../main/java/com/readrops/app/ReadropsApp.java | 5 +++++ .../fragments/settings/SettingsFragment.java | 17 ++++++++++++++++- .../app/utils/SharedPreferencesManager.java | 7 ++++--- app/src/main/res/values-fr-rFR/strings.xml | 3 +++ app/src/main/res/values-night/styles.xml | 10 ++++++++++ app/src/main/res/values/arrays.xml | 9 +++++++++ app/src/main/res/values/strings.xml | 3 +++ app/src/main/res/values/styles.xml | 4 +--- app/src/main/res/xml/preferences.xml | 9 ++++++++- 9 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/values-night/styles.xml diff --git a/app/src/main/java/com/readrops/app/ReadropsApp.java b/app/src/main/java/com/readrops/app/ReadropsApp.java index 0fcc1f7b..c7669ee3 100644 --- a/app/src/main/java/com/readrops/app/ReadropsApp.java +++ b/app/src/main/java/com/readrops/app/ReadropsApp.java @@ -5,7 +5,10 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.os.Build; +import androidx.appcompat.app.AppCompatDelegate; + import com.facebook.stetho.Stetho; +import com.readrops.app.utils.SharedPreferencesManager; import io.reactivex.plugins.RxJavaPlugins; @@ -26,6 +29,8 @@ public class ReadropsApp extends Application { } createNotificationChannels(); + if (Boolean.valueOf(SharedPreferencesManager.readString(this, SharedPreferencesManager.SharedPrefKey.DARK_THEME))) + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); } private void createNotificationChannels() { diff --git a/app/src/main/java/com/readrops/app/fragments/settings/SettingsFragment.java b/app/src/main/java/com/readrops/app/fragments/settings/SettingsFragment.java index fb5998b2..6c7608f4 100644 --- a/app/src/main/java/com/readrops/app/fragments/settings/SettingsFragment.java +++ b/app/src/main/java/com/readrops/app/fragments/settings/SettingsFragment.java @@ -3,6 +3,7 @@ package com.readrops.app.fragments.settings; import android.content.Intent; import android.os.Bundle; +import androidx.appcompat.app.AppCompatDelegate; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; @@ -21,9 +22,10 @@ public class SettingsFragment extends PreferenceFragmentCompat { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { addPreferencesFromResource(R.xml.preferences); - AtomicBoolean serviceStarted = new AtomicBoolean(false); Preference feedsColorsPreference = findPreference("reload_feeds_colors"); + Preference themePreference = findPreference("dark_theme"); + AtomicBoolean serviceStarted = new AtomicBoolean(false); feedsColorsPreference.setOnPreferenceClickListener(preference -> { Database database = Database.getInstance(getContext()); @@ -39,5 +41,18 @@ public class SettingsFragment extends PreferenceFragmentCompat { return true; }); + + themePreference.setOnPreferenceChangeListener((preference, newValue) -> { + boolean darkTheme = Boolean.parseBoolean(newValue.toString()); + + if (darkTheme) { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); + } else { + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); + } + + return true; + }); } + } diff --git a/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java b/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java index a183ac9d..67d867f9 100644 --- a/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java +++ b/app/src/main/java/com/readrops/app/utils/SharedPreferencesManager.java @@ -51,7 +51,8 @@ public final class SharedPreferencesManager { public enum SharedPrefKey { SHOW_READ_ARTICLES("show_read_articles", false), ITEMS_TO_PARSE_MAX_NB("items_to_parse_max_nb", "20"), - OPEN_ITEMS_IN("open_items_in", "0"); + OPEN_ITEMS_IN("open_items_in", "0"), + DARK_THEME("dark_theme", "false"); @NonNull private String key; @@ -59,7 +60,7 @@ public final class SharedPreferencesManager { private Object defaultValue; public boolean getBooleanDefaultValue() { - return (boolean) defaultValue; + return Boolean.valueOf(defaultValue.toString()); } public String getStringDefaultValue() { @@ -67,7 +68,7 @@ public final class SharedPreferencesManager { } public int getIntDefaultValue() { - return (int) defaultValue; + return Integer.parseInt(defaultValue.toString()); } SharedPrefKey(@NonNull String key, @NonNull Object defaultValue) { diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index 9af48fa0..068b31ad 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -106,5 +106,8 @@ Options de l\'image Télécharger l\'image Partager l\'image + Thème + Clair + Sombre \ No newline at end of file diff --git a/app/src/main/res/values-night/styles.xml b/app/src/main/res/values-night/styles.xml new file mode 100644 index 00000000..c197c241 --- /dev/null +++ b/app/src/main/res/values-night/styles.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 6eb2a4e5..48d29a56 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -39,4 +39,13 @@ 1 + + @string/light + @string/dark + + + + false + true + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b12cc24f..49f9b67b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -115,4 +115,7 @@ Image Options Download image Share image + Theme + Light + Dark diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 7470ee83..d5c86700 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,8 +1,6 @@ - -