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 alertManager() : PopupAlertManager
fun alertManager(): PopupAlertManager
fun reAuthHelper() : ReAuthHelper
fun reAuthHelper(): ReAuthHelper
@Component.Factory
interface Factory {

View File

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

View File

@ -32,45 +32,14 @@ import javax.inject.Inject
*/
class VectorConfiguration @Inject constructor(private val context: Context) {
// TODO Import mLanguageReceiver From Riot?
fun onConfigurationChanged() {
if (Locale.getDefault().toString() != VectorLocale.applicationLocale.toString()) {
Timber.v("## onConfigurationChanged(): the locale has been updated to ${Locale.getDefault()}")
Timber.v("## onConfigurationChanged(): restore the expected value ${VectorLocale.applicationLocale}")
updateApplicationSettings(VectorLocale.applicationLocale,
FontScale.getFontScaleValue(context),
ThemeUtils.getApplicationTheme(context))
Locale.setDefault(VectorLocale.applicationLocale)
}
}
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
*/
@ -92,15 +61,6 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
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
*
@ -140,7 +100,6 @@ class VectorConfiguration @Inject constructor(private val context: Context) {
* Compute the locale status value
* @return the local status value
*/
// TODO Create data class for this
fun getHash(): String {
return (VectorLocale.applicationLocale.toString()
+ "_" + FontScale.getFontScaleValue(context).preferenceValue

View File

@ -164,16 +164,16 @@ class KeysBackupSetupStep2Fragment @Inject constructor() : VectorBaseFragment()
@OnClick(R.id.keys_backup_setup_step2_button)
fun doNext() {
when {
viewModel.passphrase.value.isNullOrEmpty() -> {
viewModel.passphrase.value.isNullOrEmpty() -> {
viewModel.passphraseError.value = context?.getString(R.string.passphrase_empty_error_message)
}
viewModel.passphrase.value != viewModel.confirmPassphrase.value -> {
viewModel.confirmPassphraseError.value = context?.getString(R.string.passphrase_passphrase_does_not_match)
}
viewModel.passwordStrength.value?.score ?: 0 < 4 -> {
viewModel.passwordStrength.value?.score ?: 0 < 4 -> {
viewModel.passphraseError.value = context?.getString(R.string.passphrase_passphrase_too_weak)
}
else -> {
else -> {
viewModel.megolmBackupCreationInfo = null
viewModel.prepareRecoveryKey(activity!!, viewModel.passphrase.value)
@ -190,7 +190,7 @@ class KeysBackupSetupStep2Fragment @Inject constructor() : VectorBaseFragment()
viewModel.prepareRecoveryKey(activity!!, null)
}
else -> {
else -> {
// User has entered a passphrase but want to skip this step.
viewModel.passphraseError.value = context?.getString(R.string.keys_backup_passphrase_not_empty_error_message)
}

View File

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

View File

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

View File

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

View File

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