Migrate to DayNight Theme

This commit is contained in:
krlvm 2021-03-26 17:12:20 +03:00
parent 464d0e50b0
commit b3e2418b93
No known key found for this signature in database
GPG key ID: B8552A91FD265536
12 changed files with 47 additions and 23 deletions

View file

@ -133,6 +133,8 @@ public class MainActivity extends AppCompatActivity {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
TLSSocketFactoryCompat.setAsDefault();
}
ThemeHelper.setDayNightMode(this);
ThemeHelper.setTheme(this, ServiceHelper.getSelectedServiceId(this));
assureCorrectAppLanguage(this);

View file

@ -13,6 +13,7 @@ import androidx.preference.Preference;
import org.schabi.newpipe.R;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.ThemeHelper;
public class AppearanceSettingsFragment extends BasePreferenceFragment {
private static final boolean CAPTIONING_SETTINGS_ACCESSIBLE =
@ -89,6 +90,8 @@ public class AppearanceSettingsFragment extends BasePreferenceFragment {
defaultPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, true).apply();
defaultPreferences.edit().putString(themeKey, newValue.toString()).apply();
ThemeHelper.setDayNightMode(getContext(), newValue.toString());
if (!newValue.equals(beginningThemeKey) && getActivity() != null) {
// if it's not the current theme
ActivityCompat.recreate(getActivity());

View file

@ -31,6 +31,7 @@ import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
@ -120,6 +121,7 @@ public final class ThemeHelper {
final String selectedThemeKey = getSelectedThemeKey(context);
int baseTheme = R.style.DarkTheme; // default to dark theme
if (selectedThemeKey.equals(lightThemeKey)) {
baseTheme = R.style.LightTheme;
@ -288,4 +290,21 @@ public final class ThemeHelper {
return false;
}
}
public static void setDayNightMode(final Context context) {
setDayNightMode(context, ThemeHelper.getSelectedThemeKey(context));
}
public static void setDayNightMode(final Context context, final String selectedThemeKey) {
final Resources res = context.getResources();
if (selectedThemeKey.equals(res.getString(R.string.light_theme_key))) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else if (selectedThemeKey.equals(res.getString(R.string.dark_theme_key))
|| selectedThemeKey.equals(res.getString(R.string.black_theme_key))) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
}
}