From 9c1f15e9d4be4a650430d6f6f6c5f89281e348b8 Mon Sep 17 00:00:00 2001 From: Tien Date: Thu, 2 Jun 2022 12:56:02 +0700 Subject: [PATCH] update chat contact name in realtime --- .../GroupChannelMainActivity.java | 2 +- .../activities/CustomChannelActivity.java | 1 - .../CustomCreateChannelFragment.java | 2 +- .../CustomInviteChannelFragment.java | 2 +- .../uikit/activities/ChannelActivity.java | 28 ++-------- .../uikit/fragments/ChannelFragment.java | 33 +----------- .../fragments/MessageSearchFragment.java | 2 +- .../sendbird/uikit/utils/ChannelUtils.java | 51 +++++++++++++++++++ 8 files changed, 62 insertions(+), 59 deletions(-) diff --git a/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/GroupChannelMainActivity.java b/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/GroupChannelMainActivity.java index 870902e7..b06e095e 100644 --- a/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/GroupChannelMainActivity.java +++ b/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/GroupChannelMainActivity.java @@ -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); } diff --git a/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/activities/CustomChannelActivity.java b/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/activities/CustomChannelActivity.java index 93e173d2..183df6cf 100644 --- a/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/activities/CustomChannelActivity.java +++ b/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/activities/CustomChannelActivity.java @@ -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) diff --git a/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/fragments/CustomCreateChannelFragment.java b/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/fragments/CustomCreateChannelFragment.java index 2080044b..07cc867d 100644 --- a/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/fragments/CustomCreateChannelFragment.java +++ b/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/fragments/CustomCreateChannelFragment.java @@ -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(); } diff --git a/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/fragments/CustomInviteChannelFragment.java b/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/fragments/CustomInviteChannelFragment.java index 6702d128..c9e19bc9 100644 --- a/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/fragments/CustomInviteChannelFragment.java +++ b/uikit-custom-sample/src/main/java/com/sendbird/uikit/customsample/groupchannel/fragments/CustomInviteChannelFragment.java @@ -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); } diff --git a/uikit/src/main/java/com/sendbird/uikit/activities/ChannelActivity.java b/uikit/src/main/java/com/sendbird/uikit/activities/ChannelActivity.java index 26aad95a..afe5e835 100644 --- a/uikit/src/main/java/com/sendbird/uikit/activities/ChannelActivity.java +++ b/uikit/src/main/java/com/sendbird/uikit/activities/ChannelActivity.java @@ -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); } /** @@ -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 cls, @NonNull String channelUrl, @Nullable String title) { - return new IntentBuilder(context, cls, channelUrl, title).build(); + public static Intent newIntentFromCustomActivity(@NonNull Context context, @NonNull Class cls, @NonNull String channelUrl) { + return new IntentBuilder(context, cls, channelUrl).build(); } private String url; @@ -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)); } @@ -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 customClass = ChannelActivity.class; @@ -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; } /** @@ -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 customClass, @NonNull String channelUrl, @Nullable String title) { + public IntentBuilder(@NonNull Context context, @NonNull Class customClass, @NonNull String channelUrl) { this.context = context; this.channelUrl = channelUrl; this.customClass = customClass; - this.title = title; } /** @@ -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); } diff --git a/uikit/src/main/java/com/sendbird/uikit/fragments/ChannelFragment.java b/uikit/src/main/java/com/sendbird/uikit/fragments/ChannelFragment.java index 56e08c9b..bd263929 100644 --- a/uikit/src/main/java/com/sendbird/uikit/fragments/ChannelFragment.java +++ b/uikit/src/main/java/com/sendbird/uikit/fragments/ChannelFragment.java @@ -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(); @@ -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); @@ -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); @@ -1824,15 +1815,7 @@ public List 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)); }); } @@ -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. * diff --git a/uikit/src/main/java/com/sendbird/uikit/fragments/MessageSearchFragment.java b/uikit/src/main/java/com/sendbird/uikit/fragments/MessageSearchFragment.java index 14205b4f..6a8465cf 100644 --- a/uikit/src/main/java/com/sendbird/uikit/fragments/MessageSearchFragment.java +++ b/uikit/src/main/java/com/sendbird/uikit/fragments/MessageSearchFragment.java @@ -229,7 +229,7 @@ protected void onSearchResultReceived(List 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(); diff --git a/uikit/src/main/java/com/sendbird/uikit/utils/ChannelUtils.java b/uikit/src/main/java/com/sendbird/uikit/utils/ChannelUtils.java index e95a9be3..61e04a91 100644 --- a/uikit/src/main/java/com/sendbird/uikit/utils/ChannelUtils.java +++ b/uikit/src/main/java/com/sendbird/uikit/utils/ChannelUtils.java @@ -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; @@ -22,6 +23,56 @@ public class ChannelUtils { + private static Member getOtherMember(GroupChannel channel) { + List 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 getOtherMembers(GroupChannel channel) { + List members = channel.getMembers(); + List 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 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)) {