From 5544b20d7fdeeb3c0ac65ad5df577ec44ead9105 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Sat, 6 Mar 2021 13:28:10 +0100 Subject: [PATCH] Auto convert old themes --- CHANGELOG | 2 +- .../keepass/activities/stylish/Stylish.kt | 66 +++++++++---------- .../settings/NestedAppSettingsFragment.kt | 2 +- .../keepass/settings/PreferencesUtil.kt | 10 +++ .../metadata/android/en-US/changelogs/58.txt | 2 +- .../metadata/android/fr-FR/changelogs/58.txt | 2 +- 6 files changed, 47 insertions(+), 37 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index fc526bdd0..11e511a95 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,5 @@ KeePassDX(2.9.14) - * Dark Themes (WARNING: You must reselect your theme if upgrading from an old installation) #532 #714 + * Dark Themes #532 #714 * Fix binary deduplication #715 * Fix IconId #901 * Small changes #900 diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/stylish/Stylish.kt b/app/src/main/java/com/kunzisoft/keepass/activities/stylish/Stylish.kt index aeb971842..4d6406fab 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/stylish/Stylish.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/stylish/Stylish.kt @@ -23,7 +23,6 @@ import android.content.Context import android.content.res.Configuration import android.util.Log import androidx.annotation.StyleRes -import androidx.preference.PreferenceManager import com.kunzisoft.keepass.R import com.kunzisoft.keepass.settings.PreferencesUtil @@ -39,19 +38,14 @@ object Stylish { * @param context Context to retrieve the theme preference */ fun init(context: Context) { - val stylishPrefKey = context.getString(R.string.setting_style_key) Log.d(Stylish::class.java.name, "Attatching to " + context.packageName) - themeString = PreferenceManager.getDefaultSharedPreferences(context).getString(stylishPrefKey, context.getString(R.string.list_style_name_light)) + themeString = PreferencesUtil.getStyle(context) } - private fun retrieveEquivalentSystemStyle(styleString: String, context: Context): String { + private 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 - } + context.getString(R.string.list_style_brightness_light) -> false + context.getString(R.string.list_style_brightness_night) -> true else -> { when (context.resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK)) { Configuration.UI_MODE_NIGHT_YES -> true @@ -59,27 +53,34 @@ object Stylish { } } } - return if (systemNightMode) { - when (styleString) { - context.getString(R.string.list_style_name_light) -> context.getString(R.string.list_style_name_night) - context.getString(R.string.list_style_name_white) -> context.getString(R.string.list_style_name_black) - context.getString(R.string.list_style_name_clear) -> context.getString(R.string.list_style_name_dark) - context.getString(R.string.list_style_name_blue) -> context.getString(R.string.list_style_name_blue_night) - context.getString(R.string.list_style_name_red) -> context.getString(R.string.list_style_name_red_night) - context.getString(R.string.list_style_name_purple) -> context.getString(R.string.list_style_name_purple_dark) - else -> styleString - } + retrieveEquivalentNightStyle(context, styleString) } else { - when (styleString) { - context.getString(R.string.list_style_name_night) -> context.getString(R.string.list_style_name_light) - context.getString(R.string.list_style_name_black) -> context.getString(R.string.list_style_name_white) - context.getString(R.string.list_style_name_dark) -> context.getString(R.string.list_style_name_clear) - context.getString(R.string.list_style_name_blue_night) -> context.getString(R.string.list_style_name_blue) - context.getString(R.string.list_style_name_red_night) -> context.getString(R.string.list_style_name_red) - context.getString(R.string.list_style_name_purple_dark) -> context.getString(R.string.list_style_name_purple) - else -> styleString - } + retrieveEquivalentLightStyle(context, styleString) + } + } + + fun retrieveEquivalentLightStyle(context: Context, styleString: String): String { + return when (styleString) { + context.getString(R.string.list_style_name_night) -> context.getString(R.string.list_style_name_light) + context.getString(R.string.list_style_name_black) -> context.getString(R.string.list_style_name_white) + context.getString(R.string.list_style_name_dark) -> context.getString(R.string.list_style_name_clear) + context.getString(R.string.list_style_name_blue_night) -> context.getString(R.string.list_style_name_blue) + context.getString(R.string.list_style_name_red_night) -> context.getString(R.string.list_style_name_red) + context.getString(R.string.list_style_name_purple_dark) -> context.getString(R.string.list_style_name_purple) + else -> styleString + } + } + + private fun retrieveEquivalentNightStyle(context: Context, styleString: String): String { + return when (styleString) { + context.getString(R.string.list_style_name_light) -> context.getString(R.string.list_style_name_night) + context.getString(R.string.list_style_name_white) -> context.getString(R.string.list_style_name_black) + context.getString(R.string.list_style_name_clear) -> context.getString(R.string.list_style_name_dark) + context.getString(R.string.list_style_name_blue) -> context.getString(R.string.list_style_name_blue_night) + context.getString(R.string.list_style_name_red) -> context.getString(R.string.list_style_name_red_night) + context.getString(R.string.list_style_name_purple) -> context.getString(R.string.list_style_name_purple_dark) + else -> styleString } } @@ -87,8 +88,8 @@ object Stylish { * Assign the style to the class attribute * @param styleString Style id String */ - fun assignStyle(styleString: String, context: Context) { - themeString = retrieveEquivalentSystemStyle(styleString, context) + fun assignStyle(context: Context, styleString: String) { + themeString = retrieveEquivalentSystemStyle(context, styleString) } /** @@ -98,8 +99,7 @@ object Stylish { */ @StyleRes fun getThemeId(context: Context): Int { - - return when (retrieveEquivalentSystemStyle(themeString ?: context.getString(R.string.list_style_name_light), context)) { + return when (retrieveEquivalentSystemStyle(context, themeString ?: context.getString(R.string.list_style_name_light))) { context.getString(R.string.list_style_name_night) -> R.style.KeepassDXStyle_Night context.getString(R.string.list_style_name_white) -> R.style.KeepassDXStyle_White context.getString(R.string.list_style_name_black) -> R.style.KeepassDXStyle_Black diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/NestedAppSettingsFragment.kt b/app/src/main/java/com/kunzisoft/keepass/settings/NestedAppSettingsFragment.kt index 1d656aa33..0fcbeb393 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/NestedAppSettingsFragment.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/NestedAppSettingsFragment.kt @@ -385,7 +385,7 @@ class NestedAppSettingsFragment : NestedSettingsFragment() { } } if (styleEnabled) { - Stylish.assignStyle(styleIdString, activity) + Stylish.assignStyle(activity, styleIdString) // Relaunch the current activity to redraw theme (activity as? SettingsActivity?)?.apply { keepCurrentScreen() diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt b/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt index 013b085d0..ee5405f60 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt @@ -26,6 +26,7 @@ import android.net.Uri import androidx.preference.PreferenceManager import com.kunzisoft.keepass.BuildConfig import com.kunzisoft.keepass.R +import com.kunzisoft.keepass.activities.stylish.Stylish import com.kunzisoft.keepass.biometric.AdvancedUnlockManager import com.kunzisoft.keepass.database.element.SortNodeEnum import com.kunzisoft.keepass.timeout.TimeoutHelper @@ -132,6 +133,15 @@ object PreferencesUtil { context.resources.getBoolean(R.bool.show_uuid_default)) } + 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 styleString = PreferenceManager.getDefaultSharedPreferences(context) + .getString(stylishPrefKey, defaultStyleString) + ?: defaultStyleString + return Stylish.retrieveEquivalentLightStyle(context, styleString) + } + fun getStyleBrightness(context: Context): String? { val prefs = PreferenceManager.getDefaultSharedPreferences(context) return prefs.getString(context.getString(R.string.setting_style_brightness_key), diff --git a/fastlane/metadata/android/en-US/changelogs/58.txt b/fastlane/metadata/android/en-US/changelogs/58.txt index 96d0a2361..6acca26d6 100644 --- a/fastlane/metadata/android/en-US/changelogs/58.txt +++ b/fastlane/metadata/android/en-US/changelogs/58.txt @@ -1,4 +1,4 @@ - * Dark Themes (WARNING: You must reselect your theme if upgrading from an old installation) #532 #714 + * Dark Themes #532 #714 * Fix binary deduplication #715 * Fix IconId #901 * Small changes #900 \ No newline at end of file diff --git a/fastlane/metadata/android/fr-FR/changelogs/58.txt b/fastlane/metadata/android/fr-FR/changelogs/58.txt index d616e15c5..0da14a210 100644 --- a/fastlane/metadata/android/fr-FR/changelogs/58.txt +++ b/fastlane/metadata/android/fr-FR/changelogs/58.txt @@ -1,4 +1,4 @@ - * Thèmes sombres (ATTENTION: Vous devez reselectionner votre theme si mise à niveau d'une ancienne installation) #532 #714 + * Thèmes sombres #532 #714 * Correction de la duplication des binaires #715 * Correction IconId #901 * Petits changements #900 \ No newline at end of file