Skip to content

Commit

Permalink
added "add/remove" functionality for on-hand ingredients. fixes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
coydb committed Feb 20, 2022
1 parent 0f2e045 commit 762b747
Show file tree
Hide file tree
Showing 13 changed files with 511 additions and 122 deletions.
3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions app/src/main/java/edu/sc/purplelimited/classes/Ingredient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Ingredient> onHandIngredientsList = new ArrayList<>();
private FragmentOnHandBinding binding;
private ArrayList<Ingredient> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Ingredient> onHandList;
Context context;

public OnHandListAdapter(@NonNull Context context, ArrayList<Ingredient> 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;
}
}
Loading

0 comments on commit 762b747

Please # to comment.