Skip to content

Commit

Permalink
update chat contact name in realtime
Browse files Browse the repository at this point in the history
  • Loading branch information
tien-awair committed Jun 2, 2022
1 parent 59e9569 commit 9c1f15e
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private void redirectChannelIfNeeded(Intent intent) {
}

private void showCustomChannelActivity(String channelUrl) {
Intent intent = CustomChannelActivity.newIntentFromCustomActivity(GroupChannelMainActivity.this, CustomChannelActivity.class, channelUrl, null);
Intent intent = CustomChannelActivity.newIntentFromCustomActivity(GroupChannelMainActivity.this, CustomChannelActivity.class, channelUrl);
startActivity(intent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ protected ChannelFragment createChannelFragment(@NonNull String channelUrl, Stri
return new ChannelFragment.Builder(channelUrl, R.style.CustomMessageListStyle)
.setCustomChannelFragment(customChannelFragment)
.setUseHeader(true)
.setHeaderTitle(null)
.setUseHeaderLeftButton(true)
.setUseHeaderRightButton(true)
.setUseLastSeenAt(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private CustomUserListAdapter createCustomUserListAdapter() {

private void showCustomChannelActivity(String channelUrl) {
if (getActivity() != null && getContext() != null) {
Intent intent = CustomChannelActivity.newIntentFromCustomActivity(getActivity(), CustomChannelActivity.class, channelUrl, null);
Intent intent = CustomChannelActivity.newIntentFromCustomActivity(getActivity(), CustomChannelActivity.class, channelUrl);
startActivity(intent);
getActivity().finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private CustomUserListAdapter createCustomAdapter() {

private void showCustomChannelActivity() {
if (getContext() != null) {
Intent intent = ChannelActivity.newIntentFromCustomActivity(getContext(), CustomChannelActivity.class, channel.getUrl(), null);
Intent intent = ChannelActivity.newIntentFromCustomActivity(getContext(), CustomChannelActivity.class, channel.getUrl());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,7 @@ public class ChannelActivity extends AppCompatActivity {
* @return ChannelActivity Intent
*/
public static Intent newIntent(@NonNull Context context, @NonNull String channelUrl) {
return newIntentFromCustomActivity(context, ChannelActivity.class, channelUrl, null);
}

/**
* Create an intent for a {@link ChannelActivity}.
*
* @param context A Context of the application package implementing this class.
* @param channelUrl the url of the channel will be implemented.
* @return ChannelActivity Intent
*/
public static Intent newIntent(@NonNull Context context, @NonNull String channelUrl, @Nullable String title) {
return newIntentFromCustomActivity(context, ChannelActivity.class, channelUrl, title);
return newIntentFromCustomActivity(context, ChannelActivity.class, channelUrl);
}

/**
Expand All @@ -59,8 +48,8 @@ public static Intent newIntent(@NonNull Context context, @NonNull String channel
* @return Returns a newly created Intent that can be used to launch the activity.
* @since 1.1.2
*/
public static Intent newIntentFromCustomActivity(@NonNull Context context, @NonNull Class<? extends ChannelActivity> cls, @NonNull String channelUrl, @Nullable String title) {
return new IntentBuilder(context, cls, channelUrl, title).build();
public static Intent newIntentFromCustomActivity(@NonNull Context context, @NonNull Class<? extends ChannelActivity> cls, @NonNull String channelUrl) {
return new IntentBuilder(context, cls, channelUrl).build();
}

private String url;
Expand Down Expand Up @@ -146,9 +135,6 @@ protected ChannelFragment createChannelFragment(@NonNull String channelUrl, @Nul
ChannelFragment.Builder builder = new ChannelFragment.Builder(channelUrl)
.setUseHeader(true)
.setStartingPoint(intent.getLongExtra(StringSet.KEY_STARTING_POINT, Long.MAX_VALUE));
if (title != null) {
builder.setHeaderTitle(title);
}
if (intent.hasExtra(StringSet.KEY_HIGHLIGHT_MESSAGE_INFO)) {
builder.setHighlightMessageInfo(intent.getParcelableExtra(StringSet.KEY_HIGHLIGHT_MESSAGE_INFO));
}
Expand All @@ -164,7 +150,6 @@ protected ChannelFragment createChannelFragment(@NonNull String channelUrl, @Nul
public static class IntentBuilder {
private final Context context;
private final String channelUrl;
private final String title;
private long startingPoint = Long.MAX_VALUE;
private HighlightMessageInfo highlightMessageInfo;
private Class<? extends ChannelActivity> customClass = ChannelActivity.class;
Expand All @@ -176,10 +161,9 @@ public static class IntentBuilder {
* @param channelUrl The url of the channel will be implemented.
* @since 2.1.0
*/
public IntentBuilder(@NonNull Context context, @NonNull String channelUrl, @Nullable String title) {
public IntentBuilder(@NonNull Context context, @NonNull String channelUrl) {
this.context = context;
this.channelUrl = channelUrl;
this.title = title;
}

/**
Expand All @@ -190,11 +174,10 @@ public IntentBuilder(@NonNull Context context, @NonNull String channelUrl, @Null
* @param channelUrl The url of the channel will be implemented.
* @since 2.1.0
*/
public IntentBuilder(@NonNull Context context, @NonNull Class<? extends ChannelActivity> customClass, @NonNull String channelUrl, @Nullable String title) {
public IntentBuilder(@NonNull Context context, @NonNull Class<? extends ChannelActivity> customClass, @NonNull String channelUrl) {
this.context = context;
this.channelUrl = channelUrl;
this.customClass = customClass;
this.title = title;
}

/**
Expand Down Expand Up @@ -231,7 +214,6 @@ public Intent build() {
Intent intent = new Intent(context, customClass);
intent.putExtra(StringSet.KEY_CHANNEL_URL, channelUrl);
intent.putExtra(StringSet.KEY_STARTING_POINT, startingPoint);
intent.putExtra(StringSet.KEY_HEADER_TITLE, title);
if (highlightMessageInfo != null) {
intent.putExtra(StringSet.KEY_HIGHLIGHT_MESSAGE_INFO, highlightMessageInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ public class ChannelFragment extends BaseGroupChannelFragment implements OnIdent
private OnInputTextChangedListener editModeTextChangedListener;
final AtomicBoolean isInitCallFinished = new AtomicBoolean(false);
final AtomicBoolean shouldAnimate = new AtomicBoolean(false);
private String headerTitle = null;

private final ReplyType replyType = SendBirdUIKit.getReplyType();

Expand Down Expand Up @@ -273,9 +272,7 @@ private ChannelViewModel createViewModel(GroupChannel channel) {
private void drawChannel(GroupChannel channel) {
Logger.dev("++ drawChannel()");
if (isActive()) {
if (headerTitle == null) {
binding.chvChannelHeader.getTitleTextView().setText(ChannelUtils.makeTitleText(getContext(), channel));
}
binding.chvChannelHeader.getTitleTextView().setText(ChannelUtils.makeTitleText(channel));
ChannelUtils.loadChannelCover(binding.chvChannelHeader.getProfileView(), channel);
binding.tvInformation.setVisibility(channel.isFrozen() ? View.VISIBLE : View.GONE);
binding.tvInformation.setText(R.string.sb_text_information_channel_frozen);
Expand Down Expand Up @@ -317,18 +314,12 @@ private void initHeaderOnCreated() {
headerRightButtonIconResId = args.getInt(StringSet.KEY_HEADER_RIGHT_BUTTON_ICON_RES_ID, R.drawable.icon_info);
headerLeftButtonIconTint = args.getParcelable(StringSet.KEY_HEADER_LEFT_BUTTON_ICON_TINT);
headerRightButtonIconTint = args.getParcelable(StringSet.KEY_HEADER_RIGHT_BUTTON_ICON_TINT);
headerTitle = args.getString(StringSet.KEY_HEADER_TITLE, null);
}

binding.chvChannelHeader.setVisibility(useHeader ? View.VISIBLE : View.GONE);

binding.chvChannelHeader.setUseLeftImageButton(useHeaderLeftButton);
binding.chvChannelHeader.setUseRightButton(useHeaderRightButton);

if (headerTitle != null) {
binding.chvChannelHeader.getTitleTextView().setText(headerTitle);
}

binding.chvChannelHeader.setLeftImageButtonResource(headerLeftButtonIconResId);
if (args != null && args.containsKey(StringSet.KEY_HEADER_LEFT_BUTTON_ICON_RES_ID)) {
binding.chvChannelHeader.setLeftImageButtonTint(headerLeftButtonIconTint);
Expand Down Expand Up @@ -1824,15 +1815,7 @@ public List<Member> getMembers() {
public void onPhonebookUpdated() {
Handler handler = new Handler(getContext().getMainLooper());
handler.post(() -> {
if (isSingleChat()) {
for (Member member: getMembers()) {
if (!member.getUserId().equals(SendBirdUIKit.getAdapter().getUserInfo().getUserId())) {
String phoneNumber = member.getMetaData("phone");
String memberName = SendBirdUIKit.findPhoneBookName(phoneNumber);
binding.chvChannelHeader.getTitleTextView().setText(memberName);
}
}
}
binding.chvChannelHeader.getTitleTextView().setText(ChannelUtils.makeTitleText(channel));
});
}

Expand Down Expand Up @@ -1962,18 +1945,6 @@ public Builder setUseTypingIndicator(boolean useTypingIndicator) {
return this;
}

/**
* Sets the title of the header.
*
* @param title text to be displayed.
* @return This Builder object to allow for chaining of calls to set methods.
* @since 2.1.1
*/
public Builder setHeaderTitle(String title) {
bundle.putString(StringSet.KEY_HEADER_TITLE, title);
return this;
}

/**
* Sets the icon on the left button of the header.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ protected void onSearchResultReceived(List<BaseMessage> searchResults) {
*/
protected void onItemClicked(View view, int position, BaseMessage message) {
Logger.d(">> MessageSearchFragment::onItemClicked(position=%s)", position);
Intent intent = new ChannelActivity.IntentBuilder(getContext(), channel.getUrl(), null)
Intent intent = new ChannelActivity.IntentBuilder(getContext(), channel.getUrl())
.setStartingPoint(message.getCreatedAt())
.setHighlightMessageInfo(HighlightMessageInfo.fromMessage(message))
.build();
Expand Down
51 changes: 51 additions & 0 deletions uikit/src/main/java/com/sendbird/uikit/utils/ChannelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.sendbird.android.Sender;
import com.sendbird.android.User;
import com.sendbird.uikit.R;
import com.sendbird.uikit.SendBirdUIKit;
import com.sendbird.uikit.consts.StringSet;
import com.sendbird.uikit.widgets.ChannelCoverView;

Expand All @@ -22,6 +23,56 @@

public class ChannelUtils {

private static Member getOtherMember(GroupChannel channel) {
List<Member> members = channel.getMembers();
String userId = SendBirdUIKit.getAdapter().getUserInfo().getUserId();
for (Member member: members) {
if (!member.getUserId().equals(userId)) {
return member;
}
}
return null;
}

private static List<Member> getOtherMembers(GroupChannel channel) {
List<Member> members = channel.getMembers();
List<Member> otherMembers = new ArrayList<>();
String userId = SendBirdUIKit.getAdapter().getUserInfo().getUserId();
for (Member member: members) {
if (!member.getUserId().equals(userId)) {
otherMembers.add(member);
}
}
return otherMembers;
}

public static String makeTitleText(GroupChannel channel) {
boolean isSingleChat = channel.getMemberCount() <= 2;
if (isSingleChat) {
Member otherMember = getOtherMember(channel);
if (otherMember != null) {
String phoneNumber = otherMember.getMetaData("phone");
return SendBirdUIKit.findPhoneBookName(phoneNumber);
} else {
return "No members";
}
} else {
String channelName = channel.getName();
List<Member> otherMembers = getOtherMembers(channel);
if (!TextUtils.isEmpty(channelName)) return channelName;
else {
StringBuilder names = new StringBuilder();
for (Member member: otherMembers) {
String phoneNumber = member.getMetaData("phone");
String memberName = SendBirdUIKit.findPhoneBookName(phoneNumber);
names.append(memberName);
names.append(", ");
}
return names.toString();
}
}
}

public static String makeTitleText(@NonNull Context context, GroupChannel channel) {
String channelName = channel.getName();
if (!TextUtils.isEmpty(channelName) && !channelName.equals(StringSet.GROUP_CHANNEL)) {
Expand Down

0 comments on commit 9c1f15e

Please sign in to comment.