Search in sources :

Example 1 with EditRankRecyclerViewAdapter

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

the class EditRankFragment 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
    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_edit_rank, container, false);
    NavHostFragment navHostFragment = (NavHostFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
    NavController navController = navHostFragment.getNavController();
    activeUser = AppUtil.getActiveUser(getActivity());
    ranksList = new ArrayList<>();
    populateRanks();
    // handles edit rank by onClick
    EditRankRecyclerViewAdapter.ItemClickListener editRankClickListener = ((view, position, data) -> {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Edit Rank").setMessage("Rank Title");
        final EditText input = new EditText(getContext());
        ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        input.setLayoutParams(lp);
        builder.setView(input);
        builder.setPositiveButton("Confirm", (dialogInterface, i) -> {
            RanksDataModel rank = ranksList.get(position);
            rank.setRank_name(input.getText().toString());
            db.collection("ranks").document(rank.getDocumentId()).set(rank);
            editRankRecyclerViewAdapter.notifyDataSetChanged();
        });
        builder.setNegativeButton("Cancel", (dialogInterface, i) -> {
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    });
    // handles hold to delete
    EditRankRecyclerViewAdapter.rankLongClickListener rankLongClickListener = (view, position) -> {
        @SuppressLint("NotifyDataSetChanged") AlertDialog.Builder dialog = new AlertDialog.Builder(getContext()).setTitle("Delete rank").setMessage("Are you sure you want to delete this rank?").setPositiveButton("Yes", (dialogInterface, i) -> {
            RanksDataModel rank = ranksList.get(position);
            db.collection("ranks").document(rank.getDocumentId()).delete();
            ranksList.remove(position);
            checkRanksListEmpty();
            editRankRecyclerViewAdapter.notifyDataSetChanged();
        }).setNegativeButton("No", (dialogInterface, i) -> {
        });
        dialog.show();
    };
    binding.addRankBtn.setOnClickListener(v -> {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Create Rank").setMessage("Rank Title");
        final EditText input = new EditText(getContext());
        ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        input.setLayoutParams(lp);
        builder.setView(input);
        builder.setPositiveButton("Confirm", (dialogInterface, i) -> {
            firestoreDatabase.addRank(input.getText().toString());
            populateRanks();
        });
        builder.setNegativeButton("Cancel", (dialogInterface, i) -> {
        });
        AlertDialog dialog = builder.create();
        dialog.show();
    });
    RecyclerView editRankRecyclerView = binding.editRankRecycler;
    editRankRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    editRankRecyclerViewAdapter = new EditRankRecyclerViewAdapter(getContext(), ranksList);
    editRankRecyclerViewAdapter.setClickListener(editRankClickListener);
    editRankRecyclerViewAdapter.setLongClickListener(rankLongClickListener);
    editRankRecyclerView.setAdapter(editRankRecyclerViewAdapter);
    /*  swipe to delete (now using hold to delete)
        new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                RanksDataModel deletedRank = ranksList.get(viewHolder.getAdapterPosition());
                int pos = viewHolder.getAdapterPosition();
                ranksList.remove(pos);
                editRankRecyclerViewAdapter.notifyItemRemoved(pos);

            }
        }).attachToRecyclerView(editRankRecyclerView);
         */
    final SwipeRefreshLayout pullToRefresh = binding.rankSwipeRefreshLayout;
    pullToRefresh.setOnRefreshListener(() -> {
        populateRanks();
        pullToRefresh.setRefreshing(false);
    });
    return binding.getRoot();
}
Also used : AlertDialog(android.app.AlertDialog) EditText(android.widget.EditText) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) EventViewModel(com.example.first_responder_app.viewModels.EventViewModel) EditRankRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.EditRankRecyclerViewAdapter) Query(com.google.firebase.firestore.Query) FragmentEditRankBinding(com.example.first_responder_app.databinding.FragmentEditRankBinding) NavController(androidx.navigation.NavController) ArrayList(java.util.ArrayList) EventsDataModel(com.example.first_responder_app.dataModels.EventsDataModel) SuppressLint(android.annotation.SuppressLint) ItemTouchHelper(androidx.recyclerview.widget.ItemTouchHelper) EventGroupRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.EventGroupRecyclerViewAdapter) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) Fragment(androidx.fragment.app.Fragment) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TAG(android.content.ContentValues.TAG) NavHostFragment(androidx.navigation.fragment.NavHostFragment) Log(android.util.Log) DataBindingUtil(androidx.databinding.DataBindingUtil) GridLayout(android.widget.GridLayout) ReportDataModel(com.example.first_responder_app.dataModels.ReportDataModel) UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) ViewModelProvider(androidx.lifecycle.ViewModelProvider) NavDirections(androidx.navigation.NavDirections) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) LayoutInflater(android.view.LayoutInflater) AppUtil(com.example.first_responder_app.AppUtil) ReportGroupRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.ReportGroupRecyclerViewAdapter) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) ViewGroup(android.view.ViewGroup) EditRankViewModel(com.example.first_responder_app.viewModels.EditRankViewModel) UserViewModel(com.example.first_responder_app.viewModels.UserViewModel) AlertDialog(android.app.AlertDialog) R(com.example.first_responder_app.R) FirebaseFirestore(com.google.firebase.firestore.FirebaseFirestore) List(java.util.List) Nullable(androidx.annotation.Nullable) ConstraintLayout(androidx.constraintlayout.widget.ConstraintLayout) Navigation(androidx.navigation.Navigation) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) RanksDataModel(com.example.first_responder_app.dataModels.RanksDataModel) EditText(android.widget.EditText) EditRankRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.EditRankRecyclerViewAdapter) NavController(androidx.navigation.NavController) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) ConstraintLayout(androidx.constraintlayout.widget.ConstraintLayout) RecyclerView(androidx.recyclerview.widget.RecyclerView) RanksDataModel(com.example.first_responder_app.dataModels.RanksDataModel) NavHostFragment(androidx.navigation.fragment.NavHostFragment)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 AlertDialog (android.app.AlertDialog)1 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 GridLayout (android.widget.GridLayout)1 LinearLayout (android.widget.LinearLayout)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 ConstraintLayout (androidx.constraintlayout.widget.ConstraintLayout)1 DataBindingUtil (androidx.databinding.DataBindingUtil)1 Fragment (androidx.fragment.app.Fragment)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 NavController (androidx.navigation.NavController)1 NavDirections (androidx.navigation.NavDirections)1 Navigation (androidx.navigation.Navigation)1