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());
}
}
});
}
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;
}
Aggregations