This commit is contained in:
chenjuntao
2019-10-19 11:49:21 +08:00
parent 4398bea8d4
commit cbbf4b71c2
10 changed files with 194 additions and 160 deletions

View File

@ -4,9 +4,6 @@ import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
@ -21,6 +18,7 @@ import com.gh.common.util.CheckLoginUtils;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.EntranceUtils;
import com.gh.common.util.ErrorHelper;
import com.gh.common.util.PermissionHelper;
import com.gh.common.util.PostCommentUtils;
import com.gh.common.util.TextHelper;
import com.gh.common.util.TimestampUtils;
@ -53,6 +51,9 @@ import org.json.JSONObject;
import java.util.List;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
@ -295,106 +296,107 @@ public class MessageDetailFragment extends NormalFragment implements OnCommentCa
@OnClick(R.id.comment_send_btn)
public void OnSendCommentListener() {
final String content = mMessageDetailEt.getText().toString();
if (content.length() == 0) {
Utils.toast(getContext(), "评论内容不能为空!");
return;
}
CheckLoginUtils.checkLogin(requireContext(), "资讯文章详情-评论详情-写评论", () -> {
mSendingDialog = DialogUtils.showWaitDialog(getActivity(), getString(R.string.post_dialog_hint));
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("content", content);
} catch (JSONException e) {
e.printStackTrace();
}
if (newsId == null && mConcernEntity == null ||
newsId == null && mConcernEntity.getId() == null) {
Utils.toast(getContext(), "评论异常 id null");
mSendingDialog.cancel();
PermissionHelper.checkReadPhoneStatePermissionBeforeAction(requireContext(), () -> {
final String content = mMessageDetailEt.getText().toString();
if (content.length() == 0) {
Utils.toast(getContext(), "评论内容不能为空!");
return;
} else if (newsId == null) {
newsId = mConcernEntity.getId();
}
PostCommentUtils.addCommentData(getContext(), newsId, jsonObject.toString(), mCommentEntity,
new PostCommentUtils.PostCommentListener() {
@Override
public void postSuccess(JSONObject response) {
mSendingDialog.dismiss();
toast("发表成功");
mMessageDetailEt.setText("");
try {
JSONObject cacheObject = new JSONObject();
JSONObject cacheUser = new JSONObject();
JSONObject userData = new JSONObject();
cacheUser.put("_id", mUserInfo.getId());
cacheUser.put("icon", mUserInfo.getIcon());
cacheUser.put("name", mUserInfo.getName());
userData.put("is_comment_own", true);
cacheObject.put("_id", response.getString("_id"));
cacheObject.put("content", content);
cacheObject.put("time", System.currentTimeMillis() / 1000);
cacheObject.put("vote", 0);
cacheObject.put("user", cacheUser);
cacheObject.put("me", userData);
if (mCommentEntity != null) {
JSONObject cacheParent = new JSONObject();
JSONObject cacheParentUser = new JSONObject();
cacheParentUser.put("_id", mCommentEntity.getId());
cacheParentUser.put("name", mCommentEntity.getUser().getName());
cacheParent.put("user", cacheParentUser);
cacheParent.put("comment", mCommentEntity.getContent());
cacheObject.put("parent", cacheParent);
}
CommentEntity commentEntity = new Gson().fromJson(cacheObject.toString(), CommentEntity.class);
if (mConcernEntity != null) {
adapter.addNormalComment(commentEntity);
}
modifyNewsCommentOkhttpCache(adapter.findTheLastPriorComment(), cacheObject, newsId);
} catch (JSONException e) {
e.printStackTrace();
CheckLoginUtils.checkLogin(requireContext(), "资讯文章详情-评论详情-写评论", () -> {
mSendingDialog = DialogUtils.showWaitDialog(getActivity(), getString(R.string.post_dialog_hint));
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("content", content);
} catch (JSONException e) {
e.printStackTrace();
}
if (newsId == null && mConcernEntity == null || newsId == null && mConcernEntity.getId() == null) {
Utils.toast(getContext(), "评论异常 id null");
mSendingDialog.cancel();
return;
} else if (newsId == null) {
newsId = mConcernEntity.getId();
}
PostCommentUtils.addCommentData(getContext(), newsId, jsonObject.toString(), mCommentEntity, new PostCommentUtils.PostCommentListener() {
@Override
public void postSuccess(JSONObject response) {
mSendingDialog.dismiss();
toast("发表成功");
mMessageDetailEt.setText("");
try {
JSONObject cacheObject = new JSONObject();
JSONObject cacheUser = new JSONObject();
JSONObject userData = new JSONObject();
cacheUser.put("_id", mUserInfo.getId());
cacheUser.put("icon", mUserInfo.getIcon());
cacheUser.put("name", mUserInfo.getName());
userData.put("is_comment_own", true);
cacheObject.put("_id", response.getString("_id"));
cacheObject.put("content", content);
cacheObject.put("time", System.currentTimeMillis() / 1000);
cacheObject.put("vote", 0);
cacheObject.put("user", cacheUser);
cacheObject.put("me", userData);
if (mCommentEntity != null) {
JSONObject cacheParent = new JSONObject();
JSONObject cacheParentUser = new JSONObject();
cacheParentUser.put("_id", mCommentEntity.getId());
cacheParentUser.put("name", mCommentEntity.getUser().getName());
cacheParent.put("user", cacheParentUser);
cacheParent.put("comment", mCommentEntity.getContent());
cacheObject.put("parent", cacheParent);
}
CommentEntity commentEntity = new Gson().fromJson(cacheObject.toString(), CommentEntity.class);
if (mConcernEntity != null) {
// 完成评论操作,添加评论数
adapter.addCommentCount();
//修改评论缓存
CommentManager.updateOkhttpCacheForId(getContext(), newsId);
CommentManager.updateOkhttpCache(getContext(), newsId);
adapter.notifyItemInserted(adapter.getHotCommentListSize() + 2);
adapter.notifyItemChanged(adapter.getItemCount() - 1); //刷新脚布局高度
} else {
showNoConnection(false);
adapter.addNormalComment(commentEntity);
}
setSoftInput(false);
modifyNewsCommentOkhttpCache(adapter.findTheLastPriorComment(), cacheObject, newsId);
} catch (JSONException e) {
e.printStackTrace();
}
@Override
public void postFailed(Throwable e) {
mSendingDialog.dismiss();
String errorString = null;
if (e instanceof HttpException) {
try {
errorString = ((HttpException) e).response().errorBody().string();
} catch (Exception e1) {
e1.printStackTrace();
}
if (mConcernEntity != null) {
// 完成评论操作,添加评论数
adapter.addCommentCount();
//修改评论缓存
CommentManager.updateOkhttpCacheForId(getContext(), newsId);
CommentManager.updateOkhttpCache(getContext(), newsId);
adapter.notifyItemInserted(adapter.getHotCommentListSize() + 2);
adapter.notifyItemChanged(adapter.getItemCount() - 1); //刷新脚布局高度
} else {
showNoConnection(false);
}
setSoftInput(false);
}
@Override
public void postFailed(Throwable e) {
mSendingDialog.dismiss();
String errorString = null;
if (e instanceof HttpException) {
try {
errorString = ((HttpException) e).response().errorBody().string();
} catch (Exception e1) {
e1.printStackTrace();
}
ErrorHelper.handleError(requireContext(), errorString, false);
}
});
ErrorHelper.handleError(requireContext(), errorString, false);
}
});
});
});
}
private void modifyNewsCommentOkhttpCache(int offset, JSONObject commentData, String id) {