Skip to content

Commit

Permalink
Remove duplicate dao methods and funnel usage to the Flows
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulKlauser committed Jun 11, 2024
1 parent 0caa5a1 commit 8e72692
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.willowtree.vocable.presets.PresetCategories
import com.willowtree.vocable.utility.VocableKoinTestRule
import com.willowtree.vocable.utils.VocableSharedPreferences
import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Assert
import org.junit.Assert.assertEquals
Expand Down Expand Up @@ -341,8 +342,8 @@ class MigrationTest {
), categories
)

val customPhrases = db.phraseDao().getPhrasesForCategory("custom")
val recentPhrases = db.phraseDao().getPhrasesForCategory("recents")
val customPhrases = db.phraseDao().getPhrasesForCategory("custom").first()
val recentPhrases = db.phraseDao().getPhrasesForCategory("recents").first()
assertEquals(
listOf(
PhraseDto(
Expand Down
9 changes: 2 additions & 7 deletions app/src/main/java/com/willowtree/vocable/PhrasesUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.willowtree.vocable.utils.locale.LocaleProvider
import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.first

class PhrasesUseCase(
private val legacyPhrasesRepository: ILegacyCategoriesAndPhrasesRepository,
Expand All @@ -24,13 +25,7 @@ class PhrasesUseCase(
private val localeProvider: LocaleProvider
) : IPhrasesUseCase {
override suspend fun getPhrasesForCategory(categoryId: String): List<Phrase> {
if (categoryId == PresetCategories.RECENTS.id) {
val presets = presetPhrasesRepository.getRecentPhrases()
val stored = storedPhrasesRepository.getRecentPhrases()
return (presets + stored).sortedByDescending { it.lastSpokenDate }.take(8)
}
return storedPhrasesRepository.getPhrasesForCategory(categoryId) +
presetPhrasesRepository.getPhrasesForCategory(categoryId)
return getPhrasesForCategoryFlow(categoryId).first()
}

override fun getPhrasesForCategoryFlow(categoryId: String): Flow<List<Phrase>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.willowtree.vocable.room.PhraseDto
import com.willowtree.vocable.room.VocableDatabase
import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import org.koin.core.component.KoinComponent

class LegacyCategoriesAndPhrasesRepository(
Expand All @@ -24,11 +25,11 @@ class LegacyCategoriesAndPhrasesRepository(
}

override suspend fun getPhrasesForCategory(categoryId: String): List<PhraseDto> {
return database.phraseDao().getPhrasesForCategory(categoryId)
return database.phraseDao().getPhrasesForCategory(categoryId).first()
}

override suspend fun getRecentPhrases(): List<PhraseDto> =
database.phraseDao().getRecentPhrases()
database.phraseDao().getRecentPhrases().first()

override suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>) {
database.categoryDao().updateCategorySortOrders(categorySortOrders)
Expand Down
10 changes: 2 additions & 8 deletions app/src/main/java/com/willowtree/vocable/room/PhraseDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,10 @@ interface PhraseDao {
suspend fun updatePhraseLocalizedUtterance(phraseLocalizedUtterance: PhraseLocalizedUtterance)

@Query("SELECT * FROM Phrase WHERE last_spoken_date IS NOT NULL ORDER BY last_spoken_date DESC LIMIT 8")
suspend fun getRecentPhrases(): List<PhraseDto>

@Query("SELECT * FROM Phrase WHERE last_spoken_date IS NOT NULL ORDER BY last_spoken_date DESC LIMIT 8")
fun getRecentPhrasesFlow(): Flow<List<PhraseDto>>

@Query("SELECT * FROM Phrase WHERE parent_category_id == :categoryId")
suspend fun getPhrasesForCategory(categoryId: String): List<PhraseDto>
fun getRecentPhrases(): Flow<List<PhraseDto>>

@Query("SELECT * FROM Phrase WHERE parent_category_id == :categoryId")
fun getPhrasesForCategoryFlow(categoryId: String): Flow<List<PhraseDto>>
fun getPhrasesForCategory(categoryId: String): Flow<List<PhraseDto>>

@Query("SELECT * FROM Phrase WHERE phrase_id == :phraseId")
suspend fun getPhrase(phraseId: String): PhraseDto?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,13 @@ class RoomStoredPhrasesRepository(
)
}

override suspend fun getRecentPhrases(): List<Phrase> {
return database.phraseDao().getRecentPhrases().map { it.asPhrase() }
}

override fun getRecentPhrasesFlow(): Flow<List<Phrase>> {
return database.phraseDao().getRecentPhrasesFlow()
return database.phraseDao().getRecentPhrases()
.map { phraseList -> phraseList.map { it.asPhrase() } }
}

override suspend fun getPhrasesForCategory(categoryId: String): List<Phrase> {
return database.phraseDao().getPhrasesForCategory(categoryId).map { it.asPhrase() }
}

override fun getPhrasesForCategoryFlow(categoryId: String): Flow<List<Phrase>> {
return database.phraseDao().getPhrasesForCategoryFlow(categoryId)
return database.phraseDao().getPhrasesForCategory(categoryId)
.map { phraseList -> phraseList.map { it.asPhrase() } }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import kotlinx.coroutines.flow.Flow
interface StoredPhrasesRepository {
suspend fun addPhrase(phrase: PhraseDto)
suspend fun updatePhraseLastSpokenTime(phraseId: String)
suspend fun getRecentPhrases(): List<Phrase>
fun getRecentPhrasesFlow(): Flow<List<Phrase>>
suspend fun getPhrasesForCategory(categoryId: String): List<Phrase>
fun getPhrasesForCategoryFlow(categoryId: String): Flow<List<Phrase>>
suspend fun getPhrase(phraseId: String): Phrase?
suspend fun updatePhrase(phrase: PhraseDto)
Expand Down

This file was deleted.

0 comments on commit 8e72692

Please sign in to comment.