Search in sources :

Example 26 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project AlphaTour by Frank99DG.

the class FileReceiverActivity method saveObjects.

private void saveObjects(List<Element> elmlist) {
    db.collection("Zones").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {

        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (int i = 0; i < elmlist.size(); i++) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        Element newElement = elmlist.get(i);
                        Zone zon = document.toObject(Zone.class);
                        if (zon.getName().matches(newElement.getZoneRif())) {
                            newElement.setIdZone(document.getId());
                            id = generateidPhotoAndQrCode();
                            elm.put("idZone", newElement.getIdZone());
                            elm.put("title", newElement.getTitle());
                            elm.put("description", newElement.getDescription());
                            elm.put("photo", newElement.getPhoto().toString());
                            elm.put("qrCode", null);
                            elm.put("idPhotoAndQrCode", Long.parseLong(newElement.getIdPhotoAndQrCodeString()));
                            elm.put("idUser", user.getUid());
                            elm.put("qrData", newElement.getQrData());
                            db.collection("Elements").add(elm).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {

                                @Override
                                public void onSuccess(DocumentReference documentReference) {
                                    success = true;
                                // String id=documentReference.getId();
                                }
                            });
                            saveQrCode(newElement.getQrData(), newElement, i, id);
                        }
                    }
                }
            }
        }
    }).addOnFailureListener(new OnFailureListener() {

        @Override
        public void onFailure(@NonNull Exception e) {
            success = false;
            Toast.makeText(FileReceiverActivity.this, "Non รจ stato possibile salvare le zone e gli oggetti creati!!!", Toast.LENGTH_LONG).show();
        }
    });
}
Also used : OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) UploadTask(com.google.firebase.storage.UploadTask) Task(com.google.android.gms.tasks.Task) StorageTask(com.google.firebase.storage.StorageTask) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) Zone(com.example.alphatour.oggetti.Zone) NonNull(androidx.annotation.NonNull) Element(com.example.alphatour.oggetti.Element) Constraint(com.example.alphatour.oggetti.Constraint) DocumentReference(com.google.firebase.firestore.DocumentReference) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 27 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project BookingApp by mikeApostolidis.

the class GetAllCustomers method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    db = FirebaseFirestore.getInstance();
    View view = inflater.inflate(R.layout.fragment_get_all_customers, container, false);
    spinner = view.findViewById(R.id.spinner);
    // , R.layout.support_simple_spinner_dropdown_item);
    adapter = ArrayAdapter.createFromResource(getContext(), R.array.customer_querries_array, R.layout.support_simple_spinner_dropdown_item);
    adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    listOfCustomers = view.findViewById(R.id.customerQuery);
    runQueryBtn = view.findViewById(R.id.runCustomerQueries);
    returnBtn = view.findViewById(R.id.returnButton);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            temp = position + 1;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
    runQueryBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            switch(temp) {
                // case 1-3 show all
                case 1:
                    db.collection("Customer").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {

                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            String result = "";
                            if (task.isSuccessful() && !task.getResult().isEmpty()) {
                                Toast.makeText(getActivity(), "Query successful", Toast.LENGTH_SHORT).show();
                                for (QueryDocumentSnapshot document : task.getResult()) {
                                    result = result + "\n" + "name:" + document.get("Name") + "\n" + "surname:" + document.get("Surname") + "\n" + "age:" + document.get("Age") + "\n" + "hotel:" + document.get("Hotel") + "\n" + "travel package:" + document.get("TravelPackageId") + "\n";
                                }
                                listOfCustomers.setText(result);
                            } else {
                                Toast.makeText(getActivity(), "Query failed", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    break;
                case 2:
                    db.collection("Customer").whereGreaterThan("Age", 30).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {

                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            String result = "";
                            if (task.isSuccessful() && !task.getResult().isEmpty()) {
                                Toast.makeText(getActivity(), "Query successful", Toast.LENGTH_SHORT).show();
                                for (QueryDocumentSnapshot document : task.getResult()) {
                                    result = result + "\n" + "name:" + document.get("Name") + "\n" + "surname:" + document.get("Surname") + "\n" + "age:" + document.get("Age") + "\n" + "hotel:" + document.get("Hotel") + "\n" + "travel package:" + document.get("TravelPackageId") + "\n";
                                }
                                listOfCustomers.setText(result);
                            } else {
                                Toast.makeText(getActivity(), "Query failed", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    break;
                case 3:
                    db.collection("Customer").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {

                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            String result = "";
                            int count = 0;
                            if (task.isSuccessful() && !task.getResult().isEmpty()) {
                                Toast.makeText(getActivity(), "Query successful", Toast.LENGTH_SHORT).show();
                                for (QueryDocumentSnapshot document : task.getResult()) {
                                    if (document.get("Name").toString().startsWith("A")) {
                                        result = result + "\n" + "name:" + document.get("Name") + "\n" + "surname:" + document.get("Surname") + "\n" + "age:" + document.get("Age") + "\n" + "hotel:" + document.get("Hotel") + "\n" + "travel package:" + document.get("TravelPackageId") + "\n";
                                        count++;
                                    }
                                }
                                result = result + "\n" + "the number of customers is:" + count;
                                listOfCustomers.setText(result);
                            } else {
                                Toast.makeText(getActivity(), "Query failed", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    break;
                case 4:
                    db.collection("Customer").whereLessThan("Age", 30).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {

                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            String result = "";
                            if (task.isSuccessful() && !task.getResult().isEmpty()) {
                                Toast.makeText(getActivity(), "Query successful", Toast.LENGTH_SHORT).show();
                                for (QueryDocumentSnapshot document : task.getResult()) {
                                    if (document.get("Hotel").toString().equals("Grand Hotel")) {
                                        result = result + "\n" + "name:" + document.get("Name") + "\n" + "surname:" + document.get("Surname") + "\n" + "age:" + document.get("Age") + "\n" + "hotel:" + document.get("Hotel") + "\n" + "travel package:" + document.get("TravelPackageId") + "\n";
                                    }
                                }
                                listOfCustomers.setText(result);
                            } else {
                                Toast.makeText(getActivity(), "Query failed", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                    break;
            }
        }
    });
    returnBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            getParentFragmentManager().beginTransaction().replace(R.id.fragment_container, new CustomerManageFragment()).addToBackStack(null).commit();
        }
    });
    return view;
}
Also used : Task(com.google.android.gms.tasks.Task) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) TextView(android.widget.TextView) View(android.view.View) AdapterView(android.widget.AdapterView) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) NonNull(androidx.annotation.NonNull) AdapterView(android.widget.AdapterView)

Example 28 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project QR-Game by CMPUT301W22T15.

the class PlayerRanking method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player_ranking);
    // Set up variables
    highestScore = findViewById(R.id.highest_score_value);
    totalScans = findViewById(R.id.total_scans_value);
    totalSum = findViewById(R.id.total_sum_value);
    db = FirebaseFirestore.getInstance();
    final CollectionReference collectionReference = db.collection("Players");
    allPlayers = new ArrayList<>();
    // Player currentPlayer = singletonPlayer.player;
    // Obtain list of players
    collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {

        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException error) {
            allPlayers.clear();
            for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
                Player p = doc.toObject(Player.class);
                allPlayers.add(p);
            }
            displayInformation();
        }
    });
}
Also used : QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) CollectionReference(com.google.firebase.firestore.CollectionReference) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot)

Example 29 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project QR-Game by CMPUT301W22T15.

the class ScannerView2 method handleResult.

/**
 * Obtains data from scanned code and displays Toast message.
 * @param rawResult
 * Return result obtained from successful scan.
 */
@Override
public void handleResult(Result rawResult) {
    // TakePhoto.playerName.setText(rawResult.getText());
    Toast.makeText(ScannerView2.this, rawResult.getText(), Toast.LENGTH_SHORT).show();
    System.out.print(rawResult.getText());
    // Check to see which activity this came from
    Intent intent = this.getIntent();
    Bundle extras = intent.getExtras();
    boolean playerProfile = false;
    if (extras != null) {
        playerProfile = (boolean) extras.get("scanProfileCode");
    }
    if (playerProfile) {
        // Get User Information
        db = FirebaseFirestore.getInstance();
        CollectionReference collectionReference = db.collection("Players");
        // Obtain list of players
        collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {

            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException error) {
                allPlayers.clear();
                for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
                    Player p = doc.toObject(Player.class);
                    allPlayers.add(p);
                }
                openPlayerProfile(rawResult.getText().toString());
            }
        });
    } else {
        Intent returnIntent = new Intent();
        returnIntent.putExtra("playerhash", rawResult.getText());
        setResult(ExistingUser.RESULT_OK, returnIntent);
        finish();
        onBackPressed();
    }
}
Also used : 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)

Example 30 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project QR-Game by CMPUT301W22T15.

the class ownerListPlayers method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_owner_list_players);
    // fetch all the document to display them on listview
    db = FirebaseFirestore.getInstance();
    final CollectionReference collectionReference = db.collection("Players");
    playerList = findViewById(R.id.ownerPlayerListview);
    allPlayers = new ArrayList<>();
    playerAdapter = new OtherPlayerListAdapter(this, R.layout.other_player_listview_item, allPlayers);
    playerList.setAdapter(playerAdapter);
    collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {

        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException error) {
            allPlayers.clear();
            for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
                Player p = doc.toObject(Player.class);
                if (p.getOwner() == true) {
                // DO nothing
                } else {
                    allPlayers.add(p);
                }
            }
            playerAdapter.notifyDataSetChanged();
        }
    });
    playerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Player clickedPLayer = allPlayers.get(position);
            String playerUserName = clickedPLayer.getUsername();
            String playerHash = clickedPLayer.getPlayerHash();
            Intent intent = new Intent(ownerListPlayers.this, ownerPlayerProfile.class);
            intent.putExtra("playerUserName", playerUserName);
            intent.putExtra("playerHash", playerHash);
            startActivity(intent);
        }
    });
    playerList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            // Item to be deleted
            final int deletePlayer = i;
            new AlertDialog.Builder(ownerListPlayers.this).setIcon(android.R.drawable.ic_delete).setTitle("Confirm removal").setMessage("Would you like to remove player?").setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // Delete any players we chose
                    // URL: https://firebase.google.com/docs/firestore/manage-data/delete-data
                    // Date: 2021-11-11 UTC
                    // Update database with the removed data
                    String TAG = "working";
                    collectionReference.document(allPlayers.get(deletePlayer).getUsername()).delete().addOnSuccessListener(new OnSuccessListener<Void>() {

                        @Override
                        public void onSuccess(Void unused) {
                            Log.d(TAG, "message");
                        }
                    }).addOnFailureListener(new OnFailureListener() {

                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.e("MYAPP", "exception: " + e.getMessage());
                            Log.e("MYAPP", "exception: " + e.toString());
                        }
                    });
                // Update ranking again
                }
            }).setNegativeButton("No", null).show();
            return true;
        }
    });
// Button searchConfirmButton = findViewById(R.id.searchConfirmButton);
// searchConfirmButton.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// // TODO: could cause error when playerlist updates in DB while trying to do this.
// EditText searchbox = findViewById(R.id.searchPlayerEditText);
// String searchedPlayerName = searchbox.getText().toString();
// searchedPlayerName = searchedPlayerName.trim(); // TRIM WHITESPACES, IMPORTANT
// boolean exist = false;
// // check if entered user exist
// for (int i = 0; i < allPlayers.size(); i++) {
// String thisplayerUsername = allPlayers.get(i).getUsername();
// 
// if (thisplayerUsername.trim().equals(searchedPlayerName)) {  // case: if found
// // go to activity
// exist = true;
// searchbox.setText("");
// String playerHash = allPlayers.get(i).getPlayerHash();
// Intent intent = new Intent(OtherPlayers.this, OtherPlayerProfile.class);
// intent.putExtra("playerUserName", thisplayerUsername);
// intent.putExtra("playerHash", playerHash);
// startActivity(intent);
// // since startActivity is asynchronous call, i needed to use "exist" loic to toast
// }
// }
// // case: invalid userName
// if (!exist) {
// Toast.makeText(OtherPlayers.this, "Username Don't exist", Toast.LENGTH_SHORT).show();
// }
// searchbox.setText("");
// }
// });
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) DialogInterface(android.content.DialogInterface) CollectionReference(com.google.firebase.firestore.CollectionReference) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) Intent(android.content.Intent) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) AdapterView(android.widget.AdapterView) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

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