Search in sources :

Example 16 with CollectionReference

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

the class MyScans method onCreate.

/**
 * This method sets up the initial user interface.
 * @param savedInstanceState
 * Expects object of type Bundle.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_scans);
    // Set up variable
    ListView scanList = findViewById(R.id.scan_list);
    // Obtain list of QRCodes
    qrCodes = singletonPlayer.player.getQrCodes();
    // Create list adapter
    scanAdapter = new ScanListAdapter(this, R.layout.my_scans_adapter, qrCodes);
    scanList.setAdapter(scanAdapter);
    // Initialize variables
    totalScans = findViewById(R.id.total_scans);
    totalScore = findViewById(R.id.total_score);
    // sortByDate = findViewById(R.id.sort_by_date);
    displayExtremum = findViewById(R.id.display_minMax);
    totalScans.setText("Total Scans: " + singletonPlayer.player.numberOfCode());
    totalScore.setText("Total Score: 0");
    // Access a Cloud FireStore instance from Activity
    db = FirebaseFirestore.getInstance();
    final CollectionReference collectionReference = db.collection("Players");
    if (singletonPlayer.player.numberOfCode() == 0) {
        displayExtremum.setEnabled(false);
    }
    displayExtremum.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // display minimum and max
            QRCode minQRcode = qrCodes.get(0);
            QRCode maxQRcode = qrCodes.get(0);
            for (int i = 0; i < qrCodes.size(); i++) {
                // Less than minScore
                if (qrCodes.get(i).getScore() < minQRcode.getScore()) {
                    minQRcode = qrCodes.get(i);
                } else // Bigger than maxScore
                if (qrCodes.get(i).getScore() > maxQRcode.getScore()) {
                    maxQRcode = qrCodes.get(i);
                }
            // Else do nothing
            }
            String minQR = "QR Code with Minimum Score:" + "\n\n" + "hashedID: " + minQRcode.getId() + "\n" + "Date: " + minQRcode.getDateStr() + "\n" + "Score: " + String.valueOf(minQRcode.getScore()) + "\n" + "hasPhoto: " + String.valueOf(minQRcode.getHasPhoto()) + "\n" + "hasLocation: " + String.valueOf(minQRcode.getHasLocation());
            String maxQR = "QR Code with Maximum Score:" + "\n\n" + "hashedID: " + maxQRcode.getId() + "\n" + "Date: " + maxQRcode.getDateStr() + "\n" + "Score: " + String.valueOf(maxQRcode.getScore()) + "\n" + "hasPhoto: " + String.valueOf(maxQRcode.getHasPhoto()) + "\n" + "hasLocation: " + String.valueOf(maxQRcode.getHasLocation());
            new AlertDialog.Builder(MyScans.this).setIcon(android.R.drawable.ic_menu_info_details).setTitle("STATISTICS").setMessage(minQR + "\n\n\n" + maxQR).setPositiveButton("OK", null).show();
        }
    });
    scanList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // get the item clicked from the list.
            QRCode qrcode = qrCodes.get(i);
            Intent intent = new Intent(getApplicationContext(), ViewQRCode.class);
            intent.putExtra("qrcode_info2", (Parcelable) qrcode);
            // let viewQRcode know this is from otherplayer.
            intent.putExtra("isOtherPlayer", "false");
            startActivity(intent);
        // new ViewQRCodeFragment(qrcode).show(getSupportFragmentManager(), "View QR code");
        }
    });
    // Add ability to delete QRCodes
    scanList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

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

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    // Remove Session from listview
                    singletonPlayer.player.qrCodes.remove(deleteQRCode);
                    scanAdapter.notifyDataSetChanged();
                    // Update database with the removed data
                    String TAG = "working";
                    collectionReference.document(singletonPlayer.player.getUsername()).set(singletonPlayer.player).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());
                        }
                    });
                    updateTotalScans();
                }
            }).setNegativeButton("No", null).show();
            return true;
        }
    });
    // Update Total Count
    updateTotalScans();
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) DialogInterface(android.content.DialogInterface) CollectionReference(com.google.firebase.firestore.CollectionReference) ListView(android.widget.ListView) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) Intent(android.content.Intent) Parcelable(android.os.Parcelable) TextView(android.widget.TextView) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) AdapterView(android.widget.AdapterView) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 17 with CollectionReference

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

the class OtherPlayerProfile method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_other_player_profile);
    ListView qrCodeListView = findViewById(R.id.OtherPlayerProfileListView);
    // Create list adapter
    qrCodes = new ArrayList<>();
    scanAdapter = new ScanListAdapter(this, R.layout.my_scans_adapter, qrCodes);
    qrCodeListView.setAdapter(scanAdapter);
    db = FirebaseFirestore.getInstance();
    CollectionReference collectionReference = db.collection("Players");
    // Extract from intent
    Intent intent = this.getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null) {
        playerUserName = (String) extras.get("playerUserName");
        playerHash = (String) extras.get("playerHash");
    }
    // Extract Player from database
    DocumentReference playerDocRef = db.collection("Players").document(playerUserName);
    playerDocRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot documentSnapshot = task.getResult();
                if (documentSnapshot.exists()) {
                    player = documentSnapshot.toObject(Player.class);
                    qrCodes.clear();
                    for (int i = 0; i < player.numberOfCode(); i++) {
                        qrCodes.add(player.qrCodes.get(i));
                    }
                    scanAdapter.notifyDataSetChanged();
                    Log.d("Success", "12");
                    // display to list view
                    displayPlayerInfo();
                }
            }
        }
    });
    qrCodeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // go to viewQRcode activity
            QRCode qrcode = qrCodes.get(i);
            Intent intent = new Intent(getApplicationContext(), ViewQRCode.class);
            intent.putExtra("qrcode_info2", (Parcelable) qrcode);
            intent.putExtra("otherPlayerName", playerUserName);
            // let viewQRcode know this is from otherplayer.
            intent.putExtra("isOtherPlayer", "true");
            startActivity(intent);
        }
    });
}
Also used : Bundle(android.os.Bundle) Intent(android.content.Intent) Parcelable(android.os.Parcelable) TextView(android.widget.TextView) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) CollectionReference(com.google.firebase.firestore.CollectionReference) DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) ListView(android.widget.ListView) AdapterView(android.widget.AdapterView) DocumentReference(com.google.firebase.firestore.DocumentReference)

Example 18 with CollectionReference

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

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

the class ScannerView method onCreate.

/**
 * This method creates the inital interface and obtains the necessary permissions.
 * @param savedInstanceState
 * Expects object of type Bundle.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scanner_view);
    // Access a Cloud FireStore instance from Activity
    db = FirebaseFirestore.getInstance();
    final CollectionReference collectionReference = db.collection("Players");
    // Set variable data
    scannerView = findViewById(R.id.scanner_view);
    codeScanner = new CodeScanner(this, scannerView);
    // resultData = findViewById(R.id.code_result);
    // Decode data from QR code
    codeScanner.setDecodeCallback(new DecodeCallback() {

        @Override
        public void onDecoded(@NonNull Result result) {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // resultData.setText(result.getText());
                    Toast.makeText(ScannerView.this, "Scanned Successfully", Toast.LENGTH_SHORT).show();
                    // TODO create the location string
                    QRCode qrcode = new QRCode(result.getText(), "");
                    qrcode.setKey("classified information");
                    if (isUnique(qrcode.getId())) {
                        singletonPlayer.player.addQrcode(qrcode);
                        String TAG = "working";
                        collectionReference.document(singletonPlayer.player.getUsername()).set(singletonPlayer.player).addOnSuccessListener(new OnSuccessListener<Void>() {

                            @Override
                            public void onSuccess(Void unused) {
                                Log.d(TAG, "message");
                                Intent intent = new Intent(ScannerView.this, QRCodeEditor.class);
                                intent.putExtra("QRCodeValue", (Parcelable) qrcode);
                                startActivity(intent);
                            }
                        }).addOnFailureListener(new OnFailureListener() {

                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Log.e("MYAPP", "exception: " + e.getMessage());
                                Log.e("MYAPP", "exception: " + e.toString());
                            }
                        });
                    } else {
                        // not unique. close activity
                        Toast.makeText(ScannerView.this, "you already scanned this one", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }
            });
        }
    });
    // Restart scan if UI is clicked
    scannerView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            codeScanner.startPreview();
        }
    });
}
Also used : Intent(android.content.Intent) DecodeCallback(com.budiyev.android.codescanner.DecodeCallback) CodeScannerView(com.budiyev.android.codescanner.CodeScannerView) View(android.view.View) TextView(android.widget.TextView) CollectionReference(com.google.firebase.firestore.CollectionReference) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) Result(com.google.zxing.Result) CodeScanner(com.budiyev.android.codescanner.CodeScanner) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 20 with CollectionReference

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

Aggregations

CollectionReference (com.google.firebase.firestore.CollectionReference)43 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)15 Intent (android.content.Intent)14 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)14 View (android.view.View)13 DocumentSnapshot (com.google.firebase.firestore.DocumentSnapshot)13 NonNull (androidx.annotation.NonNull)10 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)10 DocumentReference (com.google.firebase.firestore.DocumentReference)10 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)10 ArrayList (java.util.ArrayList)8 Bundle (android.os.Bundle)7 ListView (android.widget.ListView)7 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)7 AdapterView (android.widget.AdapterView)6 TextView (android.widget.TextView)6 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)5 Task (com.google.android.gms.tasks.Task)5 List (java.util.List)5 RecyclerView (androidx.recyclerview.widget.RecyclerView)4