FontSize: rework by creating FontScaleValue data class.

This commit is contained in:
Benoit Marty 2020-05-14 11:22:44 +02:00
parent f45040c405
commit 4961bb0e08
6 changed files with 56 additions and 120 deletions

View File

@ -8,7 +8,7 @@ Improvements 🙌:
- Better connectivity lost indicator when airplane mode is on - Better connectivity lost indicator when airplane mode is on
Bugfix 🐛: Bugfix 🐛:
- - Fix issues with FontScale switch (#69, #645)
Translations 🗣: Translations 🗣:
- -

View File

@ -38,20 +38,20 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
Timber.v("## onConfigurationChanged(): the locale has been updated to ${Locale.getDefault()}") Timber.v("## onConfigurationChanged(): the locale has been updated to ${Locale.getDefault()}")
Timber.v("## onConfigurationChanged(): restore the expected value ${VectorLocale.applicationLocale}") Timber.v("## onConfigurationChanged(): restore the expected value ${VectorLocale.applicationLocale}")
updateApplicationSettings(VectorLocale.applicationLocale, updateApplicationSettings(VectorLocale.applicationLocale,
FontScale.getFontScalePrefValue(context), FontScale.getFontScaleValue(context),
ThemeUtils.getApplicationTheme(context)) ThemeUtils.getApplicationTheme(context))
} }
} }
private fun updateApplicationSettings(locale: Locale, textSize: String, theme: String) { private fun updateApplicationSettings(locale: Locale, fontScaleValue: FontScale.FontScaleValue, theme: String) {
VectorLocale.saveApplicationLocale(context, locale) VectorLocale.saveApplicationLocale(context, locale)
FontScale.saveFontScale(context, textSize) FontScale.saveFontScaleValue(context, fontScaleValue)
Locale.setDefault(locale) Locale.setDefault(locale)
val config = Configuration(context.resources.configuration) val config = Configuration(context.resources.configuration)
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
config.locale = locale config.locale = locale
config.fontScale = FontScale.getFontScale(context) config.fontScale = FontScale.getFontScaleValue(context).scale
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
context.resources.updateConfiguration(config, context.resources.displayMetrics) context.resources.updateConfiguration(config, context.resources.displayMetrics)
@ -67,8 +67,8 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
fun updateApplicationTheme(theme: String) { fun updateApplicationTheme(theme: String) {
ThemeUtils.setApplicationTheme(context, theme) ThemeUtils.setApplicationTheme(context, theme)
updateApplicationSettings(VectorLocale.applicationLocale, updateApplicationSettings(VectorLocale.applicationLocale,
FontScale.getFontScalePrefValue(context), FontScale.getFontScaleValue(context),
theme) theme)
} }
/** /**
@ -77,14 +77,14 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
fun initConfiguration() { fun initConfiguration() {
VectorLocale.init(context) VectorLocale.init(context)
val locale = VectorLocale.applicationLocale val locale = VectorLocale.applicationLocale
val fontScale = FontScale.getFontScale(context) val fontScale = FontScale.getFontScaleValue(context)
val theme = ThemeUtils.getApplicationTheme(context) val theme = ThemeUtils.getApplicationTheme(context)
Locale.setDefault(locale) Locale.setDefault(locale)
val config = Configuration(context.resources.configuration) val config = Configuration(context.resources.configuration)
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
config.locale = locale config.locale = locale
config.fontScale = fontScale config.fontScale = fontScale.scale
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
context.resources.updateConfiguration(config, context.resources.displayMetrics) context.resources.updateConfiguration(config, context.resources.displayMetrics)
@ -98,7 +98,7 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
* @param locale * @param locale
*/ */
fun updateApplicationLocale(locale: Locale) { fun updateApplicationLocale(locale: Locale) {
updateApplicationSettings(locale, FontScale.getFontScalePrefValue(context), ThemeUtils.getApplicationTheme(context)) updateApplicationSettings(locale, FontScale.getFontScaleValue(context), ThemeUtils.getApplicationTheme(context))
} }
/** /**
@ -113,7 +113,7 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
val resources = context.resources val resources = context.resources
val locale = VectorLocale.applicationLocale val locale = VectorLocale.applicationLocale
val configuration = resources.configuration val configuration = resources.configuration
configuration.fontScale = FontScale.getFontScale(context) configuration.fontScale = FontScale.getFontScaleValue(context).scale
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(locale) configuration.setLocale(locale)
@ -143,7 +143,7 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
// TODO Create data class for this // TODO Create data class for this
fun getHash(): String { fun getHash(): String {
return (VectorLocale.applicationLocale.toString() return (VectorLocale.applicationLocale.toString()
+ "_" + FontScale.getFontScalePrefValue(context) + "_" + FontScale.getFontScaleValue(context).preferenceValue
+ "_" + ThemeUtils.getApplicationTheme(context)) + "_" + ThemeUtils.getApplicationTheme(context))
} }
} }

View File

@ -17,7 +17,7 @@
package im.vector.riotx.features.settings package im.vector.riotx.features.settings
import android.content.Context import android.content.Context
import android.content.res.Configuration import androidx.annotation.StringRes
import androidx.core.content.edit import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import im.vector.riotx.R import im.vector.riotx.R
@ -29,124 +29,59 @@ object FontScale {
// Key for the SharedPrefs // Key for the SharedPrefs
private const val APPLICATION_FONT_SCALE_KEY = "APPLICATION_FONT_SCALE_KEY" private const val APPLICATION_FONT_SCALE_KEY = "APPLICATION_FONT_SCALE_KEY"
// Possible values for the SharedPrefs data class FontScaleValue(
private const val FONT_SCALE_TINY = "FONT_SCALE_TINY" val index: Int,
private const val FONT_SCALE_SMALL = "FONT_SCALE_SMALL" // Possible values for the SharedPrefs
private const val FONT_SCALE_NORMAL = "FONT_SCALE_NORMAL" val preferenceValue: String,
private const val FONT_SCALE_LARGE = "FONT_SCALE_LARGE" val scale: Float,
private const val FONT_SCALE_LARGER = "FONT_SCALE_LARGER" @StringRes
private const val FONT_SCALE_LARGEST = "FONT_SCALE_LARGEST" val nameResId: Int
private const val FONT_SCALE_HUGE = "FONT_SCALE_HUGE"
private val fontScaleToPrefValue = mapOf(
0.70f to FONT_SCALE_TINY,
0.85f to FONT_SCALE_SMALL,
1.00f to FONT_SCALE_NORMAL,
1.15f to FONT_SCALE_LARGE,
1.30f to FONT_SCALE_LARGER,
1.45f to FONT_SCALE_LARGEST,
1.60f to FONT_SCALE_HUGE
) )
private val prefValueToNameResId = mapOf( private val fontScaleValues = listOf(
FONT_SCALE_TINY to R.string.tiny, FontScaleValue(0, "FONT_SCALE_TINY", 0.70f, R.string.tiny),
FONT_SCALE_SMALL to R.string.small, FontScaleValue(1, "FONT_SCALE_SMALL", 0.85f, R.string.small),
FONT_SCALE_NORMAL to R.string.normal, FontScaleValue(2, "FONT_SCALE_NORMAL", 1.00f, R.string.normal),
FONT_SCALE_LARGE to R.string.large, FontScaleValue(3, "FONT_SCALE_LARGE", 1.15f, R.string.large),
FONT_SCALE_LARGER to R.string.larger, FontScaleValue(4, "FONT_SCALE_LARGER", 1.30f, R.string.larger),
FONT_SCALE_LARGEST to R.string.largest, FontScaleValue(5, "FONT_SCALE_LARGEST", 1.45f, R.string.largest),
FONT_SCALE_HUGE to R.string.huge FontScaleValue(6, "FONT_SCALE_HUGE", 1.60f, R.string.huge)
) )
private val normalFontScaleValue = fontScaleValues[2]
/** /**
* Get the font scale value from SharedPrefs. Init the SharedPrefs if necessary * Get the font scale value from SharedPrefs. Init the SharedPrefs if necessary
* *
* @return the font scale * @return the font scale value
*/ */
fun getFontScalePrefValue(context: Context): String { fun getFontScaleValue(context: Context): FontScaleValue {
val preferences = PreferenceManager.getDefaultSharedPreferences(context) val preferences = PreferenceManager.getDefaultSharedPreferences(context)
var scalePreferenceValue: String
if (APPLICATION_FONT_SCALE_KEY !in preferences) { return if (APPLICATION_FONT_SCALE_KEY !in preferences) {
val fontScale = context.resources.configuration.fontScale val fontScale = context.resources.configuration.fontScale
scalePreferenceValue = FONT_SCALE_NORMAL (fontScaleValues.firstOrNull { it.scale == fontScale } ?: normalFontScaleValue)
.also { preferences.edit { putString(APPLICATION_FONT_SCALE_KEY, it.preferenceValue) } }
if (fontScaleToPrefValue.containsKey(fontScale)) {
scalePreferenceValue = fontScaleToPrefValue[fontScale] as String
}
preferences.edit {
putString(APPLICATION_FONT_SCALE_KEY, scalePreferenceValue)
}
} else { } else {
scalePreferenceValue = preferences.getString(APPLICATION_FONT_SCALE_KEY, FONT_SCALE_NORMAL)!! val pref = preferences.getString(APPLICATION_FONT_SCALE_KEY, null)
fontScaleValues.firstOrNull { it.preferenceValue == pref } ?: normalFontScaleValue
} }
}
return scalePreferenceValue fun updateFontScale(context: Context, index: Int) {
fontScaleValues.getOrNull(index)?.let {
saveFontScaleValue(context, it)
}
} }
/** /**
* Provides the font scale value * Store the font scale vale
* *
* @return the font scale * @param fontScaleValue the font scale value to store
*/ */
fun getFontScale(context: Context): Float { fun saveFontScaleValue(context: Context, fontScaleValue: FontScaleValue) {
val fontScale = getFontScalePrefValue(context) PreferenceManager.getDefaultSharedPreferences(context)
.edit { putString(APPLICATION_FONT_SCALE_KEY, fontScaleValue.preferenceValue) }
if (fontScaleToPrefValue.containsValue(fontScale)) {
for ((key, value) in fontScaleToPrefValue) {
if (value == fontScale) {
return key
}
}
}
return 1.0f
}
/**
* Provides the font scale description
*
* @return the font description
*/
fun getFontScaleDescription(context: Context): String {
val fontScale = getFontScalePrefValue(context)
return if (prefValueToNameResId.containsKey(fontScale)) {
context.getString(prefValueToNameResId[fontScale] as Int)
} else context.getString(R.string.normal)
}
/**
* Update the font size from the locale description.
*
* @param fontScaleDescription the font scale description
*/
fun updateFontScale(context: Context, fontScaleDescription: String) {
for ((key, value) in prefValueToNameResId) {
if (context.getString(value) == fontScaleDescription) {
saveFontScale(context, key)
}
}
val config = Configuration(context.resources.configuration)
config.fontScale = getFontScale(context)
@Suppress("DEPRECATION")
context.resources.updateConfiguration(config, context.resources.displayMetrics)
}
/**
* Save the new font scale
*
* @param scaleValue the text scale
*/
fun saveFontScale(context: Context, scaleValue: String) {
if (scaleValue.isNotEmpty()) {
PreferenceManager.getDefaultSharedPreferences(context)
.edit {
putString(APPLICATION_FONT_SCALE_KEY, scaleValue)
}
}
} }
} }

View File

@ -24,6 +24,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.SwitchPreference import androidx.preference.SwitchPreference
import im.vector.riotx.R import im.vector.riotx.R
import im.vector.riotx.core.extensions.restart
import im.vector.riotx.core.preference.VectorListPreference import im.vector.riotx.core.preference.VectorListPreference
import im.vector.riotx.core.preference.VectorPreference import im.vector.riotx.core.preference.VectorPreference
import im.vector.riotx.features.configuration.VectorConfiguration import im.vector.riotx.features.configuration.VectorConfiguration
@ -137,7 +138,7 @@ class VectorSettingsPreferencesFragment @Inject constructor(
selectedLanguagePreference.summary = VectorLocale.localeToLocalisedString(VectorLocale.applicationLocale) selectedLanguagePreference.summary = VectorLocale.localeToLocalisedString(VectorLocale.applicationLocale)
// Text size // Text size
textSizePreference.summary = FontScale.getFontScaleDescription(activity!!) textSizePreference.summary = getString(FontScale.getFontScaleValue(activity!!).nameResId)
textSizePreference.onPreferenceClickListener = Preference.OnPreferenceClickListener { textSizePreference.onPreferenceClickListener = Preference.OnPreferenceClickListener {
activity?.let { displayTextSizeSelection(it) } activity?.let { displayTextSizeSelection(it) }
@ -160,19 +161,18 @@ class VectorSettingsPreferencesFragment @Inject constructor(
val childCount = linearLayout.childCount val childCount = linearLayout.childCount
val scaleText = FontScale.getFontScaleDescription(activity) val index = FontScale.getFontScaleValue(activity).index
for (i in 0 until childCount) { for (i in 0 until childCount) {
val v = linearLayout.getChildAt(i) val v = linearLayout.getChildAt(i)
if (v is CheckedTextView) { if (v is CheckedTextView) {
v.isChecked = v.text == scaleText v.isChecked = i == index
v.setOnClickListener { v.setOnClickListener {
dialog.dismiss() dialog.dismiss()
FontScale.updateFontScale(activity, v.text.toString()) FontScale.updateFontScale(activity, i)
activity.startActivity(activity.intent) activity.restart()
activity.finish()
} }
} }
} }

View File

@ -52,7 +52,7 @@ object ThemeUtils {
*/ */
fun getApplicationTheme(context: Context): String { fun getApplicationTheme(context: Context): String {
return PreferenceManager.getDefaultSharedPreferences(context) return PreferenceManager.getDefaultSharedPreferences(context)
.getString(APPLICATION_THEME_KEY, THEME_LIGHT_VALUE)!! .getString(APPLICATION_THEME_KEY, THEME_LIGHT_VALUE) ?: THEME_LIGHT_VALUE
} }
/** /**

View File

@ -24,6 +24,7 @@
<im.vector.riotx.core.preference.VectorPreference <im.vector.riotx.core.preference.VectorPreference
android:dialogTitle="@string/font_size" android:dialogTitle="@string/font_size"
android:key="SETTINGS_INTERFACE_TEXT_SIZE_KEY" android:key="SETTINGS_INTERFACE_TEXT_SIZE_KEY"
android:persistent="false"
android:title="@string/font_size" /> android:title="@string/font_size" />
</im.vector.riotx.core.preference.VectorPreferenceCategory> </im.vector.riotx.core.preference.VectorPreferenceCategory>