From 762b747a64844feabc1cde27c0322c5ed940c1b4 Mon Sep 17 00:00:00 2001 From: coy Date: Sun, 20 Feb 2022 17:14:56 -0500 Subject: [PATCH] added "add/remove" functionality for on-hand ingredients. fixes #13 --- .idea/misc.xml | 3 + .../sc/purplelimited/classes/Ingredient.java | 24 ++- .../OnHandIngredientsFragment.java | 174 ++++++++++++++---- .../OnHandListAdapter.java | 80 ++++++++ .../saved_recipes/SavedRecipesFragment.java | 39 ++-- .../ui/search/SearchFragment.java | 56 +++--- .../ui/suggestions/SuggestionsFragment.java | 36 ++-- .../main/res/drawable/decrease_quantity.xml | 10 + .../main/res/drawable/increase_quantity.xml | 10 + app/src/main/res/drawable/remove_icon.xml | 5 + app/src/main/res/layout/fragment_on_hand.xml | 32 ++-- .../main/res/layout/new_ingredient_popup.xml | 120 ++++++++++++ app/src/main/res/layout/on_hand_list_view.xml | 44 +++++ 13 files changed, 511 insertions(+), 122 deletions(-) create mode 100644 app/src/main/java/edu/sc/purplelimited/ui/on_hand_ingredients/OnHandListAdapter.java create mode 100644 app/src/main/res/drawable/decrease_quantity.xml create mode 100644 app/src/main/res/drawable/increase_quantity.xml create mode 100644 app/src/main/res/drawable/remove_icon.xml create mode 100644 app/src/main/res/layout/new_ingredient_popup.xml create mode 100644 app/src/main/res/layout/on_hand_list_view.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 3a2b9b2..7367d5b 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,6 +5,9 @@ + + + diff --git a/app/src/main/java/edu/sc/purplelimited/classes/Ingredient.java b/app/src/main/java/edu/sc/purplelimited/classes/Ingredient.java index 98b917e..a2f9cab 100644 --- a/app/src/main/java/edu/sc/purplelimited/classes/Ingredient.java +++ b/app/src/main/java/edu/sc/purplelimited/classes/Ingredient.java @@ -3,19 +3,20 @@ public class Ingredient { private String ingredientName; private String units; - private String quantity; + private int quantity; + private String id; public Ingredient() { this.ingredientName = "default"; this.units = "default"; - this.quantity = "0"; + this.quantity = 0; } - //TODO: parse quantity from text. example: ΒΌ -> 0.25 - public Ingredient(String ingredientName, String units, String quantity) { + public Ingredient(String ingredientName, String units, int quantity, String id) { this.ingredientName = ingredientName; this.units = units; this.quantity = quantity; + this.id = id; } public String getIngredientName() { @@ -34,14 +35,19 @@ public void setUnits(String units) { this.units = units; } - public String getQuantity() { + public int getQuantity() { return quantity; } - public void setQuantity(String quantity) { + public void setQuantity(int quantity) { this.quantity = quantity; } + public String getId() { return id; } + + public void setId(String id) { + this.id = id; + } public String toString() { String retUnits = ""; String retName = ingredientName; @@ -52,14 +58,14 @@ public String toString() { retUnits += ""; } else { retUnits += units; - if (quantity.equals("1") || quantity.equals("1.0")) { + if (quantity == 1) { retUnits += " "; } else { retUnits += "s "; } } - if (quantity.equals("0") || quantity.equals("0.0")) { - return retName + " to taste."; + if (quantity == 0) { + return retName; } else { retQuantity = quantity + " "; return retQuantity + retUnits + retName; diff --git a/app/src/main/java/edu/sc/purplelimited/ui/on_hand_ingredients/OnHandIngredientsFragment.java b/app/src/main/java/edu/sc/purplelimited/ui/on_hand_ingredients/OnHandIngredientsFragment.java index 5368694..935917f 100644 --- a/app/src/main/java/edu/sc/purplelimited/ui/on_hand_ingredients/OnHandIngredientsFragment.java +++ b/app/src/main/java/edu/sc/purplelimited/ui/on_hand_ingredients/OnHandIngredientsFragment.java @@ -1,11 +1,15 @@ package edu.sc.purplelimited.ui.on_hand_ingredients; +import android.app.AlertDialog; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; import android.widget.ListView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -19,64 +23,168 @@ import edu.sc.purplelimited.R; import edu.sc.purplelimited.classes.Ingredient; -import edu.sc.purplelimited.classes.Recipe; import edu.sc.purplelimited.databinding.FragmentOnHandBinding; import java.util.ArrayList; public class OnHandIngredientsFragment extends Fragment { - private final ArrayList onHandIngredientsList = new ArrayList<>(); - private FragmentOnHandBinding binding; + private ArrayList onHandArrayList; private ListView onHandListView; + private FragmentOnHandBinding binding; + private static FirebaseDatabase database; + private static DatabaseReference onHandDBRef; + private AlertDialog addIngredientPopup; + private EditText newIngredientName; + private EditText newIngredientUnits; + private EditText newIngredientQuantity; + private int currentQuantity = 0; - //TODO implement add/remove functionality public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - - binding = FragmentOnHandBinding.inflate(inflater, container, false); - View root1 = binding.getRoot(); - onHandListView = root1.findViewById(R.id.on_hand_list_view); - FirebaseDatabase database = FirebaseDatabase.getInstance(); + onHandArrayList = new ArrayList<>(); + database = FirebaseDatabase.getInstance(); // TODO replace hardcoded reference with userId - DatabaseReference onHand = database.getReference("users").child("1").child("onHandIngredients"); + onHandDBRef = database.getReference("users").child("1").child("onHandIngredients"); + binding = FragmentOnHandBinding.inflate(inflater, container, false); + View root = binding.getRoot(); + onHandListView = root.findViewById(R.id.on_hand_list_view); + Button createNewIngredientButton = root.findViewById(R.id.new_ingredient_button); - onHand.addChildEventListener(new ChildEventListener() { + createNewIngredientButton.setOnClickListener(new View.OnClickListener() { @Override - public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { - - String ingName = snapshot.child("ingredientName").getValue(String.class); - String ingUnit = snapshot.child("units").getValue(String.class); - String ingQuantity = snapshot.child("quantity").getValue(String.class); - onHandIngredientsList.add(new Ingredient(ingName, ingUnit, ingQuantity)); - populateOnHandIngredients(); + public void onClick(View view) { + createDialog(); } + }); + onHandDBRef.addChildEventListener(new ChildEventListener() { @Override - public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { + public void onChildAdded(@NonNull DataSnapshot ds, @Nullable String pcn) { + String name = ds.child("ingredientName").getValue(String.class); + String units = ds.child("units").getValue(String.class); + int quantity = ds.child("quantity").getValue(int.class); + String id = ds.getKey(); + onHandArrayList.add(new Ingredient(name, units, quantity, id)); + populateOnHandIngredients(); } - @Override - public void onChildRemoved(@NonNull DataSnapshot snapshot) { - String removed = snapshot.getValue(String.class); - onHandIngredientsList.remove(removed); - populateOnHandIngredients(); + public void onChildChanged(@NonNull DataSnapshot ds, @Nullable String pcn) { + String id = ds.getKey(); + int quantity = ds.child("quantity").getValue(int.class); + for (Ingredient ingredient : onHandArrayList) { + if(ingredient.getId().equals(id)) { + ingredient.setQuantity(quantity); + } + } + populateOnHandIngredients(); } - @Override - public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { + public void onChildRemoved(@NonNull DataSnapshot ds) { + int index = 0; + String id = ds.getKey(); + for(int i = 0; i < onHandArrayList.size();i++) { + Ingredient ingredient = onHandArrayList.get(i); + if (ingredient.getId().equals(id)) { + index = i; + } + } + onHandArrayList.remove(index); + populateOnHandIngredients(); } - @Override - public void onCancelled(@NonNull DatabaseError error) { - } + public void onChildMoved(@NonNull DataSnapshot ds, @Nullable String pcn) {/* empty */} + @Override + public void onCancelled(@NonNull DatabaseError error) {/* empty */} }); return binding.getRoot(); } - //TODO create custom list view for on hand ingredients w/ add, remove and + - quantity buttons + public static void addIngredient(Ingredient toAdd) { + String id = onHandDBRef.push().getKey(); + toAdd.setId(id); + onHandDBRef.child(id).setValue(toAdd); + } + + public static void changeQuantity(int quantity, String id) { + onHandDBRef.child(id).child("quantity").setValue(quantity); + } + + public static void removeIngredient(String id) { + onHandDBRef.child(id).removeValue(); + } + private void populateOnHandIngredients() { - ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, onHandIngredientsList); - onHandListView.setAdapter(arrayAdapter); + if(getContext() != null) { + OnHandListAdapter adapter = new OnHandListAdapter(this.getContext(), onHandArrayList); + onHandListView.setAdapter(adapter); + } + } + + public void createDialog(){ + AlertDialog.Builder popupBuilder = new AlertDialog.Builder(getContext()); + final View view = getLayoutInflater().inflate(R.layout.new_ingredient_popup, null); + + // Input Fields + newIngredientName = (EditText) view.findViewById(R.id.new_ingredient_name); + newIngredientQuantity = (EditText) view.findViewById(R.id.current_quantity); + newIngredientUnits = (EditText) view.findViewById(R.id.new_ingredient_units); + + // Clickable + ImageView increaseQuantity = (ImageView) view.findViewById(R.id.increase_quantity_popup); + ImageView decreaseQuantity = (ImageView) view.findViewById(R.id.decrease_quantity_popup); + Button addIngredientButton = (Button) view.findViewById(R.id.add_new_ingredient); + Button cancelButton = (Button) view.findViewById(R.id.cancel_new_ingredient); + + // PopupDialog + popupBuilder.setView(view); + addIngredientPopup = popupBuilder.create(); + addIngredientPopup.show(); + + increaseQuantity.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + currentQuantity++; + newIngredientQuantity.setText(""+currentQuantity); + } + }); + decreaseQuantity.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + if(currentQuantity > 0) { + currentQuantity--; + newIngredientQuantity.setText(""+currentQuantity); + } + } + }); + addIngredientButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String ingName = newIngredientName.getText().toString(); + String ingUnits = newIngredientUnits.getText().toString(); + int ingQuantity = currentQuantity; + + Boolean emptyName = (ingName.equals("")); + Boolean emptyUnits = (ingUnits.equals("")); + Boolean emptyQuantity = (ingQuantity == 0); + + //TODO fix toast notification + if(emptyName || emptyUnits || emptyQuantity) { + String toastText = "Please enter all required fields."; + Toast.makeText(view.getContext(), toastText, Toast.LENGTH_SHORT).show(); + } else { + addIngredient(new Ingredient(ingName, ingUnits, ingQuantity, "none")); + currentQuantity = 0; + addIngredientPopup.dismiss(); + } + } + }); + cancelButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + currentQuantity = 0; + addIngredientPopup.dismiss(); + } + }); } @Override public void onDestroyView() { diff --git a/app/src/main/java/edu/sc/purplelimited/ui/on_hand_ingredients/OnHandListAdapter.java b/app/src/main/java/edu/sc/purplelimited/ui/on_hand_ingredients/OnHandListAdapter.java new file mode 100644 index 0000000..1385688 --- /dev/null +++ b/app/src/main/java/edu/sc/purplelimited/ui/on_hand_ingredients/OnHandListAdapter.java @@ -0,0 +1,80 @@ +package edu.sc.purplelimited.ui.on_hand_ingredients; + +import android.app.Activity; +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.util.ArrayList; + +import edu.sc.purplelimited.R; +import edu.sc.purplelimited.classes.Ingredient; + +public class OnHandListAdapter extends ArrayAdapter { + ArrayList onHandList; + Context context; + + public OnHandListAdapter(@NonNull Context context, ArrayList onHandList) { + super(context, R.layout.on_hand_list_view, onHandList); + this.context = context; + this.onHandList = onHandList; + } + + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + if(convertView == null) { + LayoutInflater inflater; + inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); + convertView = inflater.inflate(R.layout.on_hand_list_view, null ); + + // Text + TextView ingredientString = convertView.findViewById(R.id.ingredient_name_in_listview); + ingredientString.setText(onHandList.get(position).toString()); + + // Clickable + ImageView increaseQuantity = convertView.findViewById(R.id.increase_quantity_list_view); + ImageView decreaseQuantity = convertView.findViewById(R.id.decrease_quantity_list_view); + ImageView removeItem = convertView.findViewById(R.id.remove); + + increaseQuantity.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String id = onHandList.get(position).getId(); + int quantity = onHandList.get(position).getQuantity(); + quantity++; + OnHandIngredientsFragment.changeQuantity(quantity, id); + } + }); + decreaseQuantity.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String id = onHandList.get(position).getId(); + int quantity = onHandList.get(position).getQuantity(); + if (quantity > 0) { + quantity--; + OnHandIngredientsFragment.changeQuantity(quantity, id); + } else { + // remove item from list if quantity goes below 0 + OnHandIngredientsFragment.removeIngredient(id); + } + } + }); + removeItem.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String id = onHandList.get(position).getId(); + OnHandIngredientsFragment.removeIngredient(id); + } + }); + } + return convertView; + } +} diff --git a/app/src/main/java/edu/sc/purplelimited/ui/saved_recipes/SavedRecipesFragment.java b/app/src/main/java/edu/sc/purplelimited/ui/saved_recipes/SavedRecipesFragment.java index fd6ffe8..a49a493 100644 --- a/app/src/main/java/edu/sc/purplelimited/ui/saved_recipes/SavedRecipesFragment.java +++ b/app/src/main/java/edu/sc/purplelimited/ui/saved_recipes/SavedRecipesFragment.java @@ -41,47 +41,40 @@ public View onCreateView(@NonNull LayoutInflater inflater, savedRecipesListView = root.findViewById(R.id.saved_recipe_list_view); FirebaseDatabase database = FirebaseDatabase.getInstance(); // TODO replace hardcoded reference with userId - DatabaseReference savedRecipes = database.getReference("users").child("1").child("savedRecipes"); + DatabaseReference savedRecipes; + savedRecipes = database.getReference("users").child("1").child("savedRecipes"); savedRecipes.addChildEventListener(new ChildEventListener() { @Override - public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { - String name = snapshot.child("name").getValue(String.class); - String description = snapshot.child("description").getValue(String.class); + public void onChildAdded(@NonNull DataSnapshot ds, @Nullable String pcn) { + String name = ds.child("name").getValue(String.class); + String description = ds.child("description").getValue(String.class); ArrayList ingredientsList= new ArrayList<>(); - DataSnapshot ingredients = snapshot.child("ingredients"); + DataSnapshot ingredients = ds.child("ingredients"); for(DataSnapshot ing : ingredients.getChildren()) { String ingName = ing.child("ingredientName").getValue(String.class); String ingUnit = ing.child("units").getValue(String.class); - String ingQuantity = ing.child("quantity").getValue(String.class); - ingredientsList.add(new Ingredient(ingName, ingUnit, ingQuantity)); + int ingQuantity = ing.child("quantity").getValue(int.class); + ingredientsList.add(new Ingredient(ingName, ingUnit, ingQuantity, "none")); } Recipe added = new Recipe(name, description, ingredientsList); savedRecipesArrayList.add(added); populateRecipeList(); } - @Override - public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { - } - + public void onChildChanged(@NonNull DataSnapshot ds, @Nullable String pcn) {/*empty*/} @Override - public void onChildRemoved(@NonNull DataSnapshot snapshot) { + public void onChildRemoved(@NonNull DataSnapshot ds) { //TODO fix this to match suggestions logic - Recipe removed = snapshot.getValue(Recipe.class); + Recipe removed = ds.getValue(Recipe.class); savedRecipesArrayList.remove(removed); populateRecipeList(); } - @Override - public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { - } - + public void onChildMoved(@NonNull DataSnapshot ds, @Nullable String pcn) {/*empty*/} @Override - public void onCancelled(@NonNull DatabaseError error) { - } + public void onCancelled(@NonNull DatabaseError error) {/*empty*/} }); - final TextView textView = binding.textDashboard; savedRecipesViewModel.getText().observe(getViewLifecycleOwner(), textView::setText); return root; @@ -95,7 +88,9 @@ public void onDestroyView() { //TODO create custom adapter/listview for recipes private void populateRecipeList() { - ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, savedRecipesArrayList); - savedRecipesListView.setAdapter(arrayAdapter); + if(getContext()!=null) { + ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, savedRecipesArrayList); + savedRecipesListView.setAdapter(arrayAdapter); + } } } \ No newline at end of file diff --git a/app/src/main/java/edu/sc/purplelimited/ui/search/SearchFragment.java b/app/src/main/java/edu/sc/purplelimited/ui/search/SearchFragment.java index 5c5b566..c73b364 100644 --- a/app/src/main/java/edu/sc/purplelimited/ui/search/SearchFragment.java +++ b/app/src/main/java/edu/sc/purplelimited/ui/search/SearchFragment.java @@ -50,7 +50,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, Button searchButton = root.findViewById(R.id.search_button); searchResultsCards = root.findViewById(R.id.view_pager_suggestions); - searchButton.setOnClickListener(view -> { FetchRecipes fetchRecipes = new FetchRecipes(); fetchRecipes.execute(); @@ -108,9 +107,9 @@ protected void onPostExecute(String s) { JSONArray recipesArrayJSON = resultsJSON.getJSONArray("results"); for (int i = 0; i < recipesArrayJSON.length(); i++) { JSONObject currentRecipeObj = recipesArrayJSON.getJSONObject(i); - String currentRecipeName = currentRecipeObj.getString("name"); - String currentRecipeDescription = currentRecipeObj.getString("description"); - ArrayList currentRecipeIngredients = new ArrayList<>(); + String recipeName = currentRecipeObj.getString("name"); + String recipeDesc = currentRecipeObj.getString("description"); + ArrayList recipeIngredients = new ArrayList<>(); try { JSONArray sectionsArray = currentRecipeObj.getJSONArray("sections"); if (sectionsArray != null) { @@ -119,35 +118,38 @@ protected void onPostExecute(String s) { JSONArray components = section.getJSONArray("components"); for (int k = 0; k < components.length(); k++) { JSONObject component = components.getJSONObject(k); - JSONObject componentIngredient = component.getJSONObject("ingredient"); - String ingredientName; - String ingredientUnit; - String ingredientQuantity; - if (componentIngredient.getString("display_singular").equals("") ) { - ingredientName = componentIngredient.getString("display_plural"); + JSONObject componentIng = component.getJSONObject("ingredient"); + String ingName; + String ingUnit; + int ingQuantity; + if (componentIng.getString("display_singular").equals("") ) { + ingName = componentIng.getString("display_plural"); } else { - ingredientName = componentIngredient.getString("display_singular"); + ingName = componentIng.getString("display_singular"); } - JSONArray componentMeasurementsArray = component.getJSONArray("measurements"); - JSONObject componentMeasurement = componentMeasurementsArray.getJSONObject(0); + JSONArray compMeasureArray = component.getJSONArray("measurements"); + JSONObject componentMeasurement = compMeasureArray.getJSONObject(0); JSONObject componentUnit = componentMeasurement.getJSONObject("unit"); if (componentUnit.getString("name").equals("") ) { - ingredientUnit = "none"; + ingUnit = "none"; } else { - ingredientUnit = componentUnit.getString("name"); + ingUnit = componentUnit.getString("name"); } - ingredientQuantity = componentMeasurement.getString("quantity"); - if (ingredientQuantity.equals("")) { - ingredientQuantity = "-1.0"; + try { + ingQuantity = componentMeasurement.getInt("quantity"); + } catch (Exception e) { + ingQuantity = 1; } - currentRecipeIngredients.add(new Ingredient(ingredientName, ingredientUnit, ingredientQuantity)); + Ingredient toAdd = new Ingredient(ingName, ingUnit, ingQuantity, "none"); + recipeIngredients.add(toAdd); } } } } catch (JSONException e) { continue; } - searchResultsArrayList.add(new Recipe(currentRecipeName, currentRecipeDescription, currentRecipeIngredients)); + Recipe toAdd = new Recipe(recipeName, recipeDesc, recipeIngredients); + searchResultsArrayList.add(toAdd); } populateSearchResults(); } catch (JSONException e) { @@ -160,11 +162,17 @@ private void populateSearchResults() { ArrayList recipeCards = new ArrayList<>(); for(int i = 0; i < 5; i++) { Recipe recipe = searchResultsArrayList.get(i); - recipeCards.add(new RecipeCard(recipe.getName(), recipe.getDescription(), recipe.getIngredients())); + String name = recipe.getName(); + String description = recipe.getDescription(); + ArrayList ingredients = recipe.getIngredients(); + RecipeCard toAdd = new RecipeCard(name, description, ingredients); + recipeCards.add(toAdd); + } + if(getContext()!=null) { + CardViewAdapter cardViewAdapter = new CardViewAdapter(getContext(), recipeCards); + searchResultsCards.setAdapter(cardViewAdapter); + searchResultsCards.setPadding(50,0, 50, 0); } - CardViewAdapter cardViewAdapter = new CardViewAdapter(getContext(), recipeCards); - searchResultsCards.setAdapter(cardViewAdapter); - searchResultsCards.setPadding(50,0, 50, 0); } @Override diff --git a/app/src/main/java/edu/sc/purplelimited/ui/suggestions/SuggestionsFragment.java b/app/src/main/java/edu/sc/purplelimited/ui/suggestions/SuggestionsFragment.java index 90259c1..e6bcd4f 100644 --- a/app/src/main/java/edu/sc/purplelimited/ui/suggestions/SuggestionsFragment.java +++ b/app/src/main/java/edu/sc/purplelimited/ui/suggestions/SuggestionsFragment.java @@ -44,46 +44,40 @@ public View onCreateView(@NonNull LayoutInflater inflater, suggestionsCards = root.findViewById(R.id.view_pager_suggestions); FirebaseDatabase database = FirebaseDatabase.getInstance(); // TODO replace hardcoded reference with userId - DatabaseReference suggestedRecipes = database.getReference("users").child("1").child("suggestedRecipes"); + DatabaseReference suggestedRecipes; + suggestedRecipes = database.getReference("users").child("1").child("suggestedRecipes"); suggestedRecipes.addChildEventListener(new ChildEventListener() { @Override - public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { - String name = snapshot.child("name").getValue(String.class); - String description = snapshot.child("description").getValue(String.class); + public void onChildAdded(@NonNull DataSnapshot ds, @Nullable String pcn) { + String name = ds.child("name").getValue(String.class); + String description = ds.child("description").getValue(String.class); ArrayList ingredientsList= new ArrayList<>(); - DataSnapshot ingredients = snapshot.child("ingredients"); + DataSnapshot ingredients = ds.child("ingredients"); for(DataSnapshot ing : ingredients.getChildren()) { String ingName = ing.child("ingredientName").getValue(String.class); String ingUnit = ing.child("units").getValue(String.class); - String ingQuantity = ing.child("quantity").getValue(String.class); - ingredientsList.add(new Ingredient(ingName, ingUnit, ingQuantity)); + int ingQuantity = ing.child("quantity").getValue(int.class); + ingredientsList.add(new Ingredient(ingName, ingUnit, ingQuantity, "none")); } Recipe added = new Recipe(name, description, ingredientsList); suggestedRecipesList.add(added); populateSuggestionCards(); } - @Override - public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { + public void onChildChanged(@NonNull DataSnapshot ds, @Nullable String pcn) { } - @Override public void onChildRemoved(@NonNull DataSnapshot snapshot) { //TODO match this with onChildAdded logic - Recipe removed = snapshot.getValue(Recipe.class); suggestedRecipesList.remove(removed); populateSuggestionCards(); } - @Override - public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { - } - + public void onChildMoved(@NonNull DataSnapshot ds, @Nullable String pcn) {/*empty*/} @Override - public void onCancelled(@NonNull DatabaseError error) { - } + public void onCancelled(@NonNull DatabaseError error) {/*empty*/} }); final TextView textView = binding.textHome; @@ -123,9 +117,11 @@ private void populateSuggestionCards() { } recipeCards.add(new Model(recipe.getName(), recipe.getDescription(), current)); } - Adapter cardViewAdapter = new Adapter(getContext(), recipeCards); - suggestionsCards.setAdapter(cardViewAdapter); - suggestionsCards.setPadding(50,0, 50, 0); + if(getContext()!=null) { + Adapter cardViewAdapter = new Adapter(getContext(), recipeCards); + suggestionsCards.setAdapter(cardViewAdapter); + suggestionsCards.setPadding(50,0, 50, 0); + } } @Override diff --git a/app/src/main/res/drawable/decrease_quantity.xml b/app/src/main/res/drawable/decrease_quantity.xml new file mode 100644 index 0000000..791a2f8 --- /dev/null +++ b/app/src/main/res/drawable/decrease_quantity.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/increase_quantity.xml b/app/src/main/res/drawable/increase_quantity.xml new file mode 100644 index 0000000..eb23254 --- /dev/null +++ b/app/src/main/res/drawable/increase_quantity.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/remove_icon.xml b/app/src/main/res/drawable/remove_icon.xml new file mode 100644 index 0000000..a537ed8 --- /dev/null +++ b/app/src/main/res/drawable/remove_icon.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/fragment_on_hand.xml b/app/src/main/res/layout/fragment_on_hand.xml index ceff365..5be12f9 100644 --- a/app/src/main/res/layout/fragment_on_hand.xml +++ b/app/src/main/res/layout/fragment_on_hand.xml @@ -1,11 +1,24 @@ - + + +