Skip to content

Commit 69a6c95

Browse files
author
Krzysztof Borowy
committed
room ktx extension
1 parent d948069 commit 69a6c95

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

android/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ dependencies {
108108
def room_version = "2.2.6"
109109
def coroutines_version = "1.4.2"
110110
def junit_version = "4.12"
111-
def robolectric_version = "4.2.1"
111+
def robolectric_version = "4.5.1"
112112
def truth_version = "1.1.2"
113113
def androidxtest_version = "1.1.0"
114114
def coroutinesTest_version = "1.4.2"
115115

116116
implementation "androidx.room:room-runtime:$room_version"
117+
implementation "androidx.room:room-ktx:$room_version"
117118
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
118119
kapt "androidx.room:room-compiler:$room_version"
119120

android/src/main/java/com/reactnativecommunity/asyncstorage/next/StorageSupplier.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import androidx.room.Query
1212
import androidx.room.Room
1313
import androidx.room.RoomDatabase
1414
import androidx.room.Transaction
15-
import androidx.room.Update
1615
import androidx.room.migration.Migration
1716
import androidx.sqlite.db.SupportSQLiteDatabase
1817
import org.json.JSONObject
@@ -35,18 +34,18 @@ internal interface StorageDao {
3534

3635
@Transaction
3736
@Query("SELECT * FROM $TABLE_NAME WHERE `$COLUMN_KEY` IN (:keys)")
38-
fun getValues(keys: List<String>): List<Entry>
37+
suspend fun getValues(keys: List<String>): List<Entry>
3938

4039
@Transaction
4140
@Insert(onConflict = OnConflictStrategy.REPLACE)
42-
fun setValues(entries: List<Entry>)
41+
suspend fun setValues(entries: List<Entry>)
4342

4443
@Transaction
4544
@Query("DELETE FROM $TABLE_NAME WHERE `$COLUMN_KEY` in (:keys)")
46-
fun removeValues(keys: List<String>)
45+
suspend fun removeValues(keys: List<String>)
4746

4847
@Transaction
49-
fun mergeValues(entries: List<Entry>) {
48+
suspend fun mergeValues(entries: List<Entry>) {
5049
val currentDbEntries = getValues(entries.map { it.key })
5150
val newEntries = mutableListOf<Entry>()
5251

@@ -67,11 +66,11 @@ internal interface StorageDao {
6766

6867
@Transaction
6968
@Query("SELECT `$COLUMN_KEY` FROM $TABLE_NAME")
70-
fun getKeys(): List<String>
69+
suspend fun getKeys(): List<String>
7170

7271
@Transaction
7372
@Query("DELETE FROM $TABLE_NAME")
74-
fun clear()
73+
suspend fun clear()
7574
}
7675

7776

@@ -148,6 +147,7 @@ class StorageSupplier internal constructor(db: StorageDb) : AsyncStorageAccess {
148147
return StorageSupplier(StorageDb.getDatabase(ctx))
149148
}
150149
}
150+
151151
private val access = db.storage()
152152

153153
override suspend fun getValues(keys: List<String>) = access.getValues(keys)

android/src/test/java/com/reactnativecommunity/asyncstorage/next/StorageTest.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
55
import androidx.test.platform.app.InstrumentationRegistry
66
import com.google.common.truth.Truth.assertThat
77
import kotlinx.coroutines.ExperimentalCoroutinesApi
8-
import kotlinx.coroutines.test.runBlockingTest
8+
import kotlinx.coroutines.runBlocking
99
import org.json.JSONObject
1010
import org.junit.After
1111
import org.junit.Before
@@ -24,7 +24,6 @@ class AsyncStorageAccessTest {
2424
database = Room.inMemoryDatabaseBuilder(
2525
InstrumentationRegistry.getInstrumentation().context, StorageDb::class.java
2626
).allowMainThreadQueries().build()
27-
2827
asyncStorage = StorageSupplier(database)
2928
}
3029

@@ -34,7 +33,7 @@ class AsyncStorageAccessTest {
3433
}
3534

3635
@Test
37-
fun performsBasicGetSetRemoveOperations() = runBlockingTest {
36+
fun performsBasicGetSetRemoveOperations() = runBlocking {
3837
val entriesCount = 10
3938
val entries = createRandomEntries(entriesCount)
4039
val keys = entries.map { it.key }
@@ -49,7 +48,7 @@ class AsyncStorageAccessTest {
4948
}
5049

5150
@Test
52-
fun readsAllKeysAndClearsDb() = runBlockingTest {
51+
fun readsAllKeysAndClearsDb() = runBlocking {
5352
val entries = createRandomEntries(8)
5453
val keys = entries.map { it.key }
5554
asyncStorage.setValues(entries)
@@ -60,7 +59,7 @@ class AsyncStorageAccessTest {
6059
}
6160

6261
@Test
63-
fun mergesDeeplyTwoValues() = runBlockingTest {
62+
fun mergesDeeplyTwoValues() = runBlocking {
6463
val initialEntry = Entry("key", VALUE_INITIAL)
6564
val overrideEntry = Entry("key", VALUE_OVERRIDES)
6665
asyncStorage.setValues(listOf(initialEntry))
@@ -70,7 +69,7 @@ class AsyncStorageAccessTest {
7069
}
7170

7271
@Test
73-
fun updatesExistingValues() = runBlockingTest {
72+
fun updatesExistingValues() = runBlocking {
7473
val key = "test_key"
7574
val value = "test_value"
7675
val entries = listOf(Entry(key, value))

0 commit comments

Comments
 (0)