@Test public void getPhotos_shouldCacheAfterFirstRemoteCall() { repository.getPhotos(loadPhotosCallback); verify(localDataSource, times(1)).getPhotos(photosCallbackCaptor.capture()); photosCallbackCaptor.getValue().onPhotosNotAvailable(); verify(remoteDataSource, times(1)).getPhotos(photosCallbackCaptor.capture()); photosCallbackCaptor.getValue().onPhotosLoaded(PHOTOS); assertThat(repository.cachedPhotos.size(), is(3)); repository.getPhotos(loadPhotosCallback); verify(remoteDataSource, times(1)) .getPhotos(any(PhotoDataSource.LoadPhotosCallback.class)); }
@Override public void getPhotos(@NonNull LoadPhotosCallback callback) { if (!cachedPhotos.isEmpty() && !isCacheDirty) { callback.onPhotosLoaded(new ArrayList<>(cachedPhotos.values())); return; } if (isCacheDirty) { getPhotosFromRemoteDataSource(callback); } else { photoLocalDataSource.getPhotos(new LoadPhotosCallback() { @Override public void onPhotosLoaded(List<Photo> photos) { refreshCache(photos); callback.onPhotosLoaded(new ArrayList<>(cachedPhotos.values())); } @Override public void onPhotosNotAvailable() { getPhotosFromRemoteDataSource(callback); } }); } }
PhotoRepository implements PhotoDataSource { @Override public void getPhotos(@NonNull LoadPhotosCallback callback) { if (!cachedPhotos.isEmpty() && !isCacheDirty) { callback.onPhotosLoaded(new ArrayList<>(cachedPhotos.values())); return; } if (isCacheDirty) { getPhotosFromRemoteDataSource(callback); } else { photoLocalDataSource.getPhotos(new LoadPhotosCallback() { @Override public void onPhotosLoaded(List<Photo> photos) { refreshCache(photos); callback.onPhotosLoaded(new ArrayList<>(cachedPhotos.values())); } @Override public void onPhotosNotAvailable() { getPhotosFromRemoteDataSource(callback); } }); } } }
PhotoRepository implements PhotoDataSource { @Override public void getPhotos(@NonNull LoadPhotosCallback callback) { if (!cachedPhotos.isEmpty() && !isCacheDirty) { callback.onPhotosLoaded(new ArrayList<>(cachedPhotos.values())); return; } if (isCacheDirty) { getPhotosFromRemoteDataSource(callback); } else { photoLocalDataSource.getPhotos(new LoadPhotosCallback() { @Override public void onPhotosLoaded(List<Photo> photos) { refreshCache(photos); callback.onPhotosLoaded(new ArrayList<>(cachedPhotos.values())); } @Override public void onPhotosNotAvailable() { getPhotosFromRemoteDataSource(callback); } }); } } PhotoRepository(@NonNull PhotoDataSource photoRemoteDataSource,
                           @NonNull PhotoDataSource photoLocalDataSource); }
PhotoRepository implements PhotoDataSource { @Override public void getPhotos(@NonNull LoadPhotosCallback callback) { if (!cachedPhotos.isEmpty() && !isCacheDirty) { callback.onPhotosLoaded(new ArrayList<>(cachedPhotos.values())); return; } if (isCacheDirty) { getPhotosFromRemoteDataSource(callback); } else { photoLocalDataSource.getPhotos(new LoadPhotosCallback() { @Override public void onPhotosLoaded(List<Photo> photos) { refreshCache(photos); callback.onPhotosLoaded(new ArrayList<>(cachedPhotos.values())); } @Override public void onPhotosNotAvailable() { getPhotosFromRemoteDataSource(callback); } }); } } PhotoRepository(@NonNull PhotoDataSource photoRemoteDataSource,
                           @NonNull PhotoDataSource photoLocalDataSource); @Override void getPhotos(@NonNull LoadPhotosCallback callback); @Override void getPhoto(int photoId, @NonNull LoadPhotoCallback callback); @Override void savePhoto(@NonNull Photo photo); @Override void deletePhoto(int photoId); @Override void deleteAllPhotos(); @Override void refreshPhotos(); }
PhotoRepository implements PhotoDataSource { @Override public void getPhotos(@NonNull LoadPhotosCallback callback) { if (!cachedPhotos.isEmpty() && !isCacheDirty) { callback.onPhotosLoaded(new ArrayList<>(cachedPhotos.values())); return; } if (isCacheDirty) { getPhotosFromRemoteDataSource(callback); } else { photoLocalDataSource.getPhotos(new LoadPhotosCallback() { @Override public void onPhotosLoaded(List<Photo> photos) { refreshCache(photos); callback.onPhotosLoaded(new ArrayList<>(cachedPhotos.values())); } @Override public void onPhotosNotAvailable() { getPhotosFromRemoteDataSource(callback); } }); } } PhotoRepository(@NonNull PhotoDataSource photoRemoteDataSource,
                           @NonNull PhotoDataSource photoLocalDataSource); @Override void getPhotos(@NonNull LoadPhotosCallback callback); @Override void getPhoto(int photoId, @NonNull LoadPhotoCallback callback); @Override void savePhoto(@NonNull Photo photo); @Override void deletePhoto(int photoId); @Override void deleteAllPhotos(); @Override void refreshPhotos();  }