Search in sources :

Example 1 with RanksDataModel

use of com.example.first_responder_app.dataModels.RanksDataModel 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)

Example 2 with RanksDataModel

use of com.example.first_responder_app.dataModels.RanksDataModel in project FirstResponse by mattpost1700.

the class FirestoreDatabase method addRank.

public void addRank(String rankTitle) {
    RanksDataModel newRank = new RanksDataModel(activeUserFireDepartmentId, rankTitle);
    db.collection(RANKS_COLLECTION_DIR).add(newRank).addOnSuccessListener(documentReference -> Log.d("edit rank page", "new rank has been successfully created in the DB")).addOnFailureListener(e -> Log.d("edit rank page", "failed to create new rank"));
}
Also used : Context(android.content.Context) ChatViewModel(com.example.first_responder_app.viewModels.ChatViewModel) NonNull(androidx.annotation.NonNull) Date(java.util.Date) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ChatRecyclerViewAdapter(com.example.first_responder_app.recyclerViews.ChatRecyclerViewAdapter) EventsDataModel(com.example.first_responder_app.dataModels.EventsDataModel) ActiveUser(com.example.first_responder_app.interfaces.ActiveUser) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) IncidentDataModel(com.example.first_responder_app.dataModels.IncidentDataModel) Map(java.util.Map) TAG(android.content.ContentValues.TAG) Log(android.util.Log) Message(com.example.first_responder_app.messaging.Message) FirebaseStorage(com.google.firebase.storage.FirebaseStorage) UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) Timestamp(com.google.firebase.Timestamp) FirebaseFirestore(com.google.firebase.firestore.FirebaseFirestore) List(java.util.List) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) StorageReference(com.google.firebase.storage.StorageReference) AnnouncementsDataModel(com.example.first_responder_app.dataModels.AnnouncementsDataModel) Pattern(java.util.regex.Pattern) RanksDataModel(com.example.first_responder_app.dataModels.RanksDataModel) RanksDataModel(com.example.first_responder_app.dataModels.RanksDataModel)

Example 3 with RanksDataModel

use of com.example.first_responder_app.dataModels.RanksDataModel in project FirstResponse by mattpost1700.

the class EditRankRecyclerViewAdapter method onBindViewHolder.

// binds the data to the TextView in each row
@Override
public void onBindViewHolder(EditRankRecyclerViewAdapter.ViewHolder holder, int position) {
    RanksDataModel rank = mData.get(position);
    holder.title.setText(rank.getRank_name());
    holder.data = rank;
}
Also used : RanksDataModel(com.example.first_responder_app.dataModels.RanksDataModel)

Example 4 with RanksDataModel

use of com.example.first_responder_app.dataModels.RanksDataModel in project FirstResponse by mattpost1700.

the class AdminEditUserFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    AdminEditUserFragmentBinding binding = DataBindingUtil.inflate(inflater, R.layout.admin_edit_user_fragment, container, false);
    SearchUserViewModel searchUserViewModel = new ViewModelProvider(requireActivity()).get(SearchUserViewModel.class);
    userToEdit = searchUserViewModel.getSelectedUser();
    if (userToEdit == null) {
    // leave
    }
    currentlySelectedRankId = userToEdit.getRank_id();
    currentlySelectedGroupIds = userToEdit.getGroup_ids();
    ArrayList<RanksDataModel> listOfRanks = new ArrayList<>();
    ArrayList<String> listOfRankNames = new ArrayList<>();
    userInGroupsNames = new ArrayList<>();
    userNotInGroupsNames = new ArrayList<>();
    addGroupSpinner = binding.addGroupSpinner;
    removeGroupSpinner = binding.removeGroupSpinner;
    Spinner rankSpinner = binding.rankSpinner;
    rankSpinner.setSelection(listOfRankNames.indexOf(currentlySelectedRankId));
    rankSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String selectedString = listOfRankNames.get(position);
            for (RanksDataModel ranksDataModel : listOfRanks) {
                String docId = ranksDataModel.getDocumentId();
                if (docId.equals(selectedString)) {
                    currentlySelectedRankId = docId;
                    break;
                }
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
    binding.adminEditUserUserName.setText(userToEdit.getFull_name());
    ArrayAdapter<String> rankAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, listOfRankNames);
    rankSpinner.setAdapter(rankAdapter);
    FirestoreDatabase.getInstance().getDb().collection(FirestoreDatabase.RANKS_COLLECTION_DIR).whereEqualTo(FirestoreDatabase.FIELD_FIRE_DEPARTMENT_ID, userToEdit.getFire_department_id()).get().addOnSuccessListener(queryDocumentSnapshots -> {
        ArrayList<RanksDataModel> temp = new ArrayList<>();
        ArrayList<String> temp2 = new ArrayList<>();
        for (QueryDocumentSnapshot queryDocumentSnapshot : queryDocumentSnapshots) {
            RanksDataModel ranksDataModel = queryDocumentSnapshot.toObject(RanksDataModel.class);
            temp.add(ranksDataModel);
            temp2.add(ranksDataModel.getRank_name());
        }
        listOfRanks.clear();
        listOfRanks.addAll(temp);
        listOfRankNames.clear();
        listOfRankNames.addAll(temp2);
        // I know this is bad...
        for (RanksDataModel ranksDataModel : listOfRanks) {
            if (ranksDataModel.getDocumentId().equals(userToEdit.getRank_id())) {
                for (String mRankName : listOfRankNames) {
                    if (mRankName.equals(ranksDataModel.getRank_name())) {
                        rankSpinner.setSelection(listOfRankNames.indexOf(mRankName));
                        break;
                    }
                }
            }
        }
        rankAdapter.notifyDataSetChanged();
    });
    FirestoreDatabase.getInstance().getDb().collection(FirestoreDatabase.GROUPS_COLLECTION_DIR).whereEqualTo(FirestoreDatabase.FIELD_FIRE_DEPARTMENT_ID, userToEdit.getFire_department_id()).get().addOnSuccessListener(queryDocumentSnapshots -> {
        try {
            List<String> userGroups = userToEdit.getGroup_ids();
            for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
                GroupDataModel currentGroup = doc.toObject(GroupDataModel.class);
                allGroups.add(currentGroup);
                allGroupsStrings.add(currentGroup.getName());
                if (userGroups.contains(currentGroup.getDocumentId())) {
                    userInGroupsNames.add(currentGroup.getName());
                } else {
                    userNotInGroupsNames.add(currentGroup.getName());
                }
            }
            addGroupAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, userNotInGroupsNames);
            addGroupSpinner.setAdapter(addGroupAdapter);
            removeGroupAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, userInGroupsNames);
            removeGroupSpinner.setAdapter(removeGroupAdapter);
        } catch (Exception e) {
            Log.e(TAG, "onCreateView: ", e);
        }
    });
    binding.adminEditUserUpdateRankButton.setOnClickListener(v -> {
        String selectedRankString = rankSpinner.getSelectedItem().toString();
        for (RanksDataModel ranksDataModel : listOfRanks) {
            if (ranksDataModel.getRank_name().equals(selectedRankString)) {
                FirestoreDatabase.getInstance().getDb().collection(FirestoreDatabase.USERS_COLLECTION_DIR).document(userToEdit.getDocumentId()).update(FirestoreDatabase.FIELD_RANK_ID, ranksDataModel.getDocumentId()).addOnFailureListener(e -> Toast.makeText(getContext(), "onCreateView: Failed to update user", Toast.LENGTH_SHORT).show());
            }
        }
    });
    binding.adminEditUserAddGroupButton.setOnClickListener(v -> {
        String selectedGroupToAdd = addGroupSpinner.getSelectedItem().toString();
        for (GroupDataModel groupDataModel : allGroups) {
            if (groupDataModel.getName().equals(selectedGroupToAdd)) {
                FirestoreDatabase.getInstance().getDb().collection(FirestoreDatabase.USERS_COLLECTION_DIR).document(userToEdit.getDocumentId()).update("group_ids", FieldValue.arrayUnion(groupDataModel.getDocumentId())).addOnFailureListener(e -> Toast.makeText(getContext(), "onCreateView: Failed to update user", Toast.LENGTH_SHORT).show());
            }
        }
        userInGroupsNames.add(selectedGroupToAdd);
        userNotInGroupsNames.remove(selectedGroupToAdd);
        addGroupAdapter.notifyDataSetChanged();
        removeGroupAdapter.notifyDataSetChanged();
    });
    binding.adminEditUserRemoveGroupButton.setOnClickListener(v -> {
        String selectedGroupToRemove = removeGroupSpinner.getSelectedItem().toString();
        for (GroupDataModel groupDataModel : allGroups) {
            if (groupDataModel.getName().equals(selectedGroupToRemove)) {
                FirestoreDatabase.getInstance().getDb().collection(FirestoreDatabase.USERS_COLLECTION_DIR).document(userToEdit.getDocumentId()).update("group_ids", FieldValue.arrayRemove(groupDataModel.getDocumentId())).addOnFailureListener(e -> Toast.makeText(getContext(), "onCreateView: Failed to update user", Toast.LENGTH_SHORT).show());
            }
        }
        userInGroupsNames.remove(selectedGroupToRemove);
        userNotInGroupsNames.add(selectedGroupToRemove);
        addGroupAdapter.notifyDataSetChanged();
        removeGroupAdapter.notifyDataSetChanged();
    });
    return binding.getRoot();
}
Also used : AdminEditUserFragmentBinding(com.example.first_responder_app.databinding.AdminEditUserFragmentBinding) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) Spinner(android.widget.Spinner) ArrayList(java.util.ArrayList) View(android.view.View) AdapterView(android.widget.AdapterView) SearchUserViewModel(com.example.first_responder_app.viewModels.SearchUserViewModel) GroupDataModel(com.example.first_responder_app.dataModels.GroupDataModel) AdapterView(android.widget.AdapterView) RanksDataModel(com.example.first_responder_app.dataModels.RanksDataModel) ArrayAdapter(android.widget.ArrayAdapter) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 5 with RanksDataModel

use of com.example.first_responder_app.dataModels.RanksDataModel in project FirstResponse by mattpost1700.

the class EditRankFragment method populateRanks.

private void populateRanks() {
    db.collection("ranks").whereEqualTo(FirestoreDatabase.FIELD_FIRE_DEPARTMENT_ID, activeUser.getFire_department_id()).get().addOnCompleteListener(ranksTask -> {
        Log.d(TAG, "READ DATABASE - EDIT RANK FRAGMENT");
        if (ranksTask.isSuccessful()) {
            ArrayList<RanksDataModel> temp = new ArrayList<>();
            for (QueryDocumentSnapshot ranksDoc : ranksTask.getResult()) {
                RanksDataModel ranksDataModel = ranksDoc.toObject(RanksDataModel.class);
                temp.add(ranksDataModel);
            }
            Log.d(TAG, "populateRankList: " + temp.size());
            ranksList.clear();
            ranksList.addAll(temp);
            checkRanksListEmpty();
            editRankRecyclerViewAdapter.notifyDataSetChanged();
        } else {
            Log.d(TAG, "db get failed in edit ranks page " + ranksTask.getException());
        }
    });
}
Also used : QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList) RanksDataModel(com.example.first_responder_app.dataModels.RanksDataModel)

Aggregations

RanksDataModel (com.example.first_responder_app.dataModels.RanksDataModel)5 ArrayList (java.util.ArrayList)4 TAG (android.content.ContentValues.TAG)2 Log (android.util.Log)2 View (android.view.View)2 NonNull (androidx.annotation.NonNull)2 ViewModelProvider (androidx.lifecycle.ViewModelProvider)2 EventsDataModel (com.example.first_responder_app.dataModels.EventsDataModel)2 UsersDataModel (com.example.first_responder_app.dataModels.UsersDataModel)2 FirebaseFirestore (com.google.firebase.firestore.FirebaseFirestore)2 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)2 List (java.util.List)2 SuppressLint (android.annotation.SuppressLint)1 AlertDialog (android.app.AlertDialog)1 Context (android.content.Context)1 Bundle (android.os.Bundle)1 LayoutInflater (android.view.LayoutInflater)1 ViewGroup (android.view.ViewGroup)1 AdapterView (android.widget.AdapterView)1 ArrayAdapter (android.widget.ArrayAdapter)1