-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3223 from CruGlobal/appLanguageListSort
GT-2190 sort the language list based on the app language
- Loading branch information
Showing
1 changed file
with
17 additions
and
7 deletions.
There are no files selected for viewing
24 changes: 17 additions & 7 deletions
24
app/src/main/kotlin/org/cru/godtools/ui/languages/app/AppLanguageViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |