Search in sources :

Example 41 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project turtleparties by CMPUT301W22T21.

the class PlayerController method getLeaderboardStatsFromFireBase.

public HashMap<String, Integer> getLeaderboardStatsFromFireBase(String find) {
    HashMap<String, Integer> qrscore = new HashMap<>();
    HashMap<String, Integer> score = new HashMap<>();
    HashMap<String, Integer> sum = new HashMap<>();
    db.collection("Users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {

        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (QueryDocumentSnapshot doc : task.getResult()) {
                    String name = doc.getId();
                    db.collection("Users").document(name).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

                        @Override
                        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                            if (task.isSuccessful()) {
                                DocumentSnapshot doc = task.getResult();
                                Integer highestqr = null;
                                Integer qrcount = null;
                                Integer qrsum = null;
                                // Highest sum
                                try {
                                    qrsum = ((Number) doc.getData().get("qrSum")).intValue();
                                    sum.put(name, qrsum);
                                } catch (Exception e) {
                                    sum.put(name, 0);
                                }
                                // Highest score
                                try {
                                    highestqr = ((Number) doc.getData().get("qrHighest")).intValue();
                                    score.put(name, highestqr);
                                } catch (Exception e) {
                                    score.put(name, 0);
                                }
                                // Highest scan amount
                                try {
                                    qrcount = ((Number) doc.getData().get("qrCount")).intValue();
                                    qrscore.put(name, qrcount);
                                } catch (Exception e) {
                                    qrscore.put(name, 0);
                                }
                                Log.d("LEADERBOARD_DEBUG", highestqr + "  " + qrcount + "  " + qrsum);
                            // personAdapter.notifyDataSetChanged();
                            }
                        }
                    });
                }
            }
        }
    });
    if (find == "score") {
        return score;
    } else if (find == "sum") {
        return sum;
    } else if (find == "qr") {
        return qrscore;
    } else {
        return null;
    }
}
Also used : Task(com.google.android.gms.tasks.Task) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) NonNull(androidx.annotation.NonNull)

Example 42 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project turtleparties by CMPUT301W22T21.

the class QRInfo method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_qrinfo);
    view = this.findViewById(android.R.id.content);
    Intent intent = getIntent();
    Bundle qrBundle = intent.getBundleExtra(MainActivity.EXTRA_QR);
    thisQr = (ScoreQrcode) qrBundle.getSerializable("qrcode");
    user = (Player) qrBundle.getSerializable("user");
    showDeleteButton = (boolean) qrBundle.getSerializable("showDeleteButton");
    Double lat = (Double) qrBundle.getSerializable("lat");
    Double lon = (Double) qrBundle.getSerializable("lon");
    String qrname = thisQr.getCode();
    thisQr.generateQRimage();
    db = FirebaseFirestore.getInstance();
    final CollectionReference collectionReference = db.collection("QR codes");
    qrImage = view.findViewById(R.id.qr_imageView);
    scoreView = view.findViewById(R.id.score_info_view);
    locationView = view.findViewById(R.id.location_view);
    deleteButton = view.findViewById(R.id.deleteQrButton);
    if (thisQr.isToShow()) {
        qrImage.setImageBitmap(thisQr.getMyBitmap());
    } else {
        qrImage.setImageResource(R.drawable.ic_baseline_qr_code_24);
    }
    scoreView.setText(String.valueOf(thisQr.getScore()));
    if (lat != 0.0) {
        locationView.setText(String.valueOf(lat + "° N " + lon + "° W"));
    } else {
        locationView.setText("n/a");
    }
    if (!showDeleteButton) {
        deleteButton.setVisibility(view.INVISIBLE);
    }
    commentList = findViewById(R.id.comment_list);
    commentDataList = new ArrayList<>();
    commentAdapter = new QRCommentList(this, commentDataList);
    commentList.setAdapter(commentAdapter);
    collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {

        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException error) {
            db.collection("QR codes").document(qrname).collection("comments").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {

                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot doc : task.getResult()) {
                            String username = doc.getId();
                            Log.d(TAG, username);
                            db.collection("QR codes").document(qrname).collection("comments").document(username).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

                                @Override
                                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                    if (task.isSuccessful()) {
                                        DocumentSnapshot doc = task.getResult();
                                        String userComment = (String) doc.getData().get("comment");
                                        commentDataList.add((new Comment(userComment, username)));
                                        Log.d(TAG, userComment + "  " + username);
                                        commentAdapter.notifyDataSetChanged();
                                    }
                                }
                            });
                        }
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });
            commentAdapter.notifyDataSetChanged();
        }
    });
}
Also used : Task(com.google.android.gms.tasks.Task) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) Bundle(android.os.Bundle) Intent(android.content.Intent) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) CollectionReference(com.google.firebase.firestore.CollectionReference) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) NonNull(androidx.annotation.NonNull)

Example 43 with QueryDocumentSnapshot

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

the class EventFragment method populateParticipantList.

private void populateParticipantList(int startIdx, int endIdx) {
    Log.d(TAG, "populateParticipantList: ");
    participants.clear();
    db.collection("users").whereIn(FieldPath.documentId(), eventInfo.getParticipants().subList(startIdx, endIdx)).get().addOnCompleteListener(participantTask -> {
        Log.d(TAG, "READ DATABASE - EVENT FRAGMENT");
        if (participantTask.isSuccessful()) {
            List<UsersDataModel> tempList = new ArrayList<>();
            for (QueryDocumentSnapshot userDoc : participantTask.getResult()) {
                UsersDataModel userData = userDoc.toObject(UsersDataModel.class);
                tempList.add(userData);
            }
            participants.addAll(tempList);
            checkParticipantsEmpty();
            Log.d(TAG, "populateParticipantList: " + participants.size());
            eventRecyclerViewAdapter.notifyDataSetChanged();
        } else {
            Log.w(TAG, "populateParticipantList: Participant data failed to query", participantTask.getException());
        }
    });
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList)

Example 44 with QueryDocumentSnapshot

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

the class EventGroupFragment method populateEventList.

private void populateEventList() {
    db.collection("events").whereEqualTo(FirestoreDatabase.FIELD_FIRE_DEPARTMENT_ID, activeUser.getFire_department_id()).orderBy("event_time", Query.Direction.ASCENDING).whereGreaterThanOrEqualTo("event_time", Timestamp.now()).get().addOnCompleteListener(eventTask -> {
        Log.d(TAG, "READ DATABASE - EVENT GROUP FRAGMENT");
        if (eventTask.isSuccessful()) {
            ArrayList<EventsDataModel> temp = new ArrayList<>();
            for (QueryDocumentSnapshot eventDoc : eventTask.getResult()) {
                EventsDataModel eventDataModel = eventDoc.toObject(EventsDataModel.class);
                temp.add(eventDataModel);
            }
            Log.d(TAG, "populateEventList: " + temp.size());
            listOfEvents.clear();
            listOfEvents.addAll(temp);
            checkEventsEmpty();
            eventGroupRecyclerViewAdapter.notifyDataSetChanged();
        } else {
            Log.d(TAG, "db get failed in event page " + eventTask.getException());
        }
    });
}
Also used : QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) ArrayList(java.util.ArrayList) EventsDataModel(com.example.first_responder_app.dataModels.EventsDataModel)

Example 45 with QueryDocumentSnapshot

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

the class HomeFragment method populateIncidents.

/**
 * Displays the active incidents
 */
private void populateIncidents() {
    db.collection("incident").whereArrayContains(FirestoreDatabase.FIELD_FIRE_DEPARTMENTS, activeUser.getFire_department_id()).whereEqualTo("incident_complete", false).get().addOnCompleteListener(incidentTask -> {
        Log.d(TAG, "READ DATABASE - HOME FRAGMENT (populateIncidents)");
        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);
            incidentRecyclerViewAdapter.notifyDataSetChanged();
        } else {
            Log.d(TAG, "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)

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