Skip to content

Commit

Permalink
Merge pull request #3223 from CruGlobal/appLanguageListSort
Browse files Browse the repository at this point in the history
GT-2190 sort the language list based on the app language
  • Loading branch information
frett authored Nov 13, 2023
2 parents affcf0e + a36e53c commit 8c5aeee
Showing 1 changed file with 17 additions and 7 deletions.
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)
}

0 comments on commit 8c5aeee

Please sign in to comment.