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

347 lines
14 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;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.DiskBasedCache;
import com.facebook.drawee.view.SimpleDraweeView;
import com.gh.base.BaseActivity;
import com.gh.common.constant.Config;
import com.gh.common.util.GzipUtils;
import com.gh.common.util.PostCommentUtils;
import com.gh.common.util.TimestampUtils;
import com.gh.common.util.TokenUtils;
import com.gh.common.util.Utils;
import com.gh.gamecenter.adapter.MessageDetailAdapter;
import com.gh.gamecenter.db.CommentDao;
import com.gh.gamecenter.db.info.CommentInfo;
import com.gh.gamecenter.entity.CommentEntity;
import com.gh.gamecenter.entity.UserEntity;
import com.gh.gamecenter.manager.CommentManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.util.Date;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTouch;
/**
* Created by khy on 2016/11/8.
*/
public class MessageDetailActivity extends BaseActivity {
@BindView(R.id.message_detail_rv) RecyclerView mMessageDetailRv;
@BindView(R.id.message_detail_user_rl) RelativeLayout mMessageDetailUserRl;
@BindView(R.id.message_detail_comment_rl) RelativeLayout mMessageDetailCommentRl;
@BindView(R.id.comment_user_icon) SimpleDraweeView mMessageDetailIconDv;
@BindView(R.id.comment_user_name) TextView mMessageDetailUserNameTv;
@BindView(R.id.comment_send) TextView mMessageDetailCommentSend;
@BindView(R.id.message_detail_comment_et) EditText mMessageDetailEt;
@BindView(R.id.message_detail_comment_hint) TextView mMessageDetailCommentHint;
private LinearLayoutManager mLayoutManager;
private MessageDetailAdapter mMessageDetailAdapter;
private SharedPreferences sp;
private boolean isDone;
private Toast mToast;
private CommentDao mCommentDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = View.inflate(this, R.layout.activity_message_detail, null);
init(contentView, "消息详情");
ButterKnife.bind(this);
sp = getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
isDone = false;
mCommentDao = new CommentDao(this);
mMessageDetailAdapter = new MessageDetailAdapter(this, mCommentDao);
mLayoutManager = new LinearLayoutManager(this);
mMessageDetailRv.setLayoutManager(mLayoutManager);
mMessageDetailRv.setAdapter(mMessageDetailAdapter);
mMessageDetailEt.addTextChangedListener(watcher);
mMessageDetailCommentSend.setEnabled(false);
mMessageDetailRv.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_IDLE && !isDone && !mMessageDetailAdapter.isLoading() &&
mLayoutManager.findLastVisibleItemPosition() == mMessageDetailAdapter.getItemCount() -1) {
mMessageDetailAdapter.addNormalComment(mMessageDetailAdapter.getItemCount()
- mMessageDetailAdapter.getHotCommentListSize() - 3);
}
}
});
//检查sp是否有用户信息
if (sp.getString("user_name", null) == null || sp.getString("user_name" , null).isEmpty()) {
new Thread(new Runnable() {
@Override
public void run() {
TokenUtils.getToken(MessageDetailActivity.this);
MessageDetailActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
mMessageDetailIconDv.setImageURI(sp.getString("user_icon", "res:///"+
R.drawable.user_default_icon_comment));
mMessageDetailUserNameTv.setText(sp.getString("user_name", "光环用户"));
}
});
}
}).start();
} else {
mMessageDetailIconDv.setImageURI(sp.getString("user_icon", "res:///"+
R.drawable.user_default_icon_comment));
mMessageDetailUserNameTv.setText(sp.getString("user_name", "光环用户"));
}
}
private TextWatcher watcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().trim().length() > 0 ) {
mMessageDetailCommentSend.setEnabled(true);
if (count > 140) {
mMessageDetailEt.setText("");
String newText = s.toString().substring(0, 140);
mMessageDetailEt.setText(newText);
Utils.toast(MessageDetailActivity.this, "评论不能多于140字");
}
} else {
mMessageDetailCommentSend.setEnabled(false);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
@OnClick(R.id.message_detail_comment_hint)
public void OnHintClikListener() {
mMessageDetailCommentHint.setVisibility(View.GONE);
mMessageDetailCommentRl.setVisibility(View.VISIBLE);
mMessageDetailUserRl.setVisibility(View.VISIBLE);
mMessageDetailEt.setFocusable(true);
mMessageDetailEt.setFocusableInTouchMode(true);
mMessageDetailEt.requestFocus();
setSoftInput(true);
}
@OnTouch(R.id.message_detail_rv)
public boolean OnRecyclerTouchListener () {
if (mMessageDetailCommentRl.getVisibility() == View.VISIBLE) {
mMessageDetailCommentHint.setVisibility(View.VISIBLE);
mMessageDetailCommentRl.setVisibility(View.GONE);
mMessageDetailUserRl.setVisibility(View.GONE);
mMessageDetailEt.setText("");
setSoftInput(false);
}
return false;
}
@OnClick(R.id.comment_send)
public void OnSendCommentListener() {
final String content = mMessageDetailEt.getText().toString();
if (content.length() ==0) {
Utils.toast(MessageDetailActivity.this, "评论内容不能为空!");
return;
}
mToast = Toast.makeText(MessageDetailActivity.this, "正在提交评论", Toast.LENGTH_SHORT);
mToast.show();
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("content", content);
} catch (JSONException e) {
e.printStackTrace();
}
final String newsId = mMessageDetailAdapter.getNewsId();
PostCommentUtils.addCommentData(Config.COMMENT_HOST + "article/" + newsId + "/comment"
, jsonObject.toString(), MessageDetailActivity.this, new PostCommentUtils.PostCommentListener() {
@Override
public void postSucced(String str) {
if (str == null) return;
JSONObject succedJson;
String commentId = null;
try {
succedJson = new JSONObject(str);
commentId = succedJson.getString("_id");
} catch (JSONException e) {
e.printStackTrace();
}
mCommentDao.add(new CommentInfo(commentId));//添加评论id到数据库 后续判断是否是自身评论
mToast.cancel();
Utils.toast(MessageDetailActivity.this, "发表成功");
mMessageDetailCommentHint.setVisibility(View.VISIBLE);
mMessageDetailCommentRl.setVisibility(View.GONE);
mMessageDetailUserRl.setVisibility(View.GONE);
mMessageDetailEt.setText("");
setSoftInput(false);
List<CommentEntity> normalCommentList = mMessageDetailAdapter.getNormalCommentList();
CommentEntity commentEntity = new CommentEntity();
UserEntity userEntity = new UserEntity();
userEntity.setIcon(sp.getString("user_icon", null));
userEntity.setName(sp.getString("user_name", "光环用户"));
commentEntity.setContent(content);
commentEntity.setId(commentId);
commentEntity.setVote(0);
commentEntity.setTime(new Date().getTime()/1000);
commentEntity.setUser(userEntity);
normalCommentList.add(0, commentEntity);
JSONObject cacheObject = new JSONObject();
try {
JSONObject cacheUser = new JSONObject();
cacheUser.put("_id", TokenUtils.getDeviceId(MessageDetailActivity.this));
cacheUser.put("icon", sp.getString("user_icon", null));
cacheUser.put("name", sp.getString("user_name", "光环用户"));
cacheObject.put("_id", commentId);
cacheObject.put("content", content);
cacheObject.put("time", new Date().getTime()/1000);
cacheObject.put("vote", 0);
cacheObject.put("user", cacheUser);
} catch (JSONException e) {
e.printStackTrace();
}
modifyNewsCommentVolleyCache(0, cacheObject, newsId);
mMessageDetailAdapter.notifyItemInserted(mMessageDetailAdapter.getHotCommentListSize() + 2);
// 完成评论操作,添加评论数
mMessageDetailAdapter.addCommentCount();
//修改评论缓存
CommentManager.updateOkhttpCache(newsId);
}
@Override
public void postFailed(VolleyError error) {
mToast.cancel();
if (error.networkResponse == null) {
return;
}
String errorData = new String(error.networkResponse.data);
try {
JSONObject errorJson = new JSONObject(errorData);
String detail = errorJson.getString("detail");
if ("too frequent".equals(detail)) {
Utils.toast(MessageDetailActivity.this, "@_@ 别话痨哦~休息一会再来评论吧~");
} else if ("user blocked".equals(detail)) {
Utils.toast(MessageDetailActivity.this, "账号状态异常,暂时无法发表评论");
} else if ("article blocked".equals(detail)) {
Utils.toast(MessageDetailActivity.this, "文章异常,无法发表评论");
mMessageDetailCommentHint.setVisibility(View.VISIBLE);
mMessageDetailCommentRl.setVisibility(View.GONE);
mMessageDetailUserRl.setVisibility(View.GONE);
setSoftInput(false);
} else if ("illegal".equals(detail)) {
Utils.toast(MessageDetailActivity.this, "评论内容可能包括敏感信息,请修改后再发表");
} else {
Utils.toast(MessageDetailActivity.this, "评论失败,未知原因");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
private static final String DEFAULT_CACHE_DIR = "volley";
private void modifyNewsCommentVolleyCache(int offset, JSONObject commentData, String id) {
File cacheDir = new File(getCacheDir(), DEFAULT_CACHE_DIR);
DiskBasedCache cache = new DiskBasedCache(cacheDir);
String key = TimestampUtils.addTimestamp(Config.COMMENT_HOST + "article/" + id + "/comment?limit=10&offset=" + offset);
byte[] data = cache.getData(key);
if (data != null) {
try {
JSONArray jsonArray = new JSONArray(new String(GzipUtils.decompressBytes(data)));
JSONArray newComment = new JSONArray();
newComment.put(commentData);
for (int i = 0, size = jsonArray.length() > 9 ? 9 : jsonArray.length(); i < size; i++) {
newComment.put(jsonArray.get(i));
}
Utils.log(newComment.toString());
cache.modify(key, GzipUtils.compressBytes(newComment.toString().getBytes()));
if (jsonArray.length() == 10) {
modifyNewsCommentVolleyCache(offset + 10, jsonArray.getJSONObject(9), id);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Utils.log("modifyNewsCommentVolleyCache is null");
}
}
@Override
public void loadDone() {
super.loadDone();
isDone = true;
}
//软键盘控制
private void setSoftInput(boolean isShow) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if (isShow){
imm.showSoftInputFromInputMethod(mMessageDetailEt.getWindowToken(), 0);
imm.toggleSoftInputFromWindow(mMessageDetailEt.getWindowToken(), 0, InputMethodManager.HIDE_NOT_ALWAYS);
} else {
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
}
}
}