Search in sources :

Example 1 with MentionMetadataModel

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);
}
Also used : ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) MentionMetadataModel(com.applozic.mobicomkit.api.conversation.MentionMetadataModel)

Example 2 with MentionMetadataModel

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;
}
Also used : ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) MentionMetadataModel(com.applozic.mobicomkit.api.conversation.MentionMetadataModel) JsonParseException(com.google.gson.JsonParseException) NonNull(androidx.annotation.NonNull)

Example 3 with MentionMetadataModel

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;
}
Also used : AppContactService(com.applozic.mobicomkit.contact.AppContactService) SpannableString(android.text.SpannableString) MentionMetadataModel(com.applozic.mobicomkit.api.conversation.MentionMetadataModel) CharacterStyle(android.text.style.CharacterStyle) Contact(com.applozic.mobicommons.people.contact.Contact) SpannableString(android.text.SpannableString) SpannableStringBuilder(android.text.SpannableStringBuilder) NonNull(androidx.annotation.NonNull)

Example 4 with MentionMetadataModel

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);
}
Also used : SpannableString(android.text.SpannableString) MentionMetadataModel(com.applozic.mobicomkit.api.conversation.MentionMetadataModel)

Example 5 with MentionMetadataModel

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;
}
Also used : SpannableString(android.text.SpannableString) MentionMetadataModel(com.applozic.mobicomkit.api.conversation.MentionMetadataModel)

Aggregations

SpannableString (android.text.SpannableString)6 MentionMetadataModel (com.applozic.mobicomkit.api.conversation.MentionMetadataModel)6 NonNull (androidx.annotation.NonNull)2 ArrayList (java.util.ArrayList)2 SpannableStringBuilder (android.text.SpannableStringBuilder)1 CharacterStyle (android.text.style.CharacterStyle)1 Mention (com.applozic.mobicomkit.api.mention.Mention)1 AppContactService (com.applozic.mobicomkit.contact.AppContactService)1 Contact (com.applozic.mobicommons.people.contact.Contact)1 JsonParseException (com.google.gson.JsonParseException)1 Matcher (java.util.regex.Matcher)1