Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a filter on FirebaseRecyclerAdapter

I am using FirebaseRecyclerAdapter to display chat messages.

private void attachRecyclerViewAdapter() {
    lastFifty = mChatRef.limitToLast(50).;
    mRecyclerViewAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(
            Chat.class, R.layout.message, ChatHolder.class, lastFifty) {

        @Override
        public void populateViewHolder(ChatHolder chatView, Chat chat, int position) {
            chatView.setName(chat.getName());
            chatView.setText(chat.getText());
            chatView.setTimeLocation(chat.getTime());
            FirebaseUser currentUser = mAuth.getCurrentUser();

            if (currentUser != null && chat.getUid().equals(currentUser.getUid())) {
                chatView.setIsSender(true);
            } else {
                chatView.setIsSender(false);
            }
        }
    };

I have a list that contains list of specific users. I would like to apply filter to see only messages from those specific users. What should I do ?

like image 915
user655561 Avatar asked Jul 18 '16 10:07

user655561


1 Answers

You can create messages with user id pair nodes. For example messages->'uid1-uid2'->...

To prevent which is first order uids alphatecially as the following messageId generator code does:

public static String getMessageId(String id1, String id2){
        String messageId;

        if(id1.compareTo(id2) < 0){
            messageId = id1 + "-" + id2;
        }else if(id1.compareTo(id2) > 0) {
            messageId = id2 + "-" + id1;
        }else{
            messageId = id1;
        }

        return messageId;
    }

When you want to see the chat history between a user and urself, obtain the user's id and generate messageId = getMessageId(id1, id2); or messageId = getMessageId(id2, id1); gives the same result since the order doesn't affect the result.

Then call the messages from the node messages -> messageId

P.S. you should restructure your messages node as i describe.

EDIT

You can convert messageId to md5 equivalent to save chars.

just change

return messageId;

to

return md5(messageId);

where md5 is:

public static String md5(final String s) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest
                .getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < messageDigest.length; i++) {
            String h = Integer.toHexString(0xFF & messageDigest[i]);
            while (h.length() < 2)
                h = "0" + h;
            hexString.append(h);
        }
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}
like image 84
ugur Avatar answered Sep 20 '22 14:09

ugur