58 lines
2.1 KiB
Java
58 lines
2.1 KiB
Java
|
|
package com.ssb.simplitend.articles;
|
||
|
|
|
||
|
|
import androidx.annotation.NonNull;
|
||
|
|
|
||
|
|
import com.ssb.simplitend.apputils.RetrofitHelper;
|
||
|
|
import com.ssb.simplitend.welcome.welcomepatient.mvvm.models.CallResponse;
|
||
|
|
|
||
|
|
import java.util.ArrayList;
|
||
|
|
|
||
|
|
import retrofit2.Call;
|
||
|
|
import retrofit2.Callback;
|
||
|
|
import retrofit2.Response;
|
||
|
|
|
||
|
|
public class ArticlePresenter {
|
||
|
|
|
||
|
|
private static ArticlePresenter articlePresenter;
|
||
|
|
|
||
|
|
private ArticleApiService apiService;
|
||
|
|
|
||
|
|
private ArticlePresenter(){
|
||
|
|
this.apiService = RetrofitHelper.getRetrofit()
|
||
|
|
.create(ArticleApiService.class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static synchronized ArticlePresenter getArticlePresenter() {
|
||
|
|
if (articlePresenter == null) {
|
||
|
|
articlePresenter = new ArticlePresenter();
|
||
|
|
}
|
||
|
|
|
||
|
|
return articlePresenter;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void getArticles(@NonNull ArticleContracts.GetArticleCallback getArticleCallback){
|
||
|
|
apiService.getArticles()
|
||
|
|
.enqueue(new Callback<CallResponse<ArrayList<ArticleResult>>>() {
|
||
|
|
@Override
|
||
|
|
public void onResponse(Call<CallResponse<ArrayList<ArticleResult>>> call, Response<CallResponse<ArrayList<ArticleResult>>> response) {
|
||
|
|
if (response.body() != null){
|
||
|
|
if (response.body().status != 200 || response.body().result == null){
|
||
|
|
getArticleCallback.onArticleFetchFailed(new Exception(), response.body().message);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
getArticleCallback.onArticlesFetched(response.body().result);
|
||
|
|
}else{
|
||
|
|
getArticleCallback.onArticleFetchFailed(new Exception(), "Please try again later.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void onFailure(Call<CallResponse<ArrayList<ArticleResult>>> call, Throwable t) {
|
||
|
|
getArticleCallback.onArticleFetchFailed(new Exception(), "Please try again later.");
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|