11package com .proxerme .library .connection ;
22
3+ import android .os .Handler ;
4+ import android .os .Looper ;
35import android .support .annotation .CheckResult ;
46import android .support .annotation .IntRange ;
57import android .support .annotation .NonNull ;
2527import java .util .LinkedList ;
2628import java .util .List ;
2729
28- import de .greenrobot .event .EventBus ;
29-
3030import static com .proxerme .library .connection .ProxerException .ErrorCodes .PROXER ;
3131import static com .proxerme .library .connection .ProxerException .ErrorCodes .UNKNOWN ;
3232import static com .proxerme .library .connection .ProxerTag .CONFERENCES ;
@@ -166,6 +166,29 @@ 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+
169192 /**
170193 * An abstract representation of a request. All requests to the API are made through this class.
171194 *
@@ -175,7 +198,7 @@ public static abstract class ProxerRequest<T> {
175198
176199 /**
177200 * Builds the request, to be used in the
178- * {@link #execute()} or
201+ * {@link #execute(ResultCallback )} or
179202 * {@link #executeSynchronized()} method.
180203 *
181204 * @param bridge The Bridge instance to build the request with.
@@ -193,12 +216,13 @@ public static abstract class ProxerRequest<T> {
193216 protected abstract int getTag ();
194217
195218 /**
196- * Asynchronously executes this request. All results are passed back through the EventBus.
219+ * Asynchronously executes this request.
197220 *
221+ * @param callback The callback for notifications about the Result.
198222 * @see #executeSynchronized()
199223 */
200224 @ RequiresPermission (android .Manifest .permission .INTERNET )
201- public final void execute () {
225+ public final void execute (@ NonNull final ResultCallback < T > callback ) {
202226 buildRequest (Bridge .client ()).tag (getTag ()).request (new Callback () {
203227 @ Override
204228 public void response (Request request , final Response response , BridgeException exception ) {
@@ -209,11 +233,26 @@ public void run() {
209233 try {
210234 final T result = parse (response .asJsonObject ());
211235
212- EventBus .getDefault ().post (result );
236+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
237+ @ Override
238+ public void run () {
239+ callback .onResult (result );
240+ }
241+ });
213242 } catch (final JSONException e ) {
214- EventBus .getDefault ().post (ErrorHandler .handleException (e ));
243+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
244+ @ Override
245+ public void run () {
246+ callback .onError (ErrorHandler .handleException (e ));
247+ }
248+ });
215249 } catch (final BridgeException e ) {
216- EventBus .getDefault ().post (ErrorHandler .handleException (e ));
250+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
251+ @ Override
252+ public void run () {
253+ callback .onError (ErrorHandler .handleException (e ));
254+ }
255+ });
217256 }
218257 }
219258 });
@@ -222,7 +261,7 @@ public void run() {
222261 parseThread .start ();
223262 } else {
224263 if (exception .reason () != BridgeException .REASON_REQUEST_CANCELLED ) {
225- EventBus . getDefault (). post (ErrorHandler .handleException (exception ));
264+ callback . onError (ErrorHandler .handleException (exception ));
226265 }
227266 }
228267 }
@@ -234,7 +273,7 @@ public void run() {
234273 *
235274 * @return The result, specified by this class.
236275 * @throws ProxerException An Exception, which might occur, while executing the request.
237- * @see #execute()
276+ * @see #execute(ResultCallback )
238277 */
239278 @ WorkerThread
240279 @ RequiresPermission (android .Manifest .permission .INTERNET )
@@ -374,4 +413,4 @@ protected List<Conference> parse(@NonNull JSONObject response) throws JSONExcept
374413 return ProxerParser .parseConferencesJSON (response );
375414 }
376415 }
377- }
416+ }
0 commit comments