diff --git a/app/src/androidTest/java/com/willowtree/vocable/screens/MainScreen.kt b/app/src/androidTest/java/com/willowtree/vocable/screens/MainScreen.kt index 9a376fe8..fce2e51f 100644 --- a/app/src/androidTest/java/com/willowtree/vocable/screens/MainScreen.kt +++ b/app/src/androidTest/java/com/willowtree/vocable/screens/MainScreen.kt @@ -47,7 +47,6 @@ class MainScreen { onView(withText(phrase)).check(matches(isDisplayed())) } } - // Taps on the selected phrase fun tapPhrase(phraseText: String) { onView(withText(phraseText)).tap() diff --git a/app/src/androidTest/java/com/willowtree/vocable/tests/MainScreenTest.kt b/app/src/androidTest/java/com/willowtree/vocable/tests/MainScreenTest.kt index e79c3bc4..29eac0e5 100644 --- a/app/src/androidTest/java/com/willowtree/vocable/tests/MainScreenTest.kt +++ b/app/src/androidTest/java/com/willowtree/vocable/tests/MainScreenTest.kt @@ -1,11 +1,8 @@ package com.willowtree.vocable.tests import androidx.test.ext.junit.runners.AndroidJUnit4 -import androidx.test.rule.GrantPermissionRule import com.willowtree.vocable.screens.MainScreen import com.willowtree.vocable.utility.assertTextMatches -import org.junit.Ignore -import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith diff --git a/app/src/main/java/com/willowtree/vocable/presets/PresetsViewModel.kt b/app/src/main/java/com/willowtree/vocable/presets/PresetsViewModel.kt index c3faf183..edbccd83 100644 --- a/app/src/main/java/com/willowtree/vocable/presets/PresetsViewModel.kt +++ b/app/src/main/java/com/willowtree/vocable/presets/PresetsViewModel.kt @@ -34,7 +34,20 @@ class PresetsViewModel( categoriesUseCase.categories(), liveSelectedCategoryId ) { categories, selectedId -> - categories.find { it.categoryId == selectedId } + val currentCategory = categories.find { it.categoryId == selectedId } + if (currentCategory?.hidden == true) { + val newSortOrder = (currentCategory.sortOrder + 1) + val newCategory = categories.find { it.sortOrder == newSortOrder} + if (newCategory != null) { + liveSelectedCategoryId.update { newCategory.categoryId } + categories.find { it.sortOrder == newSortOrder} + } else { + liveSelectedCategoryId.update { categories.first().categoryId } + categories.first() + } + } else { + currentCategory + } }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000L), null) val selectedCategoryLiveData: LiveData = selectedCategory.asLiveData() diff --git a/app/src/test/java/com/willowtree/vocable/presets/PresetsViewModelTest.kt b/app/src/test/java/com/willowtree/vocable/presets/PresetsViewModelTest.kt index 46cf498d..c44a7832 100644 --- a/app/src/test/java/com/willowtree/vocable/presets/PresetsViewModelTest.kt +++ b/app/src/test/java/com/willowtree/vocable/presets/PresetsViewModelTest.kt @@ -114,6 +114,148 @@ class PresetsViewModelTest { ) } + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `selected category is hidden and next immediate category is shown`() = runTest(UnconfinedTestDispatcher()) { + fakeCategoriesUseCase._categories.update { + listOf( + Category.StoredCategory( + categoryId = "1", + localizedName = LocalesWithText(mapOf("en_US" to "category")), + hidden = true, + sortOrder = 0 + ), + Category.StoredCategory( + categoryId = "2", + localizedName = LocalesWithText(mapOf("en_US" to "second category")), + hidden = false, + sortOrder = 1 + ) + ) + } + + val vm = createViewModel() + + vm.onCategorySelected("1") + + var category: Category? = null + val job = launch { + vm.selectedCategory.collect { + category = it + } + } + job.cancel() + + assertEquals( + Category.StoredCategory( + categoryId = "2", + localizedName = LocalesWithText(mapOf("en_US" to "second category")), + hidden = false, + sortOrder = 1 + ), + category + ) + + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `selected category (last in list) is hidden and first category is shown`() = runTest(UnconfinedTestDispatcher()) { + fakeCategoriesUseCase._categories.update { + listOf( + Category.StoredCategory( + categoryId = "1", + localizedName = LocalesWithText(mapOf("en_US" to "category")), + hidden = false, + sortOrder = 0 + ), + Category.StoredCategory( + categoryId = "2", + localizedName = LocalesWithText(mapOf("en_US" to "second category")), + hidden = false, + sortOrder = 1 + ), + Category.StoredCategory( + categoryId = "3", + localizedName = LocalesWithText(mapOf("en_US" to "third category")), + hidden = true, + sortOrder = 2 + ) + ) + } + + val vm = createViewModel() + + vm.onCategorySelected("3") + + var category: Category? = null + val job = launch { + vm.selectedCategory.collect { + category = it + } + } + job.cancel() + + assertEquals( + Category.StoredCategory( + categoryId = "1", + localizedName = LocalesWithText(mapOf("en_US" to "category")), + hidden = false, + sortOrder = 0 + ), + category + ) + } + + @OptIn(ExperimentalCoroutinesApi::class) + @Test + fun `selected category is hidden and next non-hidden category is shown`() = runTest(UnconfinedTestDispatcher()) { + fakeCategoriesUseCase._categories.update { + listOf( + Category.StoredCategory( + categoryId = "1", + localizedName = LocalesWithText(mapOf("en_US" to "category")), + hidden = true, + sortOrder = 0 + ), + Category.StoredCategory( + categoryId = "2", + localizedName = LocalesWithText(mapOf("en_US" to "second category")), + hidden = false, + sortOrder = 1 + ), + Category.StoredCategory( + categoryId = "3", + localizedName = LocalesWithText(mapOf("en_US" to "third category")), + hidden = true, + sortOrder = 2 + ) + ) + } + + val vm = createViewModel() + + vm.onCategorySelected("3") + + var category: Category? = null + val job = launch { + vm.selectedCategory.collect { + category = it + } + } + job.cancel() + + assertEquals( + Category.StoredCategory( + categoryId = "2", + localizedName = LocalesWithText(mapOf("en_US" to "second category")), + hidden = false, + sortOrder = 1 + ), + category + ) + } + @Test fun `current phrases updated when category ID changed`() { fakePhrasesUseCase._allCategories.update {