Search in sources :

Example 31 with CollectionReference

use of com.google.firebase.firestore.CollectionReference in project EZMeal by Jake-Sokol2.

the class RecipeRatingsFragment method onStop.

@Override
public void onStop() {
    super.onStop();
    MutableLiveData<List<String>> test = vmRateRecipeBubble.getRatingBubbleList();
    chosenBubbles = test.getValue();
    // even though it is initialized in the ViewModel, getRatingBubbleList will return null if a value has not been set yet by the user
    if (chosenBubbles == null) {
        chosenBubbles = new ArrayList<String>();
    }
    // Room db insert/update expects 3 bubble selections when creating the record, so give it nulls for any missing ratings in case the user only selected 0-2 text ratings
    if (chosenBubbles.size() < 3) {
        for (int i = chosenBubbles.size(); i < 3; i++) {
            chosenBubbles.add(null);
        }
    }
    // get change in rating compared to user's previous rating (if one exists)
    float oldRating = sqlDb.testDao().getSpecificRating(recipeId);
    float newRating = rateRecipe.getRating();
    float changeInRating = newRating - oldRating;
    Rating r1 = sqlDb.testDao().getSpecificRatingObject(recipeId);
    Rating r2 = sqlDb.testDao().getAllTheThings();
    // if rating is different than it was when the user entered the recipe, they added / updated their rating, so update both database entries
    if (changeInRating != 0) {
        db = FirebaseFirestore.getInstance();
        // todo: RecipesRating
        CollectionReference dbRecipes = db.collection("Recipes");
        // if user has never left a review on this recipe before, increment number of ratings in firestore
        if (oldRating == 0) {
            dbRecipes.document(recipeId).update("countRating", FieldValue.increment(1)).addOnSuccessListener(new OnSuccessListener<Void>() {

                @Override
                public void onSuccess(Void unused) {
                    Log.i("rating", "numRating updated");
                }
            });
            // add a new entry to Room so their rating displays correctly next time they return to the recipe
            Rating newRatingEntry = new Rating(recipeId, newRating, chosenBubbles);
            sqlDb.testDao().insert(newRatingEntry);
            // increment count of Ratings so onStop() knows how to calculate the correct final average rating after new rating is written to firebase
            countOfRatings++;
        } else // if newRating is not 0, they changed their rating, so update existing Room entry
        if (newRating != 0) {
            // update their existing Room entry with the correct new rating
            Rating newRatingEntry = new Rating(recipeId, newRating, chosenBubbles);
            // decrement counters in firebase for any old text rating's that the user may have chosen on their last review
            decrementTextRatingsInFirebase();
            sqlDb.testDao().updateRating(newRating, recipeId);
            sqlDb.testDao().updateTextRatings(chosenBubbles.get(0), chosenBubbles.get(1), chosenBubbles.get(2), recipeId);
        }
        // if user added or changed their rating, update the total rating score in firestore
        if (newRating != 0) {
            // increment counters in firebase for text ratings
            // using chosenBubbles global list
            incrementTextRatingsInFirebase();
            // increment or decrement (depending on if changeInRating is positive or negative) Firestore rating by new value
            dbRecipes.document(recipeId).update("rating", FieldValue.increment(changeInRating)).addOnSuccessListener(new OnSuccessListener<Void>() {

                @Override
                public void onSuccess(Void unused) {
                    Log.i("rating", "rating updated");
                }
            });
        }
        // new rating pushed Recipe above the highly rated threshold - notify firebase
        if ((highlyRated == false) && ((changeInRating + totalRatingFirebase) / countOfRatings) >= 4) {
            dbRecipes.document(recipeId).update("highlyRated", true).addOnSuccessListener(new OnSuccessListener<Void>() {

                @Override
                public void onSuccess(Void unused) {
                    Log.i("rating", "highlyRated updated");
                }
            });
        } else // new rating dropped Recipe beneath the highly rated threshold - notify firebase
        if ((highlyRated) && ((changeInRating + totalRatingFirebase) / countOfRatings) < 4) {
            dbRecipes.document(recipeId).update("highlyRated", false).addOnSuccessListener(new OnSuccessListener<Void>() {

                @Override
                public void onSuccess(Void unused) {
                    Log.i("rating", "highlyRated updated");
                }
            });
        }
        if (sqlDb.isOpen()) {
            sqlDb.close();
        }
    }
}
Also used : Rating(com.example.ezmeal.roomDatabase.Rating) List(java.util.List) ArrayList(java.util.ArrayList) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) CollectionReference(com.google.firebase.firestore.CollectionReference)

Example 32 with CollectionReference

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

the class DocSnippets method exampleData.

public void exampleData() {
    // [START example_data]
    CollectionReference cities = db.collection("cities");
    Map<String, Object> data1 = new HashMap<>();
    data1.put("name", "San Francisco");
    data1.put("state", "CA");
    data1.put("country", "USA");
    data1.put("capital", false);
    data1.put("population", 860000);
    data1.put("regions", Arrays.asList("west_coast", "norcal"));
    cities.document("SF").set(data1);
    Map<String, Object> data2 = new HashMap<>();
    data2.put("name", "Los Angeles");
    data2.put("state", "CA");
    data2.put("country", "USA");
    data2.put("capital", false);
    data2.put("population", 3900000);
    data2.put("regions", Arrays.asList("west_coast", "socal"));
    cities.document("LA").set(data2);
    Map<String, Object> data3 = new HashMap<>();
    data3.put("name", "Washington D.C.");
    data3.put("state", null);
    data3.put("country", "USA");
    data3.put("capital", true);
    data3.put("population", 680000);
    data3.put("regions", Arrays.asList("east_coast"));
    cities.document("DC").set(data3);
    Map<String, Object> data4 = new HashMap<>();
    data4.put("name", "Tokyo");
    data4.put("state", null);
    data4.put("country", "Japan");
    data4.put("capital", true);
    data4.put("population", 9000000);
    data4.put("regions", Arrays.asList("kanto", "honshu"));
    cities.document("TOK").set(data4);
    Map<String, Object> data5 = new HashMap<>();
    data5.put("name", "Beijing");
    data5.put("state", null);
    data5.put("country", "China");
    data5.put("capital", true);
    data5.put("population", 21500000);
    data5.put("regions", Arrays.asList("jingjinji", "hebei"));
    cities.document("BJ").set(data5);
// [END example_data]
}
Also used : HashMap(java.util.HashMap) CollectionReference(com.google.firebase.firestore.CollectionReference)

Example 33 with CollectionReference

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

the class DocSnippets method simpleQueries.

public void simpleQueries() {
    // [START simple_queries]
    // Create a reference to the cities collection
    CollectionReference citiesRef = db.collection("cities");
    // Create a query against the collection.
    Query query = citiesRef.whereEqualTo("state", "CA");
    // [END simple_queries]
    // [START simple_query_capital]
    Query capitalCities = db.collection("cities").whereEqualTo("capital", true);
    // [END simple_query_capital]
    // [START example_filters]
    Query stateQuery = citiesRef.whereEqualTo("state", "CA");
    Query populationQuery = citiesRef.whereLessThan("population", 100000);
    Query nameQuery = citiesRef.whereGreaterThanOrEqualTo("name", "San Francisco");
    // [END example_filters]
    // [START simple_query_not_equal]
    Query notCapitalQuery = citiesRef.whereNotEqualTo("capital", false);
// [END simple_query_not_equal]
}
Also used : Query(com.google.firebase.firestore.Query) CollectionReference(com.google.firebase.firestore.CollectionReference)

Example 34 with CollectionReference

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

the class DocSnippets method arrayContainsAnyQueries.

public void arrayContainsAnyQueries() {
    // [START array_contains_any_filter]
    CollectionReference citiesRef = db.collection("cities");
    citiesRef.whereArrayContainsAny("regions", Arrays.asList("west_coast", "east_coast"));
// [END array_contains_any_filter]
}
Also used : CollectionReference(com.google.firebase.firestore.CollectionReference)

Example 35 with CollectionReference

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

the class DocSnippets method arrayContainsQueries.

public void arrayContainsQueries() {
    // [START array_contains_filter]
    CollectionReference citiesRef = db.collection("cities");
    citiesRef.whereArrayContains("regions", "west_coast");
// [END array_contains_filter]
}
Also used : CollectionReference(com.google.firebase.firestore.CollectionReference)

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