Rename night mode to dark theme; abide by system-wide settings (API 29)

Closes issue #513
pull/538/head
Alinson S. Xavier 6 years ago
parent 3658aef2e2
commit bf644f4e09

@ -19,6 +19,8 @@
package org.isoron.uhabits.activities package org.isoron.uhabits.activities
import android.content.res.Configuration.*
import android.os.Build.VERSION.*
import android.support.v4.content.* import android.support.v4.content.*
import org.isoron.androidbase.activities.* import org.isoron.androidbase.activities.*
import org.isoron.uhabits.* import org.isoron.uhabits.*
@ -33,6 +35,16 @@ class AndroidThemeSwitcher
preferences: Preferences preferences: Preferences
) : ThemeSwitcher(preferences) { ) : ThemeSwitcher(preferences) {
override fun getSystemTheme(): Int {
if(SDK_INT < 29) return THEME_LIGHT;
val uiMode = activity.resources.configuration.uiMode
return if ((uiMode and UI_MODE_NIGHT_MASK) == UI_MODE_NIGHT_YES) {
THEME_DARK;
} else {
THEME_LIGHT;
}
}
override fun applyDarkTheme() { override fun applyDarkTheme() {
activity.setTheme(R.style.AppBaseThemeDark) activity.setTheme(R.style.AppBaseThemeDark)
activity.window.navigationBarColor = activity.window.navigationBarColor =

@ -27,14 +27,14 @@ import android.provider.*;
import android.support.annotation.*; import android.support.annotation.*;
import android.support.v7.preference.*; import android.support.v7.preference.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.R; import org.isoron.uhabits.R;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.preferences.*; import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.ui.*; import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.notifications.*; import org.isoron.uhabits.notifications.*;
import static android.media.RingtoneManager.*; import static android.media.RingtoneManager.*;
import static android.os.Build.VERSION.SDK_INT; import static android.os.Build.VERSION.*;
import static org.isoron.uhabits.activities.habits.list.ListHabitsScreenKt.*; import static org.isoron.uhabits.activities.habits.list.ListHabitsScreenKt.*;
public class SettingsFragment extends PreferenceFragmentCompat public class SettingsFragment extends PreferenceFragmentCompat

@ -169,9 +169,9 @@
<string name="troubleshooting">Troubleshooting</string> <string name="troubleshooting">Troubleshooting</string>
<string name="help_translate">Help translate this app</string> <string name="help_translate">Help translate this app</string>
<string name="night_mode">Night mode</string> <string name="night_mode">Dark theme</string>
<string name="use_pure_black">Use pure black in night mode</string> <string name="use_pure_black">Use pure black in dark theme</string>
<string name="pure_black_description">Replaces gray backgrounds with pure black in night mode. Reduces battery usage in phones with AMOLED display.</string> <string name="pure_black_description">Replaces gray backgrounds with pure black in dark theme. Reduces battery usage in phones with AMOLED display.</string>
<string name="interface_preferences">Interface</string> <string name="interface_preferences">Interface</string>
<string name="reverse_days">Reverse order of days</string> <string name="reverse_days">Reverse order of days</string>
<string name="reverse_days_description">Show days in reverse order on the main screen.</string> <string name="reverse_days_description">Show days in reverse order on the main screen.</string>

@ -186,7 +186,7 @@ public class Preferences
public int getTheme() public int getTheme()
{ {
return storage.getInt("pref_theme", ThemeSwitcher.THEME_LIGHT); return storage.getInt("pref_theme", ThemeSwitcher.THEME_AUTOMATIC);
} }
public void setTheme(int theme) public void setTheme(int theme)

@ -27,7 +27,9 @@ public abstract class ThemeSwitcher
{ {
public static final int THEME_DARK = 1; public static final int THEME_DARK = 1;
public static final int THEME_LIGHT = 0; public static final int THEME_LIGHT = 2;
public static final int THEME_AUTOMATIC = 0;
private final Preferences preferences; private final Preferences preferences;
@ -38,7 +40,7 @@ public abstract class ThemeSwitcher
public void apply() public void apply()
{ {
if (preferences.getTheme() == THEME_DARK) if (isNightMode())
{ {
if (preferences.isPureBlackEnabled()) applyPureBlackTheme(); if (preferences.isPureBlackEnabled()) applyPureBlackTheme();
else applyDarkTheme(); else applyDarkTheme();
@ -55,19 +57,36 @@ public abstract class ThemeSwitcher
public abstract void applyPureBlackTheme(); public abstract void applyPureBlackTheme();
public abstract int getSystemTheme();
public boolean isNightMode() public boolean isNightMode()
{ {
return preferences.getTheme() == THEME_DARK; int systemTheme = getSystemTheme();
} int userTheme = preferences.getTheme();
public void setTheme(int theme) return (userTheme == THEME_DARK ||
{ (systemTheme == THEME_DARK && userTheme == THEME_AUTOMATIC));
preferences.setTheme(theme);
} }
public void toggleNightMode() public void toggleNightMode()
{ {
if (isNightMode()) setTheme(THEME_LIGHT); int systemTheme = getSystemTheme();
else setTheme(THEME_DARK); int userTheme = preferences.getTheme();
if(userTheme == THEME_AUTOMATIC)
{
if(systemTheme == THEME_LIGHT) preferences.setTheme(THEME_DARK);
if(systemTheme == THEME_DARK) preferences.setTheme(THEME_LIGHT);
}
else if(userTheme == THEME_LIGHT)
{
if (systemTheme == THEME_LIGHT) preferences.setTheme(THEME_DARK);
if (systemTheme == THEME_DARK) preferences.setTheme(THEME_AUTOMATIC);
}
else if(userTheme == THEME_DARK)
{
if (systemTheme == THEME_LIGHT) preferences.setTheme(THEME_AUTOMATIC);
if (systemTheme == THEME_DARK) preferences.setTheme(THEME_LIGHT);
}
} }
} }

Loading…
Cancel
Save