Search in sources :

Example 1 with News

use of com.cryptobuddy.ryanbridges.cryptobuddy.models.rest.News 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);
                    }
                }
            });
    }
}
Also used : Action1(rx.functions.Action1) RestResults(com.grizzly.rest.Model.RestResults) ArrayList(java.util.ArrayList) Parcelable(android.os.Parcelable) News(com.cryptobuddy.ryanbridges.cryptobuddy.models.rest.News) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Parcelable (android.os.Parcelable)1 News (com.cryptobuddy.ryanbridges.cryptobuddy.models.rest.News)1 RestResults (com.grizzly.rest.Model.RestResults)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Action1 (rx.functions.Action1)1