use of com.example.first_responder_app.recyclerViews.ChatRecyclerViewAdapter in project FirstResponse by mattpost1700.
the class ChatFragment 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
ChatFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.chat_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 = FirebaseFirestore.getInstance();
activeUser = (ActiveUser) getActivity();
if (activeUser != null) {
user = activeUser.getActive();
}
mViewModel = new ViewModelProvider(requireActivity()).get(ChatViewModel.class);
c = mViewModel.getChatDetail();
listOfMembers = c.getMembers();
listOfMessages = mViewModel.getListOfMessages();
populateMessageList();
// getting data from chat
mViewModel = new ViewModelProvider(requireActivity()).get(ChatViewModel.class);
messageInfo = mViewModel.getMessageDetail();
RecyclerView chatRecyclerView = binding.chatRecyclerView;
chatRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
chatRecyclerViewAdapter = new ChatRecyclerViewAdapter(getContext(), listOfMessages, listOfMembers);
// chatRecyclerViewAdapter.setClickListener(chatClickListener);
chatRecyclerView.setAdapter(chatRecyclerViewAdapter);
chatRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
chatRecyclerView.removeOnLayoutChangeListener(this);
chatRecyclerView.scrollToPosition(chatRecyclerView.getAdapter().getItemCount() - 1);
}
});
binding.sendButton.setOnClickListener(v -> {
String userName = "";
if (user != null) {
userName = user.getFirst_name() + " " + user.getLast_name();
}
String msg = binding.editText.getText().toString();
if (!msg.equals("") && user != null) {
firestoreDatabase.addMessage(c.getId(), msg, user.getDocumentId(), chatRecyclerViewAdapter, mViewModel);
}
});
binding.leaveChat.setOnClickListener(v -> {
firestoreDatabase.removeUserFromChat(user.getDocumentId(), c.getId(), listOfMembers);
NavDirections action = ChatFragmentDirections.actionChatFragmentToChatGroupFragment();
Navigation.findNavController(binding.getRoot()).navigate(action);
});
return binding.getRoot();
}
Aggregations