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();
}
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());
}
}
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());
}
});
}
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"));
}
Aggregations