Skip to content

Commit

Permalink
Recompose the admin message
Browse files Browse the repository at this point in the history
  • Loading branch information
tien-awair committed May 20, 2022
1 parent 5fb7a5a commit d845cb3
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<AdminMessageUser> users;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public List<AdminMessageUser> getUsers() {
return users;
}

public void setUsers(List<AdminMessageUser> users) {
this.users = users;
}

public String getContent() {
if (type.equalsIgnoreCase(AdminMessageType.USER_JOINED)) {
return joinUserNames() + " joined";
}
return "";
}

public String joinUserNames() {
List<String> 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<String> getUserNames() {
List<String> names = new ArrayList<>();
for (AdminMessageUser user : users) {
names.add(user.getName());
}
return names;
}
}

Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.sendbird.uikit.model.admin;

public class AdminMessageType {
public static final String USER_JOINED = "USER_JOIN";
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit d845cb3

Please sign in to comment.