upgraded system theming

This commit is contained in:
gdlbo
2023-03-05 14:55:53 +03:00
parent 6d97ee38cd
commit 80af934df3
4 changed files with 34 additions and 59 deletions
@@ -2,12 +2,9 @@ package ru.vtosters.lite.ui.dialogs;
import android.app.Activity;
import com.vk.core.dialogs.alert.VkAlertDialog;
import com.vk.core.ui.themes.VKThemeHelper;
import com.vtosters.lite.R;
import ru.vtosters.lite.hooks.VKUIHook;
import ru.vtosters.lite.utils.Preferences;
import static ru.vtosters.lite.utils.ThemesUtils.isDarkTheme;
import ru.vtosters.lite.utils.ThemesUtils;
public class ThemeChanging {
public static void changeTheme(Activity activity, float[] f) {
@@ -17,16 +14,13 @@ public class ThemeChanging {
.setMessage(R.string.system_theme_warning)
.setCancelable(false)
.setPositiveButton(R.string.proxy_disable, (dialogInterface, i) -> { // Отключить
Preferences.getPreferences().edit().putString("currsystemtheme", !isDarkTheme() ? "dark" : "light").commit();
VKThemeHelper.b(activity, f);
VKUIHook.clearWebCache();
ThemesUtils.setThemeFL(!ThemesUtils.isDarkTheme() ? ThemesUtils.getDarkTheme() : ThemesUtils.getLightTheme(), activity, f);
Preferences.getPreferences().edit().putBoolean("system_theme", false).commit();
})
.setNeutralButton(R.string.cancel, null)
.show();
} else {
VKThemeHelper.b(activity, f);
Preferences.getPreferences().edit().putString("currsystemtheme", isDarkTheme() ? "dark" : "light").commit();
VKUIHook.clearWebCache();
ThemesUtils.setThemeFL(!ThemesUtils.isDarkTheme() ? ThemesUtils.getDarkTheme() : ThemesUtils.getLightTheme(), activity, f);
}
}
}
@@ -1,9 +1,11 @@
package ru.vtosters.lite.ui.fragments;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.StringRes;
import com.aefyr.tsg.g2.TelegramStickersPack;
import com.aefyr.tsg.g2.TelegramStickersService;
@@ -63,17 +65,6 @@ public class VTSettings extends MaterialPreferenceToolbarFragment implements Tel
return AndroidUtils.getString(R.string.vtlproxysumm) + ": " + type;
}
public static String getThemesumm() {
String[] themeTypeName = AndroidUtils.getArray("theme_type_name");
return AndroidUtils.getString("current_theme") + ": " + switch (Preferences.getString("currsystemtheme")) {
case "system" -> themeTypeName[0];
case "dark" -> themeTypeName[1];
case "light" -> themeTypeName[2];
default -> ThemesUtils.isDarkTheme() ? themeTypeName[1] : themeTypeName[2];
};
}
private void switchTheme(boolean isDarkTheme) {
ThemesUtils.setTheme(isDarkTheme ? ThemesUtils.getDarkTheme() : ThemesUtils.getLightTheme(), requireActivity());
}
@@ -164,38 +155,36 @@ public class VTSettings extends MaterialPreferenceToolbarFragment implements Tel
PreferenceFragmentUtils.addPreferenceCategory(getPreferenceScreen(), R.string.appearance_theme_use_system);
if(Build.VERSION.SDK_INT >= 28 && Preferences.milkshake()) {
PreferenceFragmentUtils.addListPreference(
getPreferenceScreen(),
"currsystemtheme",
"system",
requireContext().getString(R.string.appearance_theme_use_system),
R.drawable.ic_palette_outline_28,
getThemesumm(),
AndroidUtils.getArray("theme_type_name_checkbox"),
new String[] { "system", "dark", "light" },
(preference, o) -> {
String value = (String) o;
if(!value.equals("system")) {
var theme = value.equals("dark") ? ThemesUtils.getDarkTheme() : ThemesUtils.getLightTheme();
ThemesUtils.applyTheme(theme);
}
Preferences.getPreferences().edit().putString("currsystemtheme", value).commit();
SystemThemeChangerHook.onThemeChanged(getResources().getConfiguration());
return true;
});
} else {
PreferenceFragmentUtils.addMaterialSwitchPreference(
getPreferenceScreen(),
"",
requireContext().getString(R.string.vtsettdarktheme),
"",
R.drawable.ic_palette_outline_28,
ThemesUtils.isDarkTheme(),
(preference, o) -> {
if (Preferences.systemtheme()) {
AndroidUtils.sendToast("Включена установка темы как на устройстве");
return false;
}
final var switchPreference = (MaterialSwitchPreference) preference;
final var isDarkTheme = !switchPreference.isChecked();
switchTheme(isDarkTheme);
return true;
}
);
if (Build.VERSION.SDK_INT >= 28 && Preferences.milkshake()) {
PreferenceFragmentUtils.addMaterialSwitchPreference(
getPreferenceScreen(),
"",
requireContext().getString(R.string.vtsettdarktheme),
"",
"system_theme",
"Системная тема",
"Использовать тему установленную на устройстве",
R.drawable.ic_palette_outline_28,
ThemesUtils.isDarkTheme(),
Preferences.systemtheme(),
(preference, o) -> {
final var switchPreference = (MaterialSwitchPreference) preference;
final var isDarkTheme = !switchPreference.isChecked();
switchTheme(isDarkTheme);
Preferences.getPreferences().edit().putBoolean("system_theme", (boolean) o).commit();
SystemThemeChangerHook.onThemeChanged(requireActivity().getResources().getConfiguration());
return true;
}
);
@@ -57,7 +57,7 @@ public class Preferences {
}
public static boolean systemtheme() {
return Build.VERSION.SDK_INT >= 28 && milkshake() && (getPreferences().getString("currsystemtheme", "system").equals("system") || getPreferences().getString("currsystemtheme", "system").isEmpty());
return Build.VERSION.SDK_INT >= 28 && milkshake() && getBoolValue("system_theme", true);
}
public static boolean authorsrecomm() {
@@ -64,15 +64,7 @@ public class ThemesUtils {
}
public static void setTheme(VKTheme theme, Activity activity) {
if (activity == null) {
activity = LifecycleUtils.getCurrentActivity();
}
VKThemeHelper.theme(theme, activity, getCenterScreenCoords());
activity.recreate();
ThemeTracker.a();
VKUIHook.isLoaded = false;
new WebView(activity).clearCache(true);
WebCachePreloader.e();
setThemeFL(theme, activity, getCenterScreenCoords());
} // apply changed theme
public static void setThemeFL(VKTheme theme, Activity activity, float[] fl) {