其他分享
首页 > 其他分享> > android – 模型视图Presenter与EventBus,如何将事件返回到Presenter?

android – 模型视图Presenter与EventBus,如何将事件返回到Presenter?

作者:互联网

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

这是我所拥有的一些代码的示例,我将以事件为例. (请注意,事件与EventBus事件不同,这意味着事件中的事件是“爸爸的生日”之类的事件,但EventBus中的事件是总线事件).

分段

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 onl oadEvents(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
              }
          };
    }
}

如何让演示者和订阅者处理所有业务逻辑,并让Fragment只处理视图?

解决方法:

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

在演示者中订阅事件,在其中有一个视图实例可以提供帮助.就像presenter从eventbus获取事件并调用适当的视图方法来更新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
    }  
    }

标签:android,mvp,otto,event-bus
来源: https://codeday.me/bug/20190717/1489036.html