diff --git a/app/src/main/kotlin/org/cru/godtools/ui/languages/app/AppLanguageViewModel.kt b/app/src/main/kotlin/org/cru/godtools/ui/languages/app/AppLanguageViewModel.kt index 3a7abf566f..b07c41952e 100644 --- a/app/src/main/kotlin/org/cru/godtools/ui/languages/app/AppLanguageViewModel.kt +++ b/app/src/main/kotlin/org/cru/godtools/ui/languages/app/AppLanguageViewModel.kt @@ -1,19 +1,29 @@ package org.cru.godtools.ui.languages.app -import android.app.Application -import androidx.lifecycle.AndroidViewModel +import android.content.Context +import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import dagger.hilt.android.lifecycle.HiltViewModel +import dagger.hilt.android.qualifiers.ApplicationContext +import javax.inject.Inject import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flow -import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.shareIn import org.ccci.gto.android.common.androidx.core.app.LocaleConfigCompat import org.ccci.gto.android.common.androidx.core.os.asIterable +import org.cru.godtools.base.Settings -class AppLanguageViewModel(application: Application) : AndroidViewModel(application) { - val languages = flow { emit(LocaleConfigCompat.getSupportedLocales(application)?.asIterable() ?: emptyList()) } - .map { it.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.getDisplayName(it) }) } +@HiltViewModel +class AppLanguageViewModel @Inject constructor( + @ApplicationContext context: Context, + settings: Settings, +) : ViewModel() { + val languages = flow { emit(LocaleConfigCompat.getSupportedLocales(context)?.asIterable() ?: emptyList()) } + .combine(settings.appLanguageFlow) { langs, appLang -> + langs.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.getDisplayName(appLang) }) + } .distinctUntilChanged() - .shareIn(viewModelScope, SharingStarted.WhileSubscribed(), replay = 1) + .shareIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), replay = 1) }