Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GT-2190 sort the language list based on the app language #3223

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}