123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- package com.liaobo.im.view.chatHolder;
- import android.content.Intent;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.liaobo.im.R;
- import com.liaobo.im.bean.message.ChatMessage;
- import com.liaobo.im.bean.redpacket.EventRedReceived;
- import com.liaobo.im.bean.redpacket.OpenRedpacket;
- import com.liaobo.im.bean.redpacket.RedDialogBean;
- import com.liaobo.im.db.dao.ChatMessageDao;
- import com.liaobo.im.ui.base.CoreManager;
- import com.liaobo.im.ui.me.redpacket.RedDetailsActivity;
- import com.liaobo.im.util.HtmlUtils;
- import com.liaobo.im.util.StringUtils;
- import com.liaobo.im.view.NoDoubleClickListener;
- import com.liaobo.im.view.redDialog.RedDialog;
- import com.liaobo.im.xuan.xuanhttplibrary.okhttp.HttpUtils;
- import com.liaobo.im.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
- import com.liaobo.im.xuan.xuanhttplibrary.okhttp.result.ObjectResult;
- import java.util.HashMap;
- import de.greenrobot.event.EventBus;
- import okhttp3.Call;
- class RedViewHolder extends AChatHolderInterface {
- TextView mTvContent;
- TextView mTvType;
- private RedDialog mRedDialog;
- @Override
- public int itemLayoutId(boolean isMysend) {
- return isMysend ? R.layout.chat_from_item_redpacket : R.layout.chat_to_item_redpacket;
- }
- @Override
- public void initView(View view) {
- mTvContent = view.findViewById(R.id.chat_text);
- mTvType = view.findViewById(R.id.tv_type);
- mRootView = view.findViewById(R.id.chat_warp_view);
- }
- @Override
- public void fillData(ChatMessage message) {
- if (mdata.getFileSize() == 2) {// 已领取
- mRootView.setAlpha(0.6f);
- } else {
- mRootView.setAlpha(1f);
- }
- String s = StringUtils.replaceSpecialChar(message.getContent());
- CharSequence charSequence = HtmlUtils.transform200SpanString(s.replaceAll("\n", "\r\n"), true);
- mTvContent.setText(charSequence);
- if (!TextUtils.isEmpty(message.getToUserName())&& message.getFilePath().equals("4")){
- mTvType.setTextColor(mContext.getResources().getColor(R.color.text_select));
- String msgTypeContent = String.format(getString(R.string.exclusive_red_packet),message.getToUserName());
- mTvType.setText(msgTypeContent);
- }else {
- boolean isKeyRed = "3".equals(message.getFilePath());
- mTvType.setText(getString(isKeyRed ? R.string.chat_kl_red : R.string.chat_red));
- mTvType.setTextColor(mContext.getResources().getColor(R.color.Grey_500));
- }
- mRootView.setOnClickListener(new NoDoubleClickListener() {
- @Override
- public void onNoDoubleClick(View view) {
- RedViewHolder.super.onClick(view);
- }
- });
- }
- @Override
- public boolean isOnClick() {
- return false; // 红包消息点击后回去请求接口,所以要做一个多重点击替换
- }
- @Override
- protected void onRootClick(View v) {
- clickRedpacket();
- }
- // 点击红包
- public void clickRedpacket() {
- final String token = CoreManager.requireSelfStatus(mContext).accessToken;
- final String redId = mdata.getObjectId();
- HashMap<String, String> params = new HashMap<>();
- params.put("access_token", token);
- params.put("id", redId);
- HttpUtils.get().url(CoreManager.requireConfig(mContext).RENDPACKET_GET)
- .params(params)
- .build()
- .execute(new BaseCallback<OpenRedpacket>(OpenRedpacket.class) {
- @Override
- public void onResponse(ObjectResult<OpenRedpacket> result) {
- if (result.getData() != null) {
- // 当resultCode==1时,表示可领取
- // 当resultCode==0时,表示红包已过期、红包已退回、红包已领完
- int resultCode = result.getResultCode();
- OpenRedpacket openRedpacket = result.getData();
- Bundle bundle = new Bundle();
- Intent intent = new Intent(mContext, RedDetailsActivity.class);
- bundle.putSerializable("openRedpacket", openRedpacket);
- bundle.putInt("redAction", 0);
- if (!TextUtils.isEmpty(result.getResultMsg())) //resultMsg不为空表示红包已过期
- {
- bundle.putInt("timeOut", 1);
- } else {
- bundle.putInt("timeOut", 0);
- }
- bundle.putBoolean("isGroup", isGounp);
- bundle.putString("mToUserId", mToUserId);
- intent.putExtras(bundle);
- // 红包不可领取, 或者我发的单聊红包直接跳转
- if (resultCode != 1 || (!isGounp && isMysend)) {
- mContext.startActivity(intent);
- } else {
- // 在群里面我领取过的红包直接跳转
- String assignUserId = openRedpacket.getPacket().getAssignUserId();
- if (isGounp && ( mdata.getFileSize() != 1 || (!TextUtils.isEmpty(assignUserId) &&!mLoginUserId.equals(assignUserId)))) {
- mContext.startActivity(intent);
- } else {
- if (mdata.getFilePath().equals("3")) {
- // 口令红包编辑输入框
- changeBottomViewInputText(mdata.getContent());
- } else {
- RedDialogBean redDialogBean = new RedDialogBean(openRedpacket.getPacket().getUserId(), openRedpacket.getPacket().getUserName(),
- openRedpacket.getPacket().getGreetings(), openRedpacket.getPacket().getId());
- mRedDialog = new RedDialog(mContext, redDialogBean, () -> openRedPacket(token, redId));
- mRedDialog.show();
- }
- }
- }
- } else {
- Toast.makeText(mContext, result.getResultMsg(), Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onError(Call call, Exception e) {
- }
- });
- }
- // 打开红包
- public void openRedPacket(final String token, String redId) {
- HashMap<String, String> params = new HashMap<String, String>();
- params.put("access_token", token);
- params.put("id", redId);
- HttpUtils.get().url(CoreManager.requireConfig(mContext).REDPACKET_OPEN)
- .params(params)
- .build()
- .execute(new BaseCallback<OpenRedpacket>(OpenRedpacket.class) {
- @Override
- public void onResponse(ObjectResult<OpenRedpacket> result) {
- if (mRedDialog != null) {
- mRedDialog.dismiss();
- }
- if (result.getData() != null) {
- mdata.setFileSize(2);
- ChatMessageDao.getInstance().updateChatMessageReceiptStatus(mLoginUserId, mToUserId, mdata.getPacketId());
- fillData(mdata);
- OpenRedpacket openRedpacket = result.getData();
- Bundle bundle = new Bundle();
- Intent intent = new Intent(mContext, RedDetailsActivity.class);
- bundle.putSerializable("openRedpacket", openRedpacket);
- bundle.putInt("redAction", 1);
- bundle.putInt("timeOut", 0);
- bundle.putBoolean("isGroup", isGounp);
- bundle.putString("mToUserId", mToUserId);
- intent.putExtras(bundle);
- mContext.startActivity(intent);
- // 更新余额
- CoreManager.updateMyBalance();
- EventBus.getDefault().post(new EventRedReceived(openRedpacket));
- } else {
- Toast.makeText(mContext, result.getResultMsg(), Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onError(Call call, Exception e) {
- if (mRedDialog != null) {
- mRedDialog.dismiss();
- }
- }
- });
- }
- // 通知更新输入框
- private void changeBottomViewInputText(String text) {
- mHolderListener.onChangeInputText(text);
- }
- @Override
- public boolean enableSendRead() {
- return true;
- }
- }
|