From f64059d0160627ef5d5701366a15d31a0fe13574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20Ka=C3=A7maz?= Date: Fri, 20 Sep 2024 01:37:00 +0300 Subject: [PATCH] Show system default language as first language --- .../jetflix/ui/settings/SettingsViewModel.kt | 11 +++++++---- .../jetflix/ui/settings/SettingsViewModelTest.kt | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/yasinkacmaz/jetflix/ui/settings/SettingsViewModel.kt b/app/src/main/kotlin/com/yasinkacmaz/jetflix/ui/settings/SettingsViewModel.kt index eecf4e6..60825c6 100644 --- a/app/src/main/kotlin/com/yasinkacmaz/jetflix/ui/settings/SettingsViewModel.kt +++ b/app/src/main/kotlin/com/yasinkacmaz/jetflix/ui/settings/SettingsViewModel.kt @@ -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 { + 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) } } diff --git a/app/src/test/kotlin/com/yasinkacmaz/jetflix/ui/settings/SettingsViewModelTest.kt b/app/src/test/kotlin/com/yasinkacmaz/jetflix/ui/settings/SettingsViewModelTest.kt index d9b4de3..9d4d9b7 100644 --- a/app/src/test/kotlin/com/yasinkacmaz/jetflix/ui/settings/SettingsViewModelTest.kt +++ b/app/src/test/kotlin/com/yasinkacmaz/jetflix/ui/settings/SettingsViewModelTest.kt @@ -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() @@ -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