491 lines
16 KiB
Java
491 lines
16 KiB
Java
package com.gh.gamecenter;
|
|
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.support.v7.widget.GridLayoutManager;
|
|
import android.support.v7.widget.RecyclerView;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.View.OnClickListener;
|
|
import android.view.ViewGroup;
|
|
import android.widget.ImageView;
|
|
import android.widget.RelativeLayout;
|
|
import android.widget.TextView;
|
|
|
|
import com.android.volley.NoConnectionError;
|
|
import com.android.volley.Response;
|
|
import com.android.volley.VolleyError;
|
|
import com.gh.base.AppController;
|
|
import com.gh.base.BaseActivity;
|
|
import com.gh.common.constant.Config;
|
|
import com.gh.common.constant.Constants;
|
|
import com.gh.common.util.ConcernUtils;
|
|
import com.gh.common.util.DataUtils;
|
|
import com.gh.common.util.ImageUtils;
|
|
import com.gh.common.util.TimestampUtils;
|
|
import com.gh.common.util.Utils;
|
|
import com.gh.common.view.Concern_LinearLayout;
|
|
import com.gh.gamecenter.db.info.ConcernInfo;
|
|
import com.gh.gamecenter.entity.GameEntity;
|
|
import com.gh.gamecenter.manager.ConcernManager;
|
|
import com.gh.gamecenter.manager.DataCollectionManager;
|
|
import com.gh.gamecenter.volley.extended.JsonArrayExtendedRequest;
|
|
import com.gh.gamecenter.volley.extended.JsonObjectExtendedRequest;
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONObject;
|
|
|
|
import java.lang.reflect.Type;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
public class ConcernActivity extends BaseActivity implements OnClickListener {
|
|
|
|
private Concern_LinearLayout view;
|
|
private RecyclerView concern_rv_show, concern_rv_recommend;
|
|
private ConcernManager manager;
|
|
private RelativeLayout concern_rl_title;
|
|
private TextView concern_tv_none;
|
|
|
|
private List<ConcernInfo> list;
|
|
private List<GameEntity> recommendList;
|
|
private List<GameEntity> concernList;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
view = (Concern_LinearLayout) View.inflate(this,
|
|
R.layout.activity_concern, null);
|
|
|
|
init(view, "我的关注");
|
|
|
|
manager = new ConcernManager(getApplicationContext());
|
|
|
|
list = manager.getConcernGame();
|
|
|
|
if (list == null) {
|
|
list = new ArrayList<ConcernInfo>();
|
|
}
|
|
|
|
recommendList = new ArrayList<GameEntity>();
|
|
|
|
concernList = new ArrayList<GameEntity>();
|
|
|
|
if (!list.isEmpty()) {
|
|
initConcernGame();
|
|
} else {
|
|
concern_tv_none.setVisibility(View.VISIBLE);
|
|
concern_rv_show.setVisibility(View.GONE);
|
|
}
|
|
|
|
concern_rl_title.setOnClickListener(this);
|
|
|
|
concern_rv_show.setHasFixedSize(true);
|
|
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
|
|
concern_rv_show.setLayoutManager(gridLayoutManager);
|
|
concern_rv_show.setAdapter(new ConcernViewAdapter());
|
|
|
|
concern_rv_recommend.setLayoutManager(new GridLayoutManager(this, 4));
|
|
concern_rv_recommend.setAdapter(new RecommendViewAdapter());
|
|
|
|
initRecommendGame();
|
|
}
|
|
|
|
@Override
|
|
public void onClick(View v) {
|
|
if (v == concern_rl_title) {
|
|
if (concern_rv_recommend.getVisibility() == View.VISIBLE) {
|
|
view.hideRecyclerView();
|
|
} else {
|
|
view.showRecyclerView();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void initRecommendGame() {
|
|
JsonArrayExtendedRequest recommendRequest = new JsonArrayExtendedRequest(
|
|
TimestampUtils.addTimestamp(Config.HOST
|
|
+ "v1d45/game/remenkapai", Constants.GAME_CD),
|
|
new Response.Listener<JSONArray>() {
|
|
|
|
@Override
|
|
public void onResponse(JSONArray response) {
|
|
processingData(response);
|
|
}
|
|
}, new Response.ErrorListener() {
|
|
|
|
@Override
|
|
public void onErrorResponse(VolleyError error) {
|
|
// 无网络连接
|
|
if (error.getClass().equals(NoConnectionError.class)) {
|
|
|
|
}
|
|
}
|
|
});
|
|
AppController.addToRequestQueue(recommendRequest, ConcernActivity.class);
|
|
}
|
|
|
|
private void processingData(JSONArray response) {
|
|
Type listType = new TypeToken<ArrayList<GameEntity>>() {}.getType();
|
|
Gson gson = new Gson();
|
|
List<GameEntity> list = gson.fromJson(response.toString(), listType);
|
|
for (int i = 0, size = list.size(); i < size; i++) {
|
|
if (!manager.isConcern(list.get(i).getId())) {
|
|
recommendList.add(list.get(i));
|
|
}
|
|
}
|
|
if (!recommendList.isEmpty()) {
|
|
concern_rl_title.setVisibility(View.VISIBLE);
|
|
concern_rv_recommend.setVisibility(View.VISIBLE);
|
|
concern_rv_recommend.setLayoutManager(new GridLayoutManager(this,
|
|
recommendList.size() > 4 ? 4 : recommendList.size()));
|
|
concern_rv_recommend.getAdapter().notifyDataSetChanged();
|
|
}
|
|
}
|
|
|
|
private void initConcernGame() {
|
|
final List<JSONObject> result = new ArrayList<JSONObject>();
|
|
final int count = list.size();
|
|
for (int i = 0, size = list.size(); i < size; i++) {
|
|
JsonObjectExtendedRequest concernObjectRequest = new JsonObjectExtendedRequest(
|
|
TimestampUtils.addTimestamp(Config.HOST + "v1d45/game/"
|
|
+ list.get(i).getId() + "/digest", Constants.GAME_CD),
|
|
new Response.Listener<JSONObject>() {
|
|
@Override
|
|
public void onResponse(JSONObject response) {
|
|
result.add(response);
|
|
if (result.size() == count) {
|
|
processingConcernGame(result);
|
|
}
|
|
}
|
|
}, new Response.ErrorListener() {
|
|
|
|
@Override
|
|
public void onErrorResponse(VolleyError error) {
|
|
// 无网络连接
|
|
if (error.getClass().equals(NoConnectionError.class)) {
|
|
|
|
}
|
|
}
|
|
});
|
|
AppController.addToRequestQueue(concernObjectRequest, ConcernActivity.class);
|
|
}
|
|
}
|
|
|
|
private void processingConcernGame(List<JSONObject> data) {
|
|
List<GameEntity> gameList = new ArrayList<GameEntity>();
|
|
Gson gson = new Gson();
|
|
for (int i = 0; i < data.size(); i++) {
|
|
gameList.add(gson.fromJson(data.get(i).toString(), GameEntity.class));
|
|
}
|
|
for (int i = 0, sizei = list.size(); i < sizei; i++) {
|
|
for (int j = 0, sizej = gameList.size(); j < sizej; j++) {
|
|
if (list.get(i).getId().equals(gameList.get(j).getId())) {
|
|
concernList.add(gameList.get(j));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
concern_rv_show.getAdapter().notifyDataSetChanged();
|
|
}
|
|
|
|
private class ConcernViewHolder extends RecyclerView.ViewHolder implements
|
|
OnClickListener {
|
|
|
|
private ImageView concern_item_icon;
|
|
private TextView concern_item_name, concern_item_btn;
|
|
private View view;
|
|
|
|
public ConcernViewHolder(View convertView) {
|
|
super(convertView);
|
|
concern_item_icon = (ImageView) convertView.findViewById(R.id.concern_item_icon);
|
|
concern_item_name = (TextView) convertView.findViewById(R.id.concern_item_name);
|
|
concern_item_btn = (TextView) convertView.findViewById(R.id.concern_item_btn);
|
|
concern_item_btn.setOnClickListener(this);
|
|
convertView.setOnClickListener(this);
|
|
view = convertView;
|
|
}
|
|
|
|
@Override
|
|
public void onClick(View v) {
|
|
int position = getPosition();
|
|
if (v == concern_item_btn) {
|
|
// 删除关注表中的数据,并更新界面
|
|
if (manager.findConcernById(list.get(position).getId()) != null) {
|
|
ConcernInfo concernInfo = list.remove(position);
|
|
|
|
Map<String, Object> kv = new HashMap<String, Object>();
|
|
kv.put("状态", "取消关注");
|
|
DataUtils.onEvent(ConcernActivity.this, "游戏关注", concernInfo.getGameName(), kv);
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
map.put("game", concernInfo.getGameName());
|
|
map.put("type", "取消关注");
|
|
map.put("createdOn", System.currentTimeMillis() / 1000);
|
|
DataCollectionManager.onEvent(ConcernActivity.this, "concern", map);
|
|
|
|
manager.deleteConcern(concernInfo.getId());
|
|
concernList.remove(position);
|
|
concern_rv_show.getAdapter().notifyItemRemoved(position);
|
|
if (list.isEmpty()) {
|
|
concern_tv_none.setVisibility(View.VISIBLE);
|
|
concern_rv_show.setVisibility(View.GONE);
|
|
}
|
|
concern_item_btn.setClickable(false);
|
|
view.setClickable(false);
|
|
|
|
recommendList = new ArrayList<GameEntity>();
|
|
initRecommendGame();
|
|
} else {
|
|
toast("取消失败,请稍后再试");
|
|
}
|
|
} else {
|
|
if (concernList != null && !concernList.isEmpty()) {
|
|
GameEntity gameEntity = concernList.get(position);
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
map.put("location", "关注列表");
|
|
map.put("createOn", System.currentTimeMillis() / 1000);
|
|
map.put("game", gameEntity.getName());
|
|
map.put("page", "我的关注");
|
|
DataCollectionManager.onEvent(ConcernActivity.this, "click-item", map);
|
|
|
|
AppController.put("GameEntity", gameEntity);
|
|
Intent intent = new Intent(ConcernActivity.this, GameDetailsActivity.class);
|
|
intent.putExtra("entrance", "我的关注-列表");
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
startActivity(intent);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
private class ConcernViewAdapter extends
|
|
RecyclerView.Adapter<ConcernViewHolder> {
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return list.size();
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(ConcernViewHolder holder, int position) {
|
|
ConcernInfo concernEntity = list.get(position);
|
|
ImageUtils.getInstance(getApplicationContext()).display(
|
|
concernEntity.getIcon(), holder.concern_item_icon);
|
|
holder.concern_item_name.setText(concernEntity.getGameName());
|
|
holder.concern_item_btn.setText("取消关注");
|
|
holder.concern_item_btn.setBackgroundResource(R.drawable.textview_cancel_style);
|
|
holder.concern_item_btn.setClickable(true);
|
|
holder.view.setClickable(true);
|
|
}
|
|
|
|
@Override
|
|
public ConcernViewHolder onCreateViewHolder(ViewGroup viewGroup,
|
|
int type) {
|
|
View view = LayoutInflater.from(viewGroup.getContext()).inflate(
|
|
R.layout.concern_item, viewGroup, false);
|
|
return new ConcernViewHolder(view);
|
|
}
|
|
|
|
}
|
|
|
|
private class RecommendViewHolder extends RecyclerView.ViewHolder implements
|
|
OnClickListener {
|
|
|
|
private ImageView concern_item_icon;
|
|
private TextView concern_item_name, concern_item_btn;
|
|
private View view;
|
|
|
|
public RecommendViewHolder(View convertView) {
|
|
super(convertView);
|
|
|
|
concern_item_icon = (ImageView) convertView.findViewById(R.id.concern_item_icon);
|
|
concern_item_name = (TextView) convertView.findViewById(R.id.concern_item_name);
|
|
concern_item_btn = (TextView) convertView.findViewById(R.id.concern_item_btn);
|
|
concern_item_btn.setOnClickListener(this);
|
|
convertView.setOnClickListener(this);
|
|
view = convertView;
|
|
}
|
|
|
|
@Override
|
|
public void onClick(View v) {
|
|
int position = getPosition();
|
|
if (v == concern_item_btn) {
|
|
// 添加关注到表,并更新当前界面,插入一个关注
|
|
GameEntity gameEntity = recommendList.get(position);
|
|
|
|
Map<String, Object> kv = new HashMap<String, Object>();
|
|
kv.put("状态", "关注");
|
|
DataUtils.onEvent(ConcernActivity.this, "游戏关注", gameEntity.getName(), kv);
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
map.put("game", gameEntity.getName());
|
|
map.put("type", "关注");
|
|
map.put("createdOn", System.currentTimeMillis() / 1000);
|
|
DataCollectionManager.onEvent(ConcernActivity.this, "concern", map);
|
|
|
|
manager.addByEntity(gameEntity);
|
|
ConcernInfo concernEntity = new ConcernInfo();
|
|
concernEntity.setGameName(gameEntity.getName());
|
|
concernEntity.setConcern(true);
|
|
concernEntity.setIcon(gameEntity.getIcon());
|
|
concernEntity.setId(gameEntity.getId());
|
|
list.add(concernEntity);
|
|
if (list.size() == 1) {
|
|
concern_tv_none.setVisibility(View.GONE);
|
|
concern_rv_show.setVisibility(View.VISIBLE);
|
|
}
|
|
concern_rv_show.getAdapter().notifyItemInserted(list.size() - 1);
|
|
|
|
concernList.add(recommendList.remove(position));
|
|
if (recommendList.isEmpty()) {
|
|
concern_rl_title.setVisibility(View.GONE);
|
|
concern_rv_recommend.setVisibility(View.GONE);
|
|
} else {
|
|
if (recommendList.size() < 4) {
|
|
concern_rv_recommend.setLayoutManager(new GridLayoutManager(
|
|
ConcernActivity.this, recommendList.size()));
|
|
}
|
|
concern_rv_recommend.getAdapter().notifyItemRemoved(position);
|
|
}
|
|
concern_item_btn.setClickable(false);
|
|
view.setClickable(false);
|
|
} else {
|
|
GameEntity gameEntity = recommendList.get(position);
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
map.put("location", "热门游戏推荐");
|
|
map.put("createOn", System.currentTimeMillis() / 1000);
|
|
map.put("game", gameEntity.getName());
|
|
map.put("page", "我的关注");
|
|
DataCollectionManager.onEvent(ConcernActivity.this, "click-item", map);
|
|
|
|
AppController.put("GameEntity", gameEntity);
|
|
|
|
Intent intent = new Intent(ConcernActivity.this, GameDetailsActivity.class);
|
|
intent.putExtra("entrance", "我的关注-推荐");
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
startActivity(intent);
|
|
}
|
|
}
|
|
}
|
|
|
|
private class RecommendViewAdapter extends RecyclerView.Adapter<RecommendViewHolder> {
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
return recommendList.size();
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(RecommendViewHolder holder, int position) {
|
|
GameEntity gameEntity = recommendList.get(position);
|
|
ImageUtils.getInstance(getApplicationContext()).display(
|
|
gameEntity.getIcon(), holder.concern_item_icon);
|
|
holder.concern_item_name.setText(gameEntity.getName());
|
|
holder.concern_item_btn.setText("关注");
|
|
holder.concern_item_btn.setBackgroundResource(R.drawable.textview_blue_style);
|
|
holder.concern_item_btn.setClickable(true);
|
|
holder.view.setClickable(true);
|
|
}
|
|
|
|
@Override
|
|
public RecommendViewHolder onCreateViewHolder(ViewGroup viewGroup,
|
|
int type) {
|
|
View view = LayoutInflater.from(viewGroup.getContext()).inflate(
|
|
R.layout.concern_item, viewGroup, false);
|
|
return new RecommendViewHolder(view);
|
|
}
|
|
|
|
}
|
|
|
|
private boolean isPause = false;
|
|
|
|
@Override
|
|
protected void onPause() {
|
|
super.onPause();
|
|
isPause = true;
|
|
}
|
|
|
|
@Override
|
|
protected void onResume() {
|
|
super.onResume();
|
|
Utils.log("isPause = " + isPause);
|
|
if (isPause) {
|
|
List<ConcernInfo> concernList = manager.getConcernGame();
|
|
boolean isChanged = false;
|
|
if (concernList == null) {
|
|
if (list.size() != 0){
|
|
isChanged = true;
|
|
}
|
|
} else {
|
|
if (list.isEmpty()
|
|
|| concernList.size() > list.size()
|
|
|| concernList.size() < list.size()) {
|
|
isChanged = true;
|
|
}
|
|
}
|
|
Utils.log("isChanged = " + isChanged);
|
|
if (isChanged) {
|
|
list = concernList;
|
|
if (list == null) {
|
|
list = new ArrayList<ConcernInfo>();
|
|
}
|
|
concernList = new ArrayList<ConcernInfo>();
|
|
if (!list.isEmpty()) {
|
|
concern_tv_none.setVisibility(View.GONE);
|
|
concern_rv_show.setVisibility(View.VISIBLE);
|
|
initConcernGame();
|
|
} else {
|
|
concern_tv_none.setVisibility(View.VISIBLE);
|
|
concern_rv_show.setVisibility(View.GONE);
|
|
}
|
|
recommendList = new ArrayList<GameEntity>();
|
|
initRecommendGame();
|
|
}
|
|
isPause = false;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
String uuid = ConcernUtils.UUID(this);
|
|
JSONArray jsonArray = new JSONArray();
|
|
for (ConcernInfo concernInfo : manager.getConcernGame()) {
|
|
jsonArray.put(concernInfo.getId());
|
|
}
|
|
ConcernUtils.UpdateConcernData(Config.HOST + "v2d0/device/" + uuid + "/concern", jsonArray.toString(), new ConcernUtils.DownJsonListener() {
|
|
@Override
|
|
public void downSucced(String str) {
|
|
Utils.log("更新设备关注游戏成功");
|
|
}
|
|
|
|
@Override
|
|
public void downFailed() {
|
|
Utils.log("更新设备关注游戏失败");
|
|
}
|
|
});
|
|
view = null;
|
|
concern_rv_show = null;
|
|
concern_rv_recommend = null;
|
|
manager = null;
|
|
concern_rl_title = null;
|
|
concern_tv_none = null;
|
|
list = null;
|
|
recommendList = null;
|
|
concernList = null;
|
|
}
|
|
}
|