Issue#67 Extract runOnUIAwaitOnCurrent into separate module

This commit is contained in:
Gergely Hegedus 2022-05-27 15:08:42 +03:00
parent 4932b4b2e0
commit 689aee9702
12 changed files with 74 additions and 6 deletions

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.fnives.test.showcase.android.testutil">
</manifest>

View file

@ -0,0 +1,24 @@
package org.fnives.test.showcase.android.testutil.synchronization
import android.os.Handler
import android.os.Looper
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.runBlocking
/**
* Runs the given action on the MainThread and blocks currentThread, until it is completed.
*
* It is safe to call this from the MainThread.
*/
fun runOnUIAwaitOnCurrent(action: () -> Unit) {
if (Looper.myLooper() === Looper.getMainLooper()) {
action()
} else {
val deferred = CompletableDeferred<Unit>()
Handler(Looper.getMainLooper()).post {
action()
deferred.complete(Unit)
}
runBlocking { deferred.await() }
}
}