带有 EventBus 的 Model View Presenter,如何将事件返回给 Presenter?

Model View Presenter with an EventBus, how to get Events back to Presenter?

我正在使用 Model-View-Presenter 设计模式和 EventBus (Otto)。我实现此模式的全部原因是仅将事件与演示者分离,并让演示者更新视图。

这是我拥有的一些代码的示例,我将使用获取 Events 作为示例。 (请注意 Events 不同于 EventBus Event,这意味着 Events 中的 Event 是一个类似于 "Dad's Birthday" 的事件,但是 EventEventBus 是总线事件)。

片段

public class EventFragment extends Fragment {

    private EventPresenter mEventPresenter;

    // Initialize boilerplate code...

    private void init() {
        mEventPresenter = new EventPresenter();
        mEventPresenter.loadEvents();
    }

    // I WANT TO MOVE THESE SUBSCRIPTION METHODS TO
    // MY PRESENTER OR SUBSCRIBER, BUT THEY ARE 
    // COUPLED TO THE ADAPTER OR A VIEW

    @Subscribe
    public void onEventsLoaded(EventsLoaded eventsLoaded) {
        List<Event> events = eventsLoaded.getEvents();
        mAdapter.setEvents(events);
    }

    @Subscribe
    public void onEventJoined(EventJoined eventJoined) {
        mItemView.doAnimation();
        mTextView.setText("Leave");
        mAdapter.joinEvent(eventJoined.getEvent());
    }

    @Subscribe
    public void onLeaveEvent(LeftEvent leftEvent) {
        mItemView.doAnimation();
        mTextView.setText("Join");
        mAdapter.leftEvent(leftEvent.getEvent());
    }

}

主持人

public class EventPresenter {

    // This is the only method I have right now kind of defeats the purpose of 
    // having a presenter
    public void loadEvents() {
        EventBus.getInstance().post(new LoadEvents());
    }

}

订阅者

public class EventSubscriber extends Subscriber {

    // This class is registered on the event bus

    @Subscribe
    public void onLoadEvents(LoadEvents loadEvents) {
        sClient.getEvents(new Callback<List<Event>>() {
              @Override
              public void onSuccess(List<Event> events, Response response) {
                   EventBus.post(new EventsLoaded(events));
              }

              @Override
              public void onFailure(.....) {
                    // Handle failure
              }
          };
    }
}

如何让 Presenter 和 Subscriber 处理所有业务逻辑,而让 Fragment 只处理视图?

演示者负责发送或接收事件,然后通知片段或活动

这不是最好的可能性,但可能会帮助你。

在 presenter 中订阅事件,其中包含一个视图实例会有所帮助。就像演示者从事件总线获取事件并调用适当的视图方法来更新UI。

请注意,我没有更改更新 UI 的片段的方法名称,并且直接从演示者传递了事件对象,演示者现在确实是事件总线事件的订阅者。您可以相应地更改它。

片段---

public class EventFragment extends Fragment {

    private EventPresenter mEventPresenter;

    // Initialize boilerplate code...

    @Override
    public void onResume()
    {
         mEventPresenter.onResume();
    } 

    @Override
    public void onPause()
    {
         mEventPresenter.onPause();
    }  

    private void init() {
        mEventPresenter = new EventPresenter(this);
        mEventPresenter.loadEvents();
    }

    // I WANT TO MOVE THESE SUBSCRIPTION METHODS TO
    // MY PRESENTER OR SUBSCRIBER, BUT THEY ARE 
    // COUPLED TO THE ADAPTER OR A VIEW

    public void onEventsLoaded(EventsLoaded eventsLoaded) {
        List<Event> events = eventsLoaded.getEvents();
        mAdapter.setEvents(events);
    }

    public void onEventJoined(EventJoined eventJoined) {
        mItemView.doAnimation();
        mTextView.setText("Leave");
        mAdapter.joinEvent(eventJoined.getEvent());
    }

    public void onLeaveEvent(LeftEvent leftEvent) {
        mItemView.doAnimation();
        mTextView.setText("Join");
        mAdapter.leftEvent(leftEvent.getEvent());
    }

}

主持人----

    public class EventPresenter {
        private EventFragment targetView; 
        public EventPresenter(EventFragment myView)
        {
             targetView = myView;

        }
        // This is the only method I have right now kind of defeats the purpose of 
        // having a presenter
        public void loadEvents() {
            EventBus.getInstance().post(new LoadEvents());
        }

    @Subscribe
    public void onEventsLoaded(EventsLoaded eventsLoaded) {
        targetView.onEventsLoaded(eventsLoaded);
    }

    @Subscribe
    public void onEventJoined(EventJoined eventJoined) {
        targetView.onEventJoined(eventJoined);
    }

    @Subscribe
    public void onLeaveEvent(LeftEvent leftEvent) {
        targetView.onLeaveEvent(leftEvent);
    }


    public void onResume()
    {
         //subscride event bus
    } 


    public void onPause()
    {
         //unsubscride event bus
    }  
    }