This commit is contained in:
Benoit Marty 2020-05-14 21:10:03 +02:00
parent 19d655ec41
commit 8ac2cb0530
8 changed files with 24 additions and 66 deletions

View File

@ -130,9 +130,9 @@ interface VectorComponent {
fun emojiDataSource(): EmojiDataSource fun emojiDataSource(): EmojiDataSource
fun alertManager() : PopupAlertManager fun alertManager(): PopupAlertManager
fun reAuthHelper() : ReAuthHelper fun reAuthHelper(): ReAuthHelper
@Component.Factory @Component.Factory
interface Factory { interface Factory {

View File

@ -179,7 +179,7 @@ abstract class VectorBaseActivity : AppCompatActivity(), HasScreenInjector {
} }
}) })
sessionListener = getVectorComponent().sessionListener() sessionListener = vectorComponent.sessionListener()
sessionListener.globalErrorLiveData.observeEvent(this) { sessionListener.globalErrorLiveData.observeEvent(this) {
handleGlobalError(it) handleGlobalError(it)
} }

View File

@ -32,45 +32,14 @@ import javax.inject.Inject
*/ */
class VectorConfiguration @Inject constructor(private val context: Context) { class VectorConfiguration @Inject constructor(private val context: Context) {
// TODO Import mLanguageReceiver From Riot?
fun onConfigurationChanged() { fun onConfigurationChanged() {
if (Locale.getDefault().toString() != VectorLocale.applicationLocale.toString()) { if (Locale.getDefault().toString() != VectorLocale.applicationLocale.toString()) {
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, Locale.setDefault(VectorLocale.applicationLocale)
FontScale.getFontScaleValue(context),
ThemeUtils.getApplicationTheme(context))
} }
} }
private fun updateApplicationSettings(locale: Locale, fontScaleValue: FontScale.FontScaleValue, theme: String) {
VectorLocale.saveApplicationLocale(context, locale)
FontScale.saveFontScaleValue(context, fontScaleValue)
Locale.setDefault(locale)
val config = Configuration(context.resources.configuration)
@Suppress("DEPRECATION")
config.locale = locale
config.fontScale = FontScale.getFontScaleValue(context).scale
@Suppress("DEPRECATION")
context.resources.updateConfiguration(config, context.resources.displayMetrics)
ThemeUtils.setApplicationTheme(context, theme)
// TODO PhoneNumberUtils.onLocaleUpdate()
}
/**
* Update the application theme
*
* @param theme the new theme
*/
fun updateApplicationTheme(theme: String) {
ThemeUtils.setApplicationTheme(context, theme)
updateApplicationSettings(VectorLocale.applicationLocale,
FontScale.getFontScaleValue(context),
theme)
}
/** /**
* Init the configuration from the saved one * Init the configuration from the saved one
*/ */
@ -92,15 +61,6 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
ThemeUtils.setApplicationTheme(context, theme) ThemeUtils.setApplicationTheme(context, theme)
} }
/**
* Update the application locale
*
* @param locale
*/
fun updateApplicationLocale(locale: Locale) {
updateApplicationSettings(locale, FontScale.getFontScaleValue(context), ThemeUtils.getApplicationTheme(context))
}
/** /**
* Compute a localised context * Compute a localised context
* *
@ -140,7 +100,6 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
* Compute the locale status value * Compute the locale status value
* @return the local status value * @return the local status value
*/ */
// TODO Create data class for this
fun getHash(): String { fun getHash(): String {
return (VectorLocale.applicationLocale.toString() return (VectorLocale.applicationLocale.toString()
+ "_" + FontScale.getFontScaleValue(context).preferenceValue + "_" + FontScale.getFontScaleValue(context).preferenceValue

View File

@ -80,7 +80,7 @@ object FontScale {
* *
* @param fontScaleValue the font scale value to store * @param fontScaleValue the font scale value to store
*/ */
fun saveFontScaleValue(context: Context, fontScaleValue: FontScaleValue) { private fun saveFontScaleValue(context: Context, fontScaleValue: FontScaleValue) {
PreferenceManager.getDefaultSharedPreferences(context) PreferenceManager.getDefaultSharedPreferences(context)
.edit { putString(APPLICATION_FONT_SCALE_KEY, fontScaleValue.preferenceValue) } .edit { putString(APPLICATION_FONT_SCALE_KEY, fontScaleValue.preferenceValue) }
} }

View File

@ -50,10 +50,13 @@ object VectorLocale {
var applicationLocale = defaultLocale var applicationLocale = defaultLocale
private set private set
lateinit var context: Context
/** /**
* Init this object * Init this object
*/ */
fun init(context: Context) { fun init(context: Context) {
this.context = context
val preferences = PreferenceManager.getDefaultSharedPreferences(context) val preferences = PreferenceManager.getDefaultSharedPreferences(context)
if (preferences.contains(APPLICATION_LOCALE_LANGUAGE_KEY)) { if (preferences.contains(APPLICATION_LOCALE_LANGUAGE_KEY)) {
@ -70,7 +73,7 @@ object VectorLocale {
applicationLocale = defaultLocale applicationLocale = defaultLocale
} }
saveApplicationLocale(context, applicationLocale) saveApplicationLocale(applicationLocale)
} }
// init the known locales in background // init the known locales in background
@ -82,7 +85,7 @@ object VectorLocale {
/** /**
* Save the new application locale. * Save the new application locale.
*/ */
fun saveApplicationLocale(context: Context, locale: Locale) { fun saveApplicationLocale(locale: Locale) {
applicationLocale = locale applicationLocale = locale
PreferenceManager.getDefaultSharedPreferences(context).edit { PreferenceManager.getDefaultSharedPreferences(context).edit {

View File

@ -54,13 +54,9 @@ class VectorSettingsPreferencesFragment @Inject constructor(
findPreference<VectorListPreference>(ThemeUtils.APPLICATION_THEME_KEY)!! findPreference<VectorListPreference>(ThemeUtils.APPLICATION_THEME_KEY)!!
.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue -> .onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue is String) { if (newValue is String) {
vectorConfiguration.updateApplicationTheme(newValue) ThemeUtils.setApplicationTheme(requireContext(), newValue)
// Restart the Activity // Restart the Activity
activity?.let { activity?.restart()
// Note: recreate does not apply the color correctly
it.startActivity(it.intent)
it.finish()
}
true true
} else { } else {
false false

View File

@ -24,12 +24,10 @@ import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject import com.squareup.inject.assisted.AssistedInject
import im.vector.riotx.core.extensions.exhaustive import im.vector.riotx.core.extensions.exhaustive
import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.core.platform.VectorViewModel
import im.vector.riotx.features.configuration.VectorConfiguration
import im.vector.riotx.features.settings.VectorLocale import im.vector.riotx.features.settings.VectorLocale
class LocalePickerViewModel @AssistedInject constructor( class LocalePickerViewModel @AssistedInject constructor(
@Assisted initialState: LocalePickerViewState, @Assisted initialState: LocalePickerViewState
private val vectorConfiguration: VectorConfiguration
) : VectorViewModel<LocalePickerViewState, LocalePickerAction, LocalePickerViewEvents>(initialState) { ) : VectorViewModel<LocalePickerViewState, LocalePickerAction, LocalePickerViewEvents>(initialState) {
@AssistedInject.Factory @AssistedInject.Factory
@ -37,13 +35,15 @@ class LocalePickerViewModel @AssistedInject constructor(
fun create(initialState: LocalePickerViewState): LocalePickerViewModel fun create(initialState: LocalePickerViewState): LocalePickerViewModel
} }
companion object : MvRxViewModelFactory<LocalePickerViewModel, LocalePickerViewState> { init {
setState {
override fun initialState(viewModelContext: ViewModelContext): LocalePickerViewState? { copy(
return LocalePickerViewState(
locales = VectorLocale.supportedLocales locales = VectorLocale.supportedLocales
) )
} }
}
companion object : MvRxViewModelFactory<LocalePickerViewModel, LocalePickerViewState> {
@JvmStatic @JvmStatic
override fun create(viewModelContext: ViewModelContext, state: LocalePickerViewState): LocalePickerViewModel? { override fun create(viewModelContext: ViewModelContext, state: LocalePickerViewState): LocalePickerViewModel? {
@ -62,7 +62,7 @@ class LocalePickerViewModel @AssistedInject constructor(
} }
private fun handleSelectLocale(action: LocalePickerAction.SelectLocale) { private fun handleSelectLocale(action: LocalePickerAction.SelectLocale) {
vectorConfiguration.updateApplicationLocale(action.locale) VectorLocale.saveApplicationLocale(action.locale)
_viewEvents.post(LocalePickerViewEvents.RestartActivity) _viewEvents.post(LocalePickerViewEvents.RestartActivity)
} }
} }