From 28f8d9500ee9d88f3280bdf39940d561b5147f0c Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 18 May 2020 15:59:15 +0200 Subject: [PATCH] Better coroutine management --- .../riotx/features/settings/VectorLocale.kt | 21 ++++-------- .../settings/locale/LocalePickerViewModel.kt | 32 +++++++------------ 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/features/settings/VectorLocale.kt b/vector/src/main/java/im/vector/riotx/features/settings/VectorLocale.kt index efe4110680..a4ccfdba47 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/VectorLocale.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/VectorLocale.kt @@ -23,9 +23,6 @@ import androidx.core.content.edit import androidx.preference.PreferenceManager import im.vector.riotx.R import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.Job -import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import timber.log.Timber import java.util.Locale @@ -258,19 +255,13 @@ object VectorLocale { } } - fun loadLocales(listener: Listener): Job { - return GlobalScope.launch(Dispatchers.Main) { - if (supportedLocales.isEmpty()) { - // init the known locales in background - withContext(Dispatchers.IO) { - initApplicationLocales() - } + suspend fun getSupportedLocales(): List { + if (supportedLocales.isEmpty()) { + // init the known locales in background + withContext(Dispatchers.IO) { + initApplicationLocales() } - listener.onLoaded(supportedLocales) } - } - - interface Listener { - fun onLoaded(locales: List) + return supportedLocales } } diff --git a/vector/src/main/java/im/vector/riotx/features/settings/locale/LocalePickerViewModel.kt b/vector/src/main/java/im/vector/riotx/features/settings/locale/LocalePickerViewModel.kt index f0a4243439..e4cc64733c 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/locale/LocalePickerViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/locale/LocalePickerViewModel.kt @@ -16,6 +16,7 @@ package im.vector.riotx.features.settings.locale +import androidx.lifecycle.viewModelScope import com.airbnb.mvrx.ActivityViewModelContext import com.airbnb.mvrx.FragmentViewModelContext import com.airbnb.mvrx.MvRxViewModelFactory @@ -26,23 +27,27 @@ 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.settings.VectorLocale -import kotlinx.coroutines.Job -import java.util.Locale +import kotlinx.coroutines.launch class LocalePickerViewModel @AssistedInject constructor( @Assisted initialState: LocalePickerViewState -) : VectorViewModel(initialState), - VectorLocale.Listener { +) : VectorViewModel(initialState) { @AssistedInject.Factory interface Factory { fun create(initialState: LocalePickerViewState): LocalePickerViewModel } - private var loadingJob: Job? = null - init { - loadingJob = VectorLocale.loadLocales(this) + viewModelScope.launch { + val result = VectorLocale.getSupportedLocales() + + setState { + copy( + locales = Success(result) + ) + } + } } companion object : MvRxViewModelFactory { @@ -67,17 +72,4 @@ class LocalePickerViewModel @AssistedInject constructor( VectorLocale.saveApplicationLocale(action.locale) _viewEvents.post(LocalePickerViewEvents.RestartActivity) } - - override fun onLoaded(locales: List) { - setState { - copy( - locales = Success(locales) - ) - } - } - - override fun onCleared() { - super.onCleared() - loadingJob?.cancel() - } }