use of com.example.ezmeal.GroupLists.Model.Item 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);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(rvIngredients.getContext(), DividerItemDecoration.VERTICAL);
rvIngredients.addItemDecoration(dividerItemDecoration);
Bundle extras = getArguments();
String recipeId = extras.getString("id");
// todo: RecipesRating
db.collection("Recipes").document(recipeId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
ingredients = (ArrayList<String>) task.getResult().get("ingredients");
for (int i = 0; i < ingredients.size(); i++) {
if (!Objects.equals(ingredients.get(i), "")) {
recipeIngredientsFragmentModel.addItem(ingredients.get(i));
} else {
i = ingredients.size();
}
}
recipeIngredientsFragmentRecyclerAdapter.notifyDataSetChanged();
}
});
// ingredientsArray = extras.getStringArrayList("ingredients");
/*EZMealDatabase sqlDb = Room.databaseBuilder(getActivity().getApplicationContext(), EZMealDatabase.class, "user")
.allowMainThreadQueries().fallbackToDestructiveMigration().build();
ingredients = sqlDb.testDao().getIngredients(recipeId);
for (int i = 0; i < ingredients.size(); i++)
{
if (ingredients.get(i) != null)
{
ingredientsModel.addItem(ingredients.get(i));
}
else
{
i = ingredients.size();
}
}
ingredientsRecyclerAdapter.notifyDataSetChanged();*/
// add all ingredients for this recipe to the user's Item collection in Firestore
EZMealDatabase sqlDb = Room.databaseBuilder(getActivity().getApplicationContext(), EZMealDatabase.class, "user").allowMainThreadQueries().fallbackToDestructiveMigration().build();
// 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();
}
}
/*if (task.isSuccessful())
{
for (DocumentSnapshot document : task.getResult())
{
// if ingredient isn't already added to database, add it now
if (!document.exists())
{
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;
}
}).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();
}
}
else
{
Log.i("q", "task not successful");
}*/
}
}).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.example.ezmeal.GroupLists.Model.Item 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;
}
Aggregations