Search in sources :

Example 46 with QueryDocumentSnapshot

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

the class HomeFragment method populateResponders.

public void populateResponders() {
    db.collection("users").whereEqualTo(FirestoreDatabase.FIELD_FIRE_DEPARTMENT_ID, activeUser.getFire_department_id()).whereGreaterThanOrEqualTo(FirestoreDatabase.FIELD_RESPONDING_TIME, AppUtil.earliestTime(requireContext())).get().addOnCompleteListener(userTask -> {
        Log.d(TAG, "READ DATABASE - HOME FRAGMENT (populateResponders)");
        if (userTask.isSuccessful()) {
            ArrayList<UsersDataModel> temp = new ArrayList<>();
            for (QueryDocumentSnapshot userDoc : userTask.getResult()) {
                UsersDataModel user = userDoc.toObject(UsersDataModel.class);
                List<String> responses = user.getResponses();
                if (responses != null && responses.size() > 0 && isActive(responses.get(responses.size() - 1)))
                    temp.add(user);
            }
            respondersList.clear();
            respondersList.addAll(temp);
            respondersRecyclerViewAdapter.notifyDataSetChanged();
            // TODO: Should refresh
            Log.d("TAG", "populateResponders: ");
        }
    });
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList)

Example 47 with QueryDocumentSnapshot

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

the class IncidentFragment method populateResponders.

public void populateResponders(int startIdx, int endIdx) {
    FirestoreDatabase.getInstance().getDb().collection("users").whereIn(FieldPath.documentId(), incidentDataModel.getResponding().subList(startIdx, endIdx)).get().addOnCompleteListener(responderTask -> {
        Log.d(TAG, "READ DATABASE - EVENT FRAGMENT");
        if (responderTask.isSuccessful()) {
            List<UsersDataModel> tempList = new ArrayList<>();
            for (QueryDocumentSnapshot userDoc : responderTask.getResult()) {
                UsersDataModel userData = userDoc.toObject(UsersDataModel.class);
                tempList.add(userData);
            }
            responderList.addAll(tempList);
            responderArr = new CharSequence[responderList.size()];
            for (int i = 0; i < responderList.size(); i++) {
                responderArr[i] = responderList.get(i).getFull_name();
            }
        } else {
            Log.w(TAG, "populateResponderList: Responder data failed to query", responderTask.getException());
        }
    });
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint)

Example 48 with QueryDocumentSnapshot

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

the class IncidentGroupFragment method refreshData.

private void refreshData() {
    db.collection("incident").whereArrayContains(FirestoreDatabase.FIELD_FIRE_DEPARTMENTS, activeUser.getFire_department_id()).get().addOnCompleteListener(incidentTask -> {
        Log.d(TAG, "READ DATABASE - INCIDENT GROUP FRAGMENT");
        if (incidentTask.isSuccessful()) {
            ArrayList<IncidentDataModel> temp = new ArrayList<>();
            for (QueryDocumentSnapshot incidentDoc : incidentTask.getResult()) {
                IncidentDataModel incidentDataModel = incidentDoc.toObject(IncidentDataModel.class);
                temp.add(incidentDataModel);
            }
            listOfIncidentDataModel.clear();
            listOfIncidentDataModel.addAll(temp);
            checkIncidentsEmpty();
            incidentGroupRecyclerViewAdapter.notifyDataSetChanged();
        } else {
            Log.w(TAG, "onCreateView: get failed in HomeFragment with", incidentTask.getException());
        }
    });
}
Also used : IncidentDataModel(com.example.first_responder_app.dataModels.IncidentDataModel) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList)

Example 49 with QueryDocumentSnapshot

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

the class LoginFragment method populateUserList.

private void populateUserList(String usernameInput) {
    db.collection("users").whereEqualTo("username", usernameInput).get().addOnCompleteListener(usersTask -> {
        Log.d(TAG, "READ DATABASE - LOGIN FRAGMENT");
        if (usersTask.isSuccessful()) {
            for (QueryDocumentSnapshot userDoc : usersTask.getResult()) {
                user = userDoc.toObject(UsersDataModel.class);
                checkExist = true;
            }
        } else {
            Log.d(TAG, "db get failed in Login page " + usersTask.getException());
            checkExist = false;
        }
    });
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot)

Example 50 with QueryDocumentSnapshot

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

the class LoginFragment method onStart.

@Override
public void onStart() {
    super.onStart();
    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
    String usernameQuickLogin = sharedPref.getString("savedUsername", null);
    String passwordQuickLogin = sharedPref.getString("savedPassword", null);
    Log.d("testing", "usernameQuick: " + usernameQuickLogin);
    Log.d("testing", "pwQuick: " + passwordQuickLogin);
    if (usernameQuickLogin != null && passwordQuickLogin != null) {
        db.collection("users").whereEqualTo("username", usernameQuickLogin).whereEqualTo("password", passwordQuickLogin).get().addOnCompleteListener(usersTask -> {
            Log.d(TAG, "READ DATABASE - LOGIN FRAGMENT");
            if (usersTask.isSuccessful()) {
                for (QueryDocumentSnapshot userDoc : usersTask.getResult()) {
                    user = userDoc.toObject(UsersDataModel.class);
                    checkExist = true;
                    if (user.getPassword().equals(passwordQuickLogin)) {
                        Log.d(TAG, "onStart: LOGIN");
                        ActiveUser activeUser = ((ActiveUser) getActivity());
                        activeUser.setActive(user);
                        if (FirestoreDatabase.getInstance().setActiveUser(user)) {
                            successfullyHidAdminOptions();
                            NavDirections action = LoginFragmentDirections.actionLoginFragmentToHomeFragment();
                            Navigation.findNavController(binding.getRoot()).navigate(action);
                        } else {
                            // User cannot make good queries
                            Toast.makeText(getContext(), "User does not have a department", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            } else {
                Log.d(TAG, "db get failed in Login page " + usersTask.getException());
                checkExist = false;
            }
        });
    }
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) EncryptedSharedPreferences(androidx.security.crypto.EncryptedSharedPreferences) SharedPreferences(android.content.SharedPreferences) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ActiveUser(com.example.first_responder_app.interfaces.ActiveUser) NavDirections(androidx.navigation.NavDirections)

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