From ec8e258e459d70c0ecbfbfe6cd49aec7481f21ef Mon Sep 17 00:00:00 2001 From: 18401019693 Date: Tue, 6 Sep 2022 16:36:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A5=96=E5=8A=B1=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/shayu/phonelive/AppContext.java | 2 +- .../bean/InstructorSendRewardModel.java | 34 +++++++ .../imrongcloud/InstructorSendReward.java | 90 ++++++++++++++++++- .../InstructorSendRewardProvider.java | 29 +++++- .../layout/view_instructor_send_reward.xml | 12 +++ common/src/main/res/values-en/strings.xml | 1 + common/src/main/res/values/strings.xml | 2 +- .../live/views/RecommendLiveRoomProvider.java | 1 + 8 files changed, 164 insertions(+), 7 deletions(-) create mode 100644 common/src/main/java/com/yunbao/common/bean/InstructorSendRewardModel.java create mode 100644 common/src/main/res/layout/view_instructor_send_reward.xml diff --git a/app/src/main/java/com/shayu/phonelive/AppContext.java b/app/src/main/java/com/shayu/phonelive/AppContext.java index c4ed7dc61..4205d336e 100644 --- a/app/src/main/java/com/shayu/phonelive/AppContext.java +++ b/app/src/main/java/com/shayu/phonelive/AppContext.java @@ -177,7 +177,7 @@ public class AppContext extends CommonAppContext { RongIMClient.registerMessageType(myMessages); // 注册自定义消息模板 - RongConfigCenter.conversationConfig().addMessageProvider(new InstructorSendRewardProvider()); + RongConfigCenter.conversationConfig().addMessageProvider(new InstructorSendRewardProvider(getApplicationContext())); RongConfigCenter.conversationConfig().addMessageProvider(new RecommendLiveRoomProvider(getApplicationContext())); RongcloudIMManager.addRongcloudIMOnReceiveMessageListener(new RongIMClient.OnReceiveMessageWrapperListener() { diff --git a/common/src/main/java/com/yunbao/common/bean/InstructorSendRewardModel.java b/common/src/main/java/com/yunbao/common/bean/InstructorSendRewardModel.java new file mode 100644 index 000000000..cd00d94d6 --- /dev/null +++ b/common/src/main/java/com/yunbao/common/bean/InstructorSendRewardModel.java @@ -0,0 +1,34 @@ +package com.yunbao.common.bean; + +import com.google.gson.annotations.SerializedName; + +public class InstructorSendRewardModel extends BaseModel { + + /** + * img : https://downs.yaoulive.com/Gift_Pictures%402x.png + * admin_nickname : null + */ + + @SerializedName("img") + private String img = ""; + @SerializedName("admin_nickname") + private String adminNickname = ""; + + public String getImg() { + return img; + } + + public InstructorSendRewardModel setImg(String img) { + this.img = img; + return this; + } + + public String getAdminNickname() { + return adminNickname; + } + + public InstructorSendRewardModel setAdminNickname(String adminNickname) { + this.adminNickname = adminNickname; + return this; + } +} diff --git a/common/src/main/java/com/yunbao/common/manager/imrongcloud/InstructorSendReward.java b/common/src/main/java/com/yunbao/common/manager/imrongcloud/InstructorSendReward.java index 948275630..04163a347 100644 --- a/common/src/main/java/com/yunbao/common/manager/imrongcloud/InstructorSendReward.java +++ b/common/src/main/java/com/yunbao/common/manager/imrongcloud/InstructorSendReward.java @@ -2,18 +2,86 @@ package com.yunbao.common.manager.imrongcloud; import android.annotation.SuppressLint; import android.os.Parcel; +import android.os.Parcelable; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.UnsupportedEncodingException; + +import io.rong.common.ParcelUtils; import io.rong.imlib.MessageTag; import io.rong.imlib.model.MessageContent; @SuppressLint("ParcelCreator") @MessageTag(value = "InstructorSendReward", flag = MessageTag.ISCOUNTED) -public class InstructorSendReward extends MessageContent { +public class InstructorSendReward extends MessageContent implements Parcelable { + public InstructorSendReward(byte[] data) { + if (data == null) { + return; + } + String jsonStr = null; + try { + jsonStr = new String(data, "UTF-8"); + } catch (UnsupportedEncodingException e) { + } + if (jsonStr == null) { + return; + } + + try { + JSONObject jsonObj = new JSONObject(jsonStr); + // 消息携带用户信息时, 自定义消息需添加下面代码 + if (jsonObj.has("user")) { + setUserInfo(parseJsonToUserInfo(jsonObj.getJSONObject("user"))); + } + // 用于群组聊天, 消息携带 @ 人信息时, 自定义消息需添加下面代码 + if (jsonObj.has("mentionedInfo")) { + setMentionedInfo(parseJsonToMentionInfo(jsonObj.getJSONObject("mentionedInfo"))); + } + // 将所有自定义变量从收到的 json 解析并赋值 + if (jsonObj.has("content")) { + content = jsonObj.optString("content"); + } + } catch (JSONException e) { + } + } + + // 自定义消息变量,可以有多个 + private String content; + + public InstructorSendReward setContent(String content) { + this.content = content; + return this; + } + + public String getContent() { + return content; + } @Override public byte[] encode() { - return new byte[0]; + JSONObject jsonObj = new JSONObject(); + try { + // 消息携带用户信息时, 自定义消息需添加下面代码 + if (getJSONUserInfo() != null) { + jsonObj.putOpt("user", getJSONUserInfo()); + } + // 用于群组聊天, 消息携带 @ 人信息时, 自定义消息需添加下面代码 + if (getJsonMentionInfo() != null) { + jsonObj.putOpt("mentionedInfo", getJsonMentionInfo()); + } + // 将所有自定义消息的内容,都序列化至 json 对象中 + jsonObj.put("content", this.content); + } catch (JSONException e) { + } + + try { + return jsonObj.toString().getBytes("UTF-8"); + } catch (UnsupportedEncodingException e) { + } + return null; } @Override @@ -23,6 +91,24 @@ public class InstructorSendReward extends MessageContent { @Override public void writeToParcel(Parcel dest, int flags) { + ParcelUtils.writeToParcel(dest, getExtra()); + ParcelUtils.writeToParcel(dest, content); + } + public static final Creator CREATOR = new Creator() { + @Override + public InstructorSendReward createFromParcel(Parcel source) { + return new InstructorSendReward(source); + } + + @Override + public InstructorSendReward[] newArray(int size) { + return new InstructorSendReward[size]; + } + }; + + public InstructorSendReward(Parcel in) { + setExtra(ParcelUtils.readFromParcel(in)); + setContent(ParcelUtils.readFromParcel(in)); } } diff --git a/common/src/main/java/com/yunbao/common/manager/imrongcloud/InstructorSendRewardProvider.java b/common/src/main/java/com/yunbao/common/manager/imrongcloud/InstructorSendRewardProvider.java index 3fd1c0c85..1436c6681 100644 --- a/common/src/main/java/com/yunbao/common/manager/imrongcloud/InstructorSendRewardProvider.java +++ b/common/src/main/java/com/yunbao/common/manager/imrongcloud/InstructorSendRewardProvider.java @@ -1,9 +1,16 @@ package com.yunbao.common.manager.imrongcloud; import android.content.Context; +import android.net.Uri; import android.text.Spannable; +import android.text.SpannableString; +import android.text.TextUtils; import android.view.ViewGroup; +import com.google.gson.Gson; +import com.yunbao.common.R; +import com.yunbao.common.bean.InstructorSendRewardModel; + import java.util.List; import io.rong.imkit.conversation.messgelist.provider.BaseMessageItemProvider; @@ -16,6 +23,13 @@ import io.rong.imlib.model.MessageContent; * 自定义消息模板 */ public class InstructorSendRewardProvider extends BaseMessageItemProvider { + private Context mContext; + private String politicalInstructor = ""; + + public InstructorSendRewardProvider(Context context) { + this.mContext = context; + } + /** * 创建 ViewHolder * @@ -25,7 +39,7 @@ public class InstructorSendRewardProvider extends BaseMessageItemProvider list, IViewProviderListener listener) { - + if (uiMessage.getMessage().getContent() instanceof InstructorSendReward) { + InstructorSendReward recommendLiveRoom = (InstructorSendReward) uiMessage.getMessage().getContent(); + InstructorSendRewardModel model = new Gson().fromJson(recommendLiveRoom.getContent(), InstructorSendRewardModel.class); + holder.setImageUri(R.id.live_bg, Uri.parse(model.getImg())); + } } /** @@ -79,6 +97,11 @@ public class InstructorSendRewardProvider extends BaseMessageItemProvider + + + + + \ No newline at end of file diff --git a/common/src/main/res/values-en/strings.xml b/common/src/main/res/values-en/strings.xml index 0ed6aac4b..79f1b01b1 100644 --- a/common/src/main/res/values-en/strings.xml +++ b/common/src/main/res/values-en/strings.xml @@ -871,4 +871,5 @@ Limited ride And limited avatar frame Shield car effect You are in the current live room has recommended an anchor to you! + has sent you a reward, come and get it! diff --git a/common/src/main/res/values/strings.xml b/common/src/main/res/values/strings.xml index 4309ba804..1a3164a28 100644 --- a/common/src/main/res/values/strings.xml +++ b/common/src/main/res/values/strings.xml @@ -888,5 +888,5 @@ 人氣票 系統通知 在線客服 - + 向你發送了一個獎勵,快來領取吧! diff --git a/live/src/main/java/com/yunbao/live/views/RecommendLiveRoomProvider.java b/live/src/main/java/com/yunbao/live/views/RecommendLiveRoomProvider.java index 40439852a..cf5833f49 100644 --- a/live/src/main/java/com/yunbao/live/views/RecommendLiveRoomProvider.java +++ b/live/src/main/java/com/yunbao/live/views/RecommendLiveRoomProvider.java @@ -74,6 +74,7 @@ public class RecommendLiveRoomProvider extends BaseMessageItemProvider