use of com.example.first_responder_app.recyclerViews.AdminGroupsRecyclerViewAdapter in project FirstResponse by mattpost1700.
the class AdminEditGroupFragment method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
AdminEditGroupFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.admin_edit_group_fragment, container, false);
listOfGroups = new ArrayList<>();
activeUser = AppUtil.getActiveUser(getActivity());
db = FirestoreDatabase.getInstance().getDb();
if (activeUser == null) {
getActivity().getFragmentManager().popBackStack();
Toast.makeText(getContext(), "User is not logged in!", Toast.LENGTH_SHORT).show();
}
binding.adminEditGroupCreateNewButton.setOnClickListener(v -> {
String userInput = binding.adminEditGroupEditNameEditText.getText().toString();
GroupDataModel newGroup = new GroupDataModel(userInput, activeUser.getFire_department_id());
db.collection(FirestoreDatabase.GROUPS_COLLECTION_DIR).add(newGroup).addOnSuccessListener(documentReference -> {
listOfGroups.add(newGroup);
listOfGroups.sort(Comparator.comparing(GroupDataModel::getName));
adapter.notifyDataSetChanged();
binding.adminEditGroupEditNameEditText.setText("");
});
});
AdminGroupsRecyclerViewAdapter.GroupClickListener groupClickListener = (view, position) -> {
GroupDataModel clickedGroup = adapter.getItem(position);
String origName = clickedGroup.getName();
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Edit group");
final EditText input = new EditText(getContext());
input.setText(origName);
builder.setView(input);
builder.setPositiveButton("Save", (dialog, which) -> {
String userInput = input.getText().toString();
if (origName != null && !origName.equals(userInput)) {
// User input
db.collection(FirestoreDatabase.GROUPS_COLLECTION_DIR).document(clickedGroup.getDocumentId()).update(FirestoreDatabase.FIELD_GROUP_NAME, userInput).addOnSuccessListener(unused -> {
listOfGroups.get(position).setName(userInput);
adapter.notifyItemChanged(position);
}).addOnFailureListener(e -> Toast.makeText(getContext(), "Failed to update group", Toast.LENGTH_SHORT).show());
}
});
builder.setNeutralButton("Cancel", (dialog, which) -> dialog.cancel());
builder.setNegativeButton("Delete", (dialog, which) -> {
new AlertDialog.Builder(getContext()).setTitle("Delete").setMessage("Are you sure you want to delete this group?").setPositiveButton("yes", (dialog1, which1) -> {
// TODO: Remove group ids from users
db.collection(FirestoreDatabase.GROUPS_COLLECTION_DIR).document(clickedGroup.getDocumentId()).delete().addOnSuccessListener(unused -> {
listOfGroups.remove(position);
adapter.notifyItemRemoved(position);
});
}).setNegativeButton(android.R.string.cancel, null).show();
});
builder.show();
};
RecyclerView groupRecyclersView = binding.adminEditGroupGroupRecyclerView;
groupRecyclersView.setLayoutManager(new LinearLayoutManager(getContext()));
adapter = new AdminGroupsRecyclerViewAdapter(getContext(), listOfGroups);
adapter.setGroupClickListener(groupClickListener);
groupRecyclersView.setAdapter(adapter);
final SwipeRefreshLayout pullToRefresh = binding.adminEditGroupSwipeToRefresh;
pullToRefresh.setOnRefreshListener(() -> {
refreshGroups();
pullToRefresh.setRefreshing(false);
});
refreshGroups();
return binding.getRoot();
}
Aggregations