diff --git a/app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt b/app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt index ebae66b2..a360fb0b 100644 --- a/app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt +++ b/app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt @@ -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, @@ -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) { @@ -59,7 +62,7 @@ class CategoriesUseCase( } suspend fun deleteCategory(categoryId: String) { - presetsRepository.deleteCategory(categoryId) + legacyCategoriesAndPhrasesRepository.deleteCategory(categoryId) } } \ No newline at end of file diff --git a/app/src/main/java/com/willowtree/vocable/ICategoriesUseCase.kt b/app/src/main/java/com/willowtree/vocable/ICategoriesUseCase.kt index a1cc57b5..16f4f507 100644 --- a/app/src/main/java/com/willowtree/vocable/ICategoriesUseCase.kt +++ b/app/src/main/java/com/willowtree/vocable/ICategoriesUseCase.kt @@ -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) + suspend fun getCategoryById(categoryId: String): Category } \ No newline at end of file diff --git a/app/src/main/java/com/willowtree/vocable/presets/ILegacyCategoriesAndPhrasesRepository.kt b/app/src/main/java/com/willowtree/vocable/presets/ILegacyCategoriesAndPhrasesRepository.kt index 8d39f2da..97acd3e0 100644 --- a/app/src/main/java/com/willowtree/vocable/presets/ILegacyCategoriesAndPhrasesRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/presets/ILegacyCategoriesAndPhrasesRepository.kt @@ -24,7 +24,6 @@ interface ILegacyCategoriesAndPhrasesRepository { suspend fun updateCategorySortOrders(categorySortOrders: List) 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 suspend fun updatePhraseLastSpoken(phraseId: Long, lastSpokenDate: Long) diff --git a/app/src/main/java/com/willowtree/vocable/presets/LegacyCategoriesAndPhrasesRepository.kt b/app/src/main/java/com/willowtree/vocable/presets/LegacyCategoriesAndPhrasesRepository.kt index cf837f68..044194e7 100644 --- a/app/src/main/java/com/willowtree/vocable/presets/LegacyCategoriesAndPhrasesRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/presets/LegacyCategoriesAndPhrasesRepository.kt @@ -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 -> diff --git a/app/src/main/java/com/willowtree/vocable/presets/PresetCategoriesRepository.kt b/app/src/main/java/com/willowtree/vocable/presets/PresetCategoriesRepository.kt index 360f593c..79f631c4 100644 --- a/app/src/main/java/com/willowtree/vocable/presets/PresetCategoriesRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/presets/PresetCategoriesRepository.kt @@ -5,4 +5,5 @@ import com.willowtree.vocable.room.CategorySortOrder interface PresetCategoriesRepository { suspend fun getPresetCategories(): List suspend fun updateCategorySortOrders(categorySortOrders: List) + suspend fun getCategoryById(categoryId: String): Category.PresetCategory? } \ No newline at end of file diff --git a/app/src/main/java/com/willowtree/vocable/presets/RoomPresetCategoriesRepository.kt b/app/src/main/java/com/willowtree/vocable/presets/RoomPresetCategoriesRepository.kt index fa73d255..10356f8c 100644 --- a/app/src/main/java/com/willowtree/vocable/presets/RoomPresetCategoriesRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/presets/RoomPresetCategoriesRepository.kt @@ -38,4 +38,14 @@ class RoomPresetCategoriesRepository( override suspend fun updateCategorySortOrders(categorySortOrders: List) { 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() + ) + } } \ No newline at end of file diff --git a/app/src/main/java/com/willowtree/vocable/room/CategoryDao.kt b/app/src/main/java/com/willowtree/vocable/room/CategoryDao.kt index 96ddd547..f0c9aab8 100644 --- a/app/src/main/java/com/willowtree/vocable/room/CategoryDao.kt +++ b/app/src/main/java/com/willowtree/vocable/room/CategoryDao.kt @@ -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 @@ -19,7 +23,7 @@ interface CategoryDao { fun getAllCategoriesFlow(): Flow> @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) diff --git a/app/src/main/java/com/willowtree/vocable/room/PresetCategoryDao.kt b/app/src/main/java/com/willowtree/vocable/room/PresetCategoryDao.kt index 760ee7d0..cd2d6acb 100644 --- a/app/src/main/java/com/willowtree/vocable/room/PresetCategoryDao.kt +++ b/app/src/main/java/com/willowtree/vocable/room/PresetCategoryDao.kt @@ -15,4 +15,7 @@ interface PresetCategoryDao { @Update(entity = PresetCategoryDto::class) suspend fun updateCategorySortOrders(categorySortOrders: List) + + @Query("SELECT * FROM PresetCategory WHERE category_id = :categoryId") + suspend fun getPresetCategoryById(categoryId: String): PresetCategoryDto? } \ No newline at end of file diff --git a/app/src/main/java/com/willowtree/vocable/room/RoomStoredCategoriesRepository.kt b/app/src/main/java/com/willowtree/vocable/room/RoomStoredCategoriesRepository.kt index c72f01c1..98325737 100644 --- a/app/src/main/java/com/willowtree/vocable/room/RoomStoredCategoriesRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/room/RoomStoredCategoriesRepository.kt @@ -27,4 +27,7 @@ class RoomStoredCategoriesRepository(context: Context) : StoredCategoriesReposit override suspend fun updateCategorySortOrders(categorySortOrders: List) { database.categoryDao().updateCategorySortOrders(categorySortOrders) } + + override suspend fun getCategoryById(categoryId: String): CategoryDto? = + database.categoryDao().getCategoryById(categoryId) } \ No newline at end of file diff --git a/app/src/main/java/com/willowtree/vocable/room/StoredCategoriesRepository.kt b/app/src/main/java/com/willowtree/vocable/room/StoredCategoriesRepository.kt index 406f0310..ecb424a2 100644 --- a/app/src/main/java/com/willowtree/vocable/room/StoredCategoriesRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/room/StoredCategoriesRepository.kt @@ -7,4 +7,5 @@ interface StoredCategoriesRepository { fun getAllCategories(): Flow> suspend fun addCategory(category: Category.StoredCategory) suspend fun updateCategorySortOrders(categorySortOrders: List) + suspend fun getCategoryById(categoryId: String): CategoryDto? } \ No newline at end of file diff --git a/app/src/test/java/com/willowtree/vocable/CategoriesUseCaseTest.kt b/app/src/test/java/com/willowtree/vocable/CategoriesUseCaseTest.kt index 542972c5..1f2ff0b6 100644 --- a/app/src/test/java/com/willowtree/vocable/CategoriesUseCaseTest.kt +++ b/app/src/test/java/com/willowtree/vocable/CategoriesUseCaseTest.kt @@ -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 @@ -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, diff --git a/app/src/test/java/com/willowtree/vocable/FakeCategoriesUseCase.kt b/app/src/test/java/com/willowtree/vocable/FakeCategoriesUseCase.kt index e469641f..2fd55744 100644 --- a/app/src/test/java/com/willowtree/vocable/FakeCategoriesUseCase.kt +++ b/app/src/test/java/com/willowtree/vocable/FakeCategoriesUseCase.kt @@ -65,4 +65,8 @@ class FakeCategoriesUseCase : ICategoriesUseCase { } } } + + override suspend fun getCategoryById(categoryId: String): Category { + return _categories.value.first { it.categoryId == categoryId } + } } \ No newline at end of file diff --git a/app/src/test/java/com/willowtree/vocable/presets/FakeLegacyCategoriesAndPhrasesRepository.kt b/app/src/test/java/com/willowtree/vocable/presets/FakeLegacyCategoriesAndPhrasesRepository.kt index 0ec10b7c..7efeeda4 100644 --- a/app/src/test/java/com/willowtree/vocable/presets/FakeLegacyCategoriesAndPhrasesRepository.kt +++ b/app/src/test/java/com/willowtree/vocable/presets/FakeLegacyCategoriesAndPhrasesRepository.kt @@ -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") } diff --git a/app/src/test/java/com/willowtree/vocable/presets/FakePresetCategoriesRepository.kt b/app/src/test/java/com/willowtree/vocable/presets/FakePresetCategoriesRepository.kt index 6e484a8f..ef49b2ec 100644 --- a/app/src/test/java/com/willowtree/vocable/presets/FakePresetCategoriesRepository.kt +++ b/app/src/test/java/com/willowtree/vocable/presets/FakePresetCategoriesRepository.kt @@ -28,4 +28,8 @@ class FakePresetCategoriesRepository : PresetCategoriesRepository { } } } + + override suspend fun getCategoryById(categoryId: String): Category.PresetCategory { + return _presetCategories.first { it.categoryId == categoryId } + } } \ No newline at end of file diff --git a/app/src/test/java/com/willowtree/vocable/room/FakeStoredCategoriesRepository.kt b/app/src/test/java/com/willowtree/vocable/room/FakeStoredCategoriesRepository.kt index d41ef65e..2eb8bb4b 100644 --- a/app/src/test/java/com/willowtree/vocable/room/FakeStoredCategoriesRepository.kt +++ b/app/src/test/java/com/willowtree/vocable/room/FakeStoredCategoriesRepository.kt @@ -50,4 +50,8 @@ class FakeStoredCategoriesRepository : StoredCategoriesRepository { } } } + + override suspend fun getCategoryById(categoryId: String): CategoryDto? { + return _allCategories.value.firstOrNull { it.categoryId == categoryId } + } } \ No newline at end of file