【问题标题】:Model View Presenter with an EventBus, how to get Events back to Presenter?带有 EventBus 的 Model View Presenter,如何将事件返回给 Presenter?
【发布时间】:2015-08-22 05:56:36
【问题描述】:

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

这是我的一些代码示例,我将以获取Events 为例。 (请注意EventsEventBus Event 不同,这意味着Events 中的Event 是“爸爸的生日”之类的事件,但EventBus 中的Event 是公共汽车-事件)。

片段

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
              }
          };
    }
}

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

【问题讨论】:

  • 可能正在订阅 Presenter 中的事件,其中包含视图实例可以提供帮助。就像演示者从事件总线获取事件并调用适当的视图方法来更新 UI。
  • 你能发布一个示例答案吗?

标签: android mvp event-bus otto


【解决方案1】:

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

在 Presenter 中订阅事件,其中包含视图实例可以提供帮助。就像演示者从事件总线获取事件并调用适当的视图方法来更新 UI。

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

片段---

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

【讨论】:

  • 感谢您发布此答案,您将如何处理片段生命周期方法之类的问题?或者防止 Activity 在传入 Presenter 时泄漏?
  • 另外,您将如何取消订阅演示者中的事件总线?
  • 我的猜测是,在fragment或activity命中onPause或onDestroy的生命周期方法中,在presenter中调用类似的方法并注销总线,或者将target设置为null!更新您的答案以包含该内容,我会接受您的答案。
【解决方案2】:

这是演示者发送或接收事件以及通知片段或活动之后的责任

【讨论】:

  • 你节省了我的时间,谢谢
猜你喜欢
  • 2011-09-09
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-08
  • 1970-01-01
相关资源
最近更新 更多