use of com.grizzly.rest.Model.RestResults in project CryptoBuddy by Patchett.
the class NewsListActivity method getNewsObservable.
public void getNewsObservable(int whatToDo, boolean cache) {
// Example of framework isolation by using observables
// An standard Rx Action.
Action1<News[]> subscriber = new Action1<News[]>() {
@Override
public void call(News[] newsRestResults) {
List<NewsItem> myNews = new ArrayList<>();
if (newsRestResults != null && newsRestResults.length > 0) {
Parcelable recyclerViewState;
recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
for (News news : newsRestResults) {
NewsItem newsItem = new NewsItem(news.getTitle(), news.getUrl(), news.getBody(), news.getImageurl(), news.getSource(), news.getPublishedOn());
if (!myNews.contains(newsItem))
myNews.add(newsItem);
}
adapter.setData(myNews);
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setRefreshing(false);
recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
Log.e("News", "call successful");
} else {
swipeRefreshLayout.setRefreshing(false);
Log.e("News", "call failed");
}
}
};
switch(whatToDo) {
case 1:
// Wrapped observable call
NewsService.getObservableNews(this, true, new Action1<RestResults<News[]>>() {
@Override
public void call(RestResults<News[]> newsRestResults) {
Parcelable recyclerViewState;
recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
if (newsRestResults.isSuccessful()) {
List<NewsItem> myNews = new ArrayList<>();
for (News news : newsRestResults.getResultEntity()) {
NewsItem newsItem = new NewsItem(news.getTitle(), news.getUrl(), news.getBody(), news.getImageurl(), news.getSource(), news.getPublishedOn());
if (!myNews.contains(newsItem))
myNews.add(newsItem);
}
adapter.setData(myNews);
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setRefreshing(false);
recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
} else {
swipeRefreshLayout.setRefreshing(false);
}
}
});
break;
case 2:
// Observable instance from EasyRest
if (newsObservable == null) {
newsObservable = NewsService.getPlainObservableNews(this, cache).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
newsObservable.subscribe(subscriber);
break;
default:
// Wrapped observable call
NewsService.getObservableNews(this, true, new Action1<RestResults<News[]>>() {
@Override
public void call(RestResults<News[]> newsRestResults) {
Parcelable recyclerViewState;
recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
if (newsRestResults.isSuccessful()) {
List<NewsItem> myNews = new ArrayList<>();
for (News news : newsRestResults.getResultEntity()) {
NewsItem newsItem = new NewsItem(news.getTitle(), news.getUrl(), news.getBody(), news.getImageurl(), news.getSource(), news.getPublishedOn());
if (!myNews.contains(newsItem))
myNews.add(newsItem);
}
adapter.setData(myNews);
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setRefreshing(false);
recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
} else {
swipeRefreshLayout.setRefreshing(false);
}
}
});
}
}
Aggregations