Skip to content

Commit 64e126a

Browse files
committed
Parse on background
1 parent 396b904 commit 64e126a

1 file changed

Lines changed: 55 additions & 11 deletions

File tree

library/src/main/java/com/proxerme/library/connection/ProxerConnection.java

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.proxerme.library.connection;
22

3+
import android.os.Handler;
34
import android.support.annotation.CheckResult;
45
import android.support.annotation.IntRange;
56
import android.support.annotation.NonNull;
@@ -22,6 +23,7 @@
2223
import org.json.JSONException;
2324
import org.json.JSONObject;
2425

26+
import java.util.LinkedList;
2527
import java.util.List;
2628

2729
import static com.proxerme.library.connection.ProxerException.ErrorCodes.PROXER;
@@ -44,6 +46,8 @@ public class ProxerConnection {
4446
private static final String RESPONSE_ERROR_MESSAGE = "message";
4547
private static final String VALIDATOR_ID = "default-validator";
4648

49+
private static LinkedList<Thread> parseThreads = new LinkedList<>();
50+
4751
/**
4852
* Entry point to load News of a specified page.
4953
*
@@ -153,6 +157,11 @@ public String id() {
153157
* Activity.
154158
*/
155159
public static void cleanup() {
160+
for (Thread parseThread : parseThreads) {
161+
parseThread.interrupt();
162+
}
163+
164+
parseThreads.clear();
156165
Bridge.cleanup();
157166
}
158167

@@ -165,20 +174,23 @@ public static void cleanup() {
165174
public interface ResultCallback<T> {
166175
/**
167176
* A callback method, called if the request was successful.
177+
*
168178
* @param result The result of the specific request.
169179
*/
170180
void onResult(T result);
171181

172182
/**
173183
* A callback method, called if an error occurred during the request.
174-
* @see ProxerException
184+
*
175185
* @param exception The Exception that occurred.
186+
* @see ProxerException
176187
*/
177188
void onError(@NonNull ProxerException exception);
178189
}
179190

180191
/**
181192
* An abstract representation of a request. All requests to the API are made through this class.
193+
*
182194
* @param <T> The type of result, the inheriting request will return.
183195
*/
184196
public static abstract class ProxerRequest<T> {
@@ -187,6 +199,7 @@ public static abstract class ProxerRequest<T> {
187199
* Builds the request, to be used in the
188200
* {@link #execute(ResultCallback)} or
189201
* {@link #executeSynchronized()} method.
202+
*
190203
* @param bridge The Bridge instance to build the request with.
191204
* @return The {@link RequestBuilder} to use for further invocations.
192205
*/
@@ -195,29 +208,58 @@ public static abstract class ProxerRequest<T> {
195208

196209
/**
197210
* Returns the {@link ProxerTag} of this request.
211+
*
198212
* @return The {@link ProxerTag}.
199213
*/
200214
@ConnectionTag
201215
protected abstract int getTag();
202216

203217
/**
204218
* Asynchronously executes this request.
205-
* @see #executeSynchronized()
219+
*
206220
* @param callback The callback for notifications about the Result.
221+
* @see #executeSynchronized()
207222
*/
208223
@RequiresPermission(android.Manifest.permission.INTERNET)
209224
public final void execute(@NonNull final ResultCallback<T> callback) {
210225
buildRequest(Bridge.client()).tag(getTag()).request(new Callback() {
211226
@Override
212-
public void response(Request request, Response response, BridgeException exception) {
227+
public void response(Request request, final Response response, BridgeException exception) {
213228
if (exception == null) {
214-
try {
215-
callback.onResult(parse(response.asJsonObject()));
216-
} catch (JSONException e) {
217-
callback.onError(ErrorHandler.handleException(e));
218-
} catch (BridgeException e) {
219-
callback.onError(ErrorHandler.handleException(e));
220-
}
229+
Thread parseThread = new Thread(new Runnable() {
230+
@Override
231+
public void run() {
232+
Handler handler = new Handler();
233+
234+
try {
235+
final T result = parse(response.asJsonObject());
236+
237+
handler.post(new Runnable() {
238+
@Override
239+
public void run() {
240+
callback.onResult(result);
241+
}
242+
});
243+
} catch (final JSONException e) {
244+
handler.post(new Runnable() {
245+
@Override
246+
public void run() {
247+
callback.onError(ErrorHandler.handleException(e));
248+
}
249+
});
250+
} catch (final BridgeException e) {
251+
handler.post(new Runnable() {
252+
@Override
253+
public void run() {
254+
callback.onError(ErrorHandler.handleException(e));
255+
}
256+
});
257+
}
258+
}
259+
});
260+
261+
parseThreads.add(parseThread);
262+
parseThread.start();
221263
} else {
222264
if (exception.reason() != BridgeException.REASON_REQUEST_CANCELLED) {
223265
callback.onError(ErrorHandler.handleException(exception));
@@ -229,9 +271,10 @@ public void response(Request request, Response response, BridgeException excepti
229271

230272
/**
231273
* Synchronously executes this request.
232-
* @see #execute(ResultCallback)
274+
*
233275
* @return The result, specified by this class.
234276
* @throws ProxerException An Exception, which might occur, while executing the request.
277+
* @see #execute(ResultCallback)
235278
*/
236279
@WorkerThread
237280
@RequiresPermission(android.Manifest.permission.INTERNET)
@@ -249,6 +292,7 @@ public final T executeSynchronized() throws ProxerException {
249292

250293
/**
251294
* Parses the raw response of the server.
295+
*
252296
* @param response The response in JSON format.
253297
* @return The specific type of result of this class.
254298
* @throws JSONException An Exception, which might occur while parsing.

0 commit comments

Comments
 (0)