工具想搜索关注的游戏优先, 开服表修改显示逻辑
This commit is contained in:
@ -12,6 +12,7 @@ import android.view.inputmethod.InputMethodManager;
|
||||
import com.gh.base.AppController;
|
||||
import com.gh.common.util.EntranceUtils;
|
||||
import com.gh.common.util.ImageUtils;
|
||||
import com.gh.common.util.MD5Utils;
|
||||
import com.gh.common.util.Utils;
|
||||
import com.gh.gamecenter.NewsDetailActivity;
|
||||
import com.gh.gamecenter.R;
|
||||
@ -20,17 +21,24 @@ import com.gh.gamecenter.WebActivity;
|
||||
import com.gh.gamecenter.adapter.viewholder.FooterViewHolder;
|
||||
import com.gh.gamecenter.adapter.viewholder.LibaoSearchViewHolder;
|
||||
import com.gh.gamecenter.adapter.viewholder.ToolBoxViewHolder;
|
||||
import com.gh.gamecenter.db.info.ConcernInfo;
|
||||
import com.gh.gamecenter.entity.ToolBoxEntity;
|
||||
import com.gh.gamecenter.listener.OnCallBackListener;
|
||||
import com.gh.gamecenter.manager.ConcernManager;
|
||||
import com.gh.gamecenter.retrofit.ObservableUtil;
|
||||
import com.gh.gamecenter.retrofit.Response;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import retrofit2.HttpException;
|
||||
import rx.Observable;
|
||||
import rx.Subscriber;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
import rx.functions.Action1;
|
||||
import rx.schedulers.Schedulers;
|
||||
|
||||
import static com.gh.gamecenter.R.string.loading;
|
||||
@ -47,11 +55,16 @@ public class ToolBoxRvAdapter extends BaseRecyclerAdapter {
|
||||
private List<ToolBoxEntity> mEntityList;
|
||||
|
||||
private String mSerchKey;
|
||||
private String mConcernKey;
|
||||
|
||||
private int mConcernDataSize;
|
||||
|
||||
private boolean mIsSearch;
|
||||
private boolean mIsLoading;
|
||||
private boolean mIsOver;
|
||||
private boolean mIsNetworkError;
|
||||
private boolean mLoadKeyOver;
|
||||
private boolean mIsRequestError; // key请求返回409
|
||||
|
||||
public ToolBoxRvAdapter(Context context, OnCallBackListener listener, OnSearchCallBackListener searchListener,
|
||||
boolean isSearch, String key) {
|
||||
@ -59,49 +72,128 @@ public class ToolBoxRvAdapter extends BaseRecyclerAdapter {
|
||||
this.mIsSearch = isSearch;
|
||||
this.mSerchKey = key;
|
||||
|
||||
mConcernDataSize = 0;
|
||||
mLoadKeyOver = false;
|
||||
mIsRequestError = false;
|
||||
|
||||
mSearchListener = searchListener;
|
||||
mCallBackListener = listener;
|
||||
mEntityList = new ArrayList<>();
|
||||
loadData(isSearch, 0);
|
||||
loadDataByKey();
|
||||
}
|
||||
|
||||
private void loadDataByKey() {
|
||||
if (mIsLoading) {
|
||||
return;
|
||||
}
|
||||
mIsLoading = true;
|
||||
if (TextUtils.isEmpty(mConcernKey)) {
|
||||
ObservableUtil.computation(new Observable.OnSubscribe<String>() {
|
||||
@Override
|
||||
public void call(Subscriber<? super String> subscriber) {
|
||||
ConcernManager concernManager = new ConcernManager(mContext);
|
||||
List<ConcernInfo> concernList = concernManager.getConcernGame();
|
||||
List<String> gameIdList = new ArrayList<>();
|
||||
if (concernList == null || concernList.isEmpty()) {
|
||||
subscriber.onNext(null);
|
||||
} else {
|
||||
for (ConcernInfo concernInfo : concernList) {
|
||||
gameIdList.add(concernInfo.getId());
|
||||
}
|
||||
|
||||
// 对数据进行排序
|
||||
Collections.sort(gameIdList, new Comparator<String>() {
|
||||
@Override
|
||||
public int compare(String lhs, String rhs) {
|
||||
return lhs.compareTo(rhs);
|
||||
}
|
||||
});
|
||||
|
||||
StringBuilder keyBuilder = new StringBuilder();
|
||||
StringBuilder idsBuilder = new StringBuilder();
|
||||
for (int i = 0; i < gameIdList.size(); i++) {
|
||||
keyBuilder.append(gameIdList.get(i));
|
||||
if (i < 5) {
|
||||
idsBuilder.append(gameIdList.get(i));
|
||||
idsBuilder.append("-");
|
||||
}
|
||||
}
|
||||
mConcernKey = MD5Utils.getContentMD5(keyBuilder.toString());
|
||||
subscriber.onNext("");
|
||||
}
|
||||
subscriber.onCompleted();
|
||||
}
|
||||
}, new Action1<String>() {
|
||||
@Override
|
||||
public void call(String s) {
|
||||
if (s == null) {
|
||||
mLoadKeyOver = true;
|
||||
}
|
||||
loadData(mIsSearch, 0);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
loadData(mIsSearch, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void loadData(boolean isSearch, int offset) {
|
||||
mIsLoading = true;
|
||||
|
||||
Observable<List<ToolBoxEntity>> api;
|
||||
if (isSearch) {
|
||||
api = RetrofitManager.getApi().getToolBoxDataSearch(offset, mSerchKey);
|
||||
} else {
|
||||
api = RetrofitManager.getApi().getToolBoxData(offset);
|
||||
if (offset > 0 && !isSearch && mLoadKeyOver) {
|
||||
offset = offset - mConcernDataSize;
|
||||
}
|
||||
|
||||
Observable<List<ToolBoxEntity>> api;
|
||||
if (mIsRequestError || TextUtils.isEmpty(mConcernKey)) {
|
||||
api = RetrofitManager.getApi().getToolBoxData(offset, mSerchKey);
|
||||
} else if (!mLoadKeyOver) {
|
||||
api = RetrofitManager.getApi().getToolBoxData(offset, mConcernKey, mSerchKey);
|
||||
} else {
|
||||
api = RetrofitManager.getApi().getToolBoxDataExclude(offset, mConcernKey, mSerchKey);
|
||||
}
|
||||
final int finalOffset = offset;
|
||||
api
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<List<ToolBoxEntity>>(){
|
||||
.subscribe(new Response<List<ToolBoxEntity>>() {
|
||||
@Override
|
||||
public void onResponse(List<ToolBoxEntity> response) {
|
||||
super.onResponse(response);
|
||||
if (response.size() > 0) {
|
||||
if (!mLoadKeyOver && response.size() < 20) {
|
||||
mEntityList.addAll(response);
|
||||
mCallBackListener.loadDone();
|
||||
mConcernDataSize = mEntityList.size();
|
||||
mLoadKeyOver = true;
|
||||
loadData(mIsSearch, 0);
|
||||
} else {
|
||||
if (response.size() > 0) {
|
||||
mEntityList.addAll(response);
|
||||
mCallBackListener.loadDone();
|
||||
|
||||
if (response.size() < 20) {
|
||||
mIsOver = true;
|
||||
if (response.size() < 20) {
|
||||
mIsOver = true;
|
||||
}
|
||||
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
mIsLoading = false;
|
||||
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
mIsLoading = false;
|
||||
|
||||
if (mEntityList.size() == 0) {
|
||||
mCallBackListener.loadEmpty();
|
||||
if (mEntityList.size() == 0) {
|
||||
mCallBackListener.loadEmpty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
super.onFailure(e);
|
||||
if (e != null && e.code() == 409) {
|
||||
mIsRequestError = true;
|
||||
loadData(mIsSearch, finalOffset);
|
||||
return;
|
||||
}
|
||||
|
||||
mIsLoading = false;
|
||||
if (mEntityList.size() == 0) {
|
||||
mCallBackListener.loadError();
|
||||
@ -115,7 +207,7 @@ public class ToolBoxRvAdapter extends BaseRecyclerAdapter {
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
if (position == 0 ) {
|
||||
if (position == 0) {
|
||||
return 0;
|
||||
} else if (position == getItemCount() - 1) {
|
||||
return 1;
|
||||
@ -220,7 +312,7 @@ public class ToolBoxRvAdapter extends BaseRecyclerAdapter {
|
||||
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
loadData(false, mEntityList.size());
|
||||
loadData(mIsSearch, mEntityList.size());
|
||||
}
|
||||
});
|
||||
} else if (mIsOver) {
|
||||
|
||||
Reference in New Issue
Block a user