Skip to content

Commit d0f32fd

Browse files
committed
Refactor async-task api for EasyExecutor
1 parent 9a677c3 commit d0f32fd

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

app/src/main/java/com/haoge/sample/easyandroid/activities/mvp/login/LoginConstracts.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.haoge.sample.easyandroid.activities.mvp.login
33
import com.haoge.easyandroid.easy.EasyExecutor
44
import com.haoge.easyandroid.mvp.MVPPresenter
55
import com.haoge.easyandroid.mvp.MVPView
6+
import java.util.concurrent.Callable
67

78
interface LoginView:MVPView {
89
fun loginSuccess()
@@ -14,19 +15,17 @@ class LoginPresenter(view: LoginView?) : MVPPresenter<LoginView>(view) {
1415

1516
fun login(username:String, password:String) {
1617
getView().showLoadingDialog()
17-
executor.async(
18-
{
19-
// 延时2秒。模拟网络操作
20-
Thread.sleep(2000)
21-
},
22-
{
23-
// 异步回调中,需先过滤view detach条件。
24-
if (isViewAttached().not()) return@async
18+
executor.asyncResult<String>({
19+
// 异步回调中,需先过滤view detach条件。
20+
if (isViewAttached().not()) return@asyncResult
2521

26-
getView().hideLoadingDialog()
27-
getView().loginSuccess()
22+
getView().hideLoadingDialog()
23+
getView().loginSuccess()
24+
}).asyncTask({
25+
// 延时2秒。模拟网络操作
26+
Thread.sleep(2000)
27+
"Hello World"
28+
})
2829

29-
}
30-
)
3130
}
3231
}

utils/src/main/java/com/haoge/easyandroid/easy/EasyExecutor.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ class EasyExecutor private constructor(val executor: ExecutorService,
106106
}
107107
}
108108

109+
fun <T> asyncResult(result:RESULT<T>?) = AsyncTask(result, this)
110+
109111
/** 执行异步回调任务*/
112+
@Deprecated("This is deprecated!", ReplaceWith("this.asyncResult(result).asyncTask(task)"))
110113
fun <T> async(task:ASYNC_TASK<T>, result:RESULT<T>? = null) {
111114
postDelay {
112115
executor.execute(TaskWrapper(
@@ -119,6 +122,7 @@ class EasyExecutor private constructor(val executor: ExecutorService,
119122
}
120123

121124
/** 执行异步回调任务*/
125+
@Deprecated("This is deprecated!", ReplaceWith("this.asyncResult(task).asyncTask(task)"))
122126
fun <T> async(task:Callable<T>, result: RESULT<T>? = null) {
123127
async({
124128
task.call()
@@ -280,6 +284,12 @@ class EasyExecutor private constructor(val executor: ExecutorService,
280284
}
281285
}
282286
}
287+
288+
class AsyncTask<in T>(val result: RESULT<T>?, val executor: EasyExecutor) {
289+
fun asyncTask(task:ASYNC_TASK<T>) {
290+
executor.async(task, result)
291+
}
292+
}
283293
}
284294

285295
class Notifier(private val deliver: Executor, private val progress: PROGRESS?) {

0 commit comments

Comments
 (0)