Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions app/src/main/java/fr/gaulupeau/apps/Poche/data/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,44 @@ public void setTheme(Themes.Theme theme) {
setString(R.string.pref_key_ui_theme, theme.toString());
}

public boolean isAutoThemeEnabled() {
return getBoolean(R.string.pref_key_ui_theme_auto, false);
}

public void setAutoThemeEnabled(boolean value) {
setBoolean(R.string.pref_key_ui_theme_auto, value);
}

public Themes.Theme getAutoLightTheme() {
String themeName = getString(R.string.pref_key_ui_theme_auto_light);
Themes.Theme theme = null;
if(themeName != null) {
try {
theme = Themes.Theme.valueOf(themeName);
} catch(IllegalArgumentException ignored) {}
}
return theme != null ? theme : Themes.Theme.LIGHT;
}

public void setAutoLightTheme(Themes.Theme theme) {
setString(R.string.pref_key_ui_theme_auto_light, theme.toString());
}

public Themes.Theme getAutoDarkTheme() {
String themeName = getString(R.string.pref_key_ui_theme_auto_dark);
Themes.Theme theme = null;
if(themeName != null) {
try {
theme = Themes.Theme.valueOf(themeName);
} catch(IllegalArgumentException ignored) {}
}
return theme != null ? theme : Themes.Theme.DARK;
}

public void setAutoDarkTheme(Themes.Theme theme) {
setString(R.string.pref_key_ui_theme_auto_dark, theme.toString());
}

public boolean isVolumeButtonsScrollingEnabled() {
return getBoolean(R.string.pref_key_ui_volumeButtonsScrolling_enabled, false);
}
Expand Down
53 changes: 41 additions & 12 deletions app/src/main/java/fr/gaulupeau/apps/Poche/ui/Themes.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import fr.gaulupeau.apps.InThePoche.R;
import fr.gaulupeau.apps.Poche.App;
import android.content.Context;
import android.content.res.Configuration;

public class Themes {

Expand All @@ -28,79 +30,106 @@ public static Theme getCurrentTheme() {
return theme;
}

public static Theme getResolvedTheme(Context context) {
if (App.getSettings().isAutoThemeEnabled()) {
int currentNightMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if (currentNightMode == Configuration.UI_MODE_NIGHT_YES) {
return App.getSettings().getAutoDarkTheme();
} else {
return App.getSettings().getAutoLightTheme();
}
}
return theme;
}

public static void applyTheme(Activity activity) {
applyTheme(activity, true);
}

public static void applyTheme(Activity activity, boolean actionBar) {
activity.setTheme(actionBar ? theme.getResId() : theme.getNoActionBarResId());
appliedThemes.put(activity, theme);
Theme resolvedTheme = getResolvedTheme(activity);
activity.setTheme(actionBar ? resolvedTheme.getResId() : resolvedTheme.getNoActionBarResId());
appliedThemes.put(activity, resolvedTheme);
}

public static void applyDialogTheme(Activity activity) {
activity.setTheme(theme.getDialogResId());
appliedThemes.put(activity, theme);
Theme resolvedTheme = getResolvedTheme(activity);
activity.setTheme(resolvedTheme.getDialogResId());
appliedThemes.put(activity, resolvedTheme);
}

public static void checkTheme(Activity activity) {
Theme appliedTheme = appliedThemes.get(activity);
if(appliedTheme != theme) activity.recreate();
Theme resolvedTheme = getResolvedTheme(activity);
if(appliedTheme != resolvedTheme) activity.recreate();
}

public enum Theme {
LIGHT(
R.string.themeName_light,
R.style.LightTheme,
R.style.LightTheme_NoActionBar,
R.style.DialogTheme
R.style.DialogTheme,
false
),

LIGHT_CONTRAST(
R.string.themeName_light_contrast,
R.style.LightThemeContrast,
R.style.LightThemeContrast_NoActionBar,
R.style.DialogTheme
R.style.DialogTheme,
false
),

E_INK(
R.string.themeName_eink,
R.style.LightThemeContrast,
R.style.LightThemeContrast_NoActionBar,
R.style.DialogTheme
R.style.DialogTheme,
false
),

DARK(
R.string.themeName_dark,
R.style.DarkTheme,
R.style.DarkTheme_NoActionBar,
R.style.DialogThemeDark
R.style.DialogThemeDark,
true
),

DARK_CONTRAST(
R.string.themeName_dark_contrast,
R.style.DarkThemeContrast,
R.style.DarkThemeContrast_NoActionBar,
R.style.DialogThemeDark
R.style.DialogThemeDark,
true
),

SOLARIZED(
R.string.themeName_solarized,
R.style.SolarizedTheme,
R.style.SolarizedTheme_NoActionBar,
R.style.DialogTheme
R.style.DialogTheme,
true
);

private int nameId;
private int resId;
private int noActionBarResId;
private int dialogResId;
private boolean isDark;

Theme(@StringRes int nameId, @StyleRes int resId,
@StyleRes int noActionBarResId, @StyleRes int dialogResId) {
@StyleRes int noActionBarResId, @StyleRes int dialogResId, boolean isDark) {
this.nameId = nameId;
this.resId = resId;
this.noActionBarResId = noActionBarResId;
this.dialogResId = dialogResId;
this.isDark = isDark;
}

public boolean isDark() {
return isDark;
}

public @StringRes int getNameId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public static class SettingsFragment extends PreferenceFragmentCompat
R.string.pref_key_connection_api_clientID,
R.string.pref_key_connection_api_clientSecret,
R.string.pref_key_ui_theme,
R.string.pref_key_ui_theme_auto_light,
R.string.pref_key_ui_theme_auto_dark,
R.string.pref_key_ui_article_fontSize,
R.string.pref_key_ui_screenScrolling_percent,
R.string.pref_key_autoSync_interval,
Expand Down Expand Up @@ -444,6 +446,12 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
themeChanged = true;
break;

case R.string.pref_key_ui_theme_auto:
case R.string.pref_key_ui_theme_auto_light:
case R.string.pref_key_ui_theme_auto_dark:
themeChanged = true;
break;

case R.string.pref_key_autoSync_enabled:
autoSyncChanged = true;
break;
Expand Down Expand Up @@ -760,6 +768,8 @@ private void updateSummary(int keyResID) {
break;

case R.string.pref_key_ui_theme:
case R.string.pref_key_ui_theme_auto_light:
case R.string.pref_key_ui_theme_auto_dark:
case R.string.pref_key_autoSync_interval:
case R.string.pref_key_autoSync_type:
setListSummaryFromContent(key);
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings-preference-keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<string name="pref_key_ui_lists_sortOrder" translatable="false">ui.lists.sortOrder</string>
<string name="pref_key_ui_tagList_sortOrder" translatable="false">ui.tagList.sortOrder</string>
<string name="pref_key_ui_theme" translatable="false">ui.theme</string>
<string name="pref_key_ui_theme_auto" translatable="false">ui.theme.auto</string>
<string name="pref_key_ui_theme_auto_light" translatable="false">ui.theme.auto.light</string>
<string name="pref_key_ui_theme_auto_dark" translatable="false">ui.theme.auto.dark</string>
<string name="pref_key_ui_volumeButtonsScrolling_enabled" translatable="false">ui.volumeButtonsScrolling.enabled</string>
<string name="pref_key_ui_tapToScroll_enabled" translatable="false">ui.tapToScroll.enabled</string>
<string name="pref_key_ui_screenScrolling" translatable="false">ui.screenScrolling</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@

<string name="pref_categoryName_ui">UI</string>
<string name="pref_name_ui_theme">App theme</string>
<string name="pref_name_ui_theme_auto">Auto dark mode</string>
<string name="pref_desc_ui_theme_auto">Switch between light and dark theme based on system settings</string>
<string name="pref_name_ui_theme_auto_light">Auto light theme</string>
<string name="pref_desc_ui_theme_auto_light">Theme to use when system is in light mode</string>
<string name="pref_name_ui_theme_auto_dark">Auto dark theme</string>
<string name="pref_desc_ui_theme_auto_dark">Theme to use when system is in dark mode</string>
<string name="pref_name_ui_article_fontSize" formatted="false">Article text size (%)</string>
<string name="pref_name_ui_article_fontSerif">Serif typeface</string>
<string name="pref_desc_ui_article_fontSerif">Serif font for articles</string>
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@
android:title="@string/pref_name_ui_theme"
android:dialogTitle="@string/pref_name_ui_theme"
android:defaultValue="LIGHT"/>
<CheckBoxPreference
android:key="@string/pref_key_ui_theme_auto"
android:title="@string/pref_name_ui_theme_auto"
android:summary="@string/pref_desc_ui_theme_auto"
android:defaultValue="false"/>
<ListPreference
android:dependency="@string/pref_key_ui_theme_auto"
android:key="@string/pref_key_ui_theme_auto_light"
android:title="@string/pref_name_ui_theme_auto_light"
android:summary="@string/pref_desc_ui_theme_auto_light"
android:dialogTitle="@string/pref_name_ui_theme_auto_light"
android:defaultValue="LIGHT"/>
<ListPreference
android:dependency="@string/pref_key_ui_theme_auto"
android:key="@string/pref_key_ui_theme_auto_dark"
android:title="@string/pref_name_ui_theme_auto_dark"
android:summary="@string/pref_desc_ui_theme_auto_dark"
android:dialogTitle="@string/pref_name_ui_theme_auto_dark"
android:defaultValue="DARK"/>
<fr.gaulupeau.apps.Poche.ui.preferences.IntEditTextPreference
android:key="@string/pref_key_ui_article_fontSize"
android:title="@string/pref_name_ui_article_fontSize"
Expand Down