Skip to content

Commit 8dbd53c

Browse files
authored
Merge pull request #311 from yveskalume/main
Use @upsert from Room instead of a custom helper
2 parents 4a8f872 + b05ea47 commit 8dbd53c

File tree

8 files changed

+22
-58
lines changed

8 files changed

+22
-58
lines changed

core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestAuthorDao.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class TestAuthorDao : AuthorDao {
5757
throw NotImplementedError("Unused in tests")
5858
}
5959

60+
override suspend fun upsertAuthors(entities: List<AuthorEntity>) {
61+
entitiesStateFlow.value = entities
62+
}
63+
6064
override suspend fun deleteAuthors(ids: List<String>) {
6165
val idSet = ids.toSet()
6266
entitiesStateFlow.update { entities ->

core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNewsResourceDao.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ class TestNewsResourceDao : NewsResourceDao {
8585
throw NotImplementedError("Unused in tests")
8686
}
8787

88+
override suspend fun upsertNewsResources(newsResourceEntities: List<NewsResourceEntity>) {
89+
entitiesStateFlow.value = newsResourceEntities
90+
}
91+
8892
override suspend fun insertOrIgnoreTopicCrossRefEntities(
8993
newsResourceTopicCrossReferences: List<NewsResourceTopicCrossRef>
9094
) {

core/data/src/test/java/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestTopicDao.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class TestTopicDao : TopicDao {
6262
throw NotImplementedError("Unused in tests")
6363
}
6464

65+
override suspend fun upsertTopics(entities: List<TopicEntity>) {
66+
entitiesStateFlow.value = entities
67+
}
68+
6569
override suspend fun deleteTopics(ids: List<String>) {
6670
val idSet = ids.toSet()
6771
entitiesStateFlow.update { entities ->

core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/AuthorDao.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import androidx.room.Dao
2020
import androidx.room.Insert
2121
import androidx.room.OnConflictStrategy
2222
import androidx.room.Query
23-
import androidx.room.Transaction
2423
import androidx.room.Update
24+
import androidx.room.Upsert
2525
import com.google.samples.apps.nowinandroid.core.database.model.AuthorEntity
2626
import kotlinx.coroutines.flow.Flow
2727

@@ -56,12 +56,8 @@ interface AuthorDao {
5656
/**
5757
* Inserts or updates [entities] in the db under the specified primary keys
5858
*/
59-
@Transaction
60-
suspend fun upsertAuthors(entities: List<AuthorEntity>) = upsert(
61-
items = entities,
62-
insertMany = ::insertOrIgnoreAuthors,
63-
updateMany = ::updateAuthors
64-
)
59+
@Upsert
60+
suspend fun upsertAuthors(entities: List<AuthorEntity>)
6561

6662
/**
6763
* Deletes rows in the db matching the specified [ids]

core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/NewsResourceDao.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.room.OnConflictStrategy
2222
import androidx.room.Query
2323
import androidx.room.Transaction
2424
import androidx.room.Update
25+
import androidx.room.Upsert
2526
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceAuthorCrossRef
2627
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceEntity
2728
import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceTopicCrossRef
@@ -80,12 +81,8 @@ interface NewsResourceDao {
8081
/**
8182
* Inserts or updates [newsResourceEntities] in the db under the specified primary keys
8283
*/
83-
@Transaction
84-
suspend fun upsertNewsResources(newsResourceEntities: List<NewsResourceEntity>) = upsert(
85-
items = newsResourceEntities,
86-
insertMany = ::insertOrIgnoreNewsResources,
87-
updateMany = ::updateNewsResources
88-
)
84+
@Upsert
85+
suspend fun upsertNewsResources(newsResourceEntities: List<NewsResourceEntity>)
8986

9087
@Insert(onConflict = OnConflictStrategy.IGNORE)
9188
suspend fun insertOrIgnoreTopicCrossRefEntities(

core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/TopicDao.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import androidx.room.Dao
2020
import androidx.room.Insert
2121
import androidx.room.OnConflictStrategy
2222
import androidx.room.Query
23-
import androidx.room.Transaction
2423
import androidx.room.Update
24+
import androidx.room.Upsert
2525
import com.google.samples.apps.nowinandroid.core.database.model.TopicEntity
2626
import kotlinx.coroutines.flow.Flow
2727

@@ -64,12 +64,8 @@ interface TopicDao {
6464
/**
6565
* Inserts or updates [entities] in the db under the specified primary keys
6666
*/
67-
@Transaction
68-
suspend fun upsertTopics(entities: List<TopicEntity>) = upsert(
69-
items = entities,
70-
insertMany = ::insertOrIgnoreTopics,
71-
updateMany = ::updateTopics
72-
)
67+
@Upsert
68+
suspend fun upsertTopics(entities: List<TopicEntity>)
7369

7470
/**
7571
* Deletes rows in the db matching the specified [ids]

core/database/src/main/java/com/google/samples/apps/nowinandroid/core/database/dao/UpsertHelper.kt

Lines changed: 0 additions & 37 deletions
This file was deleted.

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protobuf = "3.21.5"
4646
protobufPlugin = "0.8.19"
4747
retrofit = "2.9.0"
4848
retrofitKotlinxSerializationJson = "0.8.0"
49-
room = "2.4.3"
49+
room = "2.5.0-alpha03"
5050
secrets = "2.0.1"
5151
turbine = "0.8.0"
5252

0 commit comments

Comments
 (0)