Search in sources :

Example 1 with Restaurant

use of com.google.firebase.example.fireeats.java.model.Restaurant in project quickstart-android by firebase.

the class MainFragment method onAddItemsClicked.

private void onAddItemsClicked() {
    // Add a bunch of random restaurants
    WriteBatch batch = mFirestore.batch();
    for (int i = 0; i < 10; i++) {
        DocumentReference restRef = mFirestore.collection("restaurants").document();
        // Create random restaurant / ratings
        Restaurant randomRestaurant = RestaurantUtil.getRandom(requireContext());
        List<Rating> randomRatings = RatingUtil.getRandomList(randomRestaurant.getNumRatings());
        randomRestaurant.setAvgRating(RatingUtil.getAverageRating(randomRatings));
        // Add restaurant
        batch.set(restRef, randomRestaurant);
        // Add ratings to subcollection
        for (Rating rating : randomRatings) {
            batch.set(restRef.collection("ratings").document(), rating);
        }
    }
    batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {

        @Override
        public void onComplete(@NonNull Task<Void> task) {
            if (task.isSuccessful()) {
                Log.d(TAG, "Write batch succeeded.");
            } else {
                Log.w(TAG, "write batch failed.", task.getException());
            }
        }
    });
}
Also used : Restaurant(com.google.firebase.example.fireeats.java.model.Restaurant) Rating(com.google.firebase.example.fireeats.java.model.Rating) WriteBatch(com.google.firebase.firestore.WriteBatch) DocumentReference(com.google.firebase.firestore.DocumentReference)

Example 2 with Restaurant

use of com.google.firebase.example.fireeats.java.model.Restaurant in project quickstart-android by firebase.

the class RestaurantUtil method getRandom.

/**
 * Create a random Restaurant POJO.
 */
public static Restaurant getRandom(Context context) {
    Restaurant restaurant = new Restaurant();
    Random random = new Random();
    // Cities (first elemnt is 'Any')
    String[] cities = context.getResources().getStringArray(R.array.cities);
    cities = Arrays.copyOfRange(cities, 1, cities.length);
    // Categories (first element is 'Any')
    String[] categories = context.getResources().getStringArray(R.array.categories);
    categories = Arrays.copyOfRange(categories, 1, categories.length);
    int[] prices = new int[] { 1, 2, 3 };
    restaurant.setName(getRandomName(random));
    restaurant.setCity(getRandomString(cities, random));
    restaurant.setCategory(getRandomString(categories, random));
    restaurant.setPhoto(getRandomImageUrl(random));
    restaurant.setPrice(getRandomInt(prices, random));
    restaurant.setNumRatings(random.nextInt(20));
    return restaurant;
}
Also used : Restaurant(com.google.firebase.example.fireeats.java.model.Restaurant) Random(java.util.Random)

Aggregations

Restaurant (com.google.firebase.example.fireeats.java.model.Restaurant)2 Rating (com.google.firebase.example.fireeats.java.model.Rating)1 DocumentReference (com.google.firebase.firestore.DocumentReference)1 WriteBatch (com.google.firebase.firestore.WriteBatch)1 Random (java.util.Random)1