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

4

2 回答 2

1

实际上 Otto 的设计目的是使用 Object 的类型来分隔消息类型。

@Subscribe
public void eventReceived(UserSignUpEvent user) {
}

@Subscribe
public void eventReceived(UserLoginEvent user) {
}

或者

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) {
    }
}

您无需像这样分隔 MessageType(而且您不应该这样做)。我建议您将代码设计模式更改为 Otto 的设计目标。否则,您必须复制整个 Otto 源代码并像您当前所做的那样进行编辑。

于 2015-03-12T15:49:04.187 回答
0

Otto 本身非常简单,因此无需扩展它。您只需为其创建事件和事件处理程序。事件可以是任何对象,因此您可以执行以下操作:

public enum LoginType {
    SUCCESS,
    FAILED
}

在您的登录处理器中:

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

并在您需要的任何地方处理此事件:

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

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

所以不需要扩展库,你只需要以适当的方式设计你的事件。

于 2015-03-13T05:56:28.323 回答