Search in sources :

Example 1 with GroupDataModel

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

Example 2 with GroupDataModel

use of com.example.first_responder_app.dataModels.GroupDataModel 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 3 with GroupDataModel

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

the class AdminGroupsRecyclerViewAdapter method onBindViewHolder.

@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    GroupDataModel groupDataModel = groupDataModelList.get(position);
    holder.groupNameTextView.setText(groupDataModel.getName());
}
Also used : GroupDataModel(com.example.first_responder_app.dataModels.GroupDataModel) SuppressLint(android.annotation.SuppressLint)

Aggregations

GroupDataModel (com.example.first_responder_app.dataModels.GroupDataModel)3 View (android.view.View)2 ViewModelProvider (androidx.lifecycle.ViewModelProvider)2 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 TAG (android.content.ContentValues.TAG)1 Bundle (android.os.Bundle)1 Log (android.util.Log)1 LayoutInflater (android.view.LayoutInflater)1 ViewGroup (android.view.ViewGroup)1 AdapterView (android.widget.AdapterView)1 ArrayAdapter (android.widget.ArrayAdapter)1 EditText (android.widget.EditText)1 Spinner (android.widget.Spinner)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