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 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.entity.CommentEntity; import com.gh.gamecenter.entity.UserEntity; 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; @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; mMessageDetailAdapter = new MessageDetailAdapter(this); mLayoutManager = new LinearLayoutManager(this); mMessageDetailRv.setLayoutManager(mLayoutManager); mMessageDetailRv.setAdapter(mMessageDetailAdapter); mMessageDetailIconDv.setImageURI(sp.getString("user_icon", null)); mMessageDetailUserNameTv.setText(sp.getString("user_name", "光环用户")); 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); } } }); } 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; } 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(); } Utils.toast(MessageDetailActivity.this, "发表成功"); mMessageDetailCommentHint.setVisibility(View.VISIBLE); mMessageDetailCommentRl.setVisibility(View.GONE); mMessageDetailUserRl.setVisibility(View.GONE); mMessageDetailEt.setText(""); setSoftInput(false); List normalCommentList = mMessageDetailAdapter.getNormalCommentList(); CommentEntity commentEntity = new CommentEntity(); UserEntity userEntity = new UserEntity(); userEntity.setIcon(sp.getString("user_icon", null)); userEntity.setName(sp.getString("user_name", "光环用户")); userEntity.setId(TokenUtils.getDeviceId(MessageDetailActivity.this)); 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); } @Override public void postFailed(VolleyError error) { Utils.log("提交失败" + new String(error.networkResponse.data)); if (error.networkResponse.data == 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 ("silence".equals(detail)) { 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); } } }