Add EhConfig for EhRequest

This commit is contained in:
seven332 committed 2016-02-01 00:17:01 +08:00
1 parent 9925abf40f
commit 7b13da66af
3 files changed
+54 -25

No files matched your search

@@ -56,11 +56,11 @@ public class EhClient {
public void execute(EhRequest request) {
if (!request.isCancelled()) {
Task task = new Task(request.method, request.callback);
task.executeOnExecutor(mRequestThreadPool, request.args);
Task task = new Task(request.getMethod(), request.getCallback(), request.getEhConfig());
task.executeOnExecutor(mRequestThreadPool, request.getArgs());
request.task = task;
} else {
request.callback.onCancel();
request.getCallback().onCancel();
}
}
@@ -68,13 +68,15 @@ public class EhClient {
private int mMethod;
private Callback mCallback;
private EhConfig mEhConfig;
private Call mCall;
private boolean mStop;
public Task(int method, Callback callback) {
public Task(int method, Callback callback, EhConfig ehConfig) {
mMethod = method;
mCallback = callback;
mEhConfig = ehConfig;
}
public void stop() {
@@ -106,8 +108,8 @@ public class EhClient {
}
}
private Object signIn(Object... params) throws Exception {
Call call = EhEngine.prepareSignIn(mOkHttpClient, (String) params[0], (String) params[1]);
private Object signIn(EhConfig ehConfig, Object... params) throws Exception {
Call call = EhEngine.prepareSignIn(mOkHttpClient, ehConfig, (String) params[0], (String) params[1]);
if (!mStop) {
mCall = call;
return EhEngine.doSignIn(call);
@@ -116,8 +118,8 @@ public class EhClient {
}
}
private Object getGalleryList(Object... params) throws Exception {
Call call = EhEngine.prepareGetGalleryList(mOkHttpClient, (String) params[0]);
private Object getGalleryList(EhConfig ehConfig, Object... params) throws Exception {
Call call = EhEngine.prepareGetGalleryList(mOkHttpClient, ehConfig, (String) params[0]);
if (!mStop) {
mCall = call;
return EhEngine.doGetGalleryList(call);
@@ -126,8 +128,8 @@ public class EhClient {
}
}
private Object getGalleryDetail(Object... params) throws Exception {
Call call = EhEngine.prepareGetGalleryDetail(mOkHttpClient, (String) params[0]);
private Object getGalleryDetail(EhConfig ehConfig, Object... params) throws Exception {
Call call = EhEngine.prepareGetGalleryDetail(mOkHttpClient, ehConfig, (String) params[0]);
if (!mStop) {
mCall = call;
return EhEngine.doGetGalleryDetail(call);
@@ -141,11 +143,11 @@ public class EhClient {
try {
switch (mMethod) {
case METHOD_SIGN_IN:
return signIn(params);
return signIn(mEhConfig, params);
case METHOD_GET_GALLERY_LIST:
return getGalleryList(params);
return getGalleryList(mEhConfig, params);
case METHOD_GET_GALLERY_DETAIL:
return getGalleryDetail(params);
return getGalleryDetail(mEhConfig, params);
default:
return new IllegalStateException("Can't detect method " + mMethod);
}
@@ -63,7 +63,8 @@ public class EhEngine {
}
}
public static Call prepareSignIn(OkHttpClient okHttpClient, String username, String password) {
public static Call prepareSignIn(OkHttpClient okHttpClient,
EhConfig ehConfig, String username, String password) {
FormBody.Builder builder = new FormBody.Builder()
.add("UserName", username)
.add("PassWord", password)
@@ -73,7 +74,7 @@ public class EhEngine {
String url = EhUrl.API_SIGN_IN;
Log.d(TAG, url);
Request request = new EhRequestBuilder(url)
Request request = new EhRequestBuilder(url, ehConfig)
.post(builder.build())
.build();
return okHttpClient.newCall(request);
@@ -95,9 +96,10 @@ public class EhEngine {
}
}
public static Call prepareGetGalleryList(OkHttpClient okHttpClient, String url) {
public static Call prepareGetGalleryList(OkHttpClient okHttpClient,
EhConfig ehConfig, String url) {
Log.d(TAG, url);
Request request = new EhRequestBuilder(url).build();
Request request = new EhRequestBuilder(url, ehConfig).build();
return okHttpClient.newCall(request);
}
@@ -117,9 +119,10 @@ public class EhEngine {
}
}
public static Call prepareGetGalleryDetail(OkHttpClient okHttpClient, String url) {
public static Call prepareGetGalleryDetail(OkHttpClient okHttpClient,
EhConfig ehConfig, String url) {
Log.d(TAG, url);
Request request = new EhRequestBuilder(url).build();
Request request = new EhRequestBuilder(url, ehConfig).build();
return okHttpClient.newCall(request);
}
@@ -16,31 +16,55 @@
package com.hippo.ehviewer.client;
import com.hippo.ehviewer.Settings;
public class EhRequest {
int method;
Object[] args;
EhClient.Callback callback;
private int mMethod;
private Object[] mArgs;
private EhClient.Callback mCallback;
private EhConfig mEhConfig;
EhClient.Task task;
private boolean mCancel = false;
public EhRequest setMethod(int method) {
this.method = method;
mMethod = method;
return this;
}
public EhRequest setArgs(Object... args) {
this.args = args;
mArgs = args;
return this;
}
public EhRequest setCallback(EhClient.Callback callback) {
this.callback = callback;
mCallback = callback;
return this;
}
public EhRequest setEhConfig(EhConfig ehConfig) {
mEhConfig = ehConfig;
return this;
}
public int getMethod() {
return mMethod;
}
public Object[] getArgs() {
return mArgs;
}
public EhClient.Callback getCallback() {
return mCallback;
}
public EhConfig getEhConfig() {
return mEhConfig != null ? mEhConfig : Settings.getEhConfig();
}
public void cancel() {
if (!mCancel) {
mCancel = true;