Search in sources :

Example 1 with NewChatRecyclerViewAdapter

use of com.example.first_responder_app.recyclerViews.NewChatRecyclerViewAdapter in project FirstResponse by mattpost1700.

the class NewChatFragment 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
    ChatNewFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.chat_new_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();
    }
    listOfUsers = new ArrayList<>();
    populateUserList();
    listOfNewUsers = new ArrayList<>();
    NewChatRecyclerViewAdapter.ItemClickListener userClickListener = ((view, position, data) -> {
        // remove user
        listOfNewUsers.remove(data);
        newChatRecyclerViewAdapter.notifyDataSetChanged();
    });
    RecyclerView newChatRecyclerView = binding.newChatRecyclerView;
    newChatRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    newChatRecyclerViewAdapter = new NewChatRecyclerViewAdapter(getContext(), listOfNewUsers);
    newChatRecyclerViewAdapter.setClickListener(userClickListener);
    newChatRecyclerView.setAdapter(newChatRecyclerViewAdapter);
    newChatRecyclerView.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) {
            newChatRecyclerView.removeOnLayoutChangeListener(this);
        // newChatRecyclerView.scrollToPosition(newChatRecyclerView.getAdapter().getItemCount() - 1);
        }
    });
    binding.newChatAddUserFab.setOnClickListener(v -> {
        String newUserName = binding.newChatUsersEditText.getText().toString();
        UsersDataModel result = listOfUsers.stream().filter(u -> newUserName.equals(u.getFull_name())).findAny().orElse(null);
        // If user exists, is not the current user, and has not already been added
        if (result != null && !result.getDocumentId().equals(user.getDocumentId()) && !listOfNewUsers.contains(result)) {
            binding.newChatErrorMsg.setText("");
            listOfNewUsers.add(result);
            newChatRecyclerViewAdapter.notifyDataSetChanged();
        } else {
            binding.newChatErrorMsg.setText("User not found");
        }
    });
    binding.newChatCreateConfirm.setOnClickListener(v -> {
        String chatName = binding.newChatChatNameEditText.getText().toString();
        if (listOfNewUsers.size() == 0) {
            binding.newChatErrorMsg.setText("Must add users to the chat");
        } else if (chatName.length() == 0) {
            binding.newChatErrorMsg.setText("Chat name must not be blank");
        } else {
            listOfNewUsers.add(user);
            firestoreDatabase.addChat(chatName, listOfNewUsers);
        }
        NavDirections action = NewChatFragmentDirections.actionNewChatFragmentToChatGroupFragment();
        Navigation.findNavController(binding.getRoot()).navigate(action);
    });
    return binding.getRoot();
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) NavController(androidx.navigation.NavController) NewChatRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.NewChatRecyclerViewAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) ChatNewFragmentBinding(com.example.first_responder_app.databinding.ChatNewFragmentBinding) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) NavDirections(androidx.navigation.NavDirections) RecyclerView(androidx.recyclerview.widget.RecyclerView) NavHostFragment(androidx.navigation.fragment.NavHostFragment) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase)

Aggregations

View (android.view.View)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 UsersDataModel (com.example.first_responder_app.dataModels.UsersDataModel)1 ChatNewFragmentBinding (com.example.first_responder_app.databinding.ChatNewFragmentBinding)1 NewChatRecyclerViewAdapter (com.example.first_responder_app.recyclerViews.NewChatRecyclerViewAdapter)1