Search in sources :

Example 1 with Chat

use of com.example.first_responder_app.messaging.Chat in project FirstResponse by mattpost1700.

the class ChatGroupFragment method populateChatList.

private void populateChatList() {
    db.collection("chat").get().addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            ArrayList<Chat> temp = new ArrayList<>();
            for (QueryDocumentSnapshot doc : task.getResult()) {
                ArrayList<String> members = (ArrayList<String>) doc.get("members");
                if (user != null) {
                    String memberName = user.getFirst_name() + " " + user.getLast_name() + "/" + user.getDocumentId();
                    if (members.contains(memberName)) {
                        Timestamp t = (Timestamp) doc.get("most_recent_message_time");
                        Chat chat = new Chat(doc.getId(), (String) doc.get("most_recent_message"), members, (String) doc.get("chat_name"), t);
                        temp.add(chat);
                    }
                }
            }
            listOfChats.clear();
            listOfChats.addAll(temp);
            Collections.sort(listOfChats);
            Collections.reverse(listOfChats);
            chatGroupRecyclerViewAdapter.notifyDataSetChanged();
        } else {
            Log.d(TAG, "db get failed in chat group page " + task.getException());
        }
    });
}
Also used : QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) Chat(com.example.first_responder_app.messaging.Chat) ArrayList(java.util.ArrayList) Timestamp(com.google.firebase.Timestamp)

Example 2 with Chat

use of com.example.first_responder_app.messaging.Chat in project FirstResponse by mattpost1700.

the class ChatGroupRecyclerViewAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Chat chat = mData.get(position);
    Context context = holder.itemView.getContext();
    String userId = "";
    ActiveUser activeUser = (ActiveUser) context;
    if (activeUser != null) {
        UsersDataModel user = activeUser.getActive();
        if (user != null) {
            userId = user.getDocumentId();
        }
    }
    ArrayList<String> members = chat.getMembers();
    String a = holder.name.getText().toString();
    String chatName = "";
    // If it is direct message, set chat name to user's name, otherwise set chat name to the name stored in db
    if (members.size() == 2) {
        String[] memberAndId0 = members.get(0).split("/");
        String[] memberAndId1 = members.get(1).split("/");
        if (memberAndId0[1].equals(userId)) {
            holder.name.setText(memberAndId1[0]);
        } else {
            holder.name.setText(memberAndId0[0]);
        }
    } else {
        holder.name.setText(chat.getChatName());
        a = holder.name.getText().toString();
    }
    holder.recentMsg.setText(chat.getMostRecentMessage());
    holder.data = chat;
}
Also used : Context(android.content.Context) UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) ActiveUser(com.example.first_responder_app.interfaces.ActiveUser) Chat(com.example.first_responder_app.messaging.Chat)

Aggregations

Chat (com.example.first_responder_app.messaging.Chat)2 Context (android.content.Context)1 UsersDataModel (com.example.first_responder_app.dataModels.UsersDataModel)1 ActiveUser (com.example.first_responder_app.interfaces.ActiveUser)1 Timestamp (com.google.firebase.Timestamp)1 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)1 ArrayList (java.util.ArrayList)1