I am using the Model-View-Presenter design pattern coupled with an EventBus (Otto). The entire reason I implemented this pattern is to decouple events to the presenter only, and have the presenter update the views.
This is an example of some of the code I have, I'll use getting Events
as an example. (Please note that Events
is different from the EventBus
Event
, meaning an Event
in Events
is an event like "Dad's Birthday", but an Event
in the EventBus
is a Bus-event).
Fragment
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());
}
}
Presenter
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());
}
}
Subscriber
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
}
};
}
}
How can I get the Presenters and the Subscribers to handle all the business logic, and have the Fragment only handle views?