This repository has been archived by the owner on Apr 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7c72eb
commit 93cd900
Showing
11 changed files
with
116 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
<manifest | ||
package="com.michaelbukachi.realmkoroutines"/> | ||
<manifest package="com.michaelbukachi.realmkoroutines" /> |
30 changes: 30 additions & 0 deletions
30
realmkoroutines/src/main/java/com/michaelbukachi/realmkoroutines/realm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,32 @@ | ||
package com.michaelbukachi.realmkoroutines | ||
|
||
import io.realm.* | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
|
||
private suspend fun <T : RealmObject, S : RealmQuery<T>> findAllAwait(query: S): RealmResults<T> = | ||
suspendCancellableCoroutine { continuation -> | ||
val listener = RealmChangeListener<RealmResults<T>> { t -> continuation.resume(t) } | ||
query.findAllAsync().addChangeListener(listener) | ||
} | ||
|
||
private suspend fun <T : RealmObject, S : RealmQuery<T>> findFirstAwait(query: S): T? = | ||
suspendCancellableCoroutine { continuation -> | ||
val listener = RealmChangeListener { t: T? -> continuation.resume(t) } | ||
query.findFirstAsync().addChangeListener(listener) | ||
} | ||
|
||
private suspend fun executeAsync(realm: Realm, block: (Realm) -> Unit): Unit = | ||
suspendCancellableCoroutine { continuation -> | ||
realm.executeTransactionAsync( | ||
{ block(it) }, | ||
{ continuation.resume(Unit) }, | ||
{ continuation.resumeWithException(it) }) | ||
} | ||
|
||
suspend fun <S : RealmObject> RealmQuery<S>.await() = findAllAwait(this) | ||
|
||
suspend fun <S : RealmObject> RealmQuery<S>.awaitFirst() = findFirstAwait(this) | ||
|
||
suspend fun Realm.transactAwait(block: (Realm) -> Unit) = executeAsync(this, block) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
sample/src/androidTest/java/com/michaelbukachi/realmkoroutines/sample/ExampleActivityTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.michaelbukachi.realmkoroutines.sample | ||
|
||
import androidx.test.core.app.ActivityScenario | ||
import androidx.test.espresso.Espresso.onView | ||
import androidx.test.espresso.assertion.ViewAssertions.matches | ||
import androidx.test.espresso.matcher.ViewMatchers.withId | ||
import androidx.test.espresso.matcher.ViewMatchers.withText | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.filters.LargeTest | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
@LargeTest | ||
class ExampleActivityTest { | ||
|
||
@Test | ||
fun testGreetingsHasText() { | ||
ActivityScenario.launch(ExampleActivity::class.java) | ||
onView(withId(R.id.greetings)).check(matches(withText("Some Test"))) | ||
} | ||
} |
22 changes: 0 additions & 22 deletions
22
...src/androidTest/java/com/michaelbukachi/realmkoroutines/sample/ExampleInstrumentedTest.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
sample/src/main/java/com/michaelbukachi/realmkoroutines/sample/ExampleActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.michaelbukachi.realmkoroutines.sample | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.lifecycleScope | ||
import com.michaelbukachi.realmkoroutines.awaitFirst | ||
import com.michaelbukachi.realmkoroutines.transactAwait | ||
import io.realm.Realm | ||
import io.realm.kotlin.where | ||
import kotlinx.android.synthetic.main.activity_main.* | ||
|
||
class ExampleActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
lifecycleScope.launchWhenResumed { | ||
val realm = Realm.getDefaultInstance() | ||
realm.transactAwait { | ||
it.deleteAll() | ||
val testObject = TestObject(name = "Some Test") | ||
it.copyToRealm(testObject) | ||
} | ||
val result = realm.where<TestObject>().awaitFirst() | ||
greetings.text = result?.name ?: "Default" | ||
} | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
sample/src/main/java/com/michaelbukachi/realmkoroutines/sample/MainActivity.kt
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
sample/src/main/java/com/michaelbukachi/realmkoroutines/sample/RealmKoroutines.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.michaelbukachi.realmkoroutines.sample | ||
|
||
import android.app.Application | ||
import io.realm.Realm | ||
|
||
class RealmKoroutines : Application() { | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
Realm.init(this) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
sample/src/main/java/com/michaelbukachi/realmkoroutines/sample/TestObject.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.michaelbukachi.realmkoroutines.sample | ||
|
||
import io.realm.RealmObject | ||
|
||
open class TestObject(var name: String? = "") : RealmObject() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters