RedViewHolder.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package com.liaobo.im.view.chatHolder;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.text.TextUtils;
  5. import android.view.View;
  6. import android.widget.TextView;
  7. import android.widget.Toast;
  8. import com.liaobo.im.R;
  9. import com.liaobo.im.bean.message.ChatMessage;
  10. import com.liaobo.im.bean.redpacket.EventRedReceived;
  11. import com.liaobo.im.bean.redpacket.OpenRedpacket;
  12. import com.liaobo.im.bean.redpacket.RedDialogBean;
  13. import com.liaobo.im.db.dao.ChatMessageDao;
  14. import com.liaobo.im.ui.base.CoreManager;
  15. import com.liaobo.im.ui.me.redpacket.RedDetailsActivity;
  16. import com.liaobo.im.util.HtmlUtils;
  17. import com.liaobo.im.util.StringUtils;
  18. import com.liaobo.im.view.NoDoubleClickListener;
  19. import com.liaobo.im.view.redDialog.RedDialog;
  20. import com.liaobo.im.xuan.xuanhttplibrary.okhttp.HttpUtils;
  21. import com.liaobo.im.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
  22. import com.liaobo.im.xuan.xuanhttplibrary.okhttp.result.ObjectResult;
  23. import java.util.HashMap;
  24. import de.greenrobot.event.EventBus;
  25. import okhttp3.Call;
  26. class RedViewHolder extends AChatHolderInterface {
  27. TextView mTvContent;
  28. TextView mTvType;
  29. private RedDialog mRedDialog;
  30. @Override
  31. public int itemLayoutId(boolean isMysend) {
  32. return isMysend ? R.layout.chat_from_item_redpacket : R.layout.chat_to_item_redpacket;
  33. }
  34. @Override
  35. public void initView(View view) {
  36. mTvContent = view.findViewById(R.id.chat_text);
  37. mTvType = view.findViewById(R.id.tv_type);
  38. mRootView = view.findViewById(R.id.chat_warp_view);
  39. }
  40. @Override
  41. public void fillData(ChatMessage message) {
  42. if (mdata.getFileSize() == 2) {// 已领取
  43. mRootView.setAlpha(0.6f);
  44. } else {
  45. mRootView.setAlpha(1f);
  46. }
  47. String s = StringUtils.replaceSpecialChar(message.getContent());
  48. CharSequence charSequence = HtmlUtils.transform200SpanString(s.replaceAll("\n", "\r\n"), true);
  49. mTvContent.setText(charSequence);
  50. if (!TextUtils.isEmpty(message.getToUserName())&& message.getFilePath().equals("4")){
  51. mTvType.setTextColor(mContext.getResources().getColor(R.color.text_select));
  52. String msgTypeContent = String.format(getString(R.string.exclusive_red_packet),message.getToUserName());
  53. mTvType.setText(msgTypeContent);
  54. }else {
  55. boolean isKeyRed = "3".equals(message.getFilePath());
  56. mTvType.setText(getString(isKeyRed ? R.string.chat_kl_red : R.string.chat_red));
  57. mTvType.setTextColor(mContext.getResources().getColor(R.color.Grey_500));
  58. }
  59. mRootView.setOnClickListener(new NoDoubleClickListener() {
  60. @Override
  61. public void onNoDoubleClick(View view) {
  62. RedViewHolder.super.onClick(view);
  63. }
  64. });
  65. }
  66. @Override
  67. public boolean isOnClick() {
  68. return false; // 红包消息点击后回去请求接口,所以要做一个多重点击替换
  69. }
  70. @Override
  71. protected void onRootClick(View v) {
  72. clickRedpacket();
  73. }
  74. // 点击红包
  75. public void clickRedpacket() {
  76. final String token = CoreManager.requireSelfStatus(mContext).accessToken;
  77. final String redId = mdata.getObjectId();
  78. HashMap<String, String> params = new HashMap<>();
  79. params.put("access_token", token);
  80. params.put("id", redId);
  81. HttpUtils.get().url(CoreManager.requireConfig(mContext).RENDPACKET_GET)
  82. .params(params)
  83. .build()
  84. .execute(new BaseCallback<OpenRedpacket>(OpenRedpacket.class) {
  85. @Override
  86. public void onResponse(ObjectResult<OpenRedpacket> result) {
  87. if (result.getData() != null) {
  88. // 当resultCode==1时,表示可领取
  89. // 当resultCode==0时,表示红包已过期、红包已退回、红包已领完
  90. int resultCode = result.getResultCode();
  91. OpenRedpacket openRedpacket = result.getData();
  92. Bundle bundle = new Bundle();
  93. Intent intent = new Intent(mContext, RedDetailsActivity.class);
  94. bundle.putSerializable("openRedpacket", openRedpacket);
  95. bundle.putInt("redAction", 0);
  96. if (!TextUtils.isEmpty(result.getResultMsg())) //resultMsg不为空表示红包已过期
  97. {
  98. bundle.putInt("timeOut", 1);
  99. } else {
  100. bundle.putInt("timeOut", 0);
  101. }
  102. bundle.putBoolean("isGroup", isGounp);
  103. bundle.putString("mToUserId", mToUserId);
  104. intent.putExtras(bundle);
  105. // 红包不可领取, 或者我发的单聊红包直接跳转
  106. if (resultCode != 1 || (!isGounp && isMysend)) {
  107. mContext.startActivity(intent);
  108. } else {
  109. // 在群里面我领取过的红包直接跳转
  110. String assignUserId = openRedpacket.getPacket().getAssignUserId();
  111. if (isGounp && ( mdata.getFileSize() != 1 || (!TextUtils.isEmpty(assignUserId) &&!mLoginUserId.equals(assignUserId)))) {
  112. mContext.startActivity(intent);
  113. } else {
  114. if (mdata.getFilePath().equals("3")) {
  115. // 口令红包编辑输入框
  116. changeBottomViewInputText(mdata.getContent());
  117. } else {
  118. RedDialogBean redDialogBean = new RedDialogBean(openRedpacket.getPacket().getUserId(), openRedpacket.getPacket().getUserName(),
  119. openRedpacket.getPacket().getGreetings(), openRedpacket.getPacket().getId());
  120. mRedDialog = new RedDialog(mContext, redDialogBean, () -> openRedPacket(token, redId));
  121. mRedDialog.show();
  122. }
  123. }
  124. }
  125. } else {
  126. Toast.makeText(mContext, result.getResultMsg(), Toast.LENGTH_SHORT).show();
  127. }
  128. }
  129. @Override
  130. public void onError(Call call, Exception e) {
  131. }
  132. });
  133. }
  134. // 打开红包
  135. public void openRedPacket(final String token, String redId) {
  136. HashMap<String, String> params = new HashMap<String, String>();
  137. params.put("access_token", token);
  138. params.put("id", redId);
  139. HttpUtils.get().url(CoreManager.requireConfig(mContext).REDPACKET_OPEN)
  140. .params(params)
  141. .build()
  142. .execute(new BaseCallback<OpenRedpacket>(OpenRedpacket.class) {
  143. @Override
  144. public void onResponse(ObjectResult<OpenRedpacket> result) {
  145. if (mRedDialog != null) {
  146. mRedDialog.dismiss();
  147. }
  148. if (result.getData() != null) {
  149. mdata.setFileSize(2);
  150. ChatMessageDao.getInstance().updateChatMessageReceiptStatus(mLoginUserId, mToUserId, mdata.getPacketId());
  151. fillData(mdata);
  152. OpenRedpacket openRedpacket = result.getData();
  153. Bundle bundle = new Bundle();
  154. Intent intent = new Intent(mContext, RedDetailsActivity.class);
  155. bundle.putSerializable("openRedpacket", openRedpacket);
  156. bundle.putInt("redAction", 1);
  157. bundle.putInt("timeOut", 0);
  158. bundle.putBoolean("isGroup", isGounp);
  159. bundle.putString("mToUserId", mToUserId);
  160. intent.putExtras(bundle);
  161. mContext.startActivity(intent);
  162. // 更新余额
  163. CoreManager.updateMyBalance();
  164. EventBus.getDefault().post(new EventRedReceived(openRedpacket));
  165. } else {
  166. Toast.makeText(mContext, result.getResultMsg(), Toast.LENGTH_SHORT).show();
  167. }
  168. }
  169. @Override
  170. public void onError(Call call, Exception e) {
  171. if (mRedDialog != null) {
  172. mRedDialog.dismiss();
  173. }
  174. }
  175. });
  176. }
  177. // 通知更新输入框
  178. private void changeBottomViewInputText(String text) {
  179. mHolderListener.onChangeInputText(text);
  180. }
  181. @Override
  182. public boolean enableSendRead() {
  183. return true;
  184. }
  185. }