Skip to content

Commit

Permalink
Rename asyncUI function to async
Browse files Browse the repository at this point in the history
  • Loading branch information
pilgr committed Aug 29, 2016
1 parent 64b8384 commit 494abbd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Experimental implementation of async/await for Android using Kotlin Coroutines
This is very basic implementation of what could be achieved using _suspendable computation_ aka _coroutines_ on Android.
For example
```Kotlin
asyncUI {
async {
progress.visibility = View.VISIBLE
text.text = "Loading..."
// Release main thread and wait until text loaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.os.Bundle
import android.util.Log
import android.view.View
import co.metalab.asyncawait.AsyncController
import co.metalab.asyncawait.asyncUI
import co.metalab.asyncawait.async
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_github.*
import retrofit2.Call
Expand All @@ -32,7 +32,7 @@ class GitHubActivity : AppCompatActivity() {
btnGetRepos.setOnClickListener { refreshRepos() }
}

private fun refreshRepos() = asyncUI {
private fun refreshRepos() = async {
txtRepos.text = ""
progressBar.isIndeterminate = true
progressBar.visibility = View.VISIBLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import co.metalab.asyncawait.asyncUI
import co.metalab.asyncawait.async
import co.metalab.asyncawait.ProgressHandler
import hugo.weaving.DebugLog
import kotlinx.android.synthetic.main.activity_main.*
Expand All @@ -24,7 +24,7 @@ class MainActivity : AppCompatActivity() {
}
}

private fun startCoroutine() = asyncUI {
private fun startCoroutine() = async {
progressBar.visibility = View.VISIBLE
txtResult.text = "Loading..."
try {
Expand All @@ -43,7 +43,7 @@ class MainActivity : AppCompatActivity() {
progressBar.visibility = View.INVISIBLE
}

private fun startCoroutineUsingMoreConvenientErrorHandling() = asyncUI {
private fun startCoroutineUsingMoreConvenientErrorHandling() = async {
progressBar.visibility = View.VISIBLE
txtResult.text = "Loading..."
// Release main thread and wait until text loaded
Expand All @@ -60,7 +60,7 @@ class MainActivity : AppCompatActivity() {
}


private fun startCoroutineWithProgress() = asyncUI {
private fun startCoroutineWithProgress() = async {
btnStart.isEnabled = false
progressBar.visibility = View.VISIBLE
progressBar.isIndeterminate = false
Expand Down
16 changes: 8 additions & 8 deletions asyncawait/src/main/kotlin/co/metalab/asyncawait/AsyncUi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import java.util.concurrent.Executors

private val executor = Executors.newSingleThreadExecutor()

fun asyncUI(coroutine c: AsyncController.() -> Continuation<Unit>): AsyncController {
return asyncUI(c, AsyncController())
fun async(coroutine c: AsyncController.() -> Continuation<Unit>): AsyncController {
return async(c, AsyncController())
}

fun Activity.asyncUI(coroutine c: AsyncController.() -> Continuation<Unit>): AsyncController {
return asyncUI(c, AsyncController(activity = this))
fun Activity.async(coroutine c: AsyncController.() -> Continuation<Unit>): AsyncController {
return async(c, AsyncController(activity = this))
}

fun Fragment.asyncUI(coroutine c: AsyncController.() -> Continuation<Unit>): AsyncController {
return asyncUI(c, AsyncController(fragment = this))
fun Fragment.async(coroutine c: AsyncController.() -> Continuation<Unit>): AsyncController {
return async(c, AsyncController(fragment = this))
}

internal fun asyncUI(c: AsyncController.() -> Continuation<Unit>,
controller: AsyncController): AsyncController {
internal fun async(c: AsyncController.() -> Continuation<Unit>,
controller: AsyncController): AsyncController {
// TODO If not in UI thread - force run resume() in UI thread
controller.c().resume(Unit)
return controller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import java.util.concurrent.TimeoutException
import kotlin.test.*

@RunWith(RobolectricTestRunner::class)
class AsyncUiTest {
class AsyncTest {

@Test
fun `Normal async-await usage`() {
var result = ""
var done = false
asyncUI {
async {
result = await { "O" } + "K"
done = true
}
Expand All @@ -33,7 +33,7 @@ class AsyncUiTest {
var result = ""
var done = false
assertEquals(Looper.getMainLooper(), Looper.myLooper(), "1. Test runs on UI thread")
asyncUI {
async {
assertEquals(Looper.getMainLooper(), Looper.myLooper(), "2. Continue on UI thread")

result = await {
Expand All @@ -54,7 +54,7 @@ class AsyncUiTest {

@Test(expected = RuntimeException::class)
fun `Unhandled exception in background thread delivered to system`() {
asyncUI {
async {
await { throw RuntimeException("Catch me!") }
@Suppress("UNREACHABLE_CODE")
fail("Exception should be thrown before this point")
Expand All @@ -65,7 +65,7 @@ class AsyncUiTest {
@Test
fun `Exception from background thread can be caught outside await block using try-catch in UI thread`() {
var done = false
asyncUI {
async {
try {
await { throw RuntimeException("Catch me!") }
@Suppress("UNREACHABLE_CODE")
Expand All @@ -82,7 +82,7 @@ class AsyncUiTest {
@Test
fun `Exception from background thread can be caught in onError block in UI thread`() {
var done = false
asyncUI {
async {
await { throw RuntimeException("Catch me!") }
@Suppress("UNREACHABLE_CODE")
fail("Exception should be thrown before this point")
Expand All @@ -97,7 +97,7 @@ class AsyncUiTest {
@Test
fun `Catch block is ignored when error block is specified`() {
var done = false
asyncUI {
async {
try {
await { throw RuntimeException("Catch me!") }
} catch (e: RuntimeException) {
Expand All @@ -124,7 +124,7 @@ class AsyncUiTest {
return "OK"
}

asyncUI {
async {
result = awaitWithProgress(::load) {
// Handle progress changes
progressValues += it
Expand All @@ -146,7 +146,7 @@ class AsyncUiTest {

var result = ""
var done = false
activity.asyncUI {
activity.async {
result = "O"
result += await { "K" }
done = true
Expand All @@ -162,7 +162,7 @@ class AsyncUiTest {

var result = ""
var done = false
activity.asyncUI {
activity.async {
result = "O"
Mockito.`when`(activity.isFinishing).thenReturn(true)
result += await { done = true; "K" }
Expand All @@ -181,7 +181,7 @@ class AsyncUiTest {

var result = ""
var done = false
fragment.asyncUI {
fragment.async {
result = "O"
result += await { "K" }
done = true
Expand All @@ -198,7 +198,7 @@ class AsyncUiTest {

var result = ""
var done = false
fragment.asyncUI {
fragment.async {
result = "O"
Mockito.`when`(fragment.isDetached).thenReturn(true)
result += await { done = true; "K" }
Expand All @@ -216,7 +216,7 @@ class AsyncUiTest {

var result = ""
var done = false
fragment.asyncUI {
fragment.async {
result = "O"
Mockito.`when`(fragment.activity).thenReturn(null)
result += await { done = true; "K" }
Expand Down

0 comments on commit 494abbd

Please sign in to comment.