Search in sources :

Example 1 with Message

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

the class ChatGroupFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // binding fragment with nav_map by using navHostFragment, throw this block of code in there and that allows you to switch to other fragments
    ChatGroupFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.chat_group_fragment, container, false);
    NavHostFragment navHostFragment = (NavHostFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
    // TODO: navCont created for side bar(still need to be implemented)
    NavController navController = navHostFragment.getNavController();
    firestoreDatabase = new FirestoreDatabase();
    db = firestoreDatabase.getDb();
    activeUser = (ActiveUser) getActivity();
    if (activeUser != null) {
        user = activeUser.getActive();
    }
    listOfChats = new ArrayList<>();
    populateChatList();
    ChatGroupRecyclerViewAdapter.ItemClickListener chatClickListener = ((view, position, data) -> {
        // passing data to chat
        mViewModel = new ViewModelProvider(requireActivity()).get(ChatViewModel.class);
        mViewModel.setChatDetail(data);
        List<Message> temp = new ArrayList<>();
        mViewModel.setListOfMessages(temp);
        NavDirections action = ChatGroupFragmentDirections.actionChatGroupFragmentToChatFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    });
    RecyclerView chatGroupRecyclerView = binding.chatGroupRecyclerView;
    chatGroupRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    chatGroupRecyclerViewAdapter = new ChatGroupRecyclerViewAdapter(getContext(), listOfChats);
    chatGroupRecyclerViewAdapter.setClickListener(chatClickListener);
    chatGroupRecyclerView.setAdapter(chatGroupRecyclerViewAdapter);
    binding.chatTextView.setOnClickListener(v -> {
        NavDirections action = ChatGroupFragmentDirections.actionChatGroupFragmentToChatFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    });
    binding.addChatTextView.setOnClickListener(v -> {
        NavDirections action = ChatGroupFragmentDirections.actionChatGroupFragmentToNewChatFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    });
    return binding.getRoot();
}
Also used : Message(com.example.first_responder_app.messaging.Message) ArrayList(java.util.ArrayList) NavController(androidx.navigation.NavController) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) NavDirections(androidx.navigation.NavDirections) ChatGroupRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.ChatGroupRecyclerViewAdapter) ChatViewModel(com.example.first_responder_app.viewModels.ChatViewModel) RecyclerView(androidx.recyclerview.widget.RecyclerView) NavHostFragment(androidx.navigation.fragment.NavHostFragment) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) ChatGroupFragmentBinding(com.example.first_responder_app.databinding.ChatGroupFragmentBinding) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 2 with Message

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

the class ChatRecyclerViewAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Message message = mData.get(position);
    if (getItemViewType(position) == 5001) {
        holder.myMsg.setText(message.getMessageText());
    } else {
        String senderId = message.getSender();
        String senderName = mMembers.get(senderId);
        holder.theirName.setText(senderName);
        holder.theirMsg.setText(message.getMessageText());
    }
}
Also used : Message(com.example.first_responder_app.messaging.Message)

Example 3 with Message

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

the class ChatFragment method populateMessageList.

private void populateMessageList() {
    String chatId = c.getId();
    db.collection("chat").document(chatId).collection("messages").get().addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            ArrayList<Message> temp = new ArrayList<>();
            for (QueryDocumentSnapshot doc : task.getResult()) {
                Timestamp t = (Timestamp) doc.get("time_sent");
                ;
                Message m = new Message(doc.getId(), (String) doc.get("message_text"), (String) doc.get("sender"), t);
                temp.add(m);
            }
            listOfMessages.clear();
            listOfMessages.addAll(temp);
            Collections.sort(listOfMessages);
            mViewModel.setListOfMessages(listOfMessages);
            chatRecyclerViewAdapter.notifyDataSetChanged();
        } else {
            Log.d(TAG, "db get failed in chat page " + task.getException());
        }
    });
}
Also used : Message(com.example.first_responder_app.messaging.Message) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList) Timestamp(com.google.firebase.Timestamp)

Example 4 with Message

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

the class FirestoreDatabase method addMessage.

public void addMessage(String chatId, String messageText, String senderId, ChatRecyclerViewAdapter chatRecyclerViewAdapter, ChatViewModel mViewModel) {
    Timestamp now = Timestamp.now();
    db.collection(CHAT_COLLECTION_DIR).document(chatId).update("most_recent_message", messageText);
    db.collection(CHAT_COLLECTION_DIR).document(chatId).update("most_recent_message_time", now);
    // It does not like when I put it into a message object and try to add it
    HashMap<String, Object> newMsg = new HashMap<>();
    newMsg.put("message_text", messageText);
    newMsg.put("sender", senderId);
    newMsg.put("time_sent", now);
    db.collection(CHAT_COLLECTION_DIR).document(chatId).collection(MESSAGE_COLLECTION_DIR).add(newMsg).addOnSuccessListener(new OnSuccessListener() {

        @Override
        public void onSuccess(Object o) {
            Log.d("chat page", "new message has been successfully created in the DB");
            List<Message> listOfMessages = mViewModel.getListOfMessages();
            Message m = new Message(messageText, senderId, now);
            listOfMessages.add(m);
            mViewModel.setListOfMessages(listOfMessages);
            chatRecyclerViewAdapter.notifyDataSetChanged();
        }
    }).addOnFailureListener(e -> Log.d("chat page", "failed to create new message"));
}
Also used : Message(com.example.first_responder_app.messaging.Message) HashMap(java.util.HashMap) Timestamp(com.google.firebase.Timestamp) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener)

Aggregations

Message (com.example.first_responder_app.messaging.Message)4 Timestamp (com.google.firebase.Timestamp)2 ArrayList (java.util.ArrayList)2 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 NavController (androidx.navigation.NavController)1 NavDirections (androidx.navigation.NavDirections)1 NavHostFragment (androidx.navigation.fragment.NavHostFragment)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 FirestoreDatabase (com.example.first_responder_app.FirestoreDatabase)1 ChatGroupFragmentBinding (com.example.first_responder_app.databinding.ChatGroupFragmentBinding)1 ChatGroupRecyclerViewAdapter (com.example.first_responder_app.recyclerViews.ChatGroupRecyclerViewAdapter)1 ChatViewModel (com.example.first_responder_app.viewModels.ChatViewModel)1 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)1 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)1 HashMap (java.util.HashMap)1