-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Kotlin and async/await support.
- Loading branch information
Showing
9 changed files
with
368 additions
and
17 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.koushikdutta.ion.kotlin" | ||
android:versionCode="1" | ||
android:versionName="1.0"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET"/> | ||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | ||
|
||
<application> | ||
</application> | ||
</manifest> |
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,46 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
apply plugin: 'kotlin-android-extensions' | ||
|
||
|
||
dependencies { | ||
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
compile project(':ion:ion') | ||
} | ||
|
||
buildscript { | ||
ext.kotlin_version = '1.1.3-eap-85' | ||
repositories { | ||
google() | ||
jcenter() | ||
maven { url "https://dl.bintray.com/kotlin/kotlin-eap-1.1"} | ||
} | ||
dependencies { | ||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | ||
|
||
|
||
classpath project.hasProperty('global_gradleAndroidPlugin') ? global_gradleAndroidPlugin : 'com.android.tools.build:gradle:3.0.0-alpha5' | ||
classpath 'com.google.gms:google-services:3.0.0' | ||
classpath 'com.google.appengine:gradle-appengine-plugin:1.9.28' | ||
} | ||
} | ||
|
||
android { | ||
sourceSets { | ||
main { | ||
manifest.srcFile 'AndroidManifest.xml' | ||
res.srcDirs = ['res/'] | ||
java.srcDirs = ['src/'] | ||
assets.srcDirs = ['assets/'] | ||
} | ||
} | ||
|
||
defaultConfig { | ||
targetSdkVersion 26 | ||
minSdkVersion 14 | ||
} | ||
|
||
compileSdkVersion project.hasProperty('global_compileSdkVersion') ? global_compileSdkVersion : 25 | ||
buildToolsVersion project.hasProperty('global_buildToolsVersion') ? global_buildToolsVersion : '25.0.2' | ||
} | ||
|
133 changes: 133 additions & 0 deletions
133
ion-kotlin/src/com/koushikdutta/ion/kotlin/AsyncAwait.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,133 @@ | ||
package com.koushikdutta.ion.kotlin | ||
|
||
import android.app.Activity | ||
import android.app.Fragment | ||
import android.app.Service | ||
import android.content.Context | ||
import android.os.Handler | ||
import android.os.Looper | ||
import com.koushikdutta.async.AsyncServer | ||
import com.koushikdutta.async.future.Future | ||
import com.koushikdutta.async.future.SimpleFuture | ||
import com.koushikdutta.ion.ContextReference | ||
import com.koushikdutta.ion.IonContext | ||
import java.util.concurrent.Executor | ||
import kotlin.coroutines.experimental.* | ||
|
||
/** | ||
* Created by koush on 7/2/17. | ||
*/ | ||
|
||
private class WrappingContextReference { | ||
|
||
} | ||
|
||
fun <T> Activity.async(block: suspend AsyncContext.() -> T): Future<T> { | ||
return async(ContextReference.ActivityContextReference(this) as ContextReference<Object>, block) | ||
} | ||
|
||
fun <T> Service.async(block: suspend AsyncContext.() -> T): Future<T> { | ||
return async(ContextReference.ServiceContextReference(this) as ContextReference<Object>, block) | ||
} | ||
|
||
fun <T> Fragment.async(block: suspend AsyncContext.() -> T): Future<T> { | ||
return async(ContextReference.FragmentContextReference(this) as ContextReference<Object>, block) | ||
} | ||
|
||
fun <T> android.support.v4.app.Fragment.async(block: suspend AsyncContext.() -> T): Future<T> { | ||
return async(ContextReference.SupportFragmentContextReference(this) as ContextReference<Object>, block) | ||
} | ||
|
||
class AsyncContext(val ionContext: IonContext, val future: SimpleFuture<Object>): IonContext { | ||
override fun getContext(): Context? { | ||
return ionContext.context | ||
} | ||
|
||
override fun isAlive(): String? { | ||
return ionContext.isAlive | ||
} | ||
|
||
suspend fun <T> Future<T>.await(): T { | ||
return suspendCoroutine { continuation -> | ||
setCallback { e, result -> | ||
if (checkLive()) { | ||
if (e != null) | ||
continuation.resumeWithException(e); | ||
else | ||
continuation.resume(result); | ||
} | ||
} | ||
} | ||
} | ||
|
||
suspend fun await(looper: Looper) { | ||
return await(Handler(looper)) | ||
} | ||
|
||
suspend fun await() { | ||
return await(Looper.getMainLooper()) | ||
} | ||
|
||
suspend fun await(handler: Handler) { | ||
return suspendCoroutine { continuation -> | ||
handler.post { | ||
if (checkLive()) | ||
continuation.resume(Unit) | ||
} | ||
} | ||
} | ||
|
||
suspend fun await(asyncServer: AsyncServer) { | ||
return suspendCoroutine { continuation -> | ||
asyncServer.post { | ||
if (checkLive()) | ||
continuation.resume(Unit) | ||
} | ||
} | ||
} | ||
|
||
suspend fun await(executor: Executor) { | ||
return suspendCoroutine { continuation -> | ||
executor.execute { | ||
if (checkLive()) | ||
continuation.resume(Unit) | ||
} | ||
} | ||
} | ||
|
||
fun checkLive(): Boolean { | ||
if (isAlive == null) | ||
return true | ||
future.cancelSilently() | ||
return false | ||
} | ||
} | ||
|
||
private class StandaloneCoroutine<T>(val asyncContext: AsyncContext, val future: SimpleFuture<T>, override val context: CoroutineContext): Continuation<T> { | ||
override fun resume(value: T) { | ||
if (asyncContext.isAlive() == null) | ||
future.setComplete(value) | ||
else | ||
future.cancelSilently() | ||
} | ||
|
||
override fun resumeWithException(exception: Throwable) { | ||
if (asyncContext.isAlive() == null) { | ||
if (exception is Exception) | ||
future.setComplete(exception) | ||
else | ||
future.setComplete(Exception(exception)) | ||
} | ||
else { | ||
future.cancelSilently() | ||
} | ||
} | ||
} | ||
|
||
internal fun <T> async(ionContext: IonContext, block: suspend AsyncContext.() -> T): Future<T> { | ||
val future = SimpleFuture<T>() | ||
val asyncContext = AsyncContext(ionContext, future as SimpleFuture<Object>) | ||
val coroutineContext = StandaloneCoroutine<T>(asyncContext, future, EmptyCoroutineContext) | ||
block.startCoroutine(asyncContext, coroutineContext) | ||
return future | ||
} |
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
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
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
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
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
Oops, something went wrong.