Skip to content

Commit

Permalink
Merge pull request #13 from inaka/ramabit.library.v1.1
Browse files Browse the repository at this point in the history
Ramabit.library.v1.1
  • Loading branch information
tenmaster committed Jan 22, 2016
2 parents 15d0617 + 82935f6 commit 21f917c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 31 deletions.
3 changes: 3 additions & 0 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inaka.killertask">

<!--Uncomment this if you want to run the example of internet connection-->
<!--<uses-permission android:name="android.permission.INTERNET" />-->

</manifest>
11 changes: 7 additions & 4 deletions library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ private class ExampleAllTogether {

init {
KillerTask(
"test", {
result: String ->
// task
{ "test" },
// onSuccess
{ result: String ->
Log.wtf("result", result)
signal.countDown()
}, {
e: Exception? ->
},
// onFailed
{ e: Exception? ->
Log.wtf("result", e.toString())
e?.printStackTrace()
signal.countDown()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.inaka.killertask

import android.util.Log
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLConnection
import java.util.concurrent.CountDownLatch

/**
Expand All @@ -9,25 +12,43 @@ import java.util.concurrent.CountDownLatch
private class ExampleFunctionsRefactor {
val signal = CountDownLatch(1);

// onSuccess function
val onSuccess: (String) -> Unit = {
result: String ->
Log.wtf("result", result)
Log.wtf("success result", result)
signal.countDown()
}

// onFailed function
val onFailed: (Exception?) -> Unit = {
e: Exception? ->
Log.wtf("result", e.toString())
Log.wtf("error result", e.toString())
e?.printStackTrace()
signal.countDown()
}

// task function
val doWork: () -> String = {
var connection: URLConnection? = null;

try {
var url = URL("https://api.github.com/gists")
connection = url.openConnection();
} catch (e: Exception) {
e.printStackTrace()
}

var httpConn = connection as HttpURLConnection;
httpConn.connectTimeout = 3000;
httpConn.readTimeout = 5000;

// return
httpConn.responseCode.toString() + " " + httpConn.responseMessage
}

init {
KillerTask(doWork(), onSuccess, onFailed).go()
KillerTask(doWork, onSuccess, onFailed).go()
signal.await()
}

fun doWork(): String {
return "test"
}
}
24 changes: 18 additions & 6 deletions library/src/main/java/com/inaka/killertask/KillerTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,57 @@ package com.inaka.killertask
import android.os.AsyncTask
import android.util.Log

class KillerTask<T>(val action: T, val onSuccess: (T) -> Any, val onFailed: (Exception?) -> Any) : AsyncTask<Void, Void, T>() {
class KillerTask<T>(val task: () -> T, val onSuccess: (T) -> Any, val onFailed: (Exception?) -> Any) : AsyncTask<Void, Void, T>() {

private var exception: Exception? = null

companion object {
private val TAG = "KillerTask"
}

/**
* Override AsyncTask's function doInBackground
*/
override fun doInBackground(vararg params: Void): T? {
try {
Log.wtf(TAG, "Enter to doInBackground")
return run { action }
return run { task() }
} catch (e: Exception) {
Log.wtf(TAG, "Error in background task")
exception = e
return null
}
}

/**
* Override AsyncTask's function onPostExecute
*/
override fun onPostExecute(result: T) {
Log.wtf(TAG, "Enter to onPostExecute")
if (!isCancelled) {
if (exception != null) {
if (!isCancelled) { // task not cancelled
if (exception != null) { // fail
Log.wtf(TAG, "Failure with Exception")
run { onFailed(exception) }
} else {
} else { // success
Log.wtf(TAG, "Success")
run { onSuccess(result) }
}
} else {
} else { // task cancelled
Log.wtf(TAG, "Failure with RuntimeException caused by task cancelled")
run { onFailed(RuntimeException("Task was cancelled")) }
}
}

/**
* Execute AsyncTask
*/
fun go() {
execute()
}

/**
* Cancel AsyncTask
*/
fun cancel() {
cancel(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,35 @@ import org.junit.Test
class KillerMainTest_KotlinVersion {
@Test
fun createKillerTask() {
KillerTask(doWork(), onSuccess, onFailed).go()

/*
That is the same as:
KillerTask("test", {
result: String ->
assert(result.equals("test"))
}, {
e: Exception? ->
e?.printStackTrace()
print(e?.message)
}).go()
KillerTask(doWork, onSuccess, onFailed).go()

/**
* That is the same as:
*
* KillerTask(
* { "test" },
* { result: String ->
* assert(result.equals("test"))
* },
* { e: Exception? ->
* e?.printStackTrace()
* print(e?.message)
* }).go()
*/
}

fun doWork(): String {
return "test"
// task function
val doWork: () -> String = {
"test"
}

// onSuccess function
val onSuccess: (String) -> Unit = {
result: String ->
assert(result.equals("test"))
}

// onFailed function
val onFailed: (Exception?) -> Unit = {
e: Exception? ->
e?.printStackTrace()
Expand Down

0 comments on commit 21f917c

Please sign in to comment.