Skip to content

Commit

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

Return stored and preset categories from getCategoryById()
  • Loading branch information
PaulKlauser authored Dec 28, 2023
2 parents 82edc8a + 8ea68de commit e533a87
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 18 deletions.
15 changes: 9 additions & 6 deletions app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

class CategoriesUseCase(
private val presetsRepository: ILegacyCategoriesAndPhrasesRepository,
private val legacyCategoriesAndPhrasesRepository: ILegacyCategoriesAndPhrasesRepository,
private val uuidProvider: UUIDProvider,
private val localeProvider: LocaleProvider,
private val storedCategoriesRepository: StoredCategoriesRepository,
Expand All @@ -27,18 +27,21 @@ class CategoriesUseCase(
}
}

suspend fun getCategoryById(categoryId: String): Category =
presetsRepository.getCategoryById(categoryId).asCategory()
override suspend fun getCategoryById(categoryId: String): Category {
return storedCategoriesRepository.getCategoryById(categoryId)?.asCategory()
?: presetCategoriesRepository.getCategoryById(categoryId)
?: throw IllegalArgumentException("Category with id $categoryId not found")
}

override suspend fun updateCategoryName(
categoryId: String,
localizedName: LocalesWithText
) {
presetsRepository.updateCategoryName(categoryId, localizedName)
legacyCategoriesAndPhrasesRepository.updateCategoryName(categoryId, localizedName)
}

suspend fun updateCategoryHidden(categoryId: String, hidden: Boolean) {
presetsRepository.updateCategoryHidden(categoryId, hidden)
legacyCategoriesAndPhrasesRepository.updateCategoryHidden(categoryId, hidden)
}

override suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>) {
Expand All @@ -59,7 +62,7 @@ class CategoriesUseCase(
}

suspend fun deleteCategory(categoryId: String) {
presetsRepository.deleteCategory(categoryId)
legacyCategoriesAndPhrasesRepository.deleteCategory(categoryId)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ interface ICategoriesUseCase {
suspend fun updateCategoryName(categoryId: String, localizedName: LocalesWithText)
suspend fun addCategory(categoryName: String, sortOrder: Int)
suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>)
suspend fun getCategoryById(categoryId: String): Category
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ interface ILegacyCategoriesAndPhrasesRepository {
suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>)
suspend fun updateCategoryName(categoryId: String, localizedName: LocalesWithText)
suspend fun updateCategoryHidden(categoryId: String, hidden: Boolean)
suspend fun getCategoryById(categoryId: String): CategoryDto
suspend fun deleteCategory(categoryId: String)
suspend fun getRecentPhrases(): List<PhraseDto>
suspend fun updatePhraseLastSpoken(phraseId: Long, lastSpokenDate: Long)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ class LegacyCategoriesAndPhrasesRepository(val context: Context) : KoinComponent
TODO("Not yet implemented")
}

override suspend fun getCategoryById(categoryId: String): CategoryDto {
return database.categoryDao().getCategoryById(categoryId)
}

//Initial DB populate
suspend fun populateDatabase() {
PresetCategories.values().forEach { presetCategory ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import com.willowtree.vocable.room.CategorySortOrder
interface PresetCategoriesRepository {
suspend fun getPresetCategories(): List<Category.PresetCategory>
suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>)
suspend fun getCategoryById(categoryId: String): Category.PresetCategory?
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ class RoomPresetCategoriesRepository(
override suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>) {
database.presetCategoryDao().updateCategorySortOrders(categorySortOrders)
}

override suspend fun getCategoryById(categoryId: String): Category.PresetCategory? =
database.presetCategoryDao().getPresetCategoryById(categoryId)?.let {
Category.PresetCategory(
it.categoryId,
it.sortOrder,
it.hidden,
PresetCategories.values().first { presetCategory -> presetCategory.id == it.categoryId }.getNameId()
)
}
}
8 changes: 6 additions & 2 deletions app/src/main/java/com/willowtree/vocable/room/CategoryDao.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.willowtree.vocable.room

import androidx.room.*
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import kotlinx.coroutines.flow.Flow

@Dao
Expand All @@ -19,7 +23,7 @@ interface CategoryDao {
fun getAllCategoriesFlow(): Flow<List<CategoryDto>>

@Query("SELECT * FROM Category WHERE category_id = :categoryId")
suspend fun getCategoryById(categoryId: String): CategoryDto
suspend fun getCategoryById(categoryId: String): CategoryDto?

@Query("DELETE FROM Category WHERE category_id = :categoryId")
suspend fun deleteCategory(categoryId: String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ interface PresetCategoryDao {

@Update(entity = PresetCategoryDto::class)
suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>)

@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 @@ -27,4 +27,7 @@ class RoomStoredCategoriesRepository(context: Context) : StoredCategoriesReposit
override suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>) {
database.categoryDao().updateCategorySortOrders(categorySortOrders)
}

override suspend fun getCategoryById(categoryId: String): CategoryDto? =
database.categoryDao().getCategoryById(categoryId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ interface StoredCategoriesRepository {
fun getAllCategories(): Flow<List<CategoryDto>>
suspend fun addCategory(category: Category.StoredCategory)
suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>)
suspend fun getCategoryById(categoryId: String): CategoryDto?
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.willowtree.vocable

import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.presets.FakePresetCategoriesRepository
import com.willowtree.vocable.presets.FakeLegacyCategoriesAndPhrasesRepository
import com.willowtree.vocable.presets.FakePresetCategoriesRepository
import com.willowtree.vocable.room.CategoryDto
import com.willowtree.vocable.room.CategorySortOrder
import com.willowtree.vocable.room.FakeStoredCategoriesRepository
Expand Down Expand Up @@ -186,6 +186,50 @@ class CategoriesUseCaseTest {
)
}

@Test
fun `get category by id returns presets and stored`() = runTest {
fakeStoredCategoriesRepository._allCategories.update {
listOf(
createCategoryDto(
categoryId = "storedCategory",
localizedName = null,
sortOrder = 0
)
)
}

fakePresetCategoriesRepository._presetCategories = listOf(
Category.PresetCategory(
categoryId = "presetCategory",
sortOrder = 1,
hidden = false,
resourceId = 0
)
)

val useCase = createUseCase()

assertEquals(
Category.StoredCategory(
"storedCategory",
resourceId = null,
localizedName = null,
hidden = false,
sortOrder = 0
),
useCase.getCategoryById("storedCategory")
)
assertEquals(
Category.PresetCategory(
"presetCategory",
sortOrder = 1,
hidden = false,
resourceId = 0
),
useCase.getCategoryById("presetCategory")
)
}

private fun createCategoryDto(
categoryId: String,
creationDate: Long = 0L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ class FakeCategoriesUseCase : ICategoriesUseCase {
}
}
}

override suspend fun getCategoryById(categoryId: String): Category {
return _categories.value.first { it.categoryId == categoryId }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ class FakeLegacyCategoriesAndPhrasesRepository : ILegacyCategoriesAndPhrasesRepo
TODO("Not yet implemented")
}

override suspend fun getCategoryById(categoryId: String): CategoryDto {
TODO("Not yet implemented")
}

override suspend fun deleteCategory(categoryId: String) {
TODO("Not yet implemented")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ class FakePresetCategoriesRepository : PresetCategoriesRepository {
}
}
}

override suspend fun getCategoryById(categoryId: String): Category.PresetCategory {
return _presetCategories.first { it.categoryId == categoryId }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ class FakeStoredCategoriesRepository : StoredCategoriesRepository {
}
}
}

override suspend fun getCategoryById(categoryId: String): CategoryDto? {
return _allCategories.value.firstOrNull { it.categoryId == categoryId }
}
}

0 comments on commit e533a87

Please sign in to comment.