Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into facetrackingmanager-util-refactor
  • Loading branch information
IanCrossCD committed Nov 29, 2023
2 parents c1bb122 + 7624349 commit 9658eef
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/willowtree/vocable/AppKoinModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,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) {
presetsRepository.addCategory(
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)
}
53 changes: 51 additions & 2 deletions app/src/test/java/com/willowtree/vocable/CategoriesUseCaseTest.kt
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
)
}
}
}

0 comments on commit 9658eef

Please sign in to comment.