3

My app has one activity and two fragments. The activity is just used as a fragment container. One of the fragment show data as text. The second fragment shows the same data as a chart. This data come from a remote JSON API. As in MVP we have to replicate the same structure for each view (module, model, presenter, repository...) my app request the data from the JSON API for every fragment, so two times. How can i do to have a more efficient architecture allowing me to respect the MVP ?

See below the code implemented for my both fragments :

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;
    }
}
Laurent
  • 1,661
  • 16
  • 29

1 Answers1

3

You must use MVP inside your activity so that only one time request is done to JSON API.After that all fragments which register from that activity can able to get that.

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147