Search in sources :

Example 1 with ChatFragmentBinding

use of com.example.first_responder_app.databinding.ChatFragmentBinding 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();
}
Also used : NavController(androidx.navigation.NavController) ChatRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.ChatRecyclerViewAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) NavDirections(androidx.navigation.NavDirections) ChatViewModel(com.example.first_responder_app.viewModels.ChatViewModel) RecyclerView(androidx.recyclerview.widget.RecyclerView) ChatFragmentBinding(com.example.first_responder_app.databinding.ChatFragmentBinding) NavHostFragment(androidx.navigation.fragment.NavHostFragment) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

View (android.view.View)1 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 ChatFragmentBinding (com.example.first_responder_app.databinding.ChatFragmentBinding)1 ChatRecyclerViewAdapter (com.example.first_responder_app.recyclerViews.ChatRecyclerViewAdapter)1 ChatViewModel (com.example.first_responder_app.viewModels.ChatViewModel)1