Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Concrete implementation to load tasks from data source to the cache
import android.database.Cursor;
import android.support.annotation.NonNull
/**
* Concrete implementation to load tasks from data source to the cache
*/
public class ChallengesRepo implements ChallengesDataStore {
private static ChallengesRepo instance = null;
private final ChallengesRemoteDataSource mChallengesRemoteDataSource;
private final ChallengesLocalDataStore mChallengesLocalDataStore;
// prevent direct instantiation
private ChallengesRepo(@NonNull ChallengesRemoteDataSource challengesRemoteDataSource,
ChallengesLocalDataStore challengesLocalDataStore) {
this.mChallengesRemoteDataSource = challengesRemoteDataSource;
this.mChallengesLocalDataStore = challengesLocalDataStore;
}
public static ChallengesRepo getInstance(ChallengesRemoteDataSource challengesDataStore,
ChallengesLocalDataStore challengesLocalDataStore) {
if (instance == null) {
instance = new ChallengesRepo(challengesDataStore, challengesLocalDataStore);
}
return instance;
}
@Override
public void getChallenges(@NonNull final GetChallengesCallback getChallengesCallback) {
mChallengesRemoteDataSource.getChallenges(new GetChallengesCallback() {
@Override
public void onChallengesLoaded(List<Challenge> challenges) {
getChallengesCallback.onChallengesLoaded(challenges);
}
@Override
public void onError(Exception exception) {
getChallengesCallback.onError(exception);
}
@Override
public void onDataNotAvailable() {
getChallengesCallback.onDataNotAvailable();
}
});
}
@Override
public void saveChallenges(@NonNull Challenge challenge) {
mChallengesRemoteDataSource.saveChallenges(challenge);
}
@Override
public void deleteAllChallenges() {
mChallengesLocalDataStore.deleteAllChallenges();
}
public interface LoadDataCallback {
void onDataLoaded(Cursor cursor);
void onDataNotAvailable();
void onDataReset();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment