/** * Copyright 2016 JustWayward Team * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.justwayward.reader.ui.presenter; import android.content.Context; import com.justwayward.reader.api.BookApi; import com.justwayward.reader.base.RxPresenter; import com.justwayward.reader.bean.BookMixAToc; import com.justwayward.reader.bean.Recommend; import com.justwayward.reader.manager.SettingManager; import com.justwayward.reader.ui.contract.RecommendContract; import com.justwayward.reader.utils.ACache; import com.justwayward.reader.utils.LogUtils; import com.justwayward.reader.utils.RxUtil; import com.justwayward.reader.utils.StringUtils; import java.util.List; import javax.inject.Inject; import rx.Observable; import rx.Observer; import rx.Subscription; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; /** * @author yuyh. * @date 2016/8/3. */ public class RecommendPresenter extends RxPresenter<RecommendContract.View> implements RecommendContract.Presenter<RecommendContract.View> { private Context mContext; private BookApi bookApi; @Inject public RecommendPresenter(Context mContext, BookApi bookApi) { this.mContext = mContext; this.bookApi = bookApi; } @Override public void getRecommendList() { String key = StringUtils.creatAcacheKey("recommend-list", SettingManager.getInstance().getUserChooseSex()); Observable<Recommend> fromNetWork = bookApi.getRecommend(SettingManager.getInstance().getUserChooseSex()) .compose(RxUtil.<Recommend>rxCacheListHelper(key)); //依次检查disk、network Subscription rxSubscription = Observable.concat(RxUtil.rxCreateDiskObservable(key, Recommend.class), fromNetWork) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<Recommend>() { @Override public void onNext(Recommend recommend) { if (recommend != null) { List<Recommend.RecommendBooks> list = recommend.books; if (list != null && !list.isEmpty() && mView != null) { mView.showRecommendList(list); } } } @Override public void onCompleted() { mView.complete(); } @Override public void onError(Throwable e) { LogUtils.e("getRecommendList", e.toString()); mView.showError(); } }); addSubscrebe(rxSubscription); } public void getTocList(final String bookId) { bookApi.getBookMixAToc(bookId, "chapters").subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<BookMixAToc>() { @Override public void onNext(BookMixAToc data) { ACache.get(mContext).put(bookId + "bookToc", data); List<BookMixAToc.mixToc.Chapters> list = data.mixToc.chapters; if (list != null && !list.isEmpty() && mView != null) { mView.showBookToc(bookId, list); } } @Override public void onCompleted() { } @Override public void onError(Throwable e) { LogUtils.e("onError: " + e); mView.showError(); } }); } }