From d845cb3df77987417e268e73b87a18c62b01081a Mon Sep 17 00:00:00 2001 From: Tien Date: Fri, 20 May 2022 16:49:10 +0700 Subject: [PATCH] Recompose the admin message --- .../uikit/model/admin/AdminMessageData.java | 61 +++++++++++++++++++ .../model/admin/AdminMessageMetaData.java | 31 ++++++++++ .../uikit/model/admin/AdminMessageType.java | 5 ++ .../uikit/model/admin/AdminMessageUser.java | 30 +++++++++ .../uikit/widgets/AdminMessageView.java | 13 +++- 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageData.java create mode 100644 uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageMetaData.java create mode 100644 uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageType.java create mode 100644 uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageUser.java diff --git a/uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageData.java b/uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageData.java new file mode 100644 index 00000000..533fdde1 --- /dev/null +++ b/uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageData.java @@ -0,0 +1,61 @@ +package com.sendbird.uikit.model.admin; + +import androidx.annotation.Keep; + +import com.sendbird.android.shadow.com.google.gson.annotations.SerializedName; + +import java.util.ArrayList; +import java.util.List; + +@Keep +public class AdminMessageData { + @SerializedName("type") + private String type; + + @SerializedName("users") + private List users; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public List getUsers() { + return users; + } + + public void setUsers(List users) { + this.users = users; + } + + public String getContent() { + if (type.equalsIgnoreCase(AdminMessageType.USER_JOINED)) { + return joinUserNames() + " joined"; + } + return ""; + } + + public String joinUserNames() { + List names = getUserNames(); + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < names.size(); i++) { + builder.append(names.get(i)); + if (i != names.size() - 1) { + builder.append(", "); + } + } + return builder.toString(); + } + + public List getUserNames() { + List names = new ArrayList<>(); + for (AdminMessageUser user : users) { + names.add(user.getName()); + } + return names; + } +} + diff --git a/uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageMetaData.java b/uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageMetaData.java new file mode 100644 index 00000000..a6f7072f --- /dev/null +++ b/uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageMetaData.java @@ -0,0 +1,31 @@ +package com.sendbird.uikit.model.admin; + +import androidx.annotation.Keep; + +import com.sendbird.android.shadow.com.google.gson.annotations.SerializedName; + +@Keep +public class AdminMessageMetaData { + + @SerializedName("username") + private String username; + + @SerializedName("phone") + private String phone; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageType.java b/uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageType.java new file mode 100644 index 00000000..6667294e --- /dev/null +++ b/uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageType.java @@ -0,0 +1,5 @@ +package com.sendbird.uikit.model.admin; + +public class AdminMessageType { + public static final String USER_JOINED = "USER_JOIN"; +} diff --git a/uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageUser.java b/uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageUser.java new file mode 100644 index 00000000..fbbc2eb1 --- /dev/null +++ b/uikit/src/main/java/com/sendbird/uikit/model/admin/AdminMessageUser.java @@ -0,0 +1,30 @@ +package com.sendbird.uikit.model.admin; + +import androidx.annotation.Keep; + +import com.sendbird.android.shadow.com.google.gson.annotations.SerializedName; +import com.sendbird.uikit.SendBirdUIKit; +import com.sendbird.uikit.utils.TextUtils; + +@Keep +public class AdminMessageUser { + + @SerializedName("metadata") + private AdminMessageMetaData metaData; + + public AdminMessageMetaData getMetaData() { + return metaData; + } + + public void setMetaData(AdminMessageMetaData metaData) { + this.metaData = metaData; + } + + public String getName() { + String phone = metaData.getPhone(); + String username = metaData.getUsername(); + + String phonebookName = SendBirdUIKit.findPhoneBookName(phone); + return TextUtils.isEmpty(phonebookName) ? username : phonebookName; + } +} diff --git a/uikit/src/main/java/com/sendbird/uikit/widgets/AdminMessageView.java b/uikit/src/main/java/com/sendbird/uikit/widgets/AdminMessageView.java index 4996c906..74700693 100644 --- a/uikit/src/main/java/com/sendbird/uikit/widgets/AdminMessageView.java +++ b/uikit/src/main/java/com/sendbird/uikit/widgets/AdminMessageView.java @@ -11,8 +11,11 @@ import com.sendbird.android.AdminMessage; import com.sendbird.android.BaseMessage; +import com.sendbird.android.shadow.com.google.gson.Gson; import com.sendbird.uikit.R; import com.sendbird.uikit.databinding.SbViewAdminMessageComponentBinding; +import com.sendbird.uikit.model.admin.AdminMessageData; +import com.sendbird.uikit.utils.TextUtils; public class AdminMessageView extends BaseMessageView { private SbViewAdminMessageComponentBinding binding; @@ -55,7 +58,15 @@ private void init(Context context, AttributeSet attrs, int defStyle) { } public void drawMessage(BaseMessage message) { - binding.tvMessage.setText(message.getMessage()); + try { + String data = message.getData(); + AdminMessageData adminMessageData = new Gson().fromJson(data, AdminMessageData.class); + String adminMessageContent = adminMessageData.getContent(); + + binding.tvMessage.setText(TextUtils.isEmpty(adminMessageContent) ? message.getMessage() : adminMessageContent); + } catch (Exception e) { + binding.tvMessage.setText(message.getMessage()); + } } @BindingAdapter("message")