Skip to content

Commit

Permalink
fix BannerFragment #93 bug & revised mvvm
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed Dec 5, 2018
1 parent 6b1045c commit 5bd3e31
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 316 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.jingbin.cloudreader.bean.wanandroid;

import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -19,6 +20,24 @@ public class WanAndroidBannerBean {
private int errorCode;
private String errorMsg;
private List<DataBean> data;
private ArrayList<String> mBannerImages;
private ArrayList<String> mBannerTitles;

public ArrayList<String> getmBannerImages() {
return mBannerImages;
}

public void setmBannerImages(ArrayList<String> mBannerImages) {
this.mBannerImages = mBannerImages;
}

public ArrayList<String> getmBannerTitles() {
return mBannerTitles;
}

public void setmBannerTitles(ArrayList<String> mBannerTitles) {
this.mBannerTitles = mBannerTitles;
}

public int getErrorCode() {
return errorCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<FragmentWanAndroidBinding> implements WanNavigator.ArticleListNavigator {
public class CollectArticleFragment extends BaseFragment<FragmentWanAndroidBinding> {

private boolean mIsPrepared;
private boolean mIsFirst = true;
private FragmentActivity activity;
private WanAndroidAdapter mAdapter;
private ArticleListListViewModel viewModel;
private ArticleListViewModel viewModel;

@Override
public int setContent() {
Expand All @@ -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();
}

Expand All @@ -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
Expand All @@ -89,7 +86,7 @@ public void onRefresh() {
public void onLoadMore() {
int page = viewModel.getPage();
viewModel.setPage(++page);
viewModel.getCollectList();
getCollectList();
}
});
}
Expand All @@ -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<HomeListBean>() {
@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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<FragmentWanAndroidBinding> implements WanNavigator.ArticleListNavigator {
public class ArticleListActivity extends BaseActivity<FragmentWanAndroidBinding> {

private ArticleListListViewModel viewModel;
private ArticleListViewModel viewModel;
private WanAndroidListViewModel androidViewModel;
private WanAndroidAdapter mAdapter;
private int cid = 0;
Expand All @@ -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();
}
}
}

Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 5bd3e31

Please sign in to comment.