Android MVPs with two segments share the same data

My application has an activity and two fragments. The activity is only used as a fragment container. One fragment displays the data as text. The second fragment displays the same data as the chart. This data comes from the remote JSON API. Just like in MVP, we must copy the same structure for each view (module, model, presenter, Repository...), My application requests data from JSON API for each fragment, so it needs to be twice. How can I have a more efficient architecture and let me respect MVP?

See the following code implemented for my two 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);
    }
}

database

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;
    }
}

resolvent:

You must use MVP in the activity so that a request is made to the JSON API only once, and then the request can be obtained from all fragments registered in the activity

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>