Skip to content

Commit c0cd2f5

Browse files
author
zsy
committed
tasks
1 parent ad41a43 commit c0cd2f5

File tree

31 files changed

+958
-184
lines changed

31 files changed

+958
-184
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,16 @@ Activity负责调度,代码如下
730730
//taskHelper.destory();//执行释放操作
731731
//taskHelper.cancelAll();//取消全部
732732

733+
## 4.结构
734+
task 任务,子类IAsyncTask异步任务,ITask同步任务
735+
ICallback task开始、progress、结果的事件回调
736+
ResponseSender 用于task执行的结果通知给ICallback
737+
TaskExecutor task执行者,实现类SyncDataSourceExecutor,AsyncDataSourceExecutor,SyncTaskExecutor,AsyncTaskExecutor
738+
TaskExecutors 负责创建TaskExecutor
739+
TaskHelper 执行多个TaskExecutor
740+
741+
taskHelper 调用TaskExecutors创建TaskExecutor,TaskExecutor执行task,通过ResponseSender通知ICallback
742+
733743
## 注意:
734744
1.权限:
735745
android.permission.INTERNET

app/src/main/java/com/shizhefei/test/controllers/MainActivity.java

+53-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@
2121
import android.util.Log;
2222
import android.view.View;
2323

24+
import com.shizhefei.mvc.RequestHandle;
25+
import com.shizhefei.mvc.ResponseSender;
2426
import com.shizhefei.task.Code;
27+
import com.shizhefei.task.IAsyncTask;
2528
import com.shizhefei.task.ICallback;
2629
import com.shizhefei.task.TaskHandle;
2730
import com.shizhefei.task.TaskHelper;
31+
import com.shizhefei.task.function.Func1;
2832
import com.shizhefei.task.imp.MemoryCacheStore;
33+
import com.shizhefei.task.tasks.LinkTask;
34+
import com.shizhefei.task.tasks.Tasks;
2935
import com.shizhefei.test.controllers.mvchelpers.CoolActivity;
3036
import com.shizhefei.test.controllers.mvchelpers.NormalActivity;
3137
import com.shizhefei.test.controllers.mvchelpers.PullrefshActivity;
@@ -73,6 +79,7 @@ public void onPostExecute(Object task, Code code, Exception exception, Object Ob
7379
Log.d("vvvv", "registCallBak onPostExecute task:" + task + " code:" + code);
7480
}
7581
});
82+
7683
}
7784

7885
@Override
@@ -87,7 +94,51 @@ protected void onDestroy() {
8794
* @param view
8895
*/
8996
public void onClickTestCase(View view) {
90-
ProxyActivity.startActivity(this, TestCaseFragment.class, "测试用例");
97+
98+
IAsyncTask<String> task = Tasks.retry(new IAsyncTask<String>() {
99+
private int times;
100+
101+
@Override
102+
public RequestHandle execute(ResponseSender<String> sender) throws Exception {
103+
if (times < 3) {
104+
throw new Exception("异常times:" + times++);
105+
}
106+
sender.sendData("ok");
107+
return null;
108+
}
109+
}).concatWith(Tasks.async(new LoginTask("", "")))
110+
.concatWith(Tasks.just(""))
111+
.concatMap(new Func1<String, IAsyncTask<String>>() {
112+
@Override
113+
public IAsyncTask<String> call(String data) throws Exception {
114+
return null;
115+
}
116+
});
117+
118+
LinkTask<String> stringLinkTask = Tasks.create(Tasks.just("data")).concatWith(Tasks.just("data2")).concatWith(new IAsyncTask<String>() {
119+
@Override
120+
public RequestHandle execute(ResponseSender<String> sender) throws Exception {
121+
throw new RuntimeException("test");
122+
}
123+
});
124+
125+
new TaskHelper<>().execute(task, new ICallback<String>() {
126+
@Override
127+
public void onPreExecute(Object task) {
128+
129+
}
130+
131+
@Override
132+
public void onProgress(Object task, int percent, long current, long total, Object extraData) {
133+
134+
}
135+
136+
@Override
137+
public void onPostExecute(Object task, Code code, Exception exception, String s) {
138+
Log.d("pppp", "task:" + task + " code:" + code + " ex:" + exception + " s:" + s);
139+
}
140+
});
141+
// ProxyActivity.startActivity(this, TestCaseFragment.class, "测试用例");
91142
}
92143

93144
/**
@@ -214,7 +265,7 @@ public void onClickTaskDemo(View view) {
214265
* @param view
215266
*/
216267
public void onClickTaskOpDemo(View view) {
217-
startActivity(new Intent(getApplicationContext(), TaskOpActivity.class));
268+
startActivity(new Intent(getApplicationContext(), TaskOpActivity.class));
218269
}
219270

220271

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ buildscript {
88
// }
99
}
1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:2.2.3'
11+
classpath 'com.android.tools.build:gradle:2.3.3'
1212
classpath 'com.novoda:bintray-release:0.3.4'
1313
// classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.9"
1414
// NOTE: Do not place your application dependencies here; they belong

mvccoolhelper/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ apply plugin: 'com.android.library'
22
apply plugin: 'com.novoda.bintray-release'//添加
33

44
android {
5-
compileSdkVersion 23
6-
buildToolsVersion "23.0.3"
5+
compileSdkVersion 25
6+
buildToolsVersion "25.0.0"
77

88
defaultConfig {
99
minSdkVersion 9

mvchelper_library/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apply plugin: 'com.android.library'
22
apply plugin: 'com.novoda.bintray-release'//添加
33
android {
4-
compileSdkVersion 24
5-
buildToolsVersion "23.0.3"
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.0"
66

77
defaultConfig {
88
minSdkVersion 9

mvchelper_library/src/main/java/com/shizhefei/task/TaskExecutors.java

+10-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.os.Build;
55
import android.os.Handler;
66
import android.os.Looper;
7-
import android.util.Log;
87

98
import com.shizhefei.mvc.IAsyncDataSource;
109
import com.shizhefei.mvc.IDataSource;
@@ -131,11 +130,6 @@ protected DATA doInBackground(Object... params) {
131130
try {
132131
return executeImp(responseSender);
133132
} catch (Exception e) {
134-
if (e instanceof InterruptedException) {
135-
Log.d("TaskHelper", realTask.toString() + e);
136-
} else {
137-
e.printStackTrace();
138-
}
139133
responseSender.sendError(e);
140134
}
141135
return null;
@@ -388,11 +382,11 @@ public boolean isRunning() {
388382
private void onPreExecute() {
389383
if (Looper.myLooper() != Looper.getMainLooper()) {
390384
handler.post(new Runnable() {
391-
@Override
392-
public void run() {
393-
onPreExecuteMainThread();
394-
}
395-
});
385+
@Override
386+
public void run() {
387+
onPreExecuteMainThread();
388+
}
389+
});
396390
} else {
397391
onPreExecuteMainThread();
398392
}
@@ -414,11 +408,11 @@ public void run() {
414408
private void onPostExecute(final Code code, final Exception exception, final DATA data) {
415409
if (Looper.myLooper() != Looper.getMainLooper()) {
416410
handler.post(new Runnable() {
417-
@Override
418-
public void run() {
419-
onPostExecuteMainThread(code, exception, data);
420-
}
421-
});
411+
@Override
412+
public void run() {
413+
onPostExecuteMainThread(code, exception, data);
414+
}
415+
});
422416
} else {
423417
onPostExecuteMainThread(code, exception, data);
424418
}

0 commit comments

Comments
 (0)