From 8e7269225253d3c2b9f0406b7fcce36a167069ca Mon Sep 17 00:00:00 2001 From: Paul Klauser Date: Tue, 11 Jun 2024 17:01:47 -0400 Subject: [PATCH] Remove duplicate dao methods and funnel usage to the Flows --- .../com/willowtree/vocable/room/MigrationTest.kt | 5 +++-- .../java/com/willowtree/vocable/PhrasesUseCase.kt | 9 ++------- .../presets/LegacyCategoriesAndPhrasesRepository.kt | 5 +++-- .../java/com/willowtree/vocable/room/PhraseDao.kt | 10 ++-------- .../vocable/room/RoomStoredPhrasesRepository.kt | 12 ++---------- .../vocable/room/StoredPhrasesRepository.kt | 2 -- .../willowtree/vocable/settings/SettingsViewModel.kt | 11 ----------- 7 files changed, 12 insertions(+), 42 deletions(-) delete mode 100644 app/src/main/java/com/willowtree/vocable/settings/SettingsViewModel.kt diff --git a/app/src/androidTest/java/com/willowtree/vocable/room/MigrationTest.kt b/app/src/androidTest/java/com/willowtree/vocable/room/MigrationTest.kt index d53b295a..6bab7088 100644 --- a/app/src/androidTest/java/com/willowtree/vocable/room/MigrationTest.kt +++ b/app/src/androidTest/java/com/willowtree/vocable/room/MigrationTest.kt @@ -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 @@ -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( diff --git a/app/src/main/java/com/willowtree/vocable/PhrasesUseCase.kt b/app/src/main/java/com/willowtree/vocable/PhrasesUseCase.kt index 39c189ee..f93d0339 100644 --- a/app/src/main/java/com/willowtree/vocable/PhrasesUseCase.kt +++ b/app/src/main/java/com/willowtree/vocable/PhrasesUseCase.kt @@ -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, @@ -24,13 +25,7 @@ class PhrasesUseCase( private val localeProvider: LocaleProvider ) : IPhrasesUseCase { override suspend fun getPhrasesForCategory(categoryId: String): List { - 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> { 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 d31c26c3..ef755aab 100644 --- a/app/src/main/java/com/willowtree/vocable/presets/LegacyCategoriesAndPhrasesRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/presets/LegacyCategoriesAndPhrasesRepository.kt @@ -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( @@ -24,11 +25,11 @@ class LegacyCategoriesAndPhrasesRepository( } override suspend fun getPhrasesForCategory(categoryId: String): List { - return database.phraseDao().getPhrasesForCategory(categoryId) + return database.phraseDao().getPhrasesForCategory(categoryId).first() } override suspend fun getRecentPhrases(): List = - database.phraseDao().getRecentPhrases() + database.phraseDao().getRecentPhrases().first() override suspend fun updateCategorySortOrders(categorySortOrders: List) { database.categoryDao().updateCategorySortOrders(categorySortOrders) diff --git a/app/src/main/java/com/willowtree/vocable/room/PhraseDao.kt b/app/src/main/java/com/willowtree/vocable/room/PhraseDao.kt index 11be3b63..ce7c8c84 100644 --- a/app/src/main/java/com/willowtree/vocable/room/PhraseDao.kt +++ b/app/src/main/java/com/willowtree/vocable/room/PhraseDao.kt @@ -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 - - @Query("SELECT * FROM Phrase WHERE last_spoken_date IS NOT NULL ORDER BY last_spoken_date DESC LIMIT 8") - fun getRecentPhrasesFlow(): Flow> - - @Query("SELECT * FROM Phrase WHERE parent_category_id == :categoryId") - suspend fun getPhrasesForCategory(categoryId: String): List + fun getRecentPhrases(): Flow> @Query("SELECT * FROM Phrase WHERE parent_category_id == :categoryId") - fun getPhrasesForCategoryFlow(categoryId: String): Flow> + fun getPhrasesForCategory(categoryId: String): Flow> @Query("SELECT * FROM Phrase WHERE phrase_id == :phraseId") suspend fun getPhrase(phraseId: String): PhraseDto? diff --git a/app/src/main/java/com/willowtree/vocable/room/RoomStoredPhrasesRepository.kt b/app/src/main/java/com/willowtree/vocable/room/RoomStoredPhrasesRepository.kt index ff5628fb..2523c3d4 100644 --- a/app/src/main/java/com/willowtree/vocable/room/RoomStoredPhrasesRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/room/RoomStoredPhrasesRepository.kt @@ -24,21 +24,13 @@ class RoomStoredPhrasesRepository( ) } - override suspend fun getRecentPhrases(): List { - return database.phraseDao().getRecentPhrases().map { it.asPhrase() } - } - override fun getRecentPhrasesFlow(): Flow> { - return database.phraseDao().getRecentPhrasesFlow() + return database.phraseDao().getRecentPhrases() .map { phraseList -> phraseList.map { it.asPhrase() } } } - override suspend fun getPhrasesForCategory(categoryId: String): List { - return database.phraseDao().getPhrasesForCategory(categoryId).map { it.asPhrase() } - } - override fun getPhrasesForCategoryFlow(categoryId: String): Flow> { - return database.phraseDao().getPhrasesForCategoryFlow(categoryId) + return database.phraseDao().getPhrasesForCategory(categoryId) .map { phraseList -> phraseList.map { it.asPhrase() } } } diff --git a/app/src/main/java/com/willowtree/vocable/room/StoredPhrasesRepository.kt b/app/src/main/java/com/willowtree/vocable/room/StoredPhrasesRepository.kt index 66c27f43..205b3172 100644 --- a/app/src/main/java/com/willowtree/vocable/room/StoredPhrasesRepository.kt +++ b/app/src/main/java/com/willowtree/vocable/room/StoredPhrasesRepository.kt @@ -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 fun getRecentPhrasesFlow(): Flow> - suspend fun getPhrasesForCategory(categoryId: String): List fun getPhrasesForCategoryFlow(categoryId: String): Flow> suspend fun getPhrase(phraseId: String): Phrase? suspend fun updatePhrase(phrase: PhraseDto) diff --git a/app/src/main/java/com/willowtree/vocable/settings/SettingsViewModel.kt b/app/src/main/java/com/willowtree/vocable/settings/SettingsViewModel.kt deleted file mode 100644 index 5c180bc0..00000000 --- a/app/src/main/java/com/willowtree/vocable/settings/SettingsViewModel.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.willowtree.vocable.settings - -import androidx.lifecycle.ViewModel -import com.willowtree.vocable.presets.LegacyCategoriesAndPhrasesRepository -import org.koin.core.component.KoinComponent -import org.koin.core.component.inject - -class SettingsViewModel : ViewModel(), KoinComponent { - - private val presetsRepository: LegacyCategoriesAndPhrasesRepository by inject() -}