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

363: Selects the next non-hidden category to display when the currently-displayed category is hidden #533

Merged
Show file tree
Hide file tree
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
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Category?> = selectedCategory.asLiveData()

Expand Down
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THESE 👏 ARE 👏 SO 👏 GOOD

Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just brought in Turbine to make this less obnoxious, could be fun to refactor this later and see how much nicer turbine makes things


assertEquals(
Category.StoredCategory(
categoryId = "2",
localizedName = LocalesWithText(mapOf("en_US" to "second category")),
hidden = false,
sortOrder = 1
),
category
)

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heck yea!


@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 {
Expand Down
Loading