From 344dd97c732c464bcdf66869c4b11ffbf6a76c85 Mon Sep 17 00:00:00 2001 From: ramabit Date: Fri, 22 Jan 2016 11:22:42 -0300 Subject: [PATCH 1/4] #11 v1.1 with improvements --- library/src/main/AndroidManifest.xml | 3 ++ .../inaka/killertask/ExampleAllTogether.kt | 8 ++--- .../killertask/ExampleFunctionsRefactor.kt | 30 +++++++++++++++---- .../java/com/inaka/killertask/KillerTask.kt | 4 +-- .../KillerMainTest_KotlinVersion.kt | 8 ++--- 5 files changed, 37 insertions(+), 16 deletions(-) diff --git a/library/src/main/AndroidManifest.xml b/library/src/main/AndroidManifest.xml index 7377946..2df9c5b 100644 --- a/library/src/main/AndroidManifest.xml +++ b/library/src/main/AndroidManifest.xml @@ -1,4 +1,7 @@ + + + diff --git a/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt b/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt index 0fb517b..6e9eedf 100644 --- a/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt +++ b/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt @@ -11,12 +11,12 @@ private class ExampleAllTogether { init { KillerTask( - "test", { - result: String -> + { "test" }, + { result: String -> Log.wtf("result", result) signal.countDown() - }, { - e: Exception? -> + }, + { e: Exception? -> Log.wtf("result", e.toString()) e?.printStackTrace() signal.countDown() diff --git a/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt b/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt index 7f63a27..b07e71f 100644 --- a/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt +++ b/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt @@ -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 /** @@ -11,23 +14,38 @@ private class ExampleFunctionsRefactor { val onSuccess: (String) -> Unit = { result: String -> - Log.wtf("result", result) + Log.wtf("success result", result) signal.countDown() } val onFailed: (Exception?) -> Unit = { e: Exception? -> - Log.wtf("result", e.toString()) + Log.wtf("error result", e.toString()) e?.printStackTrace() signal.countDown() } + val doWork: () -> String = { + var connection: URLConnection? = null; + + try{ + var url = URL("https://inaka.net/blog") + connection = url.openConnection(); + }catch (e: Exception){ + e.printStackTrace() + } + + var httpConn = connection as HttpURLConnection; + httpConn.connectTimeout = 3000; + httpConn.readTimeout = 5000; + + // return + httpConn.responseCode.toString() + } + init { - KillerTask(doWork(), onSuccess, onFailed).go() + KillerTask(doWork, onSuccess, onFailed).go() signal.await() } - fun doWork(): String { - return "test" - } } \ No newline at end of file diff --git a/library/src/main/java/com/inaka/killertask/KillerTask.kt b/library/src/main/java/com/inaka/killertask/KillerTask.kt index c94a78a..aad2839 100644 --- a/library/src/main/java/com/inaka/killertask/KillerTask.kt +++ b/library/src/main/java/com/inaka/killertask/KillerTask.kt @@ -3,7 +3,7 @@ package com.inaka.killertask import android.os.AsyncTask import android.util.Log -class KillerTask(val action: T, val onSuccess: (T) -> Any, val onFailed: (Exception?) -> Any) : AsyncTask() { +class KillerTask(val action: () -> T, val onSuccess: (T) -> Any, val onFailed: (Exception?) -> Any) : AsyncTask() { private var exception: Exception? = null @@ -14,7 +14,7 @@ class KillerTask(val action: T, val onSuccess: (T) -> Any, val onFailed: (Exc override fun doInBackground(vararg params: Void): T? { try { Log.wtf(TAG, "Enter to doInBackground") - return run { action } + return run { action() } } catch (e: Exception) { Log.wtf(TAG, "Error in background task") exception = e diff --git a/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt b/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt index 61c1d4e..8b92071 100644 --- a/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt +++ b/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt @@ -8,12 +8,12 @@ import org.junit.Test class KillerMainTest_KotlinVersion { @Test fun createKillerTask() { - KillerTask(doWork(), onSuccess, onFailed).go() + KillerTask(doWork, onSuccess, onFailed).go() /* That is the same as: - KillerTask("test", { + KillerTask({ "test" }, { result: String -> assert(result.equals("test")) }, { @@ -24,8 +24,8 @@ class KillerMainTest_KotlinVersion { */ } - fun doWork(): String { - return "test" + val doWork: () -> String = { + "test" } val onSuccess: (String) -> Unit = { From 0e01d17e8daa7fdc88275cb8d9cc555948bf35f7 Mon Sep 17 00:00:00 2001 From: ramabit Date: Fri, 22 Jan 2016 12:22:56 -0300 Subject: [PATCH 2/4] refactoring and adding comments / documentation --- .../inaka/killertask/ExampleAllTogether.kt | 3 +++ .../killertask/ExampleFunctionsRefactor.kt | 7 +++-- .../java/com/inaka/killertask/KillerTask.kt | 18 +++++++++++-- .../KillerMainTest_KotlinVersion.kt | 26 +++++++++++-------- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt b/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt index 6e9eedf..98081f6 100644 --- a/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt +++ b/library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt @@ -11,11 +11,14 @@ private class ExampleAllTogether { init { KillerTask( + // task { "test" }, + // onSuccess { result: String -> Log.wtf("result", result) signal.countDown() }, + // onFailed { e: Exception? -> Log.wtf("result", e.toString()) e?.printStackTrace() diff --git a/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt b/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt index b07e71f..06a0138 100644 --- a/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt +++ b/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt @@ -12,12 +12,14 @@ import java.util.concurrent.CountDownLatch private class ExampleFunctionsRefactor { val signal = CountDownLatch(1); + // onSuccess function val onSuccess: (String) -> Unit = { result: String -> Log.wtf("success result", result) signal.countDown() } + // onFailed function val onFailed: (Exception?) -> Unit = { e: Exception? -> Log.wtf("error result", e.toString()) @@ -25,13 +27,14 @@ private class ExampleFunctionsRefactor { signal.countDown() } + // task function val doWork: () -> String = { var connection: URLConnection? = null; - try{ + try { var url = URL("https://inaka.net/blog") connection = url.openConnection(); - }catch (e: Exception){ + } catch (e: Exception) { e.printStackTrace() } diff --git a/library/src/main/java/com/inaka/killertask/KillerTask.kt b/library/src/main/java/com/inaka/killertask/KillerTask.kt index aad2839..288fd5c 100644 --- a/library/src/main/java/com/inaka/killertask/KillerTask.kt +++ b/library/src/main/java/com/inaka/killertask/KillerTask.kt @@ -3,7 +3,7 @@ package com.inaka.killertask import android.os.AsyncTask import android.util.Log -class KillerTask(val action: () -> T, val onSuccess: (T) -> Any, val onFailed: (Exception?) -> Any) : AsyncTask() { +class KillerTask(val task: () -> T, val onSuccess: (T) -> Any, val onFailed: (Exception?) -> Any) : AsyncTask() { private var exception: Exception? = null @@ -11,10 +11,13 @@ class KillerTask(val action: () -> T, val onSuccess: (T) -> Any, val onFailed 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 @@ -22,9 +25,13 @@ class KillerTask(val action: () -> T, val onSuccess: (T) -> Any, val onFailed } } + /** + * Override AsyncTask's function onPostExecute + */ override fun onPostExecute(result: T) { Log.wtf(TAG, "Enter to onPostExecute") if (!isCancelled) { + // task not cancelled if (exception != null) { Log.wtf(TAG, "Failure with Exception") run { onFailed(exception) } @@ -33,15 +40,22 @@ class KillerTask(val action: () -> T, val onSuccess: (T) -> Any, val onFailed run { onSuccess(result) } } } 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) } diff --git a/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt b/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt index 8b92071..99cf8b5 100644 --- a/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt +++ b/library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt @@ -10,29 +10,33 @@ class KillerMainTest_KotlinVersion { 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() + /** + * That is the same as: + * + * KillerTask( + * { "test" }, + * { result: String -> + * assert(result.equals("test")) + * }, + * { e: Exception? -> + * e?.printStackTrace() + * print(e?.message) + * }).go() */ } + // 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() From 00cd082bfc3b2125e77a73b74324f7581551389c Mon Sep 17 00:00:00 2001 From: ramabit Date: Fri, 22 Jan 2016 14:17:02 -0300 Subject: [PATCH 3/4] fix example --- .../java/com/inaka/killertask/ExampleFunctionsRefactor.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt b/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt index 06a0138..319eb3f 100644 --- a/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt +++ b/library/src/main/java/com/inaka/killertask/ExampleFunctionsRefactor.kt @@ -32,7 +32,7 @@ private class ExampleFunctionsRefactor { var connection: URLConnection? = null; try { - var url = URL("https://inaka.net/blog") + var url = URL("https://api.github.com/gists") connection = url.openConnection(); } catch (e: Exception) { e.printStackTrace() @@ -43,7 +43,7 @@ private class ExampleFunctionsRefactor { httpConn.readTimeout = 5000; // return - httpConn.responseCode.toString() + httpConn.responseCode.toString() + " " + httpConn.responseMessage } init { From 82935f688876aaba8a62834caf1bf37bf347224a Mon Sep 17 00:00:00 2001 From: ramabit Date: Fri, 22 Jan 2016 15:10:48 -0300 Subject: [PATCH 4/4] fix comments --- .../src/main/java/com/inaka/killertask/KillerTask.kt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/library/src/main/java/com/inaka/killertask/KillerTask.kt b/library/src/main/java/com/inaka/killertask/KillerTask.kt index 288fd5c..dff5dda 100644 --- a/library/src/main/java/com/inaka/killertask/KillerTask.kt +++ b/library/src/main/java/com/inaka/killertask/KillerTask.kt @@ -30,17 +30,15 @@ class KillerTask(val task: () -> T, val onSuccess: (T) -> Any, val onFailed: */ override fun onPostExecute(result: T) { Log.wtf(TAG, "Enter to onPostExecute") - if (!isCancelled) { - // task not cancelled - 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 { - // task cancelled + } else { // task cancelled Log.wtf(TAG, "Failure with RuntimeException caused by task cancelled") run { onFailed(RuntimeException("Task was cancelled")) } }