How to effectively use the event bus?

I am currently exploring some options for an Android learning project. I am trying to communicate with my rails api (also a training project).

After some research, I think I settled on a scheme that uses retrofit and otto.

What I get is this. When I want to call my rails server (in this case, register), I do this in this operation.

 mBus.post(new SignupRequestEvent(new UserRequestParams(mName,mEmail,mPassword,mPasswordConfirmation )));

and then in the same activity I have.

@Subscribe
public void onSignupCompleted(SignupCompletedEvent event) {
    System.out.println(String.format("in onSignupCompleted, got token = %s ", event.getParams().getToken()));

}

The problem is that each instance of the api type and the corresponding response type will be a unique event type and will need its own class, which, apparently, is a lot of code table code types.

For example, to handle input and output, I need these two classes:

public class SignupRequestEvent {
    protected UserRequestParams mSignupParams;

    public SignupRequestEvent(UserRequestParams signupParams) {
        mSignupParams = signupParams;
    }

    public UserRequestParams getParams() {
        return mSignupParams;
    }

}

public class SignupCompletedEvent {

    private SignupCompletedParams mSignupCompletedParams;
    public SignupCompletedParams getParams() {
        return mSignupCompletedParams;
    }
    public SignupCompletedEvent(SignupCompletedParams signupCompletedParams) {
        mSignupCompletedParams = signupCompletedParams;
    }

}

, .

, 2 api, , , api, , .

- :

ApiRequestEvent apiRequestEvent = new ApiRequestEvent();
apiRequestEvent.setAction("SIGNUP");
apiRequestEvent.setParameters(new UserRequestParams(mName,mEmail,mPassword,mPasswordConfirmation ));
mBus.post(apiRequestEvent);

:

@Subscribe
public void onSignupCompleted(ApiResponseAvailable event) {
    if (event.getResponseTo != "SIGNUP") return;
    System.out.println(String.format("in onSignupCompleted, got token = %s ", event.getParams().getToken()));

, ?

- , , , ?

+4
1

- .

+4

Source: https://habr.com/ru/post/1539771/


All Articles