Skip to content

Instantly share code, notes, and snippets.

@communikein
Last active January 19, 2018 10:31
Show Gist options
  • Save communikein/b933037a87a6c8f128d7c20cbbf79612 to your computer and use it in GitHub Desktop.
Save communikein/b933037a87a6c8f128d7c20cbbf79612 to your computer and use it in GitHub Desktop.
@Singleton
public class PoisRepository {
private final PoisDao mDao;
private final WebService mWebService;
private final AppExecutors mExecutors;
private MutableLiveData<List<Poi>> mPois;
@Inject
public PoisRepository(PoisDao poisDao, WebService webService, AppExecutors executors) {
this.mDao = poisDao;
this.mWebService = webService;
MutableLiveData<List<Poi>> mWebServerPois = new MutableLiveData<>();
mWebService.getPois().enqueue((call, response) -> {
mWebServerPois.setValue(response.body());
});
mWebServerPois.observeForever(newOnlineData -> mExecutors.diskIO().execute(() -> {
if (newOnlineData != null) {
mPoisDao.deletePois();
mPoisDao.bulkInsertPois((ArrayList<Poi>) newOnlineData);
}
}
}
public LiveData<List<Poi>> getObservableAllPois() {
return mPoisDao.getObservablePois();
}
public LiveData<Poi> getObservablePoi(String id) {
return mPoisDao.getObservablePoi(id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment