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
Bugfix 🐛:
-
- Fix issues with FontScale switch (#69, #645)
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(): restore the expected value ${VectorLocale.applicationLocale}")
updateApplicationSettings(VectorLocale.applicationLocale,
FontScale.getFontScalePrefValue(context),
ThemeUtils.getApplicationTheme(context))
FontScale.getFontScaleValue(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)
FontScale.saveFontScale(context, textSize)
FontScale.saveFontScaleValue(context, fontScaleValue)
Locale.setDefault(locale)
val config = Configuration(context.resources.configuration)
@Suppress("DEPRECATION")
config.locale = locale
config.fontScale = FontScale.getFontScale(context)
config.fontScale = FontScale.getFontScaleValue(context).scale
@Suppress("DEPRECATION")
context.resources.updateConfiguration(config, context.resources.displayMetrics)
@ -67,8 +67,8 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
fun updateApplicationTheme(theme: String) {
ThemeUtils.setApplicationTheme(context, theme)
updateApplicationSettings(VectorLocale.applicationLocale,
FontScale.getFontScalePrefValue(context),
theme)
FontScale.getFontScaleValue(context),
theme)
}
/**
@ -77,14 +77,14 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
fun initConfiguration() {
VectorLocale.init(context)
val locale = VectorLocale.applicationLocale
val fontScale = FontScale.getFontScale(context)
val fontScale = FontScale.getFontScaleValue(context)
val theme = ThemeUtils.getApplicationTheme(context)
Locale.setDefault(locale)
val config = Configuration(context.resources.configuration)
@Suppress("DEPRECATION")
config.locale = locale
config.fontScale = fontScale
config.fontScale = fontScale.scale
@Suppress("DEPRECATION")
context.resources.updateConfiguration(config, context.resources.displayMetrics)
@ -98,7 +98,7 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
* @param 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 locale = VectorLocale.applicationLocale
val configuration = resources.configuration
configuration.fontScale = FontScale.getFontScale(context)
configuration.fontScale = FontScale.getFontScaleValue(context).scale
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(locale)
@ -143,7 +143,7 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
// TODO Create data class for this
fun getHash(): String {
return (VectorLocale.applicationLocale.toString()
+ "_" + FontScale.getFontScalePrefValue(context)
+ "_" + FontScale.getFontScaleValue(context).preferenceValue
+ "_" + ThemeUtils.getApplicationTheme(context))
}
}

View file

@ -17,7 +17,7 @@
package im.vector.riotx.features.settings
import android.content.Context
import android.content.res.Configuration
import androidx.annotation.StringRes
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import im.vector.riotx.R
@ -29,124 +29,59 @@ object FontScale {
// Key for the SharedPrefs
private const val APPLICATION_FONT_SCALE_KEY = "APPLICATION_FONT_SCALE_KEY"
// Possible values for the SharedPrefs
private const val FONT_SCALE_TINY = "FONT_SCALE_TINY"
private const val FONT_SCALE_SMALL = "FONT_SCALE_SMALL"
private const val FONT_SCALE_NORMAL = "FONT_SCALE_NORMAL"
private const val FONT_SCALE_LARGE = "FONT_SCALE_LARGE"
private const val FONT_SCALE_LARGER = "FONT_SCALE_LARGER"
private const val FONT_SCALE_LARGEST = "FONT_SCALE_LARGEST"
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
data class FontScaleValue(
val index: Int,
// Possible values for the SharedPrefs
val preferenceValue: String,
val scale: Float,
@StringRes
val nameResId: Int
)
private val prefValueToNameResId = mapOf(
FONT_SCALE_TINY to R.string.tiny,
FONT_SCALE_SMALL to R.string.small,
FONT_SCALE_NORMAL to R.string.normal,
FONT_SCALE_LARGE to R.string.large,
FONT_SCALE_LARGER to R.string.larger,
FONT_SCALE_LARGEST to R.string.largest,
FONT_SCALE_HUGE to R.string.huge
private val fontScaleValues = listOf(
FontScaleValue(0, "FONT_SCALE_TINY", 0.70f, R.string.tiny),
FontScaleValue(1, "FONT_SCALE_SMALL", 0.85f, R.string.small),
FontScaleValue(2, "FONT_SCALE_NORMAL", 1.00f, R.string.normal),
FontScaleValue(3, "FONT_SCALE_LARGE", 1.15f, R.string.large),
FontScaleValue(4, "FONT_SCALE_LARGER", 1.30f, R.string.larger),
FontScaleValue(5, "FONT_SCALE_LARGEST", 1.45f, R.string.largest),
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
*
* @return the font scale
* @return the font scale value
*/
fun getFontScalePrefValue(context: Context): String {
fun getFontScaleValue(context: Context): FontScaleValue {
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
scalePreferenceValue = FONT_SCALE_NORMAL
if (fontScaleToPrefValue.containsKey(fontScale)) {
scalePreferenceValue = fontScaleToPrefValue[fontScale] as String
}
preferences.edit {
putString(APPLICATION_FONT_SCALE_KEY, scalePreferenceValue)
}
(fontScaleValues.firstOrNull { it.scale == fontScale } ?: normalFontScaleValue)
.also { preferences.edit { putString(APPLICATION_FONT_SCALE_KEY, it.preferenceValue) } }
} 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 {
val fontScale = getFontScalePrefValue(context)
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)
}
}
fun saveFontScaleValue(context: Context, fontScaleValue: FontScaleValue) {
PreferenceManager.getDefaultSharedPreferences(context)
.edit { putString(APPLICATION_FONT_SCALE_KEY, fontScaleValue.preferenceValue) }
}
}

View file

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

View file

@ -52,7 +52,7 @@ object ThemeUtils {
*/
fun getApplicationTheme(context: Context): String {
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
android:dialogTitle="@string/font_size"
android:key="SETTINGS_INTERFACE_TEXT_SIZE_KEY"
android:persistent="false"
android:title="@string/font_size" />
</im.vector.riotx.core.preference.VectorPreferenceCategory>