Skip to content

Commit ea0557b

Browse files
committed
Define Events for all Requests to make the events distinguishable
1 parent 8f56f3f commit ea0557b

6 files changed

Lines changed: 146 additions & 30 deletions

File tree

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

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
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;
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

2429
import org.json.JSONException;
2530
import 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 {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.proxerme.library.event;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import com.proxerme.library.entity.Conference;
6+
7+
import java.util.List;
8+
9+
/**
10+
* TODO: Describe Class
11+
*
12+
* @author Ruben Gees
13+
*/
14+
public class ConferencesEvent implements IEvent {
15+
16+
private List<Conference> conferences;
17+
18+
public ConferencesEvent(@NonNull List<Conference> conferences) {
19+
this.conferences = conferences;
20+
}
21+
22+
@NonNull
23+
@Override
24+
public Object getItem() {
25+
return conferences;
26+
}
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.proxerme.library.event;
2+
3+
import android.support.annotation.NonNull;
4+
5+
/**
6+
* TODO: Describe Class
7+
*
8+
* @author Ruben Gees
9+
*/
10+
public interface IEvent<T> {
11+
12+
@NonNull
13+
T getItem();
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.proxerme.library.event;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import com.proxerme.library.entity.LoginUser;
6+
7+
/**
8+
* TODO: Describe Class
9+
*
10+
* @author Ruben Gees
11+
*/
12+
public class LoginEvent implements IEvent<LoginUser> {
13+
14+
private LoginUser loginUser;
15+
16+
public LoginEvent(@NonNull LoginUser loginUser) {
17+
this.loginUser = loginUser;
18+
}
19+
20+
@NonNull
21+
@Override
22+
public LoginUser getItem() {
23+
return loginUser;
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.proxerme.library.event;
2+
3+
import android.support.annotation.NonNull;
4+
5+
/**
6+
* TODO: Describe Class
7+
*
8+
* @author Ruben Gees
9+
*/
10+
public class LogoutEvent implements IEvent {
11+
12+
@NonNull
13+
@Override
14+
public Object getItem() {
15+
//noinspection ConstantConditions
16+
return null;
17+
}
18+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.proxerme.library.event;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import com.proxerme.library.entity.News;
6+
7+
import java.util.List;
8+
9+
/**
10+
* TODO: Describe Class
11+
*
12+
* @author Ruben Gees
13+
*/
14+
public class NewsEvent implements IEvent<List<News>> {
15+
16+
private List<News> item;
17+
18+
public NewsEvent(@NonNull List<News> item) {
19+
this.item = item;
20+
}
21+
22+
@Override
23+
@NonNull
24+
public List<News> getItem() {
25+
return item;
26+
}
27+
}

0 commit comments

Comments
 (0)