Skip to content

Commit

Permalink
Merge pull request #9 from inaka/ramabit.library.improvements
Browse files Browse the repository at this point in the history
Ramabit.library.improvements
  • Loading branch information
tenmaster committed Jan 21, 2016
2 parents 2182955 + 6bd5e38 commit 629a851
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
/**
* Created by inaka on 1/20/16.
*/
public class KillerMainTest extends AndroidTestCase {
public class KillerTaskBlocksTest extends AndroidTestCase {

@UiThreadTest
public void testLibrary(){
new Example();
public void testLibraryWithBlocks() {
new ExampleAllTogether();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.inaka.killertask;

import android.test.AndroidTestCase;
import android.test.UiThreadTest;


/**
* Created by inaka on 1/21/16.
*/
public class KillerTaskFunctionsTest extends AndroidTestCase {

@UiThreadTest
public void testLibraryFunctionsRefactor() {
new ExampleFunctionsRefactor();
}

}
28 changes: 28 additions & 0 deletions library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.inaka.killertask

import android.util.Log
import java.util.concurrent.CountDownLatch

/**
* Created by inaka on 1/20/16.
*/
private class ExampleAllTogether {
val signal = CountDownLatch(1);

init {
KillerTask(
"test", {
result: String ->
Log.wtf("result", result)
signal.countDown()
}, {
e: Exception? ->
Log.wtf("result", e.toString())
e?.printStackTrace()
signal.countDown()
}).go()

signal.await()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import android.util.Log
import java.util.concurrent.CountDownLatch

/**
* Created by inaka on 1/20/16.
* Created by inaka on 1/21/16.
*/
private class Example {
private class ExampleFunctionsRefactor {
val signal = CountDownLatch(1);

val onSuccess: (String) -> Unit = {
Expand All @@ -15,25 +15,19 @@ private class Example {
signal.countDown()
}

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

init {
KillerTask(
doWork(),
WhenDone(mapOf(
WhenDone.success to onSuccess,
WhenDone.failed to onFailed
))).go()
KillerTask(doWork(), onSuccess, onFailed).go()
signal.await()
}

fun doWork(): String {
return "test"
}

}
8 changes: 4 additions & 4 deletions library/src/main/java/com/inaka/killertask/KillerTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.inaka.killertask
import android.os.AsyncTask
import android.util.Log

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

private var exception: Exception? = null

Expand All @@ -27,14 +27,14 @@ class KillerTask<T>(val action: T, val callback: WhenDone<Any>) : AsyncTask<Void
if (!isCancelled) {
if (exception != null) {
Log.wtf(TAG, "Failure with Exception")
run { callback.failure(exception) }
run { onFailed(exception) }
} else {
Log.wtf(TAG, "Success")
run { callback.success(result as Any) }
run { onSuccess(result) }
}
} else {
Log.wtf(TAG, "Failure with RuntimeException caused by task cancelled")
run { callback.failure(RuntimeException("Task was cancelled")) }
run { onFailed(RuntimeException("Task was cancelled")) }
}
}

Expand Down
13 changes: 0 additions & 13 deletions library/src/main/java/com/inaka/killertask/WhenDone.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.inaka.killertask

import android.util.Log
import org.junit.Test

/**
Expand All @@ -9,13 +8,20 @@ import org.junit.Test
class KillerMainTest_KotlinVersion {
@Test
fun createKillerTask() {
KillerTask(doWork(), onSuccess, onFailed).go()

KillerTask(
doWork(),
WhenDone(mapOf(
WhenDone.success to onSuccess,
WhenDone.failed to 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 {
Expand All @@ -27,9 +33,10 @@ class KillerMainTest_KotlinVersion {
assert(result.equals("test"))
}

val onFailed: (Exception) -> Unit = {
e: Exception ->
e.printStackTrace()
val onFailed: (Exception?) -> Unit = {
e: Exception? ->
e?.printStackTrace()
print(e?.message)
}

}

0 comments on commit 629a851

Please sign in to comment.