Skip to content

Commit

Permalink
Show system default language as first language
Browse files Browse the repository at this point in the history
  • Loading branch information
yasinkacmaz committed Sep 19, 2024
1 parent 5d5e03c commit f64059d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ class SettingsViewModel(

private fun fetchLanguages() = viewModelScope.launch {
_uiState.update { it.copy(showLoading = true) }
val languages = try {
configurationService.fetchLanguages().sortedBy(Language::englishName)
} catch (exception: Exception) {
emptyList()
val languages = buildList<Language> {
try {
addAll(configurationService.fetchLanguages().sortedBy(Language::englishName))
removeAll { it.iso6391 == Language.default.iso6391 }
add(0, Language.default)
} catch (_: Exception) {
}
}
_uiState.update { it.copy(showLoading = false, languages = languages) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,22 @@ class SettingsViewModelTest {
val settingsViewModel = createViewModel()
val uiStates = settingsViewModel.uiState.test()

val sortedLanguages = listOf(Language(englishName = "1", "", ""), Language(englishName = "2", "", ""))
val sortedLanguages = listOf(Language.default, Language(englishName = "1", "", ""), Language(englishName = "2", "", ""))
uiStates.last() shouldBe SettingsViewModel.UiState(showLoading = false, sortedLanguages)
}

@Test
fun `Should move default language to the first position when fetch languages succeed`() = runTest {
val languages = listOf(Language(englishName = "1", "", ""), Language.default)
configurationService.languages = languages

val settingsViewModel = createViewModel()
val uiStates = settingsViewModel.uiState.test()

uiStates.last().languages.first() shouldBe Language.default
uiStates.last().languages.count { it == Language.default } shouldBe 1
}

@Test
fun `Should create state with empty languages when fetch languages fails`() = runTest {
configurationService.fetchLanguagesException = IOException()
Expand All @@ -49,7 +61,7 @@ class SettingsViewModelTest {
val language = Language(englishName = "Turkish", iso6391 = "tr", name = "Türkçe")
settingsViewModel.onLanguageSelected(language)

uiStates.last() shouldBe SettingsViewModel.UiState(selectedLanguage = language)
uiStates.last().selectedLanguage shouldBe language
}

@Test
Expand Down

0 comments on commit f64059d

Please sign in to comment.