diff --git a/app/src/main/java/com/example/jingbin/cloudreader/bean/wanandroid/WanAndroidBannerBean.java b/app/src/main/java/com/example/jingbin/cloudreader/bean/wanandroid/WanAndroidBannerBean.java index 602f24ac..b01f153c 100644 --- a/app/src/main/java/com/example/jingbin/cloudreader/bean/wanandroid/WanAndroidBannerBean.java +++ b/app/src/main/java/com/example/jingbin/cloudreader/bean/wanandroid/WanAndroidBannerBean.java @@ -1,5 +1,6 @@ package com.example.jingbin.cloudreader.bean.wanandroid; +import java.util.ArrayList; import java.util.List; /** @@ -19,6 +20,24 @@ public class WanAndroidBannerBean { private int errorCode; private String errorMsg; private List data; + private ArrayList mBannerImages; + private ArrayList mBannerTitles; + + public ArrayList getmBannerImages() { + return mBannerImages; + } + + public void setmBannerImages(ArrayList mBannerImages) { + this.mBannerImages = mBannerImages; + } + + public ArrayList getmBannerTitles() { + return mBannerTitles; + } + + public void setmBannerTitles(ArrayList mBannerTitles) { + this.mBannerTitles = mBannerTitles; + } public int getErrorCode() { return errorCode; diff --git a/app/src/main/java/com/example/jingbin/cloudreader/ui/menu/CollectArticleFragment.java b/app/src/main/java/com/example/jingbin/cloudreader/ui/menu/CollectArticleFragment.java index 2308a23d..1a2277cc 100644 --- a/app/src/main/java/com/example/jingbin/cloudreader/ui/menu/CollectArticleFragment.java +++ b/app/src/main/java/com/example/jingbin/cloudreader/ui/menu/CollectArticleFragment.java @@ -1,5 +1,7 @@ package com.example.jingbin.cloudreader.ui.menu; +import android.arch.lifecycle.Observer; +import android.arch.lifecycle.ViewModelProviders; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; @@ -12,24 +14,21 @@ import com.example.jingbin.cloudreader.bean.wanandroid.HomeListBean; import com.example.jingbin.cloudreader.databinding.FragmentWanAndroidBinding; import com.example.jingbin.cloudreader.utils.CommonUtils; -import com.example.jingbin.cloudreader.viewmodel.wan.ArticleListListViewModel; -import com.example.jingbin.cloudreader.viewmodel.wan.WanNavigator; +import com.example.jingbin.cloudreader.viewmodel.wan.ArticleListViewModel; import com.example.xrecyclerview.XRecyclerView; -import rx.Subscription; - /** * @author jingbin * @date 2018/09/27. * @description 文章 */ -public class CollectArticleFragment extends BaseFragment implements WanNavigator.ArticleListNavigator { +public class CollectArticleFragment extends BaseFragment { private boolean mIsPrepared; private boolean mIsFirst = true; private FragmentActivity activity; private WanAndroidAdapter mAdapter; - private ArticleListListViewModel viewModel; + private ArticleListViewModel viewModel; @Override public int setContent() { @@ -54,15 +53,13 @@ public void onCreate(Bundle savedInstanceState) { @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); + showContentView(); initRefreshView(); - - viewModel = new ArticleListListViewModel(this); + viewModel = ViewModelProviders.of(this).get(ArticleListViewModel.class); mAdapter.setCollectList(); - // 准备就绪 mIsPrepared = true; - loadData(); } @@ -77,7 +74,7 @@ private void initRefreshView() { bindingView.srlWan.setOnRefreshListener(() -> bindingView.srlWan.postDelayed(() -> { bindingView.xrvWan.reset(); viewModel.setPage(0); - viewModel.getCollectList(); + getCollectList(); }, 300)); bindingView.xrvWan.setLoadingListener(new XRecyclerView.LoadingListener() { @Override @@ -89,7 +86,7 @@ public void onRefresh() { public void onLoadMore() { int page = viewModel.getPage(); viewModel.setPage(++page); - viewModel.getCollectList(); + getCollectList(); } }); } @@ -101,59 +98,43 @@ protected void loadData() { } bindingView.srlWan.setRefreshing(true); - bindingView.srlWan.postDelayed(new Runnable() { + bindingView.srlWan.postDelayed(this::getCollectList, 150); + } + + private void getCollectList() { + viewModel.getCollectList().observe(this, new Observer() { @Override - public void run() { - viewModel.getCollectList(); + public void onChanged(@Nullable HomeListBean homeListBean) { + showContentView(); + if (bindingView.srlWan.isRefreshing()) { + bindingView.srlWan.setRefreshing(false); + } + + if (homeListBean != null) { + if (viewModel.getPage() == 0) { + mAdapter.clear(); + } + mAdapter.addAll(homeListBean.getData().getDatas()); + mAdapter.notifyDataSetChanged(); + bindingView.xrvWan.refreshComplete(); + + mIsFirst = false; + } else { + if (viewModel.getPage() == 0) { + showError(); + } else { + bindingView.xrvWan.refreshComplete(); + bindingView.xrvWan.noMoreLoading(); + } + } } - }, 150); + }); } @Override protected void onRefresh() { bindingView.srlWan.setRefreshing(true); - viewModel.getCollectList(); - } - - @Override - public void loadHomeListFailure() { - showContentView(); - if (bindingView.srlWan.isRefreshing()) { - bindingView.srlWan.setRefreshing(false); - } - if (viewModel.getPage() == 0) { - showError(); - } else { - bindingView.xrvWan.refreshComplete(); - } - } - - @Override - public void showAdapterView(HomeListBean bean) { - if (viewModel.getPage() == 0) { - mAdapter.clear(); - } - mAdapter.addAll(bean.getData().getDatas()); - mAdapter.notifyDataSetChanged(); - bindingView.xrvWan.refreshComplete(); - - mIsFirst = false; - } - - @Override - public void showListNoMoreLoading() { - bindingView.xrvWan.noMoreLoading(); - } - - @Override - public void showLoadSuccessView() { - showContentView(); - bindingView.srlWan.setRefreshing(false); - } - - @Override - public void addRxSubscription(Subscription subscription) { - addSubscription(subscription); + getCollectList(); } } diff --git a/app/src/main/java/com/example/jingbin/cloudreader/ui/wan/child/ArticleListActivity.java b/app/src/main/java/com/example/jingbin/cloudreader/ui/wan/child/ArticleListActivity.java index ec6de4b1..d2c3d8f6 100644 --- a/app/src/main/java/com/example/jingbin/cloudreader/ui/wan/child/ArticleListActivity.java +++ b/app/src/main/java/com/example/jingbin/cloudreader/ui/wan/child/ArticleListActivity.java @@ -1,5 +1,6 @@ package com.example.jingbin.cloudreader.ui.wan.child; +import android.arch.lifecycle.ViewModelProviders; import android.content.Context; import android.content.Intent; import android.os.Bundle; @@ -12,21 +13,18 @@ import com.example.jingbin.cloudreader.bean.wanandroid.HomeListBean; import com.example.jingbin.cloudreader.databinding.FragmentWanAndroidBinding; import com.example.jingbin.cloudreader.utils.CommonUtils; -import com.example.jingbin.cloudreader.viewmodel.wan.ArticleListListViewModel; +import com.example.jingbin.cloudreader.viewmodel.wan.ArticleListViewModel; import com.example.jingbin.cloudreader.viewmodel.wan.WanAndroidListViewModel; -import com.example.jingbin.cloudreader.viewmodel.wan.WanNavigator; import com.example.xrecyclerview.XRecyclerView; -import rx.Subscription; - /** - * 玩安卓文章列表 + * 玩安卓分类文章列表、我的收藏文章列表 * * @author jingbin */ -public class ArticleListActivity extends BaseActivity implements WanNavigator.ArticleListNavigator { +public class ArticleListActivity extends BaseActivity { - private ArticleListListViewModel viewModel; + private ArticleListViewModel viewModel; private WanAndroidListViewModel androidViewModel; private WanAndroidAdapter mAdapter; private int cid = 0; @@ -46,21 +44,44 @@ private void getIntentData() { if (cid != 0) { setTitle(chapterName); - androidViewModel = new WanAndroidListViewModel(); - androidViewModel.setArticleListNavigator(this); + androidViewModel = ViewModelProviders.of(this).get(WanAndroidListViewModel.class); mAdapter.setNoShowChapterName(); } else { setTitle("我的收藏"); - viewModel = new ArticleListListViewModel(this); + viewModel = ViewModelProviders.of(this).get(ArticleListViewModel.class); mAdapter.setCollectList(); } } private void loadData() { if (cid != 0) { - androidViewModel.getHomeList(cid); + androidViewModel.getHomeList(cid).observe(this, this::showContent); } else { - viewModel.getCollectList(); + viewModel.getCollectList().observe(this, this::showContent); + } + } + + private void showContent(HomeListBean homeListBean) { + showContentView(); + if (bindingView.srlWan.isRefreshing()) { + bindingView.srlWan.setRefreshing(false); + } + + if (homeListBean != null) { + if (getViewModel().getPage() == 0) { + mAdapter.clear(); + } + mAdapter.addAll(homeListBean.getData().getDatas()); + mAdapter.notifyDataSetChanged(); + bindingView.xrvWan.refreshComplete(); + + } else { + if (getViewModel().getPage() == 0) { + showError(); + } else { + bindingView.xrvWan.refreshComplete(); + bindingView.xrvWan.noMoreLoading(); + } } } @@ -99,45 +120,6 @@ public void onLoadMore() { }); } - @Override - public void loadHomeListFailure() { - showContentView(); - if (bindingView.srlWan.isRefreshing()) { - bindingView.srlWan.setRefreshing(false); - } - if (getViewModel().getPage() == 0) { - showError(); - } else { - bindingView.xrvWan.refreshComplete(); - } - } - - @Override - public void showAdapterView(HomeListBean bean) { - if (getViewModel().getPage() == 0) { - mAdapter.clear(); - } - mAdapter.addAll(bean.getData().getDatas()); - mAdapter.notifyDataSetChanged(); - bindingView.xrvWan.refreshComplete(); - } - - @Override - public void showListNoMoreLoading() { - bindingView.xrvWan.noMoreLoading(); - } - - @Override - public void showLoadSuccessView() { - showContentView(); - bindingView.srlWan.setRefreshing(false); - } - - @Override - public void addRxSubscription(Subscription subscription) { - addSubscription(subscription); - } - @Override protected void onRefresh() { super.onRefresh(); diff --git a/app/src/main/java/com/example/jingbin/cloudreader/ui/wan/child/BannerFragment.java b/app/src/main/java/com/example/jingbin/cloudreader/ui/wan/child/BannerFragment.java index 1c3856f1..fd4ef3cb 100644 --- a/app/src/main/java/com/example/jingbin/cloudreader/ui/wan/child/BannerFragment.java +++ b/app/src/main/java/com/example/jingbin/cloudreader/ui/wan/child/BannerFragment.java @@ -1,5 +1,7 @@ package com.example.jingbin.cloudreader.ui.wan.child; +import android.arch.lifecycle.Observer; +import android.arch.lifecycle.ViewModelProviders; import android.databinding.DataBindingUtil; import android.os.Bundle; import android.support.annotation.Nullable; @@ -21,20 +23,19 @@ import com.example.jingbin.cloudreader.utils.ImageLoadUtil; import com.example.jingbin.cloudreader.view.webview.WebViewActivity; import com.example.jingbin.cloudreader.viewmodel.wan.WanAndroidListViewModel; -import com.example.jingbin.cloudreader.viewmodel.wan.WanNavigator; import com.example.xrecyclerview.XRecyclerView; import com.youth.banner.BannerConfig; import java.util.ArrayList; import java.util.List; -import rx.Subscription; - /** + * 玩安卓首页 + * * @author jingbin - * Updated by jingbin on 18/02/07. + * Updated on 18/02/07...18/12/05 */ -public class BannerFragment extends BaseFragment implements WanNavigator.BannerNavigator, WanNavigator.ArticleListNavigator { +public class BannerFragment extends BaseFragment { private boolean mIsPrepared; private boolean mIsFirst = true; @@ -56,33 +57,16 @@ public static BannerFragment newInstance() { public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); showContentView(); - viewModel = new WanAndroidListViewModel(); - viewModel.setNavigator(this); - viewModel.setArticleListNavigator(this); + viewModel = ViewModelProviders.of(this).get(WanAndroidListViewModel.class); initRefreshView(); // 准备就绪 mIsPrepared = true; - /** - * 因为启动时先走loadData()再走onActivityCreated, - * 所以此处要额外调用load(),不然最初不会加载内容 - */ loadData(); } private void initRefreshView() { bindingView.srlWan.setColorSchemeColors(CommonUtils.getColor(R.color.colorTheme)); - bindingView.srlWan.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { - @Override - public void onRefresh() { - bindingView.srlWan.postDelayed(() -> { - viewModel.setPage(0); - bindingView.xrvWan.reset(); - loadCustomData(); - }, 350); - - } - }); bindingView.xrvWan.setLayoutManager(new LinearLayoutManager(getActivity())); bindingView.xrvWan.setPullRefreshEnabled(false); bindingView.xrvWan.clearHeader(); @@ -90,28 +74,48 @@ public void onRefresh() { mAdapter.setNoImage(); bindingView.xrvWan.setAdapter(mAdapter); androidBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.header_wan_android, null, false); - viewModel.getWanAndroidBanner(); bindingView.xrvWan.addHeaderView(androidBinding.getRoot()); DensityUtil.formatBannerHeight(androidBinding.banner, androidBinding.llBannerImage); + bindingView.srlWan.setOnRefreshListener(this::swipeRefresh); bindingView.xrvWan.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() { - } @Override public void onLoadMore() { int page = viewModel.getPage(); viewModel.setPage(++page); - loadCustomData(); + getHomeList(); } }); + viewModel.getWanAndroidBanner().observe(this, new Observer() { + @Override + public void onChanged(@Nullable WanAndroidBannerBean bean) { + if (bean != null) { + showBannerView(bean.getmBannerImages(), bean.getmBannerTitles(), bean.getData()); + androidBinding.rlBanner.setVisibility(View.VISIBLE); + } else { + androidBinding.rlBanner.setVisibility(View.GONE); + } + } + }); + } + + /** + * 下拉刷新 + */ + private void swipeRefresh() { + bindingView.srlWan.postDelayed(() -> { + viewModel.setPage(0); + bindingView.xrvWan.reset(); + getHomeList(); + }, 350); } /** * 设置banner图 */ - @Override public void showBannerView(ArrayList bannerImages, ArrayList mBannerTitle, List result) { androidBinding.rlBanner.setVisibility(View.VISIBLE); androidBinding.banner.setBannerTitles(mBannerTitle); @@ -138,61 +142,13 @@ public void showBannerView(ArrayList bannerImages, ArrayList mBa isLoadBanner = true; } - @Override - public void loadBannerFailure() { - androidBinding.rlBanner.setVisibility(View.GONE); - } - - @Override - public void addRxSubscription(Subscription subscription) { - addSubscription(subscription); - } - - @Override - public void loadHomeListFailure() { - showContentView(); - if (bindingView.srlWan.isRefreshing()) { - bindingView.srlWan.setRefreshing(false); - } - if (viewModel.getPage() == 0) { - showError(); - } else { - bindingView.xrvWan.refreshComplete(); - } - } - - @Override - public void showAdapterView(HomeListBean bean) { - if (viewModel.getPage() == 0) { - mAdapter.clear(); - } - mAdapter.addAll(bean.getData().getDatas()); - mAdapter.notifyDataSetChanged(); - bindingView.xrvWan.refreshComplete(); - - if (viewModel.getPage() == 0) { - mIsFirst = false; - } - } - - @Override - public void showListNoMoreLoading() { - bindingView.xrvWan.noMoreLoading(); - } - - @Override - public void showLoadSuccessView() { - showContentView(); - bindingView.srlWan.setRefreshing(false); - } - @Override protected void loadData() { if (!mIsPrepared || !mIsVisible || !mIsFirst) { return; } bindingView.srlWan.setRefreshing(true); - bindingView.srlWan.postDelayed(this::loadCustomData, 500); + bindingView.srlWan.postDelayed(this::getHomeList, 500); } @Override @@ -212,13 +168,41 @@ public void onPause() { } } - private void loadCustomData() { - viewModel.getHomeList(null); + private void getHomeList() { + viewModel.getHomeList(null).observe(this, new Observer() { + @Override + public void onChanged(@Nullable HomeListBean homeListBean) { + showContentView(); + if (bindingView.srlWan.isRefreshing()) { + bindingView.srlWan.setRefreshing(false); + } + + if (homeListBean != null) { + if (viewModel.getPage() == 0) { + mAdapter.clear(); + } + mAdapter.addAll(homeListBean.getData().getDatas()); + mAdapter.notifyDataSetChanged(); + bindingView.xrvWan.refreshComplete(); + + if (viewModel.getPage() == 0) { + mIsFirst = false; + } + } else { + if (viewModel.getPage() == 0) { + showError(); + } else { + bindingView.xrvWan.refreshComplete(); + bindingView.xrvWan.noMoreLoading(); + } + } + } + }); } @Override protected void onRefresh() { bindingView.srlWan.setRefreshing(true); - loadCustomData(); + getHomeList(); } } diff --git a/app/src/main/java/com/example/jingbin/cloudreader/ui/wan/child/TreeFragment.java b/app/src/main/java/com/example/jingbin/cloudreader/ui/wan/child/TreeFragment.java index 2bab50ec..8bafffb0 100644 --- a/app/src/main/java/com/example/jingbin/cloudreader/ui/wan/child/TreeFragment.java +++ b/app/src/main/java/com/example/jingbin/cloudreader/ui/wan/child/TreeFragment.java @@ -14,17 +14,10 @@ import com.example.jingbin.cloudreader.bean.wanandroid.TreeBean; import com.example.jingbin.cloudreader.databinding.FragmentWanAndroidBinding; import com.example.jingbin.cloudreader.databinding.HeaderItemTreeBinding; -import com.example.jingbin.cloudreader.http.HttpClient; import com.example.jingbin.cloudreader.utils.CommonUtils; import com.example.jingbin.cloudreader.utils.DebugUtil; -import com.example.jingbin.cloudreader.viewmodel.wan.NaviViewModel; import com.example.jingbin.cloudreader.viewmodel.wan.TreeViewModel; -import rx.Observer; -import rx.Subscription; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; - /** * @author jingbin * @date 2018/09/15. diff --git a/app/src/main/java/com/example/jingbin/cloudreader/viewmodel/wan/ArticleListListViewModel.java b/app/src/main/java/com/example/jingbin/cloudreader/viewmodel/wan/ArticleListListViewModel.java deleted file mode 100644 index 277f8135..00000000 --- a/app/src/main/java/com/example/jingbin/cloudreader/viewmodel/wan/ArticleListListViewModel.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.example.jingbin.cloudreader.viewmodel.wan; - -import com.example.jingbin.cloudreader.base.BaseListViewModel; -import com.example.jingbin.cloudreader.bean.wanandroid.HomeListBean; -import com.example.jingbin.cloudreader.http.HttpClient; - -import rx.Observer; -import rx.Subscription; -import rx.android.schedulers.AndroidSchedulers; -import rx.schedulers.Schedulers; - -/** - * @author jingbin - * @data 2018/5/9 - * @Description 文章列表ViewModel - */ - -public class ArticleListListViewModel extends BaseListViewModel { - - private WanNavigator.ArticleListNavigator navigator; - - public ArticleListListViewModel(WanNavigator.ArticleListNavigator navigator) { - this.navigator = navigator; - } - - /** - * 我的收藏 - */ - public void getCollectList() { - Subscription subscribe = HttpClient.Builder.getWanAndroidServer().getCollectList(mPage) - .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) - .subscribe(new Observer() { - @Override - public void onCompleted() { - } - - @Override - public void onError(Throwable e) { - navigator.loadHomeListFailure(); - } - - @Override - public void onNext(HomeListBean bean) { - navigator.showLoadSuccessView(); - if (mPage == 0) { - if (bean == null || bean.getData() == null || bean.getData().getDatas() == null || bean.getData().getDatas().size() <= 0) { - navigator.loadHomeListFailure(); - return; - } - } else { - if (bean == null || bean.getData() == null || bean.getData().getDatas() == null || bean.getData().getDatas().size() <= 0) { - navigator.showListNoMoreLoading(); - return; - } - } - navigator.showAdapterView(bean); - } - }); - navigator.addRxSubscription(subscribe); - } - - public void onDestroy() { - navigator = null; - } -} diff --git a/app/src/main/java/com/example/jingbin/cloudreader/viewmodel/wan/ArticleListViewModel.java b/app/src/main/java/com/example/jingbin/cloudreader/viewmodel/wan/ArticleListViewModel.java new file mode 100644 index 00000000..9ee5b482 --- /dev/null +++ b/app/src/main/java/com/example/jingbin/cloudreader/viewmodel/wan/ArticleListViewModel.java @@ -0,0 +1,57 @@ +package com.example.jingbin.cloudreader.viewmodel.wan; + +import android.arch.lifecycle.MutableLiveData; + +import com.example.jingbin.cloudreader.base.BaseListViewModel; +import com.example.jingbin.cloudreader.bean.wanandroid.HomeListBean; +import com.example.jingbin.cloudreader.bean.wanandroid.WanAndroidBannerBean; +import com.example.jingbin.cloudreader.http.HttpClient; + +import rx.Observer; +import rx.Subscription; +import rx.android.schedulers.AndroidSchedulers; +import rx.schedulers.Schedulers; + +/** + * @author jingbin + * @data 2018/5/9 + * @Description 文章列表ViewModel + */ + +public class ArticleListViewModel extends BaseListViewModel { + + /** + * 我的收藏 + */ + public MutableLiveData getCollectList() { + final MutableLiveData data = new MutableLiveData<>(); + HttpClient.Builder.getWanAndroidServer().getCollectList(mPage) + .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) + .subscribe(new Observer() { + @Override + public void onCompleted() { + } + + @Override + public void onError(Throwable e) { + data.setValue(null); + if (mPage > 0) { + mPage--; + } + } + + @Override + public void onNext(HomeListBean bean) { + if (bean == null + || bean.getData() == null + || bean.getData().getDatas() == null + || bean.getData().getDatas().size() <= 0) { + data.setValue(null); + } else { + data.setValue(bean); + } + } + }); + return data; + } +} diff --git a/app/src/main/java/com/example/jingbin/cloudreader/viewmodel/wan/WanAndroidListViewModel.java b/app/src/main/java/com/example/jingbin/cloudreader/viewmodel/wan/WanAndroidListViewModel.java index 5ae1b79b..c548436e 100644 --- a/app/src/main/java/com/example/jingbin/cloudreader/viewmodel/wan/WanAndroidListViewModel.java +++ b/app/src/main/java/com/example/jingbin/cloudreader/viewmodel/wan/WanAndroidListViewModel.java @@ -1,7 +1,10 @@ package com.example.jingbin.cloudreader.viewmodel.wan; +import android.arch.lifecycle.MutableLiveData; + import com.example.jingbin.cloudreader.base.BaseListViewModel; import com.example.jingbin.cloudreader.bean.wanandroid.HomeListBean; +import com.example.jingbin.cloudreader.bean.wanandroid.TreeBean; import com.example.jingbin.cloudreader.bean.wanandroid.WanAndroidBannerBean; import com.example.jingbin.cloudreader.http.HttpClient; @@ -21,21 +24,9 @@ public class WanAndroidListViewModel extends BaseListViewModel { - private WanNavigator.BannerNavigator loadBannerNavigator; - private WanNavigator.ArticleListNavigator navigator; - private ArrayList mBannerImages; - private ArrayList mBannerTitles; - - public void setNavigator(WanNavigator.BannerNavigator navigator) { - this.loadBannerNavigator = navigator; - } - - public void setArticleListNavigator(WanNavigator.ArticleListNavigator navigator) { - this.navigator = navigator; - } - - public void getWanAndroidBanner() { - Subscription subscribe = HttpClient.Builder.getWanAndroidServer().getWanAndroidBanner() + public MutableLiveData getWanAndroidBanner() { + final MutableLiveData data = new MutableLiveData<>(); + HttpClient.Builder.getWanAndroidServer().getWanAndroidBanner() .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer() { @Override @@ -44,49 +35,41 @@ public void onCompleted() { @Override public void onError(Throwable e) { - loadBannerNavigator.loadBannerFailure(); - + data.setValue(null); } @Override public void onNext(WanAndroidBannerBean wanAndroidBannerBean) { - if (mBannerImages == null) { - mBannerImages = new ArrayList(); - } else { - mBannerImages.clear(); - } - if (mBannerTitles == null) { - mBannerTitles = new ArrayList(); - } else { - mBannerTitles.clear(); - } - List result = null; - if (wanAndroidBannerBean != null && wanAndroidBannerBean.getData() != null) { - result = wanAndroidBannerBean.getData(); + ArrayList mBannerImages = new ArrayList(); + ArrayList mBannerTitles = new ArrayList(); + if (wanAndroidBannerBean != null + && wanAndroidBannerBean.getData() != null + && wanAndroidBannerBean.getData().size() > 0) { + List result = wanAndroidBannerBean.getData(); if (result != null && result.size() > 0) { for (int i = 0; i < result.size(); i++) { //获取所有图片 mBannerImages.add(result.get(i).getImagePath()); mBannerTitles.add(result.get(i).getTitle()); } - loadBannerNavigator.showBannerView(mBannerImages, mBannerTitles, result); + wanAndroidBannerBean.setmBannerImages(mBannerImages); + wanAndroidBannerBean.setmBannerTitles(mBannerTitles); + data.setValue(wanAndroidBannerBean); } + } else { + data.setValue(null); } - if (result == null) { - loadBannerNavigator.loadBannerFailure(); - } - } }); - loadBannerNavigator.addRxSubscription(subscribe); + return data; } - /** * @param cid 体系id */ - public void getHomeList(Integer cid) { - Subscription subscribe = HttpClient.Builder.getWanAndroidServer().getHomeList(mPage, cid) + public MutableLiveData getHomeList(Integer cid) { + final MutableLiveData listData = new MutableLiveData<>(); + HttpClient.Builder.getWanAndroidServer().getHomeList(mPage, cid) .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer() { @Override @@ -95,31 +78,25 @@ public void onCompleted() { @Override public void onError(Throwable e) { - navigator.loadHomeListFailure(); - + listData.setValue(null); + if (mPage > 0) { + mPage--; + } } @Override public void onNext(HomeListBean bean) { - navigator.showLoadSuccessView(); - if (mPage == 0) { - if (bean == null || bean.getData() == null || bean.getData().getDatas() == null || bean.getData().getDatas().size() <= 0) { - navigator.loadHomeListFailure(); - return; - } + if (bean == null + || bean.getData() == null + || bean.getData().getDatas() == null + || bean.getData().getDatas().size() <= 0) { + listData.setValue(null); } else { - if (bean == null || bean.getData() == null || bean.getData().getDatas() == null || bean.getData().getDatas().size() <= 0) { - navigator.showListNoMoreLoading(); - return; - } + listData.setValue(bean); } - navigator.showAdapterView(bean); } }); - navigator.addRxSubscription(subscribe); + return listData; } - public void onDestroy() { - navigator = null; - } }