387 lines
14 KiB
Java
387 lines
14 KiB
Java
package com.gh.gamecenter.adapter;
|
|
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.support.v7.widget.RecyclerView;
|
|
import android.text.TextUtils;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.inputmethod.InputMethodManager;
|
|
|
|
import com.gh.base.OnRequestCallBackListener;
|
|
import com.lightgame.adapter.BaseRecyclerAdapter;
|
|
import com.gh.common.util.EntranceUtils;
|
|
import com.gh.common.util.ImageUtils;
|
|
import com.gh.common.util.MD5Utils;
|
|
import com.lightgame.utils.Utils;
|
|
import com.gh.gamecenter.NewsDetailActivity;
|
|
import com.gh.gamecenter.R;
|
|
import com.gh.gamecenter.SuggestionActivity;
|
|
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.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;
|
|
|
|
/**
|
|
* Created by khy on 24/05/17.
|
|
*/
|
|
|
|
public class ToolBoxRvAdapter extends BaseRecyclerAdapter {
|
|
|
|
private OnRequestCallBackListener mCallBackListener;
|
|
private OnSearchCallBackListener mSearchListener;
|
|
|
|
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, OnRequestCallBackListener listener, OnSearchCallBackListener searchListener,
|
|
boolean isSearch, String key) {
|
|
super(context);
|
|
this.mIsSearch = isSearch;
|
|
this.mSerchKey = key;
|
|
|
|
mConcernDataSize = 0;
|
|
mLoadKeyOver = false;
|
|
mIsRequestError = false;
|
|
|
|
mSearchListener = searchListener;
|
|
mCallBackListener = listener;
|
|
mEntityList = new ArrayList<>();
|
|
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;
|
|
|
|
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>>() {
|
|
@Override
|
|
public void onResponse(List<ToolBoxEntity> response) {
|
|
super.onResponse(response);
|
|
if (!mLoadKeyOver && response.size() < 20) {
|
|
mEntityList.addAll(response);
|
|
mConcernDataSize = mEntityList.size();
|
|
mLoadKeyOver = true;
|
|
loadData(mIsSearch, 0);
|
|
} else {
|
|
if (response.size() > 0) {
|
|
mEntityList.addAll(response);
|
|
mCallBackListener.loadDone();
|
|
|
|
if (response.size() < 20) {
|
|
mIsOver = true;
|
|
}
|
|
|
|
notifyDataSetChanged();
|
|
}
|
|
mIsLoading = false;
|
|
|
|
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();
|
|
} else {
|
|
mIsNetworkError = true;
|
|
notifyItemChanged(getItemCount() - 1);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public int getItemViewType(int position) {
|
|
if (position == 0) {
|
|
return 0;
|
|
} else if (position == getItemCount() - 1) {
|
|
return 1;
|
|
} else {
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
|
if (viewType == 0) {
|
|
View view = mLayoutInflater.inflate(R.layout.libao_item_search, parent, false);
|
|
return new LibaoSearchViewHolder(view);
|
|
} else if (viewType == 1) {
|
|
View view = LayoutInflater.from(mContext).inflate(R.layout.refresh_footerview, parent, false);
|
|
return new FooterViewHolder(view);
|
|
} else {
|
|
View view = LayoutInflater.from(mContext).inflate(R.layout.toolbox_item, parent, false);
|
|
return new ToolBoxViewHolder(view);
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
|
if (holder instanceof ToolBoxViewHolder) {
|
|
ToolBoxViewHolder viewHolder = (ToolBoxViewHolder) holder;
|
|
ToolBoxEntity toolBoxEntity = mEntityList.get(position - 1);
|
|
initToolBoxViewHolder(viewHolder, toolBoxEntity);
|
|
} else if (holder instanceof FooterViewHolder) {
|
|
FooterViewHolder viewHolder = (FooterViewHolder) holder;
|
|
initFooterViewHolder(viewHolder);
|
|
} else if (holder instanceof LibaoSearchViewHolder) {
|
|
LibaoSearchViewHolder viewHolder = (LibaoSearchViewHolder) holder;
|
|
initSearchViewHolder(viewHolder);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
if (mEntityList.size() > 0) {
|
|
return mEntityList.size() + 2;
|
|
} else {
|
|
return 1;
|
|
}
|
|
|
|
}
|
|
|
|
private void initSearchViewHolder(final LibaoSearchViewHolder viewHolder) {
|
|
|
|
if (mSerchKey != null) {
|
|
viewHolder.searchEt.setText(mSerchKey);
|
|
}
|
|
|
|
if (mIsSearch) {
|
|
viewHolder.backTv.setVisibility(View.VISIBLE);
|
|
} else {
|
|
viewHolder.backTv.setVisibility(View.GONE);
|
|
}
|
|
|
|
viewHolder.backTv.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
mSearchListener.search(false, viewHolder.searchEt.getText().toString());
|
|
}
|
|
});
|
|
|
|
viewHolder.searchTv.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
|
|
if (TextUtils.isEmpty(viewHolder.searchEt.getText().toString())) {
|
|
Utils.toast(mContext, "请输入关键字");
|
|
return;
|
|
}
|
|
mSearchListener.search(true, viewHolder.searchEt.getText().toString());
|
|
|
|
}
|
|
});
|
|
|
|
viewHolder.searchEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
|
|
@Override
|
|
public void onFocusChange(View v, boolean hasFocus) {
|
|
if (!hasFocus) {
|
|
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
imm.hideSoftInputFromWindow(viewHolder.searchEt.getWindowToken(), 0);
|
|
}
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
private void initFooterViewHolder(FooterViewHolder viewHolder) {
|
|
viewHolder.initItemPadding();
|
|
if (mIsNetworkError) {
|
|
viewHolder.lineLeft.setVisibility(View.GONE);
|
|
viewHolder.lineRight.setVisibility(View.GONE);
|
|
viewHolder.loading.setVisibility(View.GONE);
|
|
viewHolder.hint.setText("加载失败,点击重试");
|
|
viewHolder.itemView.setClickable(true);
|
|
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
loadData(mIsSearch, mEntityList.size());
|
|
}
|
|
});
|
|
} else if (mIsOver) {
|
|
viewHolder.lineLeft.setVisibility(View.VISIBLE);
|
|
viewHolder.lineRight.setVisibility(View.VISIBLE);
|
|
viewHolder.loading.setVisibility(View.GONE);
|
|
viewHolder.hint.setText("需要什么工具,点击这里反馈");
|
|
viewHolder.itemView.setClickable(true);
|
|
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
SuggestionActivity.startSuggestionActivity(mContext, 2, null, null);
|
|
}
|
|
});
|
|
} else {
|
|
viewHolder.lineLeft.setVisibility(View.GONE);
|
|
viewHolder.lineRight.setVisibility(View.GONE);
|
|
viewHolder.loading.setVisibility(View.VISIBLE);
|
|
viewHolder.hint.setText(loading);
|
|
viewHolder.itemView.setClickable(false);
|
|
}
|
|
}
|
|
|
|
private void initToolBoxViewHolder(ToolBoxViewHolder viewHolder, final ToolBoxEntity toolBoxEntity) {
|
|
viewHolder.mDes.setText(toolBoxEntity.getDes());
|
|
viewHolder.mTitle.setText(toolBoxEntity.getName());
|
|
ImageUtils.Companion.display(viewHolder.mGameThumb, toolBoxEntity.getIcon());
|
|
|
|
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
String url = toolBoxEntity.getUrl();
|
|
// http://www.ghzhushou.com/article/59291e7ce9a64a496cfd6897.html
|
|
if (url.contains("http://www.ghzhushou.com/article/")) {
|
|
String newsId = url.substring(url.lastIndexOf("/") + 1, url.length() - 5);
|
|
Intent intent = new Intent(mContext, NewsDetailActivity.class);
|
|
intent.putExtra("newsId", newsId);
|
|
intent.putExtra(EntranceUtils.KEY_ENTRANCE, "工具箱列表");
|
|
mContext.startActivity(intent);
|
|
} else {
|
|
Intent intent = new Intent(mContext, WebActivity.class);
|
|
intent.putExtra("url", toolBoxEntity.getUrl());
|
|
intent.putExtra("isTools", true);
|
|
intent.putExtra("gameName", toolBoxEntity.getName());
|
|
intent.putExtra("ToolBoxEntity", toolBoxEntity);
|
|
mContext.startActivity(intent);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
public int dataSize() {
|
|
return mEntityList.size();
|
|
}
|
|
|
|
public boolean isOver() {
|
|
return mIsOver;
|
|
}
|
|
|
|
public boolean isLoading() {
|
|
return mIsLoading;
|
|
}
|
|
|
|
public boolean isNetworkError() {
|
|
return mIsNetworkError;
|
|
}
|
|
|
|
public interface OnSearchCallBackListener {
|
|
void search(boolean isSearch, String searchKey);
|
|
}
|
|
}
|