-
-
Save alouanemed/7aa9c2153fd80a31cfc74654743988a9 to your computer and use it in GitHub Desktop.
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
public interface ApiService { | |
@GET(ApiConstants.GET_QUESTIONS_URL) | |
Observable<HttpResult<List<Question>>> getQuestions(); | |
} | |
public interface CacheProviders { | |
@LifeCache(duration = 7, timeUnit = TimeUnit.DAYS) | |
Observable<Reply<List<Question>>> getCachedQuestions(Observable<List<Question>> items, | |
EvictDynamicKey evictDynamicKey); | |
} |
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
public class httpData { | |
private static File cacheDirectory = Utils.getcacheDirectory(); | |
private static final CacheProviders providers = new RxCache.Builder() | |
.persistence(cacheDirectory, new GsonSpeaker()) | |
.using(CacheProviders.class); | |
public httpData(ApiService mApiService){ | |
apiService = mApiService; | |
} | |
public void getQuiz(String sexe, boolean isload, Observer<List<Question>> observer) { | |
Observable observable = apiService.getQuestions().map(new HttpResultFunc<List<Question>>()); | |
Observable observableCache = providers.getCachedQuestions(observable, | |
new EvictDynamicKey(isload)).map( | |
new HttpResultFuncCached<List<Question>>()); | |
setSubscribe(observableCache, observer); | |
} | |
public static <T> void setSubscribe(Observable<T> observable, Observer<T> observer) { | |
observable.subscribeOn(Schedulers.io()) | |
.unsubscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(observer); | |
} | |
private class HttpResultFunc<T> implements Function<HttpResult<T>, T>{ | |
@Override public T apply(HttpResult<T> tHttpResult) throws Exception { | |
if (tHttpResult.getRequestResul()) { | |
throw new RuntimeException(); | |
} | |
return tHttpResult.getData(); | |
} | |
} | |
private class HttpResultFuncCached<T> implements Function<Reply<T>, T> { | |
@Override public T apply(Reply<T> httpResult) throws Exception { | |
return httpResult.getData(); | |
} | |
} | |
} |
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
public class HttpResult<T> { | |
public Boolean requestResult; | |
public T data; | |
public static final String SUCCESS = "success"; | |
public static final String Fail = "Fail"; | |
public HttpResult(Boolean requestResult, T data) { | |
this.requestResult = requestResult; | |
this.data = data; | |
} | |
public Boolean getRequestResul() { | |
return requestResult; | |
} | |
public void setRequestTime(Boolean requestResult) { | |
this.requestResult = requestResult; | |
} | |
public T getData() { | |
return data; | |
} | |
public void setData(T data) { | |
this.data = data; | |
} | |
} |
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
public void performGetQuiz(String sexe, boolean isload, OnDataListListener callback) { | |
mData.getQuiz(sexe, isload, new Observer<List<Question>>() { | |
@Override public void onSubscribe(Disposable d) { | |
} | |
@Override public void onNext(List<Question> question) { | |
Timber.d("onNext"); | |
Timber.d(question.get(0).getDesignation()); | |
callback.onSuccess(question); | |
} | |
@Override public void onError(Throwable e) { | |
callback.onFailure(e); | |
} | |
@Override public void onComplete() { | |
Timber.d("onComplete"); | |
} | |
}); | |
} |
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
public class Question { | |
@SerializedName(Constants.ID) | |
private String id; | |
@SerializedName(Constants.DESIGNATION) | |
private String designation; | |
@SerializedName(Constants.ANSWERS) | |
@Nullable | |
private ArrayList<QuestionAnswers> answers; | |
public Question(String id, String designation, ArrayList<QuestionAnswers> answers) { | |
this.answers = answers; | |
this.id = id; | |
this.designation = designation; | |
} |
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
... | |
elInteractor.performGetQuiz(Gender, isload, this); | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment