消息评论功能 基本完成
This commit is contained in:
@ -0,0 +1,585 @@
|
||||
package com.gh.gamecenter.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.text.Html;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import com.android.volley.Request;
|
||||
import com.android.volley.Response;
|
||||
import com.android.volley.VolleyError;
|
||||
import com.android.volley.toolbox.DiskBasedCache;
|
||||
import com.gh.base.AppController;
|
||||
import com.gh.common.constant.Config;
|
||||
import com.gh.common.util.ConcernContentUtils;
|
||||
import com.gh.common.util.DataUtils;
|
||||
import com.gh.common.util.DisplayUtils;
|
||||
import com.gh.common.util.GzipUtils;
|
||||
import com.gh.common.util.PostCommentUtils;
|
||||
import com.gh.common.util.Utils;
|
||||
import com.gh.common.view.CardLinearLayout;
|
||||
import com.gh.gamecenter.GameNewsActivity;
|
||||
import com.gh.gamecenter.MessageDetailActivity;
|
||||
import com.gh.gamecenter.NewsDetailActivity;
|
||||
import com.gh.gamecenter.R;
|
||||
import com.gh.gamecenter.ShareCardActivity;
|
||||
import com.gh.gamecenter.ShareCardPicActivity;
|
||||
import com.gh.gamecenter.WebActivity;
|
||||
import com.gh.gamecenter.adapter.viewholder.CommentHeadViewHolder;
|
||||
import com.gh.gamecenter.adapter.viewholder.CommentViewHolder;
|
||||
import com.gh.gamecenter.adapter.viewholder.FooterViewHolder;
|
||||
import com.gh.gamecenter.db.VoteDao;
|
||||
import com.gh.gamecenter.db.info.VoteInfo;
|
||||
import com.gh.gamecenter.entity.CommentEntity;
|
||||
import com.gh.gamecenter.entity.ConcernEntity;
|
||||
import com.gh.gamecenter.eventbus.EBReuse;
|
||||
import com.gh.gamecenter.listener.OnCallBackListener;
|
||||
import com.gh.gamecenter.manager.DataCollectionManager;
|
||||
import com.gh.gamecenter.news.News1Fragment;
|
||||
import com.gh.gamecenter.news.NewsConcernViewHolder;
|
||||
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.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Type;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import de.greenrobot.event.EventBus;
|
||||
|
||||
/**
|
||||
* Created by khy on 2016/11/8.
|
||||
*/
|
||||
public class MessageDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
||||
|
||||
private Context mContext;
|
||||
private ConcernEntity mConcernEntity;
|
||||
|
||||
private List<CommentEntity> mHotCommentList;
|
||||
private List<CommentEntity> mNormalCommentList;
|
||||
|
||||
private OnCallBackListener listener;
|
||||
|
||||
private VoteDao mVoteDao;
|
||||
|
||||
private List<VoteInfo> mVoteList;
|
||||
|
||||
private boolean isDone;
|
||||
private boolean isLoading;
|
||||
private boolean isloadError;
|
||||
|
||||
private static final int ITEM_TOP = 100;
|
||||
private static final int ITEM_TITLE = 101;
|
||||
private static final int ITEM_COMMENT = 102;
|
||||
private static final int ITEM_FOOTER = 103;
|
||||
|
||||
public MessageDetailAdapter(MessageDetailActivity context) {
|
||||
this.listener = context;
|
||||
this.mContext = context;
|
||||
|
||||
mVoteDao = new VoteDao(context);
|
||||
mVoteList = mVoteDao.getAll();
|
||||
|
||||
isDone = false;
|
||||
isLoading = false;
|
||||
isloadError = false;
|
||||
|
||||
mHotCommentList = new ArrayList<>();
|
||||
mNormalCommentList = new ArrayList<>();
|
||||
|
||||
mConcernEntity = (ConcernEntity) AppController.get("ConcernEntity", true);
|
||||
|
||||
addHotComment(0);
|
||||
}
|
||||
|
||||
private void addHotComment(int offset) {
|
||||
|
||||
String hotCommentUrl = Config.COMMENT_HOST + "article/" + mConcernEntity.getId() +
|
||||
"/comment?order=hot&limit=" + 10 + "&offset=" + offset;
|
||||
|
||||
JsonArrayExtendedRequest request = new JsonArrayExtendedRequest(hotCommentUrl,
|
||||
new Response.Listener<JSONArray>() {
|
||||
@Override
|
||||
public void onResponse(JSONArray response) {
|
||||
Type listType = new TypeToken<ArrayList<CommentEntity>>() {}.getType();
|
||||
Gson gson = new Gson();
|
||||
List<CommentEntity> list = gson.fromJson(response.toString(), listType);
|
||||
|
||||
if (list.size() != 0) {
|
||||
mHotCommentList.addAll(list);
|
||||
// notifyItemRangeInserted(1, mHotCommentList.size() + 1);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
addNormalComment(mNormalCommentList.size());
|
||||
}
|
||||
},
|
||||
new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
addNormalComment(mNormalCommentList.size());
|
||||
}
|
||||
});
|
||||
|
||||
AppController.addToRequestQueue(request, GameNewsActivity.TAG);
|
||||
}
|
||||
public void addNormalComment(int offset) {
|
||||
|
||||
isLoading = true;
|
||||
String commentUrl = Config.COMMENT_HOST + "article/" + mConcernEntity.getId() +
|
||||
"/comment?limit=" + 10 + "&offset=" + offset;
|
||||
|
||||
JsonArrayExtendedRequest request = new JsonArrayExtendedRequest( commentUrl,
|
||||
new Response.Listener<JSONArray>() {
|
||||
@Override
|
||||
public void onResponse(JSONArray response) {
|
||||
Type listType = new TypeToken<ArrayList<CommentEntity>>() {}.getType();
|
||||
Gson gson = new Gson();
|
||||
List<CommentEntity> list = gson.fromJson(response.toString(), listType);
|
||||
|
||||
if (list.size() < 10) {
|
||||
isDone = true;
|
||||
listener.loadDone();
|
||||
}
|
||||
|
||||
if (list.size() != 0) {
|
||||
mNormalCommentList.addAll(list);
|
||||
}
|
||||
|
||||
isLoading = false;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
},
|
||||
new Response.ErrorListener() {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
isDone = true;
|
||||
isLoading = false;
|
||||
isloadError = true;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
|
||||
AppController.addToRequestQueue(request, GameNewsActivity.TAG);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getItemViewType(int position) {
|
||||
int index;
|
||||
if (mHotCommentList.size() == 0) {
|
||||
index = 1;
|
||||
} else {
|
||||
index = 2;
|
||||
}
|
||||
|
||||
if (position == 0 && mConcernEntity != null) {
|
||||
return ITEM_TOP;
|
||||
} else if (mHotCommentList.size() != 0 && position == 1 ||
|
||||
mNormalCommentList.size() != 0 && mHotCommentList.size() + index == position ) {
|
||||
return ITEM_TITLE;
|
||||
} else if (getItemCount() == position + 1) {
|
||||
return ITEM_FOOTER;
|
||||
}
|
||||
return ITEM_COMMENT;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view;
|
||||
switch (viewType) {
|
||||
case ITEM_TOP:
|
||||
view = LayoutInflater.from(mContext).inflate(R.layout.concern_rv_item, parent, false);
|
||||
return new NewsConcernViewHolder(view);
|
||||
case ITEM_TITLE:
|
||||
view = LayoutInflater.from(mContext).inflate(R.layout.comment_head_item, parent, false);
|
||||
return new CommentHeadViewHolder(view);
|
||||
case ITEM_COMMENT:
|
||||
view = LayoutInflater.from(mContext).inflate(R.layout.comment_item, parent, false);
|
||||
return new CommentViewHolder(view);
|
||||
case ITEM_FOOTER:
|
||||
view = LayoutInflater.from(mContext).inflate(R.layout.refresh_footerview, parent, false);
|
||||
return new FooterViewHolder(view);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
|
||||
|
||||
if (holder instanceof NewsConcernViewHolder) {
|
||||
initConcernViewHolder((NewsConcernViewHolder) holder);
|
||||
} else if (holder instanceof CommentHeadViewHolder) {
|
||||
if (mHotCommentList.size() != 0 && position == 1) {
|
||||
((CommentHeadViewHolder) holder).commentHeadTitleTv.setText("热门评论");
|
||||
} else {
|
||||
((CommentHeadViewHolder) holder).commentHeadTitleTv.setText("最新评论");
|
||||
}
|
||||
} else if (holder instanceof CommentViewHolder) {
|
||||
|
||||
int index;
|
||||
if (mHotCommentList.size() == 0) {
|
||||
index = 2;
|
||||
} else {
|
||||
index = 3;
|
||||
}
|
||||
int commentPosition = 0;
|
||||
boolean isHotComment = false;
|
||||
CommentEntity commentEntity = null;
|
||||
if (mHotCommentList.size() != 0 && mHotCommentList.size() >= position -2) {
|
||||
commentEntity = mHotCommentList.get(position -2);
|
||||
commentPosition = position - 2;
|
||||
isHotComment = true;
|
||||
} else if (mNormalCommentList.size() != 0 &&
|
||||
mNormalCommentList.size() >= position - mHotCommentList.size() - index) {
|
||||
commentPosition = position - mHotCommentList.size() - index;
|
||||
commentEntity = mNormalCommentList.get(position - mHotCommentList.size() - index);
|
||||
isHotComment = false;
|
||||
}
|
||||
|
||||
initCommentViewHolder((CommentViewHolder) holder, commentEntity, commentPosition, isHotComment);
|
||||
} else if (holder instanceof FooterViewHolder) {
|
||||
FooterViewHolder viewHolder = (FooterViewHolder) holder;
|
||||
initFooterViewHolder(viewHolder);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
int itemCount = 0;
|
||||
if (mHotCommentList.size() != 0) {
|
||||
itemCount = itemCount + mHotCommentList.size() + 1;
|
||||
}
|
||||
if (mNormalCommentList.size() != 0) {
|
||||
itemCount = itemCount + mNormalCommentList.size() + 1;
|
||||
}
|
||||
if (mConcernEntity != null) {
|
||||
itemCount = itemCount + 1;
|
||||
}
|
||||
|
||||
return itemCount + 1;
|
||||
}
|
||||
private void initFooterViewHolder(final FooterViewHolder viewHolder) {
|
||||
if (!isDone) {
|
||||
viewHolder.footerview_tv_loading.setText("加载中...");
|
||||
viewHolder.footerview_progressbar.setVisibility(View.VISIBLE);
|
||||
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
viewHolder.footerview_item.setLayoutParams(params);
|
||||
} else if (isloadError || mNormalCommentList.size() == 0 && mHotCommentList.size() == 0) {
|
||||
viewHolder.footerview_progressbar.setVisibility(View.GONE);
|
||||
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(mContext, 185));
|
||||
if (isloadError) {
|
||||
viewHolder.footerview_tv_loading.setText("网络错误,点击重试!");
|
||||
} else {
|
||||
viewHolder.footerview_item.setLayoutParams(params);
|
||||
viewHolder.footerview_tv_loading.setText("目前还没有评论");
|
||||
}
|
||||
} else{
|
||||
viewHolder.footerview_tv_loading.setText("没有更多评论啦..");
|
||||
viewHolder.footerview_progressbar.setVisibility(View.GONE);
|
||||
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
viewHolder.footerview_item.setLayoutParams(params);
|
||||
}
|
||||
|
||||
viewHolder.footerview_item.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (isloadError) {
|
||||
viewHolder.footerview_progressbar.setVisibility(View.VISIBLE);
|
||||
viewHolder.footerview_tv_loading.setText("加载中...");
|
||||
addHotComment(0);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void initConcernViewHolder(NewsConcernViewHolder viewHolder) {
|
||||
((CardLinearLayout) viewHolder.itemView).setmTop(DisplayUtils.dip2px(mContext, 8));
|
||||
((CardLinearLayout) viewHolder.itemView).setmBottom(0);
|
||||
|
||||
if (mConcernEntity.getMessageDetailViews() != 0) {
|
||||
viewHolder.concernRead.setText("阅读 " +mConcernEntity.getMessageDetailViews() );
|
||||
}
|
||||
|
||||
if (mConcernEntity.getLink() != null) {
|
||||
viewHolder.concernLinkIcon.setImageResource(R.drawable.link_iv);
|
||||
} else {
|
||||
viewHolder.concernLinkIcon.setImageResource(R.drawable.concern_message_icon);
|
||||
}
|
||||
|
||||
if (mConcernEntity.getBrief() != null) {
|
||||
viewHolder.concernContent.setText(Html.fromHtml(mConcernEntity.getBrief()));
|
||||
viewHolder.concernContent.setMaxLines(100);
|
||||
} else {
|
||||
viewHolder.concernContent.setText(Html.fromHtml(mConcernEntity.getContent()));
|
||||
viewHolder.concernContent.setMaxLines(5);
|
||||
}
|
||||
|
||||
if (mConcernEntity.getImg().size() == 0) {
|
||||
viewHolder.contentPicLl.setVisibility(View.GONE);
|
||||
viewHolder.contentPicLl.removeAllViews();
|
||||
} else {
|
||||
viewHolder.contentPicLl.setVisibility(View.VISIBLE);
|
||||
viewHolder.contentPicLl.removeAllViews();
|
||||
ConcernContentUtils.addContentPic(mContext.getResources().getDisplayMetrics().widthPixels
|
||||
- DisplayUtils.dip2px(mContext, 34), viewHolder.contentPicLl, mConcernEntity.getImg(), mContext);
|
||||
}
|
||||
|
||||
viewHolder.concernThumb.setImageURI(mConcernEntity.getGameIcon());
|
||||
viewHolder.concernTitle.setText(mConcernEntity.getGameName());
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
|
||||
|
||||
try {
|
||||
long today = format.parse(format.format(new Date())).getTime();
|
||||
long day = Long.valueOf(mConcernEntity.getTime() + "000");
|
||||
if (day >= today && day < today + 86400 * 1000) {
|
||||
format.applyPattern("HH:mm");
|
||||
viewHolder.concerntTime.setText("今天 " + format.format(day));
|
||||
} else if (day >= today - 86400 * 1000 && day < today) {
|
||||
format.applyPattern("HH:mm");
|
||||
viewHolder.concerntTime.setText("昨天 " + format.format(day));
|
||||
} else {
|
||||
format.applyPattern("yyyy年MM月dd日 HH:mm");
|
||||
viewHolder.concerntTime.setText(format.format(day));
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
format.applyPattern("yyyy年MM月dd日 HH:mm");
|
||||
viewHolder.concerntTime.setText(format.format(Long.valueOf(mConcernEntity.getTime() + "000")));
|
||||
}
|
||||
|
||||
viewHolder.concernShareIv.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (mConcernEntity.getImg() != null && mConcernEntity.getImg().size() > 0) {
|
||||
Intent intent = new Intent(mContext, ShareCardPicActivity.class);
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("gameName", mConcernEntity.getGameName());
|
||||
bundle.putString("gameIconUrl", mConcernEntity.getGameIcon());
|
||||
bundle.putString("shareContent", mConcernEntity.getContent());
|
||||
bundle.putStringArrayList("shareArrImg", (ArrayList<String>) mConcernEntity.getImg());
|
||||
intent.putExtras(bundle);
|
||||
mContext.startActivity(intent);
|
||||
} else {
|
||||
Intent intent = new Intent(mContext, ShareCardActivity.class);
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putString("gameName", mConcernEntity.getGameName());
|
||||
bundle.putString("gameIconUrl", mConcernEntity.getGameIcon());
|
||||
bundle.putString("shareContent", mConcernEntity.getContent());
|
||||
intent.putExtras(bundle);
|
||||
mContext.startActivity(intent);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Map<String, Object> kv = new HashMap<>();
|
||||
kv.put("名字", mConcernEntity.getTitle());
|
||||
// kv.put("位置", String.valueOf(viewHolder.getPosition() + 1));
|
||||
DataUtils.onEvent(mContext, "点击", "消息详情", kv);
|
||||
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
// map.put("location", "列表");
|
||||
map.put("page", "消息详情");
|
||||
map.put("news", mConcernEntity.getTitle());
|
||||
map.put("news_id", mConcernEntity.getId());
|
||||
DataCollectionManager.onEvent(mContext, "click-item", map);
|
||||
|
||||
//统计阅读量
|
||||
statNewsViews(mConcernEntity.getId());
|
||||
|
||||
if (mConcernEntity.getLink() != null){
|
||||
Intent intent = new Intent(mContext, WebActivity.class);
|
||||
intent.putExtra("url",
|
||||
mConcernEntity.getLink());
|
||||
intent.putExtra("gameName", mConcernEntity.getGameName());
|
||||
mContext.startActivity(intent);
|
||||
}else {
|
||||
Intent intent = new Intent(mContext, NewsDetailActivity.class);
|
||||
intent.putExtra("newsId", mConcernEntity.getId());
|
||||
intent.putExtra("entrance", "(消息详情)");
|
||||
mContext.startActivity(intent);
|
||||
}
|
||||
EventBus.getDefault().post(new EBReuse("statNewsViews"));
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void initCommentViewHolder(final CommentViewHolder holder, final CommentEntity commentEntity, final int position, final boolean isHotComment) {
|
||||
holder.commentContentTv.setText(commentEntity.getContent());
|
||||
if (commentEntity.getVote() == 0) {
|
||||
holder.commentLikeCountTv.setVisibility(View.GONE);
|
||||
} else {
|
||||
for (VoteInfo voteInfo : mVoteList) {
|
||||
if (voteInfo.getCommentId().equals(commentEntity.getId())) {
|
||||
holder.commentLikeCountTv.setTextColor(mContext.getResources().getColor(R.color.theme));
|
||||
holder.commentLikeIv.setImageResource(R.drawable.comment_like_select);
|
||||
break;
|
||||
} else {
|
||||
holder.commentLikeCountTv.setTextColor(mContext.getResources().getColor(R.color.hint));
|
||||
holder.commentLikeIv.setImageResource(R.drawable.comment_like_unselect);
|
||||
}
|
||||
}
|
||||
holder.commentLikeCountTv.setVisibility(View.VISIBLE);
|
||||
holder.commentLikeCountTv.setText(commentEntity.getVote() + "");
|
||||
}
|
||||
|
||||
holder.commentUserNameTv.setText(commentEntity.getUser().getName());
|
||||
holder.commentUserIconDv.setImageURI(commentEntity.getUser().getIcon());
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
|
||||
try {
|
||||
long today = format.parse(format.format(new Date())).getTime();
|
||||
long day = Long.valueOf(commentEntity.getTime() + "000");
|
||||
if (day >= today && day < today + 86400 * 1000) {
|
||||
long min = new Date().getTime()/1000 - day/1000;
|
||||
int hour = (int) (min/ (60 * 60));
|
||||
if (hour == 0) {
|
||||
if (min < 60) {
|
||||
holder.commentTimeTv.setText("刚刚");
|
||||
} else {
|
||||
holder.commentTimeTv.setText((int)(min/60) + "分钟前");
|
||||
}
|
||||
} else {
|
||||
holder.commentTimeTv.setText(hour + "小时前");
|
||||
}
|
||||
} else if (day >= today - 86400 * 1000 && day < today) {
|
||||
format.applyPattern("HH:mm");
|
||||
holder.commentTimeTv.setText("昨天 ");
|
||||
} else {
|
||||
format.applyPattern("yyyy-MM-dd");
|
||||
holder.commentTimeTv.setText(format.format(day));
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
format.applyPattern("yyyy-MM-dd");
|
||||
holder.commentTimeTv.setText(format.format(Long.valueOf(mConcernEntity.getTime() + "000")));
|
||||
}
|
||||
|
||||
holder.commentLikeIv.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (holder.commentLikeCountTv.getCurrentTextColor() == mContext.getResources().getColor(R.color.theme)) {
|
||||
Utils.toast(mContext, "已经点过赞啦!");
|
||||
return;
|
||||
}
|
||||
PostCommentUtils.addCommentVoto(commentEntity.getId(), mContext, null);
|
||||
holder.commentLikeCountTv.setTextColor(mContext.getResources().getColor(R.color.theme));
|
||||
holder.commentLikeIv.setImageResource(R.drawable.comment_like_select);
|
||||
holder.commentLikeCountTv.setText((commentEntity.getVote() +1) + "");
|
||||
holder.commentLikeCountTv.setVisibility(View.VISIBLE);
|
||||
|
||||
mVoteDao.add(new VoteInfo(commentEntity.getId()));
|
||||
|
||||
int index = (position/10) * 10;
|
||||
|
||||
String cacheUrl;
|
||||
if (isHotComment) {
|
||||
cacheUrl = Config.COMMENT_HOST + "article/" + mConcernEntity.getId() +
|
||||
"/comment?order=hot&limit=" + 10 + "&offset=" + index;
|
||||
} else {
|
||||
cacheUrl = Config.COMMENT_HOST + "article/" + mConcernEntity.getId() +
|
||||
"/comment?limit=" + 10 + "&offset=" + index;
|
||||
}
|
||||
|
||||
modifyVolleyCache(commentEntity.getId(), cacheUrl);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void statNewsViews(final String news_id) {
|
||||
String url = Config.DATA_HOST + "news/stat?news_id=" + news_id;
|
||||
JsonObjectExtendedRequest request = new JsonObjectExtendedRequest(
|
||||
Request.Method.POST, url,
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
public void onResponse(JSONObject response) {
|
||||
try {
|
||||
if ("success".equals(response.getString("status"))) {
|
||||
|
||||
mConcernEntity.setMessageDetailViews(mConcernEntity.getMessageDetailViews() +1);
|
||||
notifyItemChanged(0);
|
||||
|
||||
//TODO 修改关注资讯volley缓存
|
||||
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, null);
|
||||
request.setShouldCache(false);
|
||||
AppController.addToRequestQueue(request, News1Fragment.TAG);
|
||||
}
|
||||
|
||||
private static final String DEFAULT_CACHE_DIR = "volley";
|
||||
|
||||
private void modifyVolleyCache(String id, String url) {
|
||||
if (url == null) {
|
||||
return;
|
||||
}
|
||||
File cacheDir = new File(mContext.getCacheDir(), DEFAULT_CACHE_DIR);
|
||||
DiskBasedCache cache = new DiskBasedCache(cacheDir);
|
||||
byte[] data = cache.getData(url);
|
||||
if (data != null) {
|
||||
try {
|
||||
JSONArray jsonArray = new JSONArray(new String(GzipUtils.decompressBytes(data)));
|
||||
Log.e("modifyVolleyCache11111", jsonArray.toString());
|
||||
JSONObject jsonObject;
|
||||
for (int i = 0, size = jsonArray.length(); i < size; i++) {
|
||||
jsonObject = jsonArray.getJSONObject(i);
|
||||
if (jsonObject.getString("_id").equals(id)) {
|
||||
jsonObject.put("vote", jsonObject.getInt("vote") + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
Utils.log(jsonArray.toString());
|
||||
Log.e("modifyVolleyCache2222", jsonArray.toString());
|
||||
cache.modify(url, GzipUtils.compressBytes(jsonArray.toString().getBytes()));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
Utils.log("modifyVolleyCache is null");
|
||||
}
|
||||
}
|
||||
|
||||
public String getNewsId (){
|
||||
return mConcernEntity.getId();
|
||||
}
|
||||
public boolean isLoading() {
|
||||
return isLoading;
|
||||
}
|
||||
public List<CommentEntity> getNormalCommentList (){
|
||||
return mNormalCommentList;
|
||||
}
|
||||
|
||||
public int getHotCommentListSize (){
|
||||
int index = 0;
|
||||
if (mHotCommentList.size() != 0) {
|
||||
index = mHotCommentList.size() + 1;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user