Skip to content

Commit 4d7579f

Browse files
committed
Use EventBus instead of callbacks
1 parent 670cc93 commit 4d7579f

2 files changed

Lines changed: 11 additions & 49 deletions

File tree

library/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies {
2525
compile fileTree(dir: 'libs', include: ['*.jar'])
2626
compile 'com.android.support:support-annotations:23.1.1'
2727
compile 'com.github.afollestad:bridge:1.6.4@aar'
28+
compile 'de.greenrobot:eventbus:2.4.0'
2829
}
2930

3031
// build a jar with source files

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

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

3-
import android.os.Handler;
4-
import android.os.Looper;
53
import android.support.annotation.CheckResult;
64
import android.support.annotation.IntRange;
75
import android.support.annotation.NonNull;
@@ -27,6 +25,8 @@
2725
import java.util.LinkedList;
2826
import java.util.List;
2927

28+
import de.greenrobot.event.EventBus;
29+
3030
import static com.proxerme.library.connection.ProxerException.ErrorCodes.PROXER;
3131
import static com.proxerme.library.connection.ProxerException.ErrorCodes.UNKNOWN;
3232
import static com.proxerme.library.connection.ProxerTag.CONFERENCES;
@@ -166,29 +166,6 @@ public static void cleanup() {
166166
Bridge.cleanup();
167167
}
168168

169-
/**
170-
* An abstract representation of a callback, passed to the
171-
* {@link ProxerRequest#execute(ResultCallback)} method.
172-
*
173-
* @param <T> The generic parameter.
174-
*/
175-
public interface ResultCallback<T> {
176-
/**
177-
* A callback method, called if the request was successful.
178-
*
179-
* @param result The result of the specific request.
180-
*/
181-
void onResult(T result);
182-
183-
/**
184-
* A callback method, called if an error occurred during the request.
185-
*
186-
* @param exception The Exception that occurred.
187-
* @see ProxerException
188-
*/
189-
void onError(@NonNull ProxerException exception);
190-
}
191-
192169
/**
193170
* An abstract representation of a request. All requests to the API are made through this class.
194171
*
@@ -198,7 +175,7 @@ public static abstract class ProxerRequest<T> {
198175

199176
/**
200177
* Builds the request, to be used in the
201-
* {@link #execute(ResultCallback)} or
178+
* {@link #execute()} or
202179
* {@link #executeSynchronized()} method.
203180
*
204181
* @param bridge The Bridge instance to build the request with.
@@ -216,13 +193,12 @@ public static abstract class ProxerRequest<T> {
216193
protected abstract int getTag();
217194

218195
/**
219-
* Asynchronously executes this request.
196+
* Asynchronously executes this request. All results are passed back through the EventBus.
220197
*
221-
* @param callback The callback for notifications about the Result.
222198
* @see #executeSynchronized()
223199
*/
224200
@RequiresPermission(android.Manifest.permission.INTERNET)
225-
public final void execute(@NonNull final ResultCallback<T> callback) {
201+
public final void execute() {
226202
buildRequest(Bridge.client()).tag(getTag()).request(new Callback() {
227203
@Override
228204
public void response(Request request, final Response response, BridgeException exception) {
@@ -233,26 +209,11 @@ public void run() {
233209
try {
234210
final T result = parse(response.asJsonObject());
235211

236-
new Handler(Looper.getMainLooper()).post(new Runnable() {
237-
@Override
238-
public void run() {
239-
callback.onResult(result);
240-
}
241-
});
212+
EventBus.getDefault().post(result);
242213
} catch (final JSONException e) {
243-
new Handler(Looper.getMainLooper()).post(new Runnable() {
244-
@Override
245-
public void run() {
246-
callback.onError(ErrorHandler.handleException(e));
247-
}
248-
});
214+
EventBus.getDefault().post(ErrorHandler.handleException(e));
249215
} catch (final BridgeException e) {
250-
new Handler(Looper.getMainLooper()).post(new Runnable() {
251-
@Override
252-
public void run() {
253-
callback.onError(ErrorHandler.handleException(e));
254-
}
255-
});
216+
EventBus.getDefault().post(ErrorHandler.handleException(e));
256217
}
257218
}
258219
});
@@ -261,7 +222,7 @@ public void run() {
261222
parseThread.start();
262223
} else {
263224
if (exception.reason() != BridgeException.REASON_REQUEST_CANCELLED) {
264-
callback.onError(ErrorHandler.handleException(exception));
225+
EventBus.getDefault().post(ErrorHandler.handleException(exception));
265226
}
266227
}
267228
}
@@ -273,7 +234,7 @@ public void run() {
273234
*
274235
* @return The result, specified by this class.
275236
* @throws ProxerException An Exception, which might occur, while executing the request.
276-
* @see #execute(ResultCallback)
237+
* @see #execute()
277238
*/
278239
@WorkerThread
279240
@RequiresPermission(android.Manifest.permission.INTERNET)

0 commit comments

Comments
 (0)