use of com.applozic.mobicomkit.api.conversation.MentionMetadataModel in project Applozic-Android-SDK by AppLozic.
the class MentionHelper method getServerSendReadyMentionPair.
public static MentionPair getServerSendReadyMentionPair(String notServerSendReadyMessageString, ArrayList<MentionMetadataModel> notServerSendReadyMentionMetadataList) {
if (TextUtils.isEmpty(notServerSendReadyMessageString)) {
return new MentionPair(Utils.EMPTY_STRING, null);
}
if (notServerSendReadyMentionMetadataList == null || notServerSendReadyMentionMetadataList.isEmpty()) {
return new MentionPair(notServerSendReadyMessageString, null);
}
try {
// sorting in ascending order is required so that server ready mention indexes can be calculated
// reason: when replacing mention identifiers with server send ready @userId, the indexes in the metadata model remain no longer valid
// because the lengths of the replacements and originals differ
Collections.sort(notServerSendReadyMentionMetadataList, new Comparator<MentionMetadataModel>() {
@Override
public int compare(MentionMetadataModel o1, MentionMetadataModel o2) {
int comparatorValue = 0;
if (o1.indices[0] < o2.indices[0]) {
comparatorValue = -1;
} else if (o1.indices[0] > o2.indices[0]) {
comparatorValue = +1;
}
return comparatorValue;
}
});
} catch (NullPointerException | IndexOutOfBoundsException exception) {
exception.printStackTrace();
return new MentionPair(notServerSendReadyMessageString, null);
}
ArrayList<MentionMetadataModel> serverSendReadyMentionMetadataList = new ArrayList<>();
String serverSendReadyMessageString = notServerSendReadyMessageString;
// replacing mentions identifiers in edit text string and side by side also creating new metadata model list
for (MentionMetadataModel oldMentionMetadataModel : notServerSendReadyMentionMetadataList) {
String mentionIdentifier = MentionHelper.getMentionIdentifierString(oldMentionMetadataModel.displayName, oldMentionMetadataModel.userId);
int indexOfMentionIdentifierStart = serverSendReadyMessageString.indexOf(mentionIdentifier);
if (indexOfMentionIdentifierStart == -1) {
continue;
}
serverSendReadyMessageString = serverSendReadyMessageString.replaceFirst(mentionIdentifier, oldMentionMetadataModel.userId);
MentionMetadataModel serverSendReadyMentionMetadataModel = new MentionMetadataModel();
serverSendReadyMentionMetadataModel.userId = oldMentionMetadataModel.userId;
serverSendReadyMentionMetadataModel.displayName = oldMentionMetadataModel.displayName;
int[] indices = new int[2];
// - 1 because we need index from mention token start character (@)
indices[0] = indexOfMentionIdentifierStart - 1;
indices[1] = indexOfMentionIdentifierStart + serverSendReadyMentionMetadataModel.userId.length();
serverSendReadyMentionMetadataModel.indices = indices;
serverSendReadyMentionMetadataList.add(serverSendReadyMentionMetadataModel);
}
return new MentionPair(serverSendReadyMessageString, serverSendReadyMentionMetadataList);
}
use of com.applozic.mobicomkit.api.conversation.MentionMetadataModel in project Applozic-Android-SDK by AppLozic.
the class MentionHelper method getMentionsDataFromMessageMetadata.
@NonNull
public static List<MentionMetadataModel> getMentionsDataFromMessageMetadata(@Nullable Map<String, String> messageMetadata) {
List<MentionMetadataModel> mentionMetadataModels = new ArrayList<>();
if (messageMetadata == null) {
return mentionMetadataModels;
}
try {
String mentionMetadataModelsString = messageMetadata.get(MentionMetadataModel.AL_MEMBER_MENTION);
if (!TextUtils.isEmpty(mentionMetadataModelsString)) {
MentionMetadataModel[] mentionMetadataModelsArray = (MentionMetadataModel[]) GsonUtils.getObjectFromJson(mentionMetadataModelsString, MentionMetadataModel[].class);
mentionMetadataModels = Arrays.asList(mentionMetadataModelsArray);
}
} catch (JsonParseException exception) {
exception.printStackTrace();
return mentionMetadataModels;
}
return mentionMetadataModels;
}
use of com.applozic.mobicomkit.api.conversation.MentionMetadataModel in project Applozic-Android-SDK by AppLozic.
the class MentionHelper method getMessageSpannableStringForMentionsDisplay.
@NonNull
private static Spannable getMessageSpannableStringForMentionsDisplay(Context context, String messageStringWithMentionsUserId, List<MentionMetadataModel> mentionMetadataModels, boolean isDetailedConversationList, int detailedSpanColor) {
if (TextUtils.isEmpty(messageStringWithMentionsUserId)) {
return new SpannableString(Utils.EMPTY_STRING);
}
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(messageStringWithMentionsUserId);
if (mentionMetadataModels == null) {
return spannableStringBuilder;
}
try {
// the list must be sorted in descending order position of mention to avoid the side-effect of replacing with indexes
Collections.sort(mentionMetadataModels, new Comparator<MentionMetadataModel>() {
@Override
public int compare(MentionMetadataModel o1, MentionMetadataModel o2) {
int comparatorValue = 0;
if (o1.indices[0] > o2.indices[0]) {
comparatorValue = -1;
} else if (o1.indices[0] < o2.indices[0]) {
comparatorValue = +1;
}
return comparatorValue;
}
});
} catch (NullPointerException | IndexOutOfBoundsException exception) {
exception.printStackTrace();
return spannableStringBuilder;
}
for (MentionMetadataModel metadataModel : mentionMetadataModels) {
if (metadataModel == null || TextUtils.isEmpty(metadataModel.userId) || metadataModel.indices == null || metadataModel.indices.length < 2) {
continue;
}
int start = metadataModel.indices[0];
Contact contact = new AppContactService(context).getContactById(metadataModel.userId);
String userIdOrDisplayName = !TextUtils.isEmpty(contact.getDisplayName()) ? contact.getDisplayName() : contact.getUserId();
int end = metadataModel.indices[1];
int replacedEnd = metadataModel.indices[0] + userIdOrDisplayName.length();
int length = spannableStringBuilder.length();
if (start >= 0 && start < length && end >= 0 && end - 1 < length) {
spannableStringBuilder.replace(start + 1, end, userIdOrDisplayName);
} else {
continue;
}
int replacedLength = spannableStringBuilder.length();
if (start < replacedLength && replacedEnd >= 0 && replacedEnd < replacedLength) {
CharacterStyle characterStyle = isDetailedConversationList ? new ReceivedDetailedConversationMessageMentionDisplaySpan(detailedSpanColor) : new ReceivedQuickConversationMessageMentionDisplaySpan();
spannableStringBuilder.setSpan(characterStyle, start, replacedEnd + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return spannableStringBuilder;
}
use of com.applozic.mobicomkit.api.conversation.MentionMetadataModel in project Applozic-Android-SDK by AppLozic.
the class MentionAutoCompleteTextView method getMentionPairForMessageSend.
private MentionHelper.MentionPair getMentionPairForMessageSend(MentionAutoCompleteTextView mentionAutoCompleteTextView) {
// items in this list must have identifierStart
ArrayList<MentionMetadataModel> notServerSendReadyMentionMetadataList = mentionAutoCompleteTextView.getMentionMetadata();
String notServerSendReadyMessageString = mentionAutoCompleteTextView.getText().toString();
return MentionHelper.getServerSendReadyMentionPair(notServerSendReadyMessageString, notServerSendReadyMentionMetadataList);
}
use of com.applozic.mobicomkit.api.conversation.MentionMetadataModel in project Applozic-Android-SDK by AppLozic.
the class MentionHelper method isLoggedInUserMentionedInChannelMessage.
public static boolean isLoggedInUserMentionedInChannelMessage(@NonNull Context context, @NonNull Message message) {
String loggedInUserId = MobiComUserPreference.getInstance(context).getUserId();
List<MentionMetadataModel> mentionMetadataModelList = getMentionsDataFromMessageMetadata(message.getMetadata());
if (mentionMetadataModelList.isEmpty()) {
return false;
}
boolean isLoggedInUserMentioned = false;
for (MentionMetadataModel metadataModel : mentionMetadataModelList) {
if (loggedInUserId.equals(metadataModel.userId)) {
isLoggedInUserMentioned = true;
break;
}
}
return message.getGroupId() != null && message.getGroupId() != 0 && isLoggedInUserMentioned;
}
Aggregations