Skip to content

Commit 21f917c

Browse files
committed
Merge pull request #13 from inaka/ramabit.library.v1.1
Ramabit.library.v1.1
2 parents 15d0617 + 82935f6 commit 21f917c

File tree

5 files changed

+74
-31
lines changed

5 files changed

+74
-31
lines changed

library/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="com.inaka.killertask">
33

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

library/src/main/java/com/inaka/killertask/ExampleAllTogether.kt

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ private class ExampleAllTogether {
1111

1212
init {
1313
KillerTask(
14-
"test", {
15-
result: String ->
14+
// task
15+
{ "test" },
16+
// onSuccess
17+
{ result: String ->
1618
Log.wtf("result", result)
1719
signal.countDown()
18-
}, {
19-
e: Exception? ->
20+
},
21+
// onFailed
22+
{ e: Exception? ->
2023
Log.wtf("result", e.toString())
2124
e?.printStackTrace()
2225
signal.countDown()
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.inaka.killertask
22

33
import android.util.Log
4+
import java.net.HttpURLConnection
5+
import java.net.URL
6+
import java.net.URLConnection
47
import java.util.concurrent.CountDownLatch
58

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

15+
// onSuccess function
1216
val onSuccess: (String) -> Unit = {
1317
result: String ->
14-
Log.wtf("result", result)
18+
Log.wtf("success result", result)
1519
signal.countDown()
1620
}
1721

22+
// onFailed function
1823
val onFailed: (Exception?) -> Unit = {
1924
e: Exception? ->
20-
Log.wtf("result", e.toString())
25+
Log.wtf("error result", e.toString())
2126
e?.printStackTrace()
2227
signal.countDown()
2328
}
2429

30+
// task function
31+
val doWork: () -> String = {
32+
var connection: URLConnection? = null;
33+
34+
try {
35+
var url = URL("https://api.github.com/gists")
36+
connection = url.openConnection();
37+
} catch (e: Exception) {
38+
e.printStackTrace()
39+
}
40+
41+
var httpConn = connection as HttpURLConnection;
42+
httpConn.connectTimeout = 3000;
43+
httpConn.readTimeout = 5000;
44+
45+
// return
46+
httpConn.responseCode.toString() + " " + httpConn.responseMessage
47+
}
48+
2549
init {
26-
KillerTask(doWork(), onSuccess, onFailed).go()
50+
KillerTask(doWork, onSuccess, onFailed).go()
2751
signal.await()
2852
}
2953

30-
fun doWork(): String {
31-
return "test"
32-
}
3354
}

library/src/main/java/com/inaka/killertask/KillerTask.kt

+18-6
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,57 @@ package com.inaka.killertask
33
import android.os.AsyncTask
44
import android.util.Log
55

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

88
private var exception: Exception? = null
99

1010
companion object {
1111
private val TAG = "KillerTask"
1212
}
1313

14+
/**
15+
* Override AsyncTask's function doInBackground
16+
*/
1417
override fun doInBackground(vararg params: Void): T? {
1518
try {
1619
Log.wtf(TAG, "Enter to doInBackground")
17-
return run { action }
20+
return run { task() }
1821
} catch (e: Exception) {
1922
Log.wtf(TAG, "Error in background task")
2023
exception = e
2124
return null
2225
}
2326
}
2427

28+
/**
29+
* Override AsyncTask's function onPostExecute
30+
*/
2531
override fun onPostExecute(result: T) {
2632
Log.wtf(TAG, "Enter to onPostExecute")
27-
if (!isCancelled) {
28-
if (exception != null) {
33+
if (!isCancelled) { // task not cancelled
34+
if (exception != null) { // fail
2935
Log.wtf(TAG, "Failure with Exception")
3036
run { onFailed(exception) }
31-
} else {
37+
} else { // success
3238
Log.wtf(TAG, "Success")
3339
run { onSuccess(result) }
3440
}
35-
} else {
41+
} else { // task cancelled
3642
Log.wtf(TAG, "Failure with RuntimeException caused by task cancelled")
3743
run { onFailed(RuntimeException("Task was cancelled")) }
3844
}
3945
}
4046

47+
/**
48+
* Execute AsyncTask
49+
*/
4150
fun go() {
4251
execute()
4352
}
4453

54+
/**
55+
* Cancel AsyncTask
56+
*/
4557
fun cancel() {
4658
cancel(true)
4759
}

library/src/test/java/com/inaka/killertask/KillerMainTest_KotlinVersion.kt

+19-15
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,35 @@ import org.junit.Test
88
class KillerMainTest_KotlinVersion {
99
@Test
1010
fun createKillerTask() {
11-
KillerTask(doWork(), onSuccess, onFailed).go()
12-
13-
/*
14-
That is the same as:
15-
16-
KillerTask("test", {
17-
result: String ->
18-
assert(result.equals("test"))
19-
}, {
20-
e: Exception? ->
21-
e?.printStackTrace()
22-
print(e?.message)
23-
}).go()
11+
KillerTask(doWork, onSuccess, onFailed).go()
12+
13+
/**
14+
* That is the same as:
15+
*
16+
* KillerTask(
17+
* { "test" },
18+
* { result: String ->
19+
* assert(result.equals("test"))
20+
* },
21+
* { e: Exception? ->
22+
* e?.printStackTrace()
23+
* print(e?.message)
24+
* }).go()
2425
*/
2526
}
2627

27-
fun doWork(): String {
28-
return "test"
28+
// task function
29+
val doWork: () -> String = {
30+
"test"
2931
}
3032

33+
// onSuccess function
3134
val onSuccess: (String) -> Unit = {
3235
result: String ->
3336
assert(result.equals("test"))
3437
}
3538

39+
// onFailed function
3640
val onFailed: (Exception?) -> Unit = {
3741
e: Exception? ->
3842
e?.printStackTrace()

0 commit comments

Comments
 (0)