From cd475e7d4695dbddde4e5c19242fd70f78df20ac Mon Sep 17 00:00:00 2001 From: Paul Klauser Date: Mon, 27 Nov 2023 17:12:07 -0500 Subject: [PATCH 1/2] Implement adding a Category in CategoriesUseCase --- .../com/willowtree/vocable/AppKoinModule.kt | 2 +- .../willowtree/vocable/CategoriesUseCase.kt | 10 ++-- .../room/RoomStoredCategoriesRepository.kt | 14 +++++ .../room/StoredCategoriesRepository.kt | 2 + .../vocable/CategoriesUseCaseTest.kt | 53 ++++++++++++++++++- .../room/FakeStoredCategoriesRepository.kt | 34 +++++++++--- 6 files changed, 97 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/willowtree/vocable/AppKoinModule.kt b/app/src/main/java/com/willowtree/vocable/AppKoinModule.kt index 2475214b..5147ff1d 100644 --- a/app/src/main/java/com/willowtree/vocable/AppKoinModule.kt +++ b/app/src/main/java/com/willowtree/vocable/AppKoinModule.kt @@ -35,7 +35,7 @@ object AppKoinModule { single { PresetsRepository(get()) } bind IPresetsRepository::class single { Moshi.Builder().add(KotlinJsonAdapterFactory()).build() } single { LocalizedResourceUtility() } bind ILocalizedResourceUtility::class - single { CategoriesUseCase(get(), get(), get(), get(), get(), get()) } bind ICategoriesUseCase::class + single { CategoriesUseCase(get(), get(), get(), get(), get()) } bind ICategoriesUseCase::class single { PhrasesUseCase(get(), get()) } single { RandomUUIDProvider() } bind UUIDProvider::class single { JavaDateProvider() } bind DateProvider::class diff --git a/app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt b/app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt index 5fd20b73..5e409b26 100644 --- a/app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt +++ b/app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt @@ -4,20 +4,17 @@ import com.willowtree.vocable.presets.Category import com.willowtree.vocable.presets.IPresetsRepository import com.willowtree.vocable.presets.PresetCategoriesRepository import com.willowtree.vocable.presets.asCategory -import com.willowtree.vocable.room.CategoryDto import com.willowtree.vocable.room.CategorySortOrder import com.willowtree.vocable.room.StoredCategoriesRepository -import com.willowtree.vocable.utils.DateProvider import com.willowtree.vocable.utils.UUIDProvider -import com.willowtree.vocable.utils.locale.LocalesWithText import com.willowtree.vocable.utils.locale.LocaleProvider +import com.willowtree.vocable.utils.locale.LocalesWithText import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map class CategoriesUseCase( private val presetsRepository: IPresetsRepository, private val uuidProvider: UUIDProvider, - private val dateProvider: DateProvider, private val localeProvider: LocaleProvider, private val storedCategoriesRepository: StoredCategoriesRepository, private val presetCategoriesRepository: PresetCategoriesRepository @@ -49,10 +46,9 @@ class CategoriesUseCase( } override suspend fun addCategory(categoryName: String, sortOrder: Int) { - presetsRepository.addCategory( - CategoryDto( + storedCategoriesRepository.addCategory( + Category.StoredCategory( uuidProvider.randomUUIDString(), - dateProvider.currentTimeMillis(), null, LocalesWithText(mapOf(Pair(localeProvider.getDefaultLocaleString(), categoryName))), false, 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 89d526e9..d04d0f83 100644 --- a/app/src/main/java/com/willowtree/vocable/room/RoomStoredCategoriesRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/room/RoomStoredCategoriesRepository.kt @@ -1,6 +1,7 @@ package com.willowtree.vocable.room import android.content.Context +import com.willowtree.vocable.presets.Category import kotlinx.coroutines.flow.Flow class RoomStoredCategoriesRepository(context: Context) : StoredCategoriesRepository { @@ -9,4 +10,17 @@ class RoomStoredCategoriesRepository(context: Context) : StoredCategoriesReposit override fun getAllCategories(): Flow> { return database.categoryDao().getAllCategoriesFlow() } + + override suspend fun addCategory(category: Category.StoredCategory) { + database.categoryDao().insertCategory( + CategoryDto( + category.categoryId, + 0L, + category.resourceId, + category.localizedName, + category.hidden, + category.sortOrder + ) + ) + } } \ 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 7752d0dc..dedd8685 100644 --- a/app/src/main/java/com/willowtree/vocable/room/StoredCategoriesRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/room/StoredCategoriesRepository.kt @@ -1,7 +1,9 @@ package com.willowtree.vocable.room +import com.willowtree.vocable.presets.Category import kotlinx.coroutines.flow.Flow interface StoredCategoriesRepository { fun getAllCategories(): Flow> + suspend fun addCategory(category: Category.StoredCategory) } \ 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 8ac1736b..0402a3f2 100644 --- a/app/src/test/java/com/willowtree/vocable/CategoriesUseCaseTest.kt +++ b/app/src/test/java/com/willowtree/vocable/CategoriesUseCaseTest.kt @@ -3,10 +3,10 @@ package com.willowtree.vocable import com.willowtree.vocable.presets.Category import com.willowtree.vocable.presets.FakePresetCategoriesRepository import com.willowtree.vocable.presets.FakePresetsRepository +import com.willowtree.vocable.room.CategoryDto import com.willowtree.vocable.room.FakeStoredCategoriesRepository import com.willowtree.vocable.room.createCategoryDto import com.willowtree.vocable.utils.ConstantUUIDProvider -import com.willowtree.vocable.utils.FakeDateProvider import com.willowtree.vocable.utils.FakeLocaleProvider import com.willowtree.vocable.utils.locale.LocalesWithText import kotlinx.coroutines.flow.first @@ -25,7 +25,6 @@ class CategoriesUseCaseTest { return CategoriesUseCase( fakePresetsRepository, ConstantUUIDProvider(), - FakeDateProvider(), FakeLocaleProvider(), fakeStoredCategoriesRepository, fakePresetCategoriesRepository @@ -87,4 +86,54 @@ class CategoriesUseCaseTest { ) } + @Test + fun `category added to stored repository`() = runTest { + fakeStoredCategoriesRepository._allCategories.update { emptyList() } + + fakePresetCategoriesRepository._presetCategories = listOf( + Category.PresetCategory( + categoryId = "presetCategory", + sortOrder = 3, + hidden = false, + resourceId = 0 + ) + ) + + val useCase = createUseCase() + + useCase.addCategory("My Category", 0) + + assertEquals( + listOf( + Category.StoredCategory( + categoryId = "1", + resourceId = null, + localizedName = LocalesWithText(mapOf("en_US" to "My Category")), + hidden = false, + sortOrder = 0 + ), + Category.PresetCategory( + categoryId = "presetCategory", + sortOrder = 3, + hidden = false, + resourceId = 0 + ), + ), + useCase.categories().first() + ) + assertEquals( + listOf( + CategoryDto( + categoryId = "1", + creationDate = 0L, + resourceId = null, + localizedName = LocalesWithText(mapOf("en_US" to "My Category")), + hidden = false, + sortOrder = 0 + ) + ), + fakeStoredCategoriesRepository._allCategories.first() + ) + } + } \ 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 316e3ef3..ad165a0c 100644 --- a/app/src/test/java/com/willowtree/vocable/room/FakeStoredCategoriesRepository.kt +++ b/app/src/test/java/com/willowtree/vocable/room/FakeStoredCategoriesRepository.kt @@ -1,21 +1,39 @@ package com.willowtree.vocable.room +import com.willowtree.vocable.presets.Category import com.willowtree.vocable.utils.locale.LocalesWithText import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.update class FakeStoredCategoriesRepository : StoredCategoriesRepository { - val _allCategories = MutableStateFlow(listOf(CategoryDto( - "categoryId", - 0L, - 0, - LocalesWithText( emptyMap()), - false, - 0 + val _allCategories = MutableStateFlow( + listOf( + CategoryDto( + "categoryId", + 0L, + 0, + LocalesWithText(emptyMap()), + false, + 0 + ) + ) ) - )) override fun getAllCategories(): Flow> { return _allCategories } + + override suspend fun addCategory(category: Category.StoredCategory) { + _allCategories.update { + it + CategoryDto( + category.categoryId, + 0L, + category.resourceId, + category.localizedName, + category.hidden, + category.sortOrder + ) + } + } } \ No newline at end of file From 70d9348ee5cfa16a461028b02cdcc6deee95d681 Mon Sep 17 00:00:00 2001 From: Paul Klauser Date: Tue, 28 Nov 2023 13:47:43 -0500 Subject: [PATCH 2/2] Remove unused PresetsRepository#addCategory --- .../java/com/willowtree/vocable/presets/IPresetsRepository.kt | 1 - .../java/com/willowtree/vocable/presets/PresetsRepository.kt | 4 ---- .../com/willowtree/vocable/presets/FakePresetsRepository.kt | 4 ---- 3 files changed, 9 deletions(-) diff --git a/app/src/main/java/com/willowtree/vocable/presets/IPresetsRepository.kt b/app/src/main/java/com/willowtree/vocable/presets/IPresetsRepository.kt index 855f206b..dfe9ef90 100644 --- a/app/src/main/java/com/willowtree/vocable/presets/IPresetsRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/presets/IPresetsRepository.kt @@ -23,7 +23,6 @@ interface IPresetsRepository { suspend fun updateCategorySortOrders(categorySortOrders: List) suspend fun updateCategoryName(categoryId: String, localizedName: LocalesWithText) suspend fun updateCategoryHidden(categoryId: String, hidden: Boolean) - suspend fun addCategory(category: CategoryDto) suspend fun getCategoryById(categoryId: String): CategoryDto suspend fun deleteCategory(categoryId: String) suspend fun getRecentPhrases(): List diff --git a/app/src/main/java/com/willowtree/vocable/presets/PresetsRepository.kt b/app/src/main/java/com/willowtree/vocable/presets/PresetsRepository.kt index 57bab248..579d03d3 100644 --- a/app/src/main/java/com/willowtree/vocable/presets/PresetsRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/presets/PresetsRepository.kt @@ -36,10 +36,6 @@ class PresetsRepository(val context: Context) : KoinComponent, IPresetsRepositor database.phraseDao().insertPhrase(phrase) } - override suspend fun addCategory(category: CategoryDto) { - database.categoryDao().insertCategory(category) - } - private suspend fun populatePhrases(phrases: List) { database.phraseDao().insertPhrases(*phrases.toTypedArray()) } diff --git a/app/src/test/java/com/willowtree/vocable/presets/FakePresetsRepository.kt b/app/src/test/java/com/willowtree/vocable/presets/FakePresetsRepository.kt index 66c9d1a6..4e3e2856 100644 --- a/app/src/test/java/com/willowtree/vocable/presets/FakePresetsRepository.kt +++ b/app/src/test/java/com/willowtree/vocable/presets/FakePresetsRepository.kt @@ -97,10 +97,6 @@ class FakePresetsRepository : IPresetsRepository { TODO("Not yet implemented") } - override suspend fun addCategory(category: CategoryDto) { - _allCategories.update { it + category } - } - override suspend fun getCategoryById(categoryId: String): CategoryDto { TODO("Not yet implemented") }