Search in sources :

Example 1 with AdminEditGroupFragmentBinding

use of com.example.first_responder_app.databinding.AdminEditGroupFragmentBinding 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();
}
Also used : AdminEditGroupFragmentBinding(com.example.first_responder_app.databinding.AdminEditGroupFragmentBinding) Bundle(android.os.Bundle) AlertDialog(androidx.appcompat.app.AlertDialog) NonNull(androidx.annotation.NonNull) Query(com.google.firebase.firestore.Query) ArrayList(java.util.ArrayList) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) Toast(android.widget.Toast) Fragment(androidx.fragment.app.Fragment) AdminEditGroupViewModel(com.example.first_responder_app.viewModels.AdminEditGroupViewModel) View(android.view.View) ListenerRegistration(com.google.firebase.firestore.ListenerRegistration) RecyclerView(androidx.recyclerview.widget.RecyclerView) TAG(android.content.ContentValues.TAG) Log(android.util.Log) DataBindingUtil(androidx.databinding.DataBindingUtil) UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider) LayoutInflater(android.view.LayoutInflater) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) AdminGroupsRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.AdminGroupsRecyclerViewAdapter) AppUtil(com.example.first_responder_app.AppUtil) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) ViewGroup(android.view.ViewGroup) R(com.example.first_responder_app.R) FirebaseFirestore(com.google.firebase.firestore.FirebaseFirestore) List(java.util.List) Nullable(androidx.annotation.Nullable) GroupDataModel(com.example.first_responder_app.dataModels.GroupDataModel) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) Comparator(java.util.Comparator) EditText(android.widget.EditText) AlertDialog(androidx.appcompat.app.AlertDialog) EditText(android.widget.EditText) AdminGroupsRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.AdminGroupsRecyclerViewAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) GroupDataModel(com.example.first_responder_app.dataModels.GroupDataModel) RecyclerView(androidx.recyclerview.widget.RecyclerView) AdminEditGroupFragmentBinding(com.example.first_responder_app.databinding.AdminEditGroupFragmentBinding)

Aggregations

TAG (android.content.ContentValues.TAG)1 Bundle (android.os.Bundle)1 Log (android.util.Log)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 EditText (android.widget.EditText)1 Toast (android.widget.Toast)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 AlertDialog (androidx.appcompat.app.AlertDialog)1 DataBindingUtil (androidx.databinding.DataBindingUtil)1 Fragment (androidx.fragment.app.Fragment)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 SwipeRefreshLayout (androidx.swiperefreshlayout.widget.SwipeRefreshLayout)1 AppUtil (com.example.first_responder_app.AppUtil)1 FirestoreDatabase (com.example.first_responder_app.FirestoreDatabase)1 R (com.example.first_responder_app.R)1