Skip to content

Commit ba70bd8

Browse files
committed
Define ErrorEvents
1 parent ea0557b commit ba70bd8

10 files changed

Lines changed: 138 additions & 17 deletions

File tree

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

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020
import com.proxerme.library.entity.LoginData;
2121
import com.proxerme.library.entity.LoginUser;
2222
import com.proxerme.library.entity.News;
23-
import com.proxerme.library.event.ConferencesEvent;
2423
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;
24+
import com.proxerme.library.event.error.ConferencesErrorEvent;
25+
import com.proxerme.library.event.error.ErrorEvent;
26+
import com.proxerme.library.event.error.LoginErrorEvent;
27+
import com.proxerme.library.event.error.LogoutErrorEvent;
28+
import com.proxerme.library.event.error.NewsErrorEvent;
29+
import com.proxerme.library.event.success.ConferencesEvent;
30+
import com.proxerme.library.event.success.LoginEvent;
31+
import com.proxerme.library.event.success.LogoutEvent;
32+
import com.proxerme.library.event.success.NewsEvent;
2833

2934
import org.json.JSONException;
3035
import org.json.JSONObject;
@@ -174,7 +179,7 @@ public static void cleanup() {
174179
*
175180
* @param <T> The type of result, the inheriting request will return.
176181
*/
177-
public static abstract class ProxerRequest<T, E extends IEvent> {
182+
public static abstract class ProxerRequest<T, E extends IEvent, EE extends ErrorEvent> {
178183

179184
/**
180185
* Builds the request, to be used in the
@@ -214,16 +219,16 @@ public void run() {
214219
JSONObject json = response.asJsonObject();
215220

216221
if (json == null) {
217-
EventBus.getDefault().post(new ProxerException(UNKNOWN));
222+
EventBus.getDefault().post(createErrorEvent(new ProxerException(UNKNOWN)));
218223
} else {
219224
final E result = createEvent(parse(json));
220225

221226
EventBus.getDefault().post(result);
222227
}
223228
} catch (final JSONException e) {
224-
EventBus.getDefault().post(ErrorHandler.handleException(e));
229+
EventBus.getDefault().post(createErrorEvent(ErrorHandler.handleException(e)));
225230
} catch (final BridgeException e) {
226-
EventBus.getDefault().post(ErrorHandler.handleException(e));
231+
EventBus.getDefault().post(createErrorEvent(ErrorHandler.handleException(e)));
227232
}
228233

229234
parseThreads.remove(getThread());
@@ -234,7 +239,7 @@ public void run() {
234239
parseThread.start();
235240
} else {
236241
if (exception.reason() != BridgeException.REASON_REQUEST_CANCELLED) {
237-
EventBus.getDefault().post(ErrorHandler.handleException(exception));
242+
EventBus.getDefault().post(createErrorEvent(ErrorHandler.handleException(exception)));
238243
}
239244
}
240245
}
@@ -276,12 +281,14 @@ public final T executeSynchronized() throws ProxerException {
276281
protected abstract T parse(@NonNull JSONObject response) throws JSONException;
277282

278283
protected abstract E createEvent(@NonNull T result);
284+
285+
protected abstract EE createErrorEvent(@NonNull ProxerException exception);
279286
}
280287

281288
/**
282289
* A request, returning a List of {@link News}.
283290
*/
284-
public static class NewsRequest extends ProxerRequest<List<News>, NewsEvent> {
291+
public static class NewsRequest extends ProxerRequest<List<News>, NewsEvent, NewsErrorEvent> {
285292

286293
private int page;
287294

@@ -310,12 +317,17 @@ protected List<News> parse(@NonNull JSONObject response) throws JSONException {
310317
protected NewsEvent createEvent(@NonNull List<News> result) {
311318
return new NewsEvent(result);
312319
}
320+
321+
@Override
322+
protected NewsErrorEvent createErrorEvent(@NonNull ProxerException exception) {
323+
return new NewsErrorEvent(exception);
324+
}
313325
}
314326

315327
/**
316328
* A request for the login, returning a {@link LoginUser} on success.
317329
*/
318-
public static class LoginRequest extends ProxerRequest<LoginUser, LoginEvent> {
330+
public static class LoginRequest extends ProxerRequest<LoginUser, LoginEvent, LoginErrorEvent> {
319331

320332
private LoginUser user;
321333

@@ -351,12 +363,17 @@ protected LoginUser parse(@NonNull JSONObject response) throws JSONException {
351363
protected LoginEvent createEvent(@NonNull LoginUser result) {
352364
return new LoginEvent(result);
353365
}
366+
367+
@Override
368+
protected LoginErrorEvent createErrorEvent(@NonNull ProxerException exception) {
369+
return new LoginErrorEvent(exception);
370+
}
354371
}
355372

356373
/**
357374
* A request for the logout.
358375
*/
359-
public static class LogoutRequest extends ProxerRequest<Void, LogoutEvent> {
376+
public static class LogoutRequest extends ProxerRequest<Void, LogoutEvent, LogoutErrorEvent> {
360377

361378
@NonNull
362379
@Override
@@ -379,12 +396,18 @@ protected Void parse(@NonNull JSONObject response) throws JSONException {
379396
protected LogoutEvent createEvent(@NonNull Void result) {
380397
return new LogoutEvent();
381398
}
399+
400+
@Override
401+
protected LogoutErrorEvent createErrorEvent(@NonNull ProxerException exception) {
402+
return new LogoutErrorEvent(exception);
403+
}
382404
}
383405

384406
/**
385407
* A request for retrieval of the {@link Conference}s of a user.
386408
*/
387-
public static class ConferencesRequest extends ProxerRequest<List<Conference>, ConferencesEvent> {
409+
public static class ConferencesRequest extends ProxerRequest<List<Conference>,
410+
ConferencesEvent, ConferencesErrorEvent> {
388411

389412
private int page;
390413

@@ -412,6 +435,11 @@ protected List<Conference> parse(@NonNull JSONObject response) throws JSONExcept
412435
protected ConferencesEvent createEvent(@NonNull List<Conference> result) {
413436
return new ConferencesEvent(result);
414437
}
438+
439+
@Override
440+
protected ConferencesErrorEvent createErrorEvent(@NonNull ProxerException exception) {
441+
return new ConferencesErrorEvent(exception);
442+
}
415443
}
416444

417445
private static class ParseThread extends Thread {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.proxerme.library.event.error;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import com.proxerme.library.connection.ProxerException;
6+
7+
/**
8+
* TODO: Describe Class
9+
*
10+
* @author Ruben Gees
11+
*/
12+
public class ConferencesErrorEvent extends ErrorEvent {
13+
public ConferencesErrorEvent(@NonNull ProxerException exception) {
14+
super(exception);
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.proxerme.library.event.error;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import com.proxerme.library.connection.ProxerException;
6+
7+
/**
8+
* TODO: Describe Class
9+
*
10+
* @author Ruben Gees
11+
*/
12+
public abstract class ErrorEvent {
13+
14+
private ProxerException exception;
15+
16+
public ErrorEvent(@NonNull ProxerException exception) {
17+
this.exception = exception;
18+
}
19+
20+
@NonNull
21+
public ProxerException getException() {
22+
return exception;
23+
}
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.proxerme.library.event.error;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import com.proxerme.library.connection.ProxerException;
6+
7+
/**
8+
* TODO: Describe Class
9+
*
10+
* @author Ruben Gees
11+
*/
12+
public class LoginErrorEvent extends ErrorEvent {
13+
public LoginErrorEvent(@NonNull ProxerException exception) {
14+
super(exception);
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.proxerme.library.event.error;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import com.proxerme.library.connection.ProxerException;
6+
7+
/**
8+
* TODO: Describe Class
9+
*
10+
* @author Ruben Gees
11+
*/
12+
public class LogoutErrorEvent extends ErrorEvent {
13+
public LogoutErrorEvent(@NonNull ProxerException exception) {
14+
super(exception);
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.proxerme.library.event.error;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import com.proxerme.library.connection.ProxerException;
6+
7+
/**
8+
* TODO: Describe Class
9+
*
10+
* @author Ruben Gees
11+
*/
12+
public class NewsErrorEvent extends ErrorEvent {
13+
public NewsErrorEvent(@NonNull ProxerException exception) {
14+
super(exception);
15+
}
16+
}

library/src/main/java/com/proxerme/library/event/ConferencesEvent.java renamed to library/src/main/java/com/proxerme/library/event/success/ConferencesEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package com.proxerme.library.event;
1+
package com.proxerme.library.event.success;
22

33
import android.support.annotation.NonNull;
44

55
import com.proxerme.library.entity.Conference;
6+
import com.proxerme.library.event.IEvent;
67

78
import java.util.List;
89

library/src/main/java/com/proxerme/library/event/LoginEvent.java renamed to library/src/main/java/com/proxerme/library/event/success/LoginEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package com.proxerme.library.event;
1+
package com.proxerme.library.event.success;
22

33
import android.support.annotation.NonNull;
44

55
import com.proxerme.library.entity.LoginUser;
6+
import com.proxerme.library.event.IEvent;
67

78
/**
89
* TODO: Describe Class

library/src/main/java/com/proxerme/library/event/LogoutEvent.java renamed to library/src/main/java/com/proxerme/library/event/success/LogoutEvent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package com.proxerme.library.event;
1+
package com.proxerme.library.event.success;
22

33
import android.support.annotation.NonNull;
44

5+
import com.proxerme.library.event.IEvent;
6+
57
/**
68
* TODO: Describe Class
79
*

library/src/main/java/com/proxerme/library/event/NewsEvent.java renamed to library/src/main/java/com/proxerme/library/event/success/NewsEvent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package com.proxerme.library.event;
1+
package com.proxerme.library.event.success;
22

33
import android.support.annotation.NonNull;
44

55
import com.proxerme.library.entity.News;
6+
import com.proxerme.library.event.IEvent;
67

78
import java.util.List;
89

0 commit comments

Comments
 (0)