Android MVP与两个片段共享相同的数据
问题描述:
我的应用程序有一个活动和两个片段。该活动仅用作片段容器。其中一个片段将数据显示为文本。第二个片段显示与图表相同的数据。这些数据来自远程JSON API。和MVP一样,我们必须为每个视图复制相同的结构(模块,模型,演示者,存储库...),我的应用程序会为每个片段请求来自JSON API的数据,这样两次。我怎么能有一个更高效的架构让我尊重MVP?Android MVP与两个片段共享相同的数据
见我的两个片段执行下面的代码:
模块
@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);
}
}
库
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;
}
}
http://*.com/questions/34257883/mvp-for-activity-with-multiple-fragments?rq=1 – Manish