Skip to content

Commit

Permalink
Merge pull request #563 from willowtreeapps/chore/508-abstract-sort-h…
Browse files Browse the repository at this point in the history
…andling-6

Chore/508 abstract sort handling 6
  • Loading branch information
PaulKlauser authored Jun 6, 2024
2 parents 5d9d99d + 8d5b116 commit 1c8fee5
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 150 deletions.
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

0 comments on commit 1c8fee5

Please sign in to comment.