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

Chore/508 abstract sort handling 6 #563

Merged
merged 3 commits into from
Jun 6, 2024
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
@@ -0,0 +1,140 @@
package com.willowtree.vocable.settings

import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import app.cash.turbine.test
import com.willowtree.vocable.CategoriesUseCase
import com.willowtree.vocable.FakeUUIDProvider
import com.willowtree.vocable.MainDispatcherRule
import com.willowtree.vocable.PhrasesUseCase
import com.willowtree.vocable.basetest.utils.FakeLocaleProvider
import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.presets.PresetCategories
import com.willowtree.vocable.presets.RoomPresetCategoriesRepository
import com.willowtree.vocable.room.RoomPresetPhrasesRepository
import com.willowtree.vocable.room.RoomStoredCategoriesRepository
import com.willowtree.vocable.room.RoomStoredPhrasesRepository
import com.willowtree.vocable.room.VocableDatabase
import com.willowtree.vocable.utility.FakeDateProvider
import com.willowtree.vocable.utility.StubLegacyCategoriesAndPhrasesRepository
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test

class EditCategoriesViewModelTest {

@get:Rule
val mainDispatcherRule = MainDispatcherRule()

private val database = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
VocableDatabase::class.java
).build()

private val presetCategoriesRepository = RoomPresetCategoriesRepository(
database
)

private val storedCategoriesRepository = RoomStoredCategoriesRepository(
database
)

private val presetPhrasesRepository = RoomPresetPhrasesRepository(
database.presetPhrasesDao(),
FakeDateProvider()
)

private val storedPhrasesRepository = RoomStoredPhrasesRepository(
database,
FakeDateProvider()
)

private val categoriesUseCase = CategoriesUseCase(
FakeUUIDProvider(),
FakeLocaleProvider(),
storedCategoriesRepository,
presetCategoriesRepository,
PhrasesUseCase(
StubLegacyCategoriesAndPhrasesRepository(),
storedPhrasesRepository,
presetPhrasesRepository,
FakeDateProvider(),
FakeUUIDProvider()
)
)

private fun createViewModel(): EditCategoriesViewModel {
return EditCategoriesViewModel(
categoriesUseCase
)
}

@Test
fun categories_are_populated() = runTest {
val vm = createViewModel()
vm.refreshCategories()

vm.categoryList.test {
assertEquals(
listOf(
Category.PresetCategory(PresetCategories.GENERAL.id, 0, false),
Category.PresetCategory(PresetCategories.BASIC_NEEDS.id, 1, false),
Category.PresetCategory(PresetCategories.PERSONAL_CARE.id, 2, false),
Category.PresetCategory(PresetCategories.CONVERSATION.id, 3, false),
Category.PresetCategory(PresetCategories.ENVIRONMENT.id, 4, false),
Category.PresetCategory(PresetCategories.USER_KEYPAD.id, 5, false),
Category.PresetCategory(PresetCategories.RECENTS.id, 6, false),
),
awaitItem()
)
}
}

@Test
fun move_category_up() = runTest {
val vm = createViewModel()
vm.refreshCategories()
vm.moveCategoryUp(PresetCategories.RECENTS.id)

vm.categoryList.test {
awaitItem() // Skip unmoved emission
assertEquals(
listOf(
Category.PresetCategory(PresetCategories.GENERAL.id, 0, false),
Category.PresetCategory(PresetCategories.BASIC_NEEDS.id, 1, false),
Category.PresetCategory(PresetCategories.PERSONAL_CARE.id, 2, false),
Category.PresetCategory(PresetCategories.CONVERSATION.id, 3, false),
Category.PresetCategory(PresetCategories.ENVIRONMENT.id, 4, false),
Category.PresetCategory(PresetCategories.RECENTS.id, 5, false),
Category.PresetCategory(PresetCategories.USER_KEYPAD.id, 6, false),
),
awaitItem()
)
}
}

@Test
fun move_category_down() = runTest {
val vm = createViewModel()
vm.refreshCategories()
vm.moveCategoryDown(PresetCategories.GENERAL.id)

vm.categoryList.test {
awaitItem() // Skip unmoved emission
assertEquals(
listOf(
Category.PresetCategory(PresetCategories.BASIC_NEEDS.id, 0, false),
Category.PresetCategory(PresetCategories.GENERAL.id, 1, false),
Category.PresetCategory(PresetCategories.PERSONAL_CARE.id, 2, false),
Category.PresetCategory(PresetCategories.CONVERSATION.id, 3, false),
Category.PresetCategory(PresetCategories.ENVIRONMENT.id, 4, false),
Category.PresetCategory(PresetCategories.USER_KEYPAD.id, 5, false),
Category.PresetCategory(PresetCategories.RECENTS.id, 6, false),
),
awaitItem()
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.willowtree.vocable.ICategoriesUseCase
import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.room.CategorySortOrder
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -48,45 +47,16 @@ class EditCategoriesViewModel(
}

fun moveCategoryUp(categoryId: String) {
viewModelScope.launch {
val categories = categoriesUseCase.categories().first()
val catIndex = categories.indexOfFirst { it.categoryId == categoryId }
if (catIndex in 1 until categories.size) {
val category = categories[catIndex]
val previousCat = categories[catIndex - 1]

swapSortOrders(categories, previousCat, category)
}
viewModelScope.launch {
categoriesUseCase.moveCategoryUp(categoryId)
}
}

fun moveCategoryDown(categoryId: String) {
viewModelScope.launch {
val categories = categoriesUseCase.categories().first()
val catIndex = categories.indexOfFirst { it.categoryId == categoryId }
if (catIndex in 0 until categories.size - 1) {
val category = categories[catIndex]
val nextCat = categories[catIndex + 1]

swapSortOrders(categories, category, nextCat)
}
categoriesUseCase.moveCategoryDown(categoryId)
}
}

private suspend fun swapSortOrders(
categories: List<Category>,
leftCategory: Category,
rightCategory: Category
) {
categoriesUseCase.updateCategorySortOrders(
categories.map {
val sortOrder = when (it.categoryId) {
rightCategory.categoryId -> leftCategory.sortOrder
leftCategory.categoryId -> rightCategory.sortOrder
else -> it.sortOrder
}
CategorySortOrder(it.categoryId, sortOrder)
}
)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.willowtree.vocable.presets
package com.willowtree.vocable.basetest.utils.presets

import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.utils.locale.LocalesWithText

fun createStoredCategory(
Expand Down
Loading