Skip to content

Commit

Permalink
Merge pull request #460 from willowtreeapps/chore/452-abstract-over-s…
Browse files Browse the repository at this point in the history
…aved-preset-categories-5

Handle updating category name regardless of whether it's preset or stored
  • Loading branch information
PaulKlauser authored Jan 8, 2024
2 parents e533a87 + 164cbd6 commit 305f7f8
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 45 deletions.
32 changes: 28 additions & 4 deletions app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,40 @@ class CategoriesUseCase(

override suspend fun getCategoryById(categoryId: String): Category {
return storedCategoriesRepository.getCategoryById(categoryId)?.asCategory()
?: presetCategoriesRepository.getCategoryById(categoryId)
?: presetCategoriesRepository.getCategoryById(categoryId)?.let { if (it.hidden) null else it }
?: throw IllegalArgumentException("Category with id $categoryId not found")
}

override suspend fun updateCategoryName(
categoryId: String,
localizedName: LocalesWithText
) {
legacyCategoriesAndPhrasesRepository.updateCategoryName(categoryId, localizedName)
val storedCategory = storedCategoriesRepository.getCategoryById(categoryId)
if (storedCategory != null) {
storedCategoriesRepository.upsertCategory(
Category.StoredCategory(
storedCategory.categoryId,
localizedName,
storedCategory.hidden,
storedCategory.sortOrder
)
)
} else {
val presetCategory = presetCategoriesRepository.getCategoryById(categoryId)
if (presetCategory != null) {
presetCategoriesRepository.hidePresetCategory(categoryId)
storedCategoriesRepository.upsertCategory(
Category.StoredCategory(
presetCategory.categoryId,
localizedName,
presetCategory.hidden,
presetCategory.sortOrder
)
)
} else {
throw IllegalArgumentException("Category with id $categoryId not found")
}
}
}

suspend fun updateCategoryHidden(categoryId: String, hidden: Boolean) {
Expand All @@ -50,10 +75,9 @@ class CategoriesUseCase(
}

override suspend fun addCategory(categoryName: String, sortOrder: Int) {
storedCategoriesRepository.addCategory(
storedCategoriesRepository.upsertCategory(
Category.StoredCategory(
uuidProvider.randomUUIDString(),
null,
LocalesWithText(mapOf(Pair(localeProvider.getDefaultLocaleString(), categoryName))),
false,
sortOrder
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/willowtree/vocable/presets/Category.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ sealed class Category : Parcelable {
@Parcelize
data class StoredCategory(
override val categoryId: String,
override val resourceId: Int?,
override val localizedName: LocalesWithText?,
override var hidden: Boolean,
override var sortOrder: Int
) : Category() {
override val resourceId: Int? = null
override fun withSortOrder(sortOrder: Int): Category = copy(sortOrder = sortOrder)
override fun withHidden(hidden: Boolean): Category = copy(hidden = hidden)
}
Expand Down Expand Up @@ -63,7 +63,6 @@ fun CategoryDto.asCategory(): Category {
} else {
Category.StoredCategory(
categoryId,
resourceId,
localizedName,
hidden,
sortOrder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.coroutines.flow.Flow

@Deprecated("This is the old way of accessing categories and phrases. Prefer using" +
" ICategoriesUseCase instead.")
" ICategoriesUseCase and PhrasesUseCase instead.")
interface ILegacyCategoriesAndPhrasesRepository {
suspend fun getPhrasesForCategory(categoryId: String): List<PhraseDto>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ interface PresetCategoriesRepository {
suspend fun getPresetCategories(): List<Category.PresetCategory>
suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>)
suspend fun getCategoryById(categoryId: String): Category.PresetCategory?
suspend fun hidePresetCategory(categoryId: String)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.willowtree.vocable.presets

import android.content.Context
import com.willowtree.vocable.room.CategoryHidden
import com.willowtree.vocable.room.CategorySortOrder
import com.willowtree.vocable.room.PresetCategoryDto
import com.willowtree.vocable.room.VocableDatabase
Expand Down Expand Up @@ -45,7 +46,12 @@ class RoomPresetCategoriesRepository(
it.categoryId,
it.sortOrder,
it.hidden,
PresetCategories.values().first { presetCategory -> presetCategory.id == it.categoryId }.getNameId()
PresetCategories.values()
.first { presetCategory -> presetCategory.id == it.categoryId }.getNameId()
)
}

override suspend fun hidePresetCategory(categoryId: String) {
database.presetCategoryDao().updateCategoryHidden(CategoryHidden(categoryId, true))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.willowtree.vocable.room

import androidx.room.ColumnInfo

data class CategoryHidden(
@ColumnInfo(name = "category_id") val categoryId: String,
@ColumnInfo(name = "hidden") val hidden: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ interface PresetCategoryDao {
@Update(entity = PresetCategoryDto::class)
suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>)

@Update(entity = PresetCategoryDto::class)
suspend fun updateCategoryHidden(categoryHidden: CategoryHidden)

@Query("SELECT * FROM PresetCategory WHERE category_id = :categoryId")
suspend fun getPresetCategoryById(categoryId: String): PresetCategoryDto?
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class RoomStoredCategoriesRepository(context: Context) : StoredCategoriesReposit
return database.categoryDao().getAllCategoriesFlow()
}

override suspend fun addCategory(category: Category.StoredCategory) {
override suspend fun upsertCategory(category: Category.StoredCategory) {
database.categoryDao().insertCategory(
CategoryDto(
category.categoryId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlinx.coroutines.flow.Flow

interface StoredCategoriesRepository {
fun getAllCategories(): Flow<List<CategoryDto>>
suspend fun addCategory(category: Category.StoredCategory)
suspend fun upsertCategory(category: Category.StoredCategory)
suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>)
suspend fun getCategoryById(categoryId: String): CategoryDto?
}
57 changes: 52 additions & 5 deletions app/src/test/java/com/willowtree/vocable/CategoriesUseCaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ class CategoriesUseCaseTest {
listOf(
Category.StoredCategory(
categoryId = "customCategory1",
resourceId = 1,
localizedName = LocalesWithText(mapOf("en_US" to "Custom")),
hidden = false,
sortOrder = 0
),
Category.StoredCategory(
categoryId = "customCategory2",
resourceId = 2,
localizedName = LocalesWithText(mapOf("en_US" to "Other")),
hidden = false,
sortOrder = 0
Expand Down Expand Up @@ -107,7 +105,6 @@ class CategoriesUseCaseTest {
listOf(
Category.StoredCategory(
categoryId = "1",
resourceId = null,
localizedName = LocalesWithText(mapOf("en_US" to "My Category")),
hidden = false,
sortOrder = 0
Expand Down Expand Up @@ -170,7 +167,6 @@ class CategoriesUseCaseTest {
listOf(
Category.StoredCategory(
"storedCategory",
resourceId = null,
localizedName = null,
hidden = false,
sortOrder = 1
Expand Down Expand Up @@ -212,7 +208,6 @@ class CategoriesUseCaseTest {
assertEquals(
Category.StoredCategory(
"storedCategory",
resourceId = null,
localizedName = null,
hidden = false,
sortOrder = 0
Expand All @@ -230,6 +225,58 @@ class CategoriesUseCaseTest {
)
}

@Test
fun `update category name updates stored category`() = runTest {
fakeStoredCategoriesRepository._allCategories.update {
listOf(
createCategoryDto(
categoryId = "storedCategory1",
localizedName = LocalesWithText(mapOf("en_US" to "storedCategory1"))
)
)
}

val useCase = createUseCase()

useCase.updateCategoryName("storedCategory1", LocalesWithText(mapOf("en_US" to "newStoredCategory1")))

assertEquals(
Category.StoredCategory(
"storedCategory1",
localizedName = LocalesWithText(mapOf("en_US" to "newStoredCategory1")),
hidden = false,
sortOrder = 0
),
useCase.getCategoryById("storedCategory1")
)
}

@Test
fun `update category name hides preset category and creates stored category with new name`() = runTest {
fakePresetCategoriesRepository._presetCategories = listOf(
Category.PresetCategory(
categoryId = "presetCategory1",
sortOrder = 1,
hidden = false,
resourceId = 0
)
)

val useCase = createUseCase()

useCase.updateCategoryName("presetCategory1", LocalesWithText(mapOf("en_US" to "newPresetCategory1")))

assertEquals(
Category.StoredCategory(
"presetCategory1",
sortOrder = 1,
hidden = false,
localizedName = LocalesWithText(mapOf("en_US" to "newPresetCategory1")),
),
useCase.getCategoryById("presetCategory1")
)
}

private fun createCategoryDto(
categoryId: String,
creationDate: Long = 0L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class FakeCategoriesUseCase : ICategoriesUseCase {
listOf(
Category.StoredCategory(
"categoryId",
0,
null,
false,
0
Expand Down Expand Up @@ -44,7 +43,6 @@ class FakeCategoriesUseCase : ICategoriesUseCase {
_categories.update {
it + Category.StoredCategory(
"",
null,
LocalesWithText(mapOf("en_US" to categoryName)),
false,
sortOrder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import com.willowtree.vocable.utils.locale.LocalesWithText

fun createStoredCategory(
categoryId: String,
resourceId: Int? = null,
localizedName: LocalesWithText? = LocalesWithText(mapOf("en_US" to "category")),
hidden: Boolean = false,
sortOrder: Int = 0
): Category.StoredCategory = Category.StoredCategory(
categoryId = categoryId,
resourceId = resourceId,
localizedName = localizedName,
hidden = hidden,
sortOrder = sortOrder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ class FakePresetCategoriesRepository : PresetCategoriesRepository {
}
}

override suspend fun getCategoryById(categoryId: String): Category.PresetCategory {
return _presetCategories.first { it.categoryId == categoryId }
override suspend fun getCategoryById(categoryId: String): Category.PresetCategory? {
return _presetCategories.firstOrNull { it.categoryId == categoryId }
}

override suspend fun hidePresetCategory(categoryId: String) {
_presetCategories = _presetCategories.map { categoryDto ->
if (categoryDto.categoryId == categoryId) {
categoryDto.copy(hidden = true)
} else {
categoryDto
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class PresetsViewModelTest {
listOf(
Category.StoredCategory(
categoryId = "1",
resourceId = null,
localizedName = LocalesWithText(mapOf("en_US" to "category")),
hidden = false,
sortOrder = 0
Expand All @@ -55,7 +54,6 @@ class PresetsViewModelTest {
listOf(
Category.StoredCategory(
categoryId = "1",
resourceId = null,
localizedName = LocalesWithText(mapOf("en_US" to "category")),
hidden = false,
sortOrder = 0
Expand All @@ -71,14 +69,12 @@ class PresetsViewModelTest {
listOf(
Category.StoredCategory(
categoryId = "1",
resourceId = null,
localizedName = LocalesWithText(mapOf("en_US" to "category")),
hidden = false,
sortOrder = 0
),
Category.StoredCategory(
categoryId = "2",
resourceId = null,
localizedName = LocalesWithText(mapOf("en_US" to "second category")),
hidden = false,
sortOrder = 0
Expand All @@ -100,7 +96,6 @@ class PresetsViewModelTest {
assertEquals(
Category.StoredCategory(
categoryId = "1",
resourceId = null,
localizedName = LocalesWithText(mapOf("en_US" to "category")),
hidden = false,
sortOrder = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,33 @@ class FakeStoredCategoriesRepository : StoredCategoriesRepository {
return _allCategories
}

override suspend fun addCategory(category: Category.StoredCategory) {
_allCategories.update {
it + CategoryDto(
category.categoryId,
0L,
category.resourceId,
category.localizedName,
category.hidden,
category.sortOrder
)
override suspend fun upsertCategory(category: Category.StoredCategory) {
_allCategories.update { allCategories ->
if (allCategories.none { it.categoryId == category.categoryId }) {
allCategories + CategoryDto(
category.categoryId,
0L,
category.resourceId,
category.localizedName,
category.hidden,
category.sortOrder
)
} else {
allCategories.map { categoryDto ->
if (categoryDto.categoryId == category.categoryId) {
CategoryDto(
category.categoryId,
0L,
category.resourceId,
category.localizedName,
category.hidden,
category.sortOrder
)
} else {
categoryDto
}
}
}
}
}

Expand Down
Loading

0 comments on commit 305f7f8

Please sign in to comment.