0

I am using repository pattern in mvp with dagger .In App scope I binded my RemoteDataSource and LocalDataSource:

@Binds
    @AppScope
    @Remote
    abstract MainDataSource RemoteDataSource(RemoteDataSource remoteDataSource);

    @Binds
    @AppScope
    @Local
    abstract MainDataSource LocalDataSource(LocalDataSource localDataSource);

And i injected main repository in app scope:

 @Inject
    public MainRepository(@Remote MainDataSource remoteDataSource,
                          @Local MainDataSource localDataSource) {
        this.remoteDataSource = checkNotNull(remoteDataSource);
        this.localDataSource = checkNotNull(localDataSource);
    }

Now in fragment scope in mainpresenter i passed MainRepository in it's contractor :

@MainFragScope
@Component(modules = {MainFragModule.class}, dependencies = AppComponent.class)
public interface MainFragComponent {

Presenter constructor:

private MainDataSource remoteDataSource;
private MainDataSource localDataSource;
 @Inject
    public MainPresenter(MainRepository repository, ArrayAdapter<String> typesAdapter) {
        this.repository = checkNotNull(repository);
        this.typesAdapter = checkNotNull(typesAdapter);
    }
@Override
public void loadChart(String district, String date, String type) {
    remoteDataSource.loadChart(district,date,type);
}

In RemoteDataSource i have a method called loadChart and it's job is fetch data from remote server by retrofit:

   public void loadChart(String district, String date, String type) {
        JsonObject joParam = new JsonObject();
        apiService.getAnalyticalReport(joParam).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.isSuccessful()) {
                    // need presenter reference to pass response to it

After fetching data i need to return this data from server to fragment presenter(MainPresenter).I need to presenter reference. How could i get presenter without destroy mvp roles!!? Because in AppScope i do not access to MainPresenter.

Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149

1 Answers1

0

IMHO in native way, before solving the communication between MainPresenter and remoteDataSource, it could be nice solving the one that between MainDataSource and MainRepositoryfirstly. Because there is a bridge something like this

Presenter --> MainRepository --> MainDataSource

MainDataSource and MainRepository could provide an interface for its users to communicate. Also with this way, MainDataSource or MainRepository won't need MainPresenter

So the data transferring will be like this,

  • MainRepository(Implements interface A) --> MainDataSource(provides interface A)

  • MainPresenter(Implements interface B) --> MainRepository(provides interface B)

blackkara
  • 4,900
  • 4
  • 28
  • 58
  • dude, do you have any example(simple project) to your description? @blackkara – Cyrus the Great Dec 18 '18 at 08:25
  • Seems you are using also clean architecture. Check this [link](https://github.com/bufferapp/clean-architecture-components-boilerplate) – blackkara Dec 18 '18 at 08:32
  • Most of projects, use RxJava2. In rx and retrofit you do not need do anything because with rx with retrofit you can have a return value but in retrofit alone, you can not return a value simply.I'll check recommended github project and announce you .@blackkara – Cyrus the Great Dec 18 '18 at 08:38
  • BTW, i started answering with 'in native way'. You can make simple the whole process with reactive way. I hope it helps you :-) – blackkara Dec 18 '18 at 08:43