Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement adding a Category in CategoriesUseCase #453

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,10 +46,9 @@ class CategoriesUseCase(
}

override suspend fun addCategory(categoryName: String, sortOrder: Int) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate discussions from your code but I'm curious on your thoughts on these sort of mono use cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meaning, use cases that can do more than one thing? I don't have super strong feelings, and I know a lot of folks go with the one-operation use cases, but I find that doesn't buy you a whole lot for the extra verbosity. What are your thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't buy you much for sure. It feel like its conflating two patterns but doesn't feel too wrong otherwise.

presetsRepository.addCategory(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the PresetsRepository.addCategory still used?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great ask, looks like it's not

CategoryDto(
storedCategoriesRepository.addCategory(
Category.StoredCategory(
uuidProvider.randomUUIDString(),
dateProvider.currentTimeMillis(),
null,
LocalesWithText(mapOf(Pair(localeProvider.getDefaultLocaleString(), categoryName))),
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ interface IPresetsRepository {
suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>)
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<PhraseDto>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhraseDto>) {
database.phraseDao().insertPhrases(*phrases.toTypedArray())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -9,4 +10,17 @@ class RoomStoredCategoriesRepository(context: Context) : StoredCategoriesReposit
override fun getAllCategories(): Flow<List<CategoryDto>> {
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
)
)
}
}
Original file line number Diff line number Diff line change
@@ -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<List<CategoryDto>>
suspend fun addCategory(category: Category.StoredCategory)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +25,6 @@ class CategoriesUseCaseTest {
return CategoriesUseCase(
fakePresetsRepository,
ConstantUUIDProvider(),
FakeDateProvider(),
FakeLocaleProvider(),
fakeStoredCategoriesRepository,
fakePresetCategoriesRepository
Expand Down Expand Up @@ -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()
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<CategoryDto>> {
return _allCategories
}

override suspend fun addCategory(category: Category.StoredCategory) {
_allCategories.update {
it + CategoryDto(
category.categoryId,
0L,
category.resourceId,
category.localizedName,
category.hidden,
category.sortOrder
)
}
}
}