Search in sources :

Example 36 with CollectionReference

use of com.google.firebase.firestore.CollectionReference in project snippets-android by firebase.

the class DocSnippets method orderAndLimitInvalid.

public void orderAndLimitInvalid() {
    CollectionReference citiesRef = db.collection("cities");
    // [START invalid_filter_and_order]
    citiesRef.whereGreaterThan("population", 100000).orderBy("country");
// [END invalid_filter_and_order]
}
Also used : CollectionReference(com.google.firebase.firestore.CollectionReference)

Example 37 with CollectionReference

use of com.google.firebase.firestore.CollectionReference in project snippets-android by firebase.

the class DocSnippets method inQueries.

public void inQueries() {
    // [START in_filter]
    CollectionReference citiesRef = db.collection("cities");
    citiesRef.whereIn("country", Arrays.asList("USA", "Japan"));
    // [END in_filter]
    // [START not_in_filter]
    citiesRef.whereNotIn("country", Arrays.asList("USA", "Japan"));
    // [END not_in_filter]
    // [START in_filter_with_array]
    citiesRef.whereIn("regions", Arrays.asList(new String[] { "west_coast" }, new String[] { "east_coast" }));
// [END in_filter_with_array]
}
Also used : CollectionReference(com.google.firebase.firestore.CollectionReference)

Example 38 with CollectionReference

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

the class ExistingUser method onCreate.

/**
 * This method creates the initial interface and obtains the necessary permissions.
 * @param savedInstanceState
 * Expects a Bundle object.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_existing_user);
    scanButton = findViewById(R.id.scan_button);
    rememberMe = (CheckBox) findViewById(R.id.login_checkbox_existing);
    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);
            }
        }
    });
    // Open ScannerView2 to scan a new QRcode
    scanButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(ExistingUser.this, ScannerView2.class);
            startActivityForResult(intent, 1);
        }
    });
}
Also used : QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) Intent(android.content.Intent) View(android.view.View) CollectionReference(com.google.firebase.firestore.CollectionReference) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot)

Example 39 with CollectionReference

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

the class ExistingUser method login.

/**
 * This method is called when the user taps the Log In button, and it opens the user menu activity
 * if the user successfully logs in.
 * @param view
 * Expects an object from the View class.
 */
public void login(View view) {
    EditText usernameEdit = (EditText) findViewById(R.id.username1_text);
    String username = usernameEdit.getText().toString();
    if (rememberMe.isChecked()) {
        // Create key-value pair using SharedPreferences
        SharedPreferences preferences = getSharedPreferences("rememberMeBox", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("remember", username);
        editor.apply();
        Toast.makeText(ExistingUser.this, "Persistence Enabled", Toast.LENGTH_SHORT).show();
    } else if (!rememberMe.isChecked()) {
        // Create key-value pair using SharedPreferences
        SharedPreferences preferences = getSharedPreferences("rememberMeBox", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("remember", "");
        editor.apply();
        Toast.makeText(ExistingUser.this, "Persistence Disabled", Toast.LENGTH_SHORT).show();
    }
    // Setup player so that they can referenced throughout the app
    singletonPlayer.player.setUsername(username);
    db = FirebaseFirestore.getInstance();
    final CollectionReference collectionReference = db.collection("Players");
    final CollectionReference collectionReferenceQR = db.collection("QRCodes");
    DocumentReference playerDocRef = db.collection("Players").document(username);
    playerDocRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot documentSnapshot = task.getResult();
                if (documentSnapshot.exists()) {
                    singletonPlayer.player = documentSnapshot.toObject(Player.class);
                    Log.d("Success", "12");
                    if (singletonPlayer.player.getOwner() == true) {
                        // Open new activity
                        Intent intent = new Intent(getApplicationContext(), OwnerMenu.class);
                        startActivity(intent);
                    } else {
                        // Open new activity
                        Intent intent = new Intent(getApplicationContext(), UserMenu.class);
                        intent.putExtra("userMenu_session", (String) null);
                        startActivity(intent);
                    }
                }
            }
        }
    });
}
Also used : EditText(android.widget.EditText) SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) CollectionReference(com.google.firebase.firestore.CollectionReference) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) DocumentReference(com.google.firebase.firestore.DocumentReference)

Example 40 with CollectionReference

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

the class MyScans method updateTotalScans.

/**
 * Updates the total scans after user deletes.
 */
private void updateTotalScans() {
    // Initialize Variable
    int totalNumberScans = singletonPlayer.player.numberOfCode();
    int totalSums = singletonPlayer.player.getTotalScore();
    // Update count
    totalScans.setText("Total Scans: " + String.valueOf(totalNumberScans));
    // Update sum
    totalScore.setText("Total Score: " + String.valueOf(totalSums));
    // Update user information
    singletonPlayer.player.setScore(totalSums);
    // Access a Cloud FireStore instance from Activity
    db = FirebaseFirestore.getInstance();
    final CollectionReference collectionReference = db.collection("Players");
    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());
        }
    });
}
Also used : OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) CollectionReference(com.google.firebase.firestore.CollectionReference) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

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