0

I am use Otto library in my project. And me need any functionality from this library wherein not.I want to do so:

Bus.post(MessageType.USER_SIGN_UP_SUCCESS, user);

and in my method realise do so:

@Subscribe({MessageType.USER_LOGGED_IN_SUCCESS, MessageType.USER_SIGN_UP_SUCCESS})
        public void getUserFromServer(User user) {
,,,,,,,,,,,,,,
}

for this I had to copy all Otto classes from githab, and change them. I could not implement from Otto because some variables private.

and changed the access modifiers in Bus class and extends from it.

public class SkipBus extends Bus {

    public void post(MessageType messageType, Object event) {
        if (event == null) {
            throw new NullPointerException("Event to post must not be null.");
        }
        enforcer.enforce(this);

        Set<Class<?>> dispatchTypes = flattenHierarchy(event.getClass());

        boolean dispatched = false;
        for (Class<?> eventType : dispatchTypes) {
            Set<EventHandler> wrappers = getHandlersForEventType(eventType);

            if (null == wrappers || wrappers.isEmpty()) {
                continue;
            }

            dispatched = true;
            for (EventHandler wrapper : wrappers) {
                Subscribe annotation = wrapper.method.getAnnotation(Subscribe.class);

                boolean isFounded = false;
                MessageType messageTypes[] = annotation.value();
                for (MessageType type : messageTypes) {
                    if (type == messageType) {
                        isFounded = true;
                        break;
                    }
                }

                if (isFounded) {
                    enqueueEvent(event, wrapper);
                }
            }
        }

        if (!dispatched && !(event instanceof DeadEvent)) {
            post(new DeadEvent(this, event));
        }

        dispatchQueuedEvents();
    }
}

but for this I had to copy all the classes in my project. tell me how can I make it easier? or tell me another library that can do what I want

2 Answers2

1

Actually Otto was designed to separate the message type using Object's type.

@Subscribe
public void eventReceived(UserSignUpEvent user) {
}

@Subscribe
public void eventReceived(UserLoginEvent user) {
}

or

MainActivity.java

@Subscribe
public void eventReceived(User user) {
    if (user.getMessageType() == MessageType.USER_SIGN_UP_SUCCESS) {
    }
}

SecondActivity.java

@Subscribe
public void eventReceived(User user) {
    if (user.getMessageType() == MessageType.USER_LOGIN_SUCCESS) {
    }
}

You have no need to separate the MessageType like this (and you should not). I suggest you to change the code design pattern to what that Otto is designed for. Otherwise, you have to copy the whole Otto source code and edit like you are currently doing.

nuuneoi
  • 1,788
  • 1
  • 17
  • 14
  • that's the thing that I want to use MessageType. I may have a few methods that have parameter user. I do not want all of them to catch the event the user. but only those that I want to send. may have libraries which allow this? – Pavel Petrashov Mar 12 '15 at 15:56
  • What a class UserSignUpEvent and UserLoginEvent ? exactly the same classes that have different names?and if I have 15 events that take the user, and I want to separate them? I create another 15 similar classes? delirium – Pavel Petrashov Mar 12 '15 at 15:59
  • Calm down... Don't be rude, guy. You came here for help ... Plz see the updated answer. That's my last suggestion. – nuuneoi Mar 12 '15 at 16:16
  • I am not rude to you. just what you have written so there is documentation Otto. I just asked, can eat other libraries that allow what I want – Pavel Petrashov Mar 12 '15 at 16:21
  • Oh yeah, about your second answer. What if I have a few MessageType's? if{} if{} if{}.....? – Pavel Petrashov Mar 12 '15 at 16:24
  • @nuuneoi one point: methods annotated `@Subscribe` must be public. – MightySeal Mar 13 '15 at 05:36
  • @PavelPetrashov you can use `switch` for enums. This answer is totally correct, see my answer with concept explanation. – MightySeal Mar 13 '15 at 05:40
0

Otto itself is pretty simple so there's no need to extend it. You just create event and event handler for it. Event can be any object thus you can do something like:

public enum LoginType {
    SUCCESS,
    FAILED
}

In your login processor:

private void login(...) {
    // process login
    // if everything is ok
    if(...) {
        bus.post(LoginType.SUCCESS);
    } else {
        bus.post(LoginType.FAILED);
    }
}

And handle this event wherever you need:

@Subscribe
public void onLogin(LoginType loginType) {
    switch(loginType) {
        case SUCCESS:
            // do what you need
        break;

        case FAILED:
            // show an error or something
        break;
    }
}

So no need to extend library, you just have to design your events in a proper way.

MightySeal
  • 2,293
  • 2
  • 17
  • 32