diff --git a/Listify/app/src/main/java/com/example/listify/ListPage.java b/Listify/app/src/main/java/com/example/listify/ListPage.java index fa449d2..6f3044f 100644 --- a/Listify/app/src/main/java/com/example/listify/ListPage.java +++ b/Listify/app/src/main/java/com/example/listify/ListPage.java @@ -10,20 +10,28 @@ import android.widget.*; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; + +import com.bumptech.glide.Glide; + import com.example.listify.data.Item; import com.example.listify.data.List; import com.example.listify.data.ListEntry; -import org.json.JSONException; import java.io.IOException; +import java.text.DecimalFormat; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; +import org.json.JSONException; + import static com.example.listify.MainActivity.am; public class ListPage extends AppCompatActivity { ListView listView; MyAdapter myAdapter; + Requestor requestor; Button incrQuan; Button decrQuan; @@ -33,17 +41,27 @@ public class ListPage extends AppCompatActivity { ArrayList pStores = new ArrayList<>(); ArrayList pPrices = new ArrayList<>(); ArrayList pQuantity = new ArrayList<>(); - ArrayList pImages = new ArrayList<>(); + ArrayList pImages = new ArrayList<>(); ArrayList pListItemPair = new ArrayList<>(); - Requestor requestor; + Map totalPriceByStore = new HashMap<>(); + Map storeHeaderIndex = new HashMap<>(); + + DecimalFormat df = new DecimalFormat("0.00"); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { // Read list ID from caller final int listID = (int) getIntent().getSerializableExtra("listID"); + pNames.add("Total Price"); + pStores.add(""); + pPrices.add("0.00"); + pQuantity.add("-1"); + pImages.add("-1"); + pListItemPair.add(null); + Properties configs = new Properties(); try { configs = AuthManager.loadProperties(this, "android.resource://" + getPackageName() + "/raw/auths.json"); @@ -52,7 +70,6 @@ public class ListPage extends AppCompatActivity { } requestor = new Requestor(am, configs.getProperty("apiKey")); SynchronousReceiver lr = new SynchronousReceiver<>(); - //ListReceiver lr = new ListReceiver<>(); requestor.getObject(Integer.toString(listID), List.class, lr); List list; @@ -77,34 +94,55 @@ public class ListPage extends AppCompatActivity { item = null; } if(item != null) { - pNames.add(item.getDescription()); - pStores.add("Kroger"); - pPrices.add(item.getPrice().toString()); - pQuantity.add(entry.getQuantity().toString()); - pImages.add(R.drawable.placeholder); - pListItemPair.add(entry); + if(!totalPriceByStore.containsKey("Kroger")) { + totalPriceByStore.put("Kroger", item.getPrice().doubleValue() * entry.getQuantity()); + storeHeaderIndex.put("Kroger", pNames.size()); + + double newTotal = Double.parseDouble(pPrices.get(0)) + (item.getPrice().doubleValue() * entry.getQuantity()); + pPrices.set(0, String.valueOf(newTotal)); + + pNames.add("Kroger"); + pStores.add(""); + pPrices.add(df.format(totalPriceByStore.get("Kroger"))); + pQuantity.add("-1"); + pImages.add("-1"); + pListItemPair.add(null); + + pNames.add(item.getDescription()); + pStores.add("Kroger"); + pPrices.add(df.format(item.getPrice())); + pQuantity.add(entry.getQuantity().toString()); + pImages.add(item.getImageURL()); + pListItemPair.add(entry); + } + else { + int index = storeHeaderIndex.get("Kroger"); + + totalPriceByStore.put("Kroger", totalPriceByStore.get("Kroger") + (item.getPrice().doubleValue() * entry.getQuantity())); + pPrices.set(index, df.format(totalPriceByStore.get("Kroger"))); + + double newTotal = Double.parseDouble(pPrices.get(0)) + (item.getPrice().doubleValue() * entry.getQuantity()); + pPrices.set(0, df.format(newTotal)); + + index++; + + pNames.add(index, item.getDescription()); + pStores.add(index, "Kroger"); + pPrices.add(index, df.format(item.getPrice())); + pQuantity.add(index, entry.getQuantity().toString()); + pImages.add(index, item.getImageURL()); + pListItemPair.add(index, entry); + + for(String store : storeHeaderIndex.keySet()) { + if(storeHeaderIndex.get(store) > index) { + storeHeaderIndex.put(store, storeHeaderIndex.get(store) + 1); + } + } + } } } } - /*pNames.add("Half-gallon organic whole milk"); - pStores.add("Kroger"); - pPrices.add("$5.00"); - pQuantity.add("1"); - pImages.add(R.drawable.milk); - - pNames.add("5-bunch medium bananas"); - pStores.add("Kroger"); - pPrices.add("$3.00"); - pQuantity.add("1"); - pImages.add(R.drawable.bananas); - - pNames.add("JIF 40-oz creamy peanut butter"); - pStores.add("Kroger"); - pPrices.add("$7.00"); - pQuantity.add("1"); - pImages.add(R.drawable.peanutbutter);*/ - super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); @@ -120,9 +158,9 @@ public class ListPage extends AppCompatActivity { ArrayList pStores; ArrayList pPrices; ArrayList pQuantity; - ArrayList pImages; + ArrayList pImages; - MyAdapter (Context c, ArrayList names, ArrayList stores, ArrayList prices, ArrayList quantity, ArrayList images) { + MyAdapter (Context c, ArrayList names, ArrayList stores, ArrayList prices, ArrayList quantity, ArrayList images) { super(c, R.layout.activity_listproductentry, R.id.productView, names); context = c; pNames = names; @@ -147,6 +185,10 @@ public class ListPage extends AppCompatActivity { public void onClick(View v) { int q = Integer.parseInt(pQuantity.get(position)) - 1; pQuantity.set(position, Integer.toString(q)); + totalPriceByStore.put(pStores.get(position), totalPriceByStore.get(pStores.get(position)) - Double.parseDouble(pPrices.get(position))); + pPrices.set(storeHeaderIndex.get(pStores.get(position)), df.format(totalPriceByStore.get(pStores.get(position)))); + double newTotal = Double.parseDouble(pPrices.get(0)) - Double.parseDouble(pPrices.get(position)); + pPrices.set(0, df.format(newTotal)); ListEntry le = pListItemPair.remove(position); le.setQuantity(le.getQuantity() - 1); pListItemPair.add(position, le); @@ -176,6 +218,10 @@ public class ListPage extends AppCompatActivity { public void onClick(View v) { int q = Integer.parseInt(pQuantity.get(position)) + 1; pQuantity.set(position, Integer.toString(q)); + totalPriceByStore.put(pStores.get(position), totalPriceByStore.get(pStores.get(position)) + Double.parseDouble(pPrices.get(position))); + pPrices.set(storeHeaderIndex.get(pStores.get(position)), df.format(totalPriceByStore.get(pStores.get(position)))); + double newTotal = Double.parseDouble(pPrices.get(0)) + Double.parseDouble(pPrices.get(position)); + pPrices.set(0, df.format(newTotal)); ListEntry le = pListItemPair.remove(position); le.setQuantity(le.getQuantity() + 1); pListItemPair.add(position, le); @@ -203,13 +249,41 @@ public class ListPage extends AppCompatActivity { removeItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - pNames.remove(position); - pStores.remove(position); - pPrices.remove(position); - pQuantity.remove(position); - pImages.remove(position); + if(position == 0) { + pNames.clear(); + pStores.clear(); + pPrices.clear(); + pQuantity.clear(); + pImages.clear(); - requestor.deleteObject(pListItemPair.remove(position)); + pNames.add("Total Price"); + pStores.add(""); + pPrices.add("0.00"); + pQuantity.add("-1"); + pImages.add("-1"); + + while(pListItemPair.size() > 1) { + try { + requestor.deleteObject(pListItemPair.remove(1)); + } + catch(Exception e) {} + } + } + else { + totalPriceByStore.put("Kroger", totalPriceByStore.get("Kroger") - (Double.parseDouble(pPrices.get(position)) * Integer.parseInt(pQuantity.get(position)))); + pPrices.set(storeHeaderIndex.get(pStores.get(position)), df.format(totalPriceByStore.get(pStores.get(position)))); + + double newTotal = Double.parseDouble(pPrices.get(0)) - (Double.parseDouble(pPrices.get(position)) * Integer.parseInt(pQuantity.get(position))); + pPrices.set(0, df.format(newTotal)); + + pNames.remove(position); + pStores.remove(position); + pPrices.remove(position); + pQuantity.remove(position); + pImages.remove(position); + + requestor.deleteObject(pListItemPair.remove(position)); + } listView.setAdapter(myAdapter); } @@ -224,36 +298,33 @@ public class ListPage extends AppCompatActivity { if(!pNames.isEmpty()) { name.setText(pNames.get(position)); store.setText(pStores.get(position)); - price.setText(pPrices.get(position)); - quantity.setText(pQuantity.get(position)); - image.setImageResource(pImages.get(position)); + price.setText("$" + pPrices.get(position)); + + if(pQuantity.get(position).equals("-1")) { + quantity.setVisibility(View.INVISIBLE); + decrQuan.setVisibility(View.INVISIBLE); + incrQuan.setVisibility(View.INVISIBLE); + + if(position == 0) { + removeItem.setText("Clear all"); + } + else { + removeItem.setVisibility(View.INVISIBLE); + } + } + else { + quantity.setText(pQuantity.get(position)); + } + + if(pImages.get(position).equals("-1")) { + image.setVisibility(View.INVISIBLE); + } + else { + Glide.with(getContext()).load(pImages.get(position)).into(image); + } } return listproduct; } } - - class ListReceiver implements Requestor.Receiver { - @Override - public void acceptDelivery(T delivered) { - for(ListEntry entry : ((List) delivered).getEntries()) { - int product = entry.getProductID(); - ProductReceiver pr = new ProductReceiver<>(); - requestor.getObject(Integer.toString(product), Item.class, pr); - pQuantity.add(entry.getQuantity().toString()); - pListItemPair.add(entry); - } - } - } - - class ProductReceiver implements Requestor.Receiver { - @Override - public void acceptDelivery(T delivered) { - Item i = (Item) delivered; - pNames.add(i.getDescription()); - pStores.add("Kroger"); - pPrices.add(i.getPrice().toString()); - pImages.add(R.drawable.placeholder); - } - } } diff --git a/Listify/app/src/main/java/com/example/listify/ui/ForgotPasswordPage.java b/Listify/app/src/main/java/com/example/listify/ui/ForgotPasswordPage.java index 7b58531..fe59b9c 100644 --- a/Listify/app/src/main/java/com/example/listify/ui/ForgotPasswordPage.java +++ b/Listify/app/src/main/java/com/example/listify/ui/ForgotPasswordPage.java @@ -84,9 +84,7 @@ public class ForgotPasswordPage extends AppCompatActivity { }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override - public void onClick(DialogInterface dialog, int which) { - return; - } + public void onClick(DialogInterface dialog, int which) {} }); AlertDialog dialog = builder.create(); dialog.show(); diff --git a/Listify/app/src/main/java/com/example/listify/ui/SignupPage.java b/Listify/app/src/main/java/com/example/listify/ui/SignupPage.java index e438f7a..6e1eaeb 100644 --- a/Listify/app/src/main/java/com/example/listify/ui/SignupPage.java +++ b/Listify/app/src/main/java/com/example/listify/ui/SignupPage.java @@ -103,9 +103,7 @@ public class SignupPage extends AppCompatActivity { }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override - public void onClick(DialogInterface dialog, int which) { - return; - } + public void onClick(DialogInterface dialog, int which) {} }); AlertDialog dialog = builder.create(); dialog.show(); diff --git a/Listify/app/src/main/java/com/example/listify/ui/home/HomeFragment.java b/Listify/app/src/main/java/com/example/listify/ui/home/HomeFragment.java index 8eb6840..13ddebe 100644 --- a/Listify/app/src/main/java/com/example/listify/ui/home/HomeFragment.java +++ b/Listify/app/src/main/java/com/example/listify/ui/home/HomeFragment.java @@ -70,9 +70,7 @@ public class HomeFragment extends Fragment { }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override - public void onClick(DialogInterface dialog, int which) { - return; - } + public void onClick(DialogInterface dialog, int which) {} }); AlertDialog dialog = builder.create(); dialog.show(); diff --git a/Listify/app/src/main/res/menu/activity_main_drawer.xml b/Listify/app/src/main/res/menu/activity_main_drawer.xml index 26a2072..b4aec58 100644 --- a/Listify/app/src/main/res/menu/activity_main_drawer.xml +++ b/Listify/app/src/main/res/menu/activity_main_drawer.xml @@ -20,11 +20,6 @@ android:id="@+id/nav_lists" android:icon="@drawable/ic_baseline_list_alt_28" android:title="@string/menu_lists" /> - - - - -