i'd like to ask about the way MVP and dagger working. So, in android development, we know that Activity and fragment is a view, and we make presenter by implementing observer (in my case, im using rxjava + retrofit). And the dagger in this case I use to inject Rest service (Network Component -> retrofit).
I still confuse to inject Rest service to my presenter, because all the example I found is injecting to activity.
here is my code.
As view :
public class PageFragment extends Fragment implements ScheduleViewInterface{
private ScheduleCursorAdapter scheduleAdapter;
@Inject RestApi restApiInject;
private SchedulePresenter mPresenterJson;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
App.getApiComponent(getActivity()).inject(this);
mPresenterJson.fetchSchedule();
}
@Override
public Observable<ScheduleList> getSchedule() {
return restApiInject.getScheduleListByUrl("url here");
}
As presenter :
public class SchedulePresenter implements Observer<ScheduleList> {
private ScheduleViewInterface mInterface;
public SchedulePresenter(ScheduleViewInterface viewInterface){
mInterface = viewInterface;
}
@Override
public void onCompleted() {
mInterface.jsonCompleted();
}
@Override
public void onError(Throwable e) {
mInterface.jsonError(e.getMessage());
}
@Override
public void onNext(ScheduleList scheduleList) {
mInterface.jsonScheduleList(scheduleList);
}
public void fetchSchedules(){
unSubscribeAll();
subscribe(mInterface.getSchedule(), SchedulePresenter.this);
}
and this is my dagger2 code
@CustomScope
@Component(modules = ApiModule.class, dependencies = NetworkComponent.class)
public interface ApiComponent {
MainActivity inject(MainActivity activity);
PageFragment inject(PageFragment fragment);
}