use of com.example.first_responder_app.databinding.ChatNewFragmentBinding 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();
}
Aggregations