com.socks.jiandan.cache.FreshNewsCache Java Examples

The following examples show how to use com.socks.jiandan.cache.FreshNewsCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FreshNewsAdapter.java    From JianDanRxJava with Apache License 2.0 6 votes vote down vote up
private void loadDate() {
    Subscription subscription = JDApi.getFreshNews(page)
            .observeOn(Schedulers.io())
            .doOnNext(freshNewses -> {
                if (page == 1) {
                    mFreshNews.clear();
                    FreshNewsCache.getInstance(mActivity).clearAllCache();
                }
                FreshNewsCache.getInstance(mActivity).addResultCache(GsonHelper.toString(freshNewses),
                        page);
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(freshNewses -> {
                mFreshNews.addAll(freshNewses);
                notifyDataSetChanged();
                mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
                mLoadFinisCallBack.loadFinish(null);
            }, e -> {
                mLoadResultCallBack.onError(LoadResultCallBack.ERROR_NET);
                mLoadFinisCallBack.loadFinish(null);
            });
    mActivity.addSubscription(subscription);
}
 
Example #2
Source File: FreshNewsAdapter.java    From JianDanRxJava with Apache License 2.0 6 votes vote down vote up
private void loadFromCache() {
    Observable.create(new Observable.OnSubscribe<ArrayList<FreshNews>>() {
        @Override
        public void call(Subscriber<? super ArrayList<FreshNews>> subscriber) {
            subscriber.onNext(FreshNewsCache.getInstance(mActivity).getCacheByPage(page));
            subscriber.onCompleted();
        }
    }).compose(JDApi.applySchedulers())
            .doOnNext(freshNewses -> {
                if (page == 1) {
                    mFreshNews.clear();
                    ToastHelper.Short(ConstantString.LOAD_NO_NETWORK);
                }
            })
            .subscribe(freshNewses -> {
                mFreshNews.addAll(freshNewses);
                notifyDataSetChanged();
                mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
                mLoadFinisCallBack.loadFinish(null);
            });
}
 
Example #3
Source File: FreshNewsAdapter.java    From JianDan_OkHttpWithVolley with Apache License 2.0 4 votes vote down vote up
private void loadDataByNetworkType() {

        if (NetWorkUtil.isNetWorkConnected(mActivity)) {
            RequestManager.addRequest(new Request4FreshNews(FreshNews.getUrlFreshNews(page),
                    new Response.Listener<ArrayList<FreshNews>>() {
                        @Override
                        public void onResponse(ArrayList<FreshNews> response) {

                            mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
                            mLoadFinisCallBack.loadFinish(null);

                            if (page == 1) {
                                mFreshNews.clear();
                                FreshNewsCache.getInstance(mActivity).clearAllCache();
                            }

                            mFreshNews.addAll(response);
                            notifyDataSetChanged();

                            FreshNewsCache.getInstance(mActivity).addResultCache(JSONParser.toString(response),
                                    page);
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    mLoadResultCallBack.onError(LoadResultCallBack.ERROR_NET, error.getMessage());
                    mLoadFinisCallBack.loadFinish(null);
                }
            }), mActivity);
        } else {
            mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
            mLoadFinisCallBack.loadFinish(null);

            if (page == 1) {
                mFreshNews.clear();
                ShowToast.Short(ConstantString.LOAD_NO_NETWORK);
            }

            mFreshNews.addAll(FreshNewsCache.getInstance(mActivity).getCacheByPage(page));
            notifyDataSetChanged();
        }

    }
 
Example #4
Source File: FreshNewsAdapter.java    From JianDan with Apache License 2.0 4 votes vote down vote up
private void loadDataByNetworkType() {

        if (NetWorkUtil.isNetWorkConnected(mActivity)) {
            RequestManager.addRequest(new Request4FreshNews(FreshNews.getUrlFreshNews(page),
                    new Response.Listener<ArrayList<FreshNews>>() {
                        @Override
                        public void onResponse(ArrayList<FreshNews> response) {

                            mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
                            mLoadFinisCallBack.loadFinish(null);

                            if (page == 1) {
                                mFreshNews.clear();
                                FreshNewsCache.getInstance(mActivity).clearAllCache();
                            }

                            mFreshNews.addAll(response);
                            notifyDataSetChanged();

                            FreshNewsCache.getInstance(mActivity).addResultCache(JSONParser.toString(response),
                                    page);
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    mLoadResultCallBack.onError(LoadResultCallBack.ERROR_NET, error.getMessage());
                    mLoadFinisCallBack.loadFinish(null);
                }
            }), mActivity);
        } else {
            mLoadResultCallBack.onSuccess(LoadResultCallBack.SUCCESS_OK, null);
            mLoadFinisCallBack.loadFinish(null);

            if (page == 1) {
                mFreshNews.clear();
                ShowToast.Short(ConstantString.LOAD_NO_NETWORK);
            }

            mFreshNews.addAll(FreshNewsCache.getInstance(mActivity).getCacheByPage(page));
            notifyDataSetChanged();
        }

    }