Android MVP with two snippets using the same data

My application has one activity and two fragments. The activity is simply used as a container for fragments. One of the snippets shows the data as text. The second snippet shows the same data as the chart. This data comes from a remote JSON API. As in MVP, we have to replicate the same structure for each view (module, model, presenter, repository ...), my application requests data from the JSON API for each fragment, so twice. How can I do to have a more efficient architecture that allows me to respect MVP?

Below is the code implemented for both of my snippets:

Module

@Module
public class PollutionLevelsModule {
    @Provides
        public PollutionLevelsFragmentMVP.Presenter providePollutionLevelsFragmentPresenter(PollutionLevelsFragmentMVP.Model pollutionLevelsModel) {
        return new PollutionLevelsPresenter(pollutionLevelsModel);
    }

    @Provides
    public PollutionLevelsFragmentMVP.Model providePollutionLevelsFragmentModel(Repository repository) {
        return new PollutionLevelsModel(repository);
    }

    @Singleton
    @Provides
    public Repository provideRepo(PollutionApiService pollutionApiService) {
        return new PollutionLevelsRepository(pollutionApiService);
    }
}

      

Repository

public class PollutionLevelsRepository implements Repository {
    private PollutionApiService pollutionApiService;

    public PollutionLevelsRepository(PollutionApiService pollutionApiService) {
        this.pollutionApiService = pollutionApiService;
    }

    @Override
    public Observable<Aqicn> getDataFromNetwork(String city, String authToken) {
        Observable<Aqicn> aqicn = pollutionApiService.getPollutionObservable(city, authToken);

        return aqicn;
    }
}

      

+3


source to share


1 answer


You must use MVP inside your activity so that only one request at a time is made for the JSON API. After that, all fragments that register from this action can receive this.



+3


source







All Articles