Files
assistant-android/app/src/main/java/com/gh/gamecenter/adapter/LibaoDetailAdapter.java

367 lines
16 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.gh.gamecenter.adapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.gh.common.util.DisplayUtils;
import com.gh.common.util.ImageUtils;
import com.gh.common.util.LibaoUtils;
import com.gh.common.util.PlatformUtils;
import com.gh.gamecenter.LibaoDetailActivity;
import com.gh.gamecenter.SuggestionActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.adapter.viewholder.FooterViewHolder;
import com.gh.gamecenter.adapter.viewholder.LibaoDetailTopViewHolder;
import com.gh.gamecenter.db.LibaoDao;
import com.gh.gamecenter.db.info.LibaoInfo;
import com.gh.gamecenter.entity.LibaoDetailEntity;
import com.gh.gamecenter.entity.LibaoEntity;
import com.gh.gamecenter.entity.LibaoStatusEntity;
import com.gh.gamecenter.gamedetail.GameDetailNewsViewHolder;
import com.gh.gamecenter.listener.OnCallBackListener;
import com.gh.gamecenter.retrofit.Response;
import com.gh.gamecenter.retrofit.RetrofitManager;
import java.text.SimpleDateFormat;
import java.util.List;
import retrofit2.adapter.rxjava.HttpException;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
* Created by khy on 2016/12/13.
*/
public class LibaoDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private OnCallBackListener mCallBackListener;
private OnCodeScrollListener scrollListener;
private TextView mDownloadTv;
private LibaoEntity mLibaoEntity;
private LibaoDetailEntity mLibaoDetailEntity;
private LibaoDao mLibaoDao;
private List<LibaoInfo> mLibaoInfos;
private String entrance;
public LibaoDetailAdapter(LibaoDetailActivity libaoDetailActivity, LibaoEntity libaoEntity, TextView tvDownload) {
this.mContext = libaoDetailActivity;
this.mCallBackListener = libaoDetailActivity;
this.scrollListener = libaoDetailActivity;
this.mLibaoEntity = libaoEntity;
this.mDownloadTv = tvDownload;
entrance = libaoDetailActivity.getIntent().getStringExtra("entrance");
mLibaoDao = new LibaoDao(mContext);
mLibaoInfos = mLibaoDao.getAll();
}
public void addLibaoDetail() {
RetrofitManager.getLibao().getLibaoDetail(mLibaoEntity.getId())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Response<LibaoDetailEntity>() {
@Override
public void onResponse(LibaoDetailEntity response) {
mLibaoDetailEntity = response;
mCallBackListener.loadDone();
notifyDataSetChanged();
}
@Override
public void onFailure(HttpException e) {
mCallBackListener.loadEmpty();
}
});
}
@Override
public int getItemViewType(int position) {
if (position == getItemCount() - 1) {
return 100;
}
return position;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 0) {
View view = LayoutInflater.from(mContext).inflate(R.layout.libaodetail_item_top, parent, false);
return new LibaoDetailTopViewHolder(view);
} else if (viewType == 100) {
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.gamedetail_item_news, parent, false);
return new GameDetailNewsViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof LibaoDetailTopViewHolder) {
initLibaoDetailTop((LibaoDetailTopViewHolder)holder);
} else if (holder instanceof GameDetailNewsViewHolder) {
initLibaoDetailContent((GameDetailNewsViewHolder)holder, position);
} else if (holder instanceof FooterViewHolder) {
FooterViewHolder viewHolder = (FooterViewHolder) holder;
viewHolder.loading.setVisibility(View.GONE);
viewHolder.hint.setText("此礼包有问题?点击反馈");
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, SuggestionActivity.class);
intent.putExtra("suggestType", 1);
intent.putExtra("suggestHintType", "libao");
intent.putExtra("content", mLibaoEntity.getGame().getName() + "" + mLibaoEntity.getName() + "有问题:");
mContext.startActivity(intent);
}
});
}
}
@Override
public int getItemCount() {
int index = 1;
if (mLibaoDetailEntity != null) {
if (mLibaoEntity != null) {
index = index + 2;
}
if (mLibaoDetailEntity.getTime() != null || mLibaoDetailEntity.getInstallRequired()) {
index ++;
}
if (mLibaoDetailEntity.getDes() != null) {
index ++;
}
}
return index;
}
private void initLibaoDetailContent(GameDetailNewsViewHolder holder, int position) {
holder.gamedetail_item_news_more.setVisibility(View.GONE);
holder.gamedetail_item_line.setVisibility(View.VISIBLE);
holder.gamedetail_item_news_list.setPadding(0, DisplayUtils.dip2px(mContext, 10)
, 0, DisplayUtils.dip2px(mContext, 10));
holder.gamedetail_item_news_list.removeAllViews();
if (mLibaoEntity.getContent() != null && position == 1) {
holder.gamedetail_item_news_title.setText("礼包内容");
TextView textView = new TextView(mContext);
textView.setTextColor(Color.parseColor("#717171"));
textView.setText(Html.fromHtml(mLibaoEntity.getContent()));
holder.gamedetail_item_news_list.addView(textView);
} else if (mLibaoDetailEntity != null && position == 2) {
holder.gamedetail_item_news_title.setText("领取规则");
if (mLibaoDetailEntity.getInstallRequired()) {
TextView textView = new TextView(mContext);
textView.setTextColor(Color.parseColor("#717171"));
Spanned content;
if (TextUtils.isEmpty(mLibaoEntity.getPlatform())) {
content = Html.fromHtml("领取条件:" + "<font color=\"#06D0A8\">安装《" +
mLibaoEntity.getGame().getName() + "》</font>");
} else {
content = Html.fromHtml("领取条件:" + "<font color=\"#06D0A8\">安装《" +
mLibaoEntity.getGame().getName() + "" + PlatformUtils.getInstance(mContext)
.getPlatformName(mLibaoEntity.getPlatform()) + "版</font>");
}
textView.setText(content);
holder.gamedetail_item_news_list.addView(textView);
}
if (mLibaoDetailEntity.getTime() != null) {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
String start = format.format((mLibaoDetailEntity.getTime().getStart() * 1000));
String end = format.format((mLibaoDetailEntity.getTime().getEnd() * 1000));
TextView tvTime = new TextView(mContext);
tvTime.setTextColor(Color.parseColor("#717171"));
tvTime.setText("领取时间:" + start + " - " + end);
holder.gamedetail_item_news_list.addView(tvTime);
}
} else if (mLibaoDetailEntity.getDes() != null && position == getItemCount() - 2) {
holder.gamedetail_item_news_title.setText("使用说明");
TextView desTv = new TextView(mContext);
desTv.setTextColor(Color.parseColor("#717171"));
desTv.setText(Html.fromHtml(mLibaoDetailEntity.getDes()));
holder.gamedetail_item_news_list.addView(desTv);
}
}
private void initLibaoDetailTop(LibaoDetailTopViewHolder holder) {
ImageUtils.display(holder.libaoGameIcon, mLibaoEntity.getIcon());
holder.libaoName.setText(mLibaoEntity.getName());
if (TextUtils.isEmpty(mLibaoEntity.getPlatform())) {
holder.libaoGameName.setText(mLibaoEntity.getGame().getName());
} else {
holder.libaoGameName.setText(mLibaoEntity.getGame().getName() + " - " +PlatformUtils.getInstance(mContext)
.getPlatformName(mLibaoEntity.getPlatform()));
}
holder.libaoCodeRv.setLayoutManager(new LinearLayoutManager(mContext));
int count = (int)(((float)mLibaoEntity.getAvailable()/(float)mLibaoEntity.getTotal()) * 100);
Spanned content = null;
String status = mLibaoEntity.getStatus();
if ("coming".equals(status) || "finish".equals(status)) {
content = Html.fromHtml("剩余:--");
} else if ("ling".equals(status)) {
content = Html.fromHtml("剩余:" + "<font color=\"#06D0A8\">" + count + "%" + "</font>");
} else if ("tao".equals(status)) {
content = Html.fromHtml("剩余:" + count + "%");
} else if ("used_up".equals(status)) {
content = Html.fromHtml("剩余0% ");
}
//已领取或已淘号
for (final LibaoInfo libaoInfo : mLibaoInfos) {
if (TextUtils.isEmpty(libaoInfo.getLibaoId()) || TextUtils.isEmpty(mLibaoEntity.getId())) {
continue;
}
if (libaoInfo.getLibaoId().equals(mLibaoEntity.getId())) {
LibaoUtils.initLibaoBtn(holder.libaoCopyBtn, mLibaoEntity, mLibaoDao,
mLibaoDetailEntity.getInstallRequired(), this, entrance + "+(礼包详情[" + mLibaoEntity.getName() + "])");
// holder.libaoCopyBtn.setBackgroundResource(R.drawable.textview_blue_style);
// holder.libaoCopyBtn.setTextColor(Color.WHITE);
// holder.libaoCopyBtn.setText("复制");
// holder.libaoCopyBtn.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// if (mLibaoDetailEntity != null && mLibaoDetailEntity.getInstallRequired()) {
// Spanned msg;
// if ("ling".equals(libaoInfo.getStatus())) {
// msg = Html.fromHtml("礼包码:" + "<font color=\"#00B7FA\">" + libaoInfo.getCode() + "</font>" + " 复制成功" +
// "<br/>请尽快进入游戏兑换");
// } else {
// msg = Html.fromHtml("礼包码:" +"<font color=\"#ffb13c\">" + libaoInfo.getCode() + "</font>" + " 复制成功" +
// "<br/>淘号礼包不保证可用,请尽快进入游戏尝试兑换");
// }
//
// LibaoUtils.lunningAppDialog(mContext, msg, mLibaoEntity);
// }
// LibaoUtils.copyLink(libaoInfo.getCode(), mContext);
// }
// });
// Spanned libaoCode;
// if ("ling".equals(libaoInfo.getStatus())) {
// libaoCode = Html.fromHtml("已领取:" + "<font color=\"#00B7FA\">" +libaoInfo.getCode()+ "</font>");
// if (count == 0) {
// content = Html.fromHtml("剩余:" + count + "%");
// } else {
// content = Html.fromHtml("剩余:" + "<font color=\"#06D0A8\">" + count + "%" + "</font>");
// }
// } else {
// libaoCode = Html.fromHtml("已淘号:" + "<font color=\"#ffb13c\">" +libaoInfo.getCode()+ "</font>");
// content = Html.fromHtml("剩余:" + count + "%" );
// }
// holder.libaoCode.setVisibility(View.VISIBLE);
// holder.libaoCode.setText(libaoCode);
holder.libaoDes.setText(content);
final List<LibaoInfo> codes = mLibaoDao.findCodes(mLibaoEntity.getId());
if (codes == null && codes.isEmpty()) return;
holder.libaoCodeRv.setVisibility(View.VISIBLE);
holder.libaoCodeRv.setAdapter(new LiBaoCodeAdapter(mContext, codes));
ViewGroup.LayoutParams params = holder.libaoCodeRv.getLayoutParams();
if (codes.size() <= 3) {
params.height = codes.size() * DisplayUtils.dip2px(mContext, 40);
} else {
params.height = 3 * DisplayUtils.dip2px(mContext, 40);
}
holder.libaoCodeRv.setLayoutParams(params);
holder.libaoCodeRv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
scrollListener.isScroll(false);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
scrollListener.isScroll(true);
}
return false;
}
});
return;
}
}
holder.libaoDes.setText(content);
if (mLibaoEntity.getStatus() != null && mLibaoDetailEntity != null) {
LibaoUtils.initLibaoBtn(holder.libaoCopyBtn, mLibaoEntity, mLibaoDao,
mLibaoDetailEntity.getInstallRequired(), this, entrance + "+(礼包详情[" + mLibaoEntity.getName() + "])");
}
// 判断按钮状态是否为空(礼包详情进入),重新获取
if (TextUtils.isEmpty(content)) {
LibaoUtils.getLibaoStatus(mLibaoEntity.getId(), new LibaoUtils.PostLibaoListener() {
@Override
public void postSucced(Object response) {
List<LibaoStatusEntity> statusList = (List<LibaoStatusEntity>) response;
if (statusList.size() != 0) {
mLibaoEntity.setStatus(statusList.get(0).getStatus());
mLibaoEntity.setAvailable(statusList.get(0).getAvailable());
mLibaoEntity.setTotal(statusList.get(0).getTotal());
notifyItemChanged(0);
}
}
@Override
public void postFailed(Throwable error) {
}
});
}
}
public LibaoEntity getLibaoEntity() {
return mLibaoEntity;
}
//初始化获取数据库的列表
public void initLibaoDao() {
mLibaoDao = new LibaoDao(mContext);
mLibaoInfos = mLibaoDao.getAll();
}
// 点击立即安装打开下载页面,如果只有一个包 直接下载
public void openDownload() {
mDownloadTv.performClick();
}
public interface OnCodeScrollListener {
void isScroll(boolean isScroll);
}
}