/ChallengesRepo.java Secret
Last active
November 19, 2016 06:26
Star
You must be signed in to star a gist
Concrete implementation to load tasks from data source to the cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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