2020import com .proxerme .library .entity .LoginData ;
2121import com .proxerme .library .entity .LoginUser ;
2222import com .proxerme .library .entity .News ;
23+ import com .proxerme .library .event .ConferencesEvent ;
24+ import com .proxerme .library .event .IEvent ;
25+ import com .proxerme .library .event .LoginEvent ;
26+ import com .proxerme .library .event .LogoutEvent ;
27+ import com .proxerme .library .event .NewsEvent ;
2328
2429import org .json .JSONException ;
2530import org .json .JSONObject ;
@@ -164,35 +169,12 @@ public static void cleanup() {
164169 Bridge .destroy ();
165170 }
166171
167- /**
168- * An abstract representation of a callback, passed to the
169- * {@link ProxerRequest#execute()} method.
170- *
171- * @param <T> The generic parameter.
172- */
173- public interface ResultCallback <T > {
174- /**
175- * A callback method, called if the request was successful.
176- *
177- * @param result The result of the specific request.
178- */
179- void onResult (T result );
180-
181- /**
182- * A callback method, called if an error occurred during the request.
183- *
184- * @param exception The Exception that occurred.
185- * @see ProxerException
186- */
187- void onError (@ NonNull ProxerException exception );
188- }
189-
190172 /**
191173 * An abstract representation of a request. All requests to the API are made through this class.
192174 *
193175 * @param <T> The type of result, the inheriting request will return.
194176 */
195- public static abstract class ProxerRequest <T > {
177+ public static abstract class ProxerRequest <T , E extends IEvent > {
196178
197179 /**
198180 * Builds the request, to be used in the
@@ -213,7 +195,8 @@ public static abstract class ProxerRequest<T> {
213195 protected abstract int getTag ();
214196
215197 /**
216- * Asynchronously executes this request.
198+ * Asynchronously executes this request. The result will be delivered on the event bus
199+ * method.
217200 *
218201 * @see #executeSynchronized()
219202 */
@@ -233,7 +216,7 @@ public void run() {
233216 if (json == null ) {
234217 EventBus .getDefault ().post (new ProxerException (UNKNOWN ));
235218 } else {
236- final T result = parse (json );
219+ final E result = createEvent ( parse (json ) );
237220
238221 EventBus .getDefault ().post (result );
239222 }
@@ -291,12 +274,14 @@ public final T executeSynchronized() throws ProxerException {
291274 * @throws JSONException An Exception, which might occur while parsing.
292275 */
293276 protected abstract T parse (@ NonNull JSONObject response ) throws JSONException ;
277+
278+ protected abstract E createEvent (@ NonNull T result );
294279 }
295280
296281 /**
297282 * A request, returning a List of {@link News}.
298283 */
299- public static class NewsRequest extends ProxerRequest <List <News >> {
284+ public static class NewsRequest extends ProxerRequest <List <News >, NewsEvent > {
300285
301286 private int page ;
302287
@@ -320,12 +305,17 @@ protected int getTag() {
320305 protected List <News > parse (@ NonNull JSONObject response ) throws JSONException {
321306 return ProxerParser .parseNewsJSON (response );
322307 }
308+
309+ @ Override
310+ protected NewsEvent createEvent (@ NonNull List <News > result ) {
311+ return new NewsEvent (result );
312+ }
323313 }
324314
325315 /**
326316 * A request for the login, returning a {@link LoginUser} on success.
327317 */
328- public static class LoginRequest extends ProxerRequest <LoginUser > {
318+ public static class LoginRequest extends ProxerRequest <LoginUser , LoginEvent > {
329319
330320 private LoginUser user ;
331321
@@ -356,12 +346,17 @@ protected LoginUser parse(@NonNull JSONObject response) throws JSONException {
356346 return new LoginUser (user .getUsername (), user .getPassword (), data .getId (),
357347 data .getImageId ());
358348 }
349+
350+ @ Override
351+ protected LoginEvent createEvent (@ NonNull LoginUser result ) {
352+ return new LoginEvent (result );
353+ }
359354 }
360355
361356 /**
362357 * A request for the logout.
363358 */
364- public static class LogoutRequest extends ProxerRequest <Void > {
359+ public static class LogoutRequest extends ProxerRequest <Void , LogoutEvent > {
365360
366361 @ NonNull
367362 @ Override
@@ -379,12 +374,17 @@ protected int getTag() {
379374 protected Void parse (@ NonNull JSONObject response ) throws JSONException {
380375 return null ;
381376 }
377+
378+ @ Override
379+ protected LogoutEvent createEvent (@ NonNull Void result ) {
380+ return new LogoutEvent ();
381+ }
382382 }
383383
384384 /**
385385 * A request for retrieval of the {@link Conference}s of a user.
386386 */
387- public static class ConferencesRequest extends ProxerRequest <List <Conference >> {
387+ public static class ConferencesRequest extends ProxerRequest <List <Conference >, ConferencesEvent > {
388388
389389 private int page ;
390390
@@ -407,6 +407,11 @@ protected int getTag() {
407407 protected List <Conference > parse (@ NonNull JSONObject response ) throws JSONException {
408408 return ProxerParser .parseConferencesJSON (response );
409409 }
410+
411+ @ Override
412+ protected ConferencesEvent createEvent (@ NonNull List <Conference > result ) {
413+ return new ConferencesEvent (result );
414+ }
410415 }
411416
412417 private static class ParseThread extends Thread {
0 commit comments