Eventcallback

Retrofit callbacks on steroids

View the Project on GitHub byoutline/EventCallback

EventCallback

EventCallback allows creating instances of Retrofit callbacks using short readable syntax.

Instead of creating anonymous classes manually (where you have to take care of not using parent class fields that can change by the time server response arrives)

new Callback<SuccessDTO>() {

    @Override
    public void success(SuccessDTO s, Response response) {
        boolean stillSameSession = myCodeCheckingIfItIsStillSameSession();
        if(stillSameSession) {
            bus.post(new MyEvent());
            bus.post(new SuccessEvent());
        }
    }

    @Override
    public void failure(RetrofitError error) {
        RestErrorWithMsg restErrorWithMsg = myCodeThatTriesToConvertRetrofitErrorToReasonCallFailed(error);
        bus.post(new LoginValidationFailedEvent(restErrorWithMsg));
    }
};

you can use EventCallback like this:

EventCallback.<SuccessDTO>builder(config, new TypeToken<RestErrorWithMsg>(){})
    .onSuccess().postEvents(new MyEvent(), new SuccessEvent()).validThisSessionOnly()
    .onError().postResponseEvents(new LoginValidationFailedEvent()).validBetweenSessions()
    .build();