Search in sources :

Example 51 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot 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 52 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project FirstResponse by mattpost1700.

the class ChatFragment method populateMessageList.

private void populateMessageList() {
    String chatId = c.getId();
    db.collection("chat").document(chatId).collection("messages").get().addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            ArrayList<Message> temp = new ArrayList<>();
            for (QueryDocumentSnapshot doc : task.getResult()) {
                Timestamp t = (Timestamp) doc.get("time_sent");
                ;
                Message m = new Message(doc.getId(), (String) doc.get("message_text"), (String) doc.get("sender"), t);
                temp.add(m);
            }
            listOfMessages.clear();
            listOfMessages.addAll(temp);
            Collections.sort(listOfMessages);
            mViewModel.setListOfMessages(listOfMessages);
            chatRecyclerViewAdapter.notifyDataSetChanged();
        } else {
            Log.d(TAG, "db get failed in chat page " + task.getException());
        }
    });
}
Also used : Message(com.example.first_responder_app.messaging.Message) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList) Timestamp(com.google.firebase.Timestamp)

Example 53 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot 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)

Example 54 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project FirstResponse by mattpost1700.

the class NewChatFragment method populateUserList.

private void populateUserList() {
    db.collection("users").get().addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot doc : task.getResult()) {
                UsersDataModel u = new UsersDataModel(doc.getId(), (String) doc.get("first_name"), (String) doc.get("last_name"));
                listOfUsers.add(u);
            }
            UsersDataModel currentU = new UsersDataModel(user.getDocumentId(), user.getFirst_name(), user.getLast_name());
            listOfUsers.add(currentU);
        } else {
            Log.d(TAG, "db get failed in new chat page " + task.getException());
        }
    });
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot)

Example 55 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project FirstResponse by mattpost1700.

the class RespondingFragment method refreshData.

private void refreshData() {
    db.collection("users").whereEqualTo(FirestoreDatabase.FIELD_FIRE_DEPARTMENT_ID, activeUser.getFire_department_id()).whereGreaterThanOrEqualTo("responding_time", AppUtil.earliestTime(requireContext())).get().addOnCompleteListener(userTask -> {
        Log.d(TAG, "READ DATABASE - RESPONDING FRAGMENT");
        if (userTask.isSuccessful()) {
            ArrayList<UsersDataModel> temp = new ArrayList<>();
            for (QueryDocumentSnapshot userDoc : userTask.getResult()) {
                UsersDataModel user = userDoc.toObject(UsersDataModel.class);
                List<String> responses = user.getResponses();
                Log.d(TAG, "refreshData: " + isActive(responses.get(responses.size() - 1)));
                if (responses != null && responses.size() > 0 && isActive(responses.get(responses.size() - 1)))
                    temp.add(user);
            }
            Log.d(TAG, "refreshData: " + AppUtil.earliestTime(requireContext()).toDate());
            listOfRespondingDataModel.clear();
            listOfRespondingDataModel.addAll(temp);
            checkRespondersEmpty();
            respondingRecyclerViewAdapter.notifyDataSetChanged();
            Log.d("TAG", "populateResponders: ");
        } else {
            Log.w(TAG, "refreshData: Could not refresh", userTask.getException());
        }
    });
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList)

Aggregations

QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)73 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)33 ArrayList (java.util.ArrayList)30 View (android.view.View)22 NonNull (androidx.annotation.NonNull)19 Task (com.google.android.gms.tasks.Task)18 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)17 Intent (android.content.Intent)16 CollectionReference (com.google.firebase.firestore.CollectionReference)13 AdapterView (android.widget.AdapterView)12 FirebaseFirestore (com.google.firebase.firestore.FirebaseFirestore)12 TextView (android.widget.TextView)10 Bundle (android.os.Bundle)9 ListView (android.widget.ListView)9 DocumentSnapshot (com.google.firebase.firestore.DocumentSnapshot)9 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)8 UsersDataModel (com.example.first_responder_app.dataModels.UsersDataModel)7 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)6 FirebaseUser (com.google.firebase.auth.FirebaseUser)6 DocumentReference (com.google.firebase.firestore.DocumentReference)6