use of com.google.firebase.firestore.CollectionReference in project EZMeal by Jake-Sokol2.
the class RecipeIngredientsFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_recipe_ingredients, container, false);
rvIngredients = (RecyclerView) view.findViewById(R.id.rvIngredientList);
recipeIngredientsFragmentRecyclerAdapter = new RecipeIngredientsFragmentRecyclerAdapter(recipeIngredientsFragmentModel.getIngredients());
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
rvIngredients.setAdapter(recipeIngredientsFragmentRecyclerAdapter);
rvIngredients.setLayoutManager(layoutManager);
// Recyclerview borders
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(rvIngredients.getContext(), DividerItemDecoration.VERTICAL);
rvIngredients.addItemDecoration(dividerItemDecoration);
Bundle extras = getArguments();
String recipeId = extras.getString("id");
EZMealDatabase sqlDb = Room.databaseBuilder(getActivity().getApplicationContext(), EZMealDatabase.class, "user").allowMainThreadQueries().fallbackToDestructiveMigration().build();
// retrieve ingredients for current recipe from Room and populate in recyclerview
ingredients = sqlDb.testDao().getIngredients(recipeId);
for (int i = 0; i < ingredients.size(); i++) {
if (ingredients.get(i) != null) {
// insert into recyclerview
recipeIngredientsFragmentModel.addItem(ingredients.get(i));
} else {
i = ingredients.size();
}
}
recipeIngredientsFragmentRecyclerAdapter.notifyDataSetChanged();
// add all ingredients for this recipe to the user's Item collection in Firestore
btnAddToList = view.findViewById(R.id.btnAddToList);
btnAddToList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// in case user edited ingredient list, re-read ingredients from database
ingredients = sqlDb.testDao().getIngredients(recipeId);
FirebaseUser mCurrentUser = mAuth.getCurrentUser();
String email = mCurrentUser.getEmail();
for (int i = 0; i < ingredients.size(); i++) {
if (ingredients.get(i) != null) {
mAuth = FirebaseAuth.getInstance();
CollectionReference dbItems = db.collection("Items");
Item item = new Item(ingredients.get(i), null, email);
// prevent user from adding same list of ingredients twice
dbItems.whereEqualTo("name", ingredients.get(i)).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.getResult().isEmpty()) {
dbItems.add(item).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
// keep track that an item was added so we can tell the user with a toast later
itemAdded = true;
Toast.makeText(getContext(), "Item added", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
// if any items were actually added, tell the user
if (itemAdded) {
Toast.makeText(getContext(), "Item added", Toast.LENGTH_SHORT).show();
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("q", "get failed");
}
});
} else {
// escape loop if rest of ingredients list is null (nulls are a symptom of database insertion)
i = ingredients.size();
}
}
btnAddToList.setEnabled(false);
btnAddToList.setTextColor(Color.parseColor("#808080"));
}
});
return view;
}
use of com.google.firebase.firestore.CollectionReference in project House-Organizer by House-Organizer.
the class FirestoreTask method storeTaskList.
// Might be unnecessary in the future
public static void storeTaskList(TaskList taskList, CollectionReference taskListRoot, String documentName) throws ExecutionException, InterruptedException {
Map<String, Object> data = new HashMap<>();
data.put("title", taskList.getTitle());
data.put("owner", taskList.getOwner().uid());
com.google.android.gms.tasks.Task<Void> task = taskListRoot.document(documentName).set(data);
Tasks.await(task);
if (task.isSuccessful()) {
DocumentReference documentReference = taskListRoot.document(documentName);
CollectionReference taskListRef = documentReference.collection("tasks");
for (Task t : taskList.getTasks()) {
Tasks.await(storeTask(t, taskListRef));
}
}
}
use of com.google.firebase.firestore.CollectionReference in project snippets-android by firebase.
the class DocSnippets method compoundQueries.
public void compoundQueries() {
CollectionReference citiesRef = db.collection("cities");
// [START chain_filters]
citiesRef.whereEqualTo("state", "CO").whereEqualTo("name", "Denver");
citiesRef.whereEqualTo("state", "CA").whereLessThan("population", 1000000);
// [END chain_filters]
// [START valid_range_filters]
citiesRef.whereGreaterThanOrEqualTo("state", "CA").whereLessThanOrEqualTo("state", "IN");
citiesRef.whereEqualTo("state", "CA").whereGreaterThan("population", 1000000);
// [END valid_range_filters]
}
use of com.google.firebase.firestore.CollectionReference in project snippets-android by firebase.
the class DocSnippets method orderAndLimit.
public void orderAndLimit() {
CollectionReference citiesRef = db.collection("cities");
// [START order_and_limit]
citiesRef.orderBy("name").limit(3);
// [END order_and_limit]
// [START order_and_limit_desc]
citiesRef.orderBy("name", Direction.DESCENDING).limit(3);
// [END order_and_limit_desc]
// [START order_by_multiple]
citiesRef.orderBy("state").orderBy("population", Direction.DESCENDING);
// [END order_by_multiple]
// [START filter_and_order]
citiesRef.whereGreaterThan("population", 100000).orderBy("population").limit(2);
// [END filter_and_order]
// [START valid_filter_and_order]
citiesRef.whereGreaterThan("population", 100000).orderBy("population");
// [END valid_filter_and_order]
}
use of com.google.firebase.firestore.CollectionReference in project snippets-android by firebase.
the class DocSnippets method compoundQueriesInvalid.
public void compoundQueriesInvalid() {
CollectionReference citiesRef = db.collection("cities");
// [START invalid_range_filters]
citiesRef.whereGreaterThanOrEqualTo("state", "CA").whereGreaterThan("population", 100000);
// [END invalid_range_filters]
}
Aggregations