forked from mirrors/Fedilab
Switch between themes
This commit is contained in:
parent
13b9a43bc3
commit
0c2a6e2aad
4 changed files with 116 additions and 2 deletions
|
@ -16,14 +16,19 @@ package app.fedilab.android.activities;
|
|||
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import com.vanniktech.emoji.EmojiManager;
|
||||
import com.vanniktech.emoji.one.EmojiOneProvider;
|
||||
|
||||
import app.fedilab.android.R;
|
||||
import app.fedilab.android.helper.Helper;
|
||||
import app.fedilab.android.helper.ThemeHelper;
|
||||
|
||||
|
@ -38,6 +43,58 @@ public class BaseActivity extends AppCompatActivity {
|
|||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
|
||||
final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
String currentTheme = sharedpreferences.getString(getString(R.string.SET_THEME_BASE), getString(R.string.SET_DEFAULT_THEME));
|
||||
//Default automatic switch
|
||||
if (currentTheme.equals(getString(R.string.SET_DEFAULT_THEME))) {
|
||||
|
||||
|
||||
int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
|
||||
switch (currentNightMode) {
|
||||
case Configuration.UI_MODE_NIGHT_NO:
|
||||
String defaultLight = sharedpreferences.getString(getString(R.string.SET_THEME_DEFAULT_LIGHT), "LIGHT");
|
||||
switch (defaultLight) {
|
||||
case "LIGHT":
|
||||
setTheme(R.style.AppTheme);
|
||||
break;
|
||||
case "SOLARIZED_LIGHT":
|
||||
setTheme(R.style.SolarizedAppTheme);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Configuration.UI_MODE_NIGHT_YES:
|
||||
String defaultDark = sharedpreferences.getString(getString(R.string.SET_THEME_DEFAULT_DARK), "DARK");
|
||||
switch (defaultDark) {
|
||||
case "DARK":
|
||||
setTheme(R.style.AppTheme);
|
||||
break;
|
||||
case "SOLARIZED_DARK":
|
||||
setTheme(R.style.SolarizedAppTheme);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
switch (currentTheme) {
|
||||
case "LIGHT":
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
|
||||
setTheme(R.style.AppTheme);
|
||||
break;
|
||||
case "DARK":
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
|
||||
setTheme(R.style.AppTheme);
|
||||
break;
|
||||
case "SOLARIZED_LIGHT":
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
|
||||
setTheme(R.style.SolarizedAppTheme);
|
||||
break;
|
||||
case "SOLARIZED_DARK":
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
|
||||
setTheme(R.style.SolarizedAppTheme);
|
||||
break;
|
||||
}
|
||||
}
|
||||
super.onCreate(savedInstanceState);
|
||||
ThemeHelper.adjustFontScale(this, getResources().getConfiguration());
|
||||
Helper.setLocale(this);
|
||||
|
|
|
@ -16,14 +16,19 @@ package app.fedilab.android.activities;
|
|||
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import com.vanniktech.emoji.EmojiManager;
|
||||
import com.vanniktech.emoji.one.EmojiOneProvider;
|
||||
|
||||
import app.fedilab.android.R;
|
||||
import app.fedilab.android.helper.Helper;
|
||||
import app.fedilab.android.helper.ThemeHelper;
|
||||
|
||||
|
@ -38,6 +43,57 @@ public class BaseBarActivity extends AppCompatActivity {
|
|||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
String currentTheme = sharedpreferences.getString(getString(R.string.SET_THEME_BASE), getString(R.string.SET_DEFAULT_THEME));
|
||||
//Default automatic switch
|
||||
if (currentTheme.equals(getString(R.string.SET_DEFAULT_THEME))) {
|
||||
|
||||
int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
|
||||
switch (currentNightMode) {
|
||||
case Configuration.UI_MODE_NIGHT_NO:
|
||||
String defaultLight = sharedpreferences.getString(getString(R.string.SET_THEME_DEFAULT_LIGHT), "LIGHT");
|
||||
switch (defaultLight) {
|
||||
case "LIGHT":
|
||||
setTheme(R.style.AppThemeBar);
|
||||
break;
|
||||
case "SOLARIZED_LIGHT":
|
||||
setTheme(R.style.SolarizedAppThemeBar);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Configuration.UI_MODE_NIGHT_YES:
|
||||
String defaultDark = sharedpreferences.getString(getString(R.string.SET_THEME_DEFAULT_DARK), "DARK");
|
||||
switch (defaultDark) {
|
||||
case "DARK":
|
||||
setTheme(R.style.AppThemeBar);
|
||||
break;
|
||||
case "SOLARIZED_DARK":
|
||||
setTheme(R.style.SolarizedAppThemeBar);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
} else {
|
||||
switch (currentTheme) {
|
||||
case "LIGHT":
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
|
||||
setTheme(R.style.AppThemeBar);
|
||||
break;
|
||||
case "DARK":
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
|
||||
setTheme(R.style.AppThemeBar);
|
||||
break;
|
||||
case "SOLARIZED_LIGHT":
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
|
||||
setTheme(R.style.SolarizedAppThemeBar);
|
||||
break;
|
||||
case "SOLARIZED_DARK":
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
|
||||
setTheme(R.style.SolarizedAppThemeBar);
|
||||
break;
|
||||
}
|
||||
}
|
||||
super.onCreate(savedInstanceState);
|
||||
ThemeHelper.adjustFontScale(this, getResources().getConfiguration());
|
||||
Helper.setLocale(this);
|
||||
|
|
|
@ -267,9 +267,9 @@ public class ThemeHelper {
|
|||
}
|
||||
|
||||
public static void switchTo(String themePref) {
|
||||
if (themes.LIGHT.name().equals(themePref)) {
|
||||
if (themes.LIGHT.name().equals(themePref) || themes.SOLARIZED_LIGHT.name().equals(themePref)) {
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
|
||||
} else if (themes.DARK.name().equals(themePref)) {
|
||||
} else if (themes.DARK.name().equals(themePref) || themes.SOLARIZED_DARK.name().equals(themePref)) {
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
|
||||
} else {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
|
|
|
@ -62,6 +62,7 @@ public class FragmentThemingSettings extends PreferenceFragmentCompat implements
|
|||
ListPreference SET_THEME_BASE = findPreference(getString(R.string.SET_THEME_BASE));
|
||||
if (SET_THEME_BASE != null) {
|
||||
ThemeHelper.switchTo(SET_THEME_BASE.getValue());
|
||||
requireActivity().recreate();
|
||||
}
|
||||
}
|
||||
Helper.recreateMainActivity(requireActivity());
|
||||
|
|
Loading…
Reference in a new issue