Better stylish implementation

This commit is contained in:
J-Jamet
2021-04-08 16:26:14 +02:00
parent ff185f6505
commit 1742d265f3
4 changed files with 28 additions and 11 deletions

View File

@@ -37,12 +37,12 @@ object Stylish {
* Initialize the class with a theme preference
* @param context Context to retrieve the theme preference
*/
fun init(context: Context) {
fun load(context: Context) {
Log.d(Stylish::class.java.name, "Attatching to " + context.packageName)
themeString = PreferencesUtil.getStyle(context)
}
private fun retrieveEquivalentSystemStyle(context: Context, styleString: String): String {
fun retrieveEquivalentSystemStyle(context: Context, styleString: String): String {
val systemNightMode = when (PreferencesUtil.getStyleBrightness(context)) {
context.getString(R.string.list_style_brightness_light) -> false
context.getString(R.string.list_style_brightness_night) -> true
@@ -84,12 +84,16 @@ object Stylish {
}
}
fun defaultStyle(context: Context): String {
return context.getString(R.string.list_style_name_light)
}
/**
* Assign the style to the class attribute
* @param styleString Style id String
*/
fun assignStyle(context: Context, styleString: String) {
themeString = retrieveEquivalentSystemStyle(context, styleString)
PreferencesUtil.setStyle(context, styleString)
}
/**

View File

@@ -29,7 +29,7 @@ class App : MultiDexApplication() {
override fun onCreate() {
super.onCreate()
Stylish.init(this)
Stylish.load(this)
PRNGFixes.apply()
}

View File

@@ -137,18 +137,32 @@ object PreferencesUtil {
}
fun getStyle(context: Context): String {
val stylishPrefKey = context.getString(R.string.setting_style_key)
val defaultStyleString = context.getString(R.string.list_style_name_light)
val defaultStyleString = Stylish.defaultStyle(context)
val styleString = PreferenceManager.getDefaultSharedPreferences(context)
.getString(stylishPrefKey, defaultStyleString)
.getString(context.getString(R.string.setting_style_key), defaultStyleString)
?: defaultStyleString
return Stylish.retrieveEquivalentLightStyle(context, styleString)
// Return the system style
return Stylish.retrieveEquivalentSystemStyle(context, styleString)
}
fun setStyle(context: Context, styleString: String) {
var tempThemeString = styleString
if (tempThemeString in BuildConfig.STYLES_DISABLED) {
tempThemeString = Stylish.defaultStyle(context)
}
// Store light style to show selection in array list
tempThemeString = Stylish.retrieveEquivalentLightStyle(context, tempThemeString)
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putString(context.getString(R.string.setting_style_key), tempThemeString)
.apply()
Stylish.load(context)
}
fun getStyleBrightness(context: Context): String? {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
return prefs.getString(context.getString(R.string.setting_style_brightness_key),
context.resources.getString(R.string.list_style_brightness_follow_system))
context.getString(R.string.list_style_brightness_follow_system))
}
/**
@@ -609,7 +623,7 @@ object PreferencesUtil {
context.getString(R.string.autofill_application_id_blocklist_key) -> editor.putStringSet(name, getStringSetFromProperties(value))
context.getString(R.string.autofill_web_domain_blocklist_key) -> editor.putStringSet(name, getStringSetFromProperties(value))
context.getString(R.string.setting_style_key) -> editor.putString(name, value)
context.getString(R.string.setting_style_key) -> setStyle(context, value)
context.getString(R.string.setting_style_brightness_key) -> editor.putString(name, value)
context.getString(R.string.setting_icon_pack_choose_key) -> editor.putString(name, value)
context.getString(R.string.list_entries_show_username_key) -> editor.putBoolean(name, value.toBoolean())

View File

@@ -273,7 +273,6 @@ open class SettingsActivity
PreferencesUtil.setAppProperties(this, appProperties)
// Restart the current activity
Stylish.assignStyle(this, PreferencesUtil.getStyle(this))
relaunchCurrentScreen()
Toast.makeText(this, R.string.success_import_app_properties, Toast.LENGTH_LONG).show()
}