From 87720a9b43c746cfb6098255501314eec0b47f9b Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Wed, 4 Nov 2020 21:05:09 -0800 Subject: [PATCH 1/2] Can now properly display items from multiple stores in shopping list --- .../java/com/example/listify/ListPage.java | 56 +++++++++++++------ 1 file changed, 39 insertions(+), 17 deletions(-) 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 67f894e..d5f48c2 100644 --- a/Listify/app/src/main/java/com/example/listify/ListPage.java +++ b/Listify/app/src/main/java/com/example/listify/ListPage.java @@ -16,6 +16,7 @@ import androidx.appcompat.app.AppCompatActivity; import com.bumptech.glide.Glide; +import com.example.listify.data.Chain; import com.example.listify.data.Item; import com.example.listify.data.List; import com.example.listify.data.ListEntry; @@ -56,7 +57,8 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver { ArrayList pListItemPair = new ArrayList<>(); double totalPrice = 0; - + + HashMap storeID2Name = new HashMap<>(); Map totalPriceByStore = new HashMap<>(); Map storeHeaderIndex = new HashMap<>(); @@ -159,34 +161,54 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver { item = null; } if(item != null) { - if(!totalPriceByStore.containsKey("Kroger")) { - totalPriceByStore.put("Kroger", item.getPrice().doubleValue() * entry.getQuantity()); - storeHeaderIndex.put("Kroger", pNames.size()); + int storeID = (Integer)(item.getChainID()); + if(!storeID2Name.containsKey(storeID)) { + Properties configs = new Properties(); + try { + configs = AuthManager.loadProperties(this, "android.resource://" + getPackageName() + "/raw/auths.json"); + } catch (IOException | JSONException e) { + e.printStackTrace(); + } - pNames.add("Kroger"); + SynchronousReceiver chainReciever = new SynchronousReceiver<>(); + Requestor requestor = new Requestor(am, configs.getProperty("apiKey")); + requestor.getObject(Integer.toString(item.getChainID()), Chain.class, chainReciever); + + try { + storeID2Name.put(storeID, chainReciever.await().getName()); + } + catch (Exception e) { + e.printStackTrace(); + } + } + if(!totalPriceByStore.containsKey(storeID2Name.get(storeID))) { + totalPriceByStore.put(storeID2Name.get(storeID), item.getPrice().doubleValue() * entry.getQuantity()); + storeHeaderIndex.put(storeID2Name.get(storeID), pNames.size()); + + pNames.add(storeID2Name.get(storeID)); pStores.add(""); - pPrices.add(df.format(totalPriceByStore.get("Kroger"))); + pPrices.add(df.format(totalPriceByStore.get(storeID2Name.get(storeID)))); pQuantity.add("-1"); pImages.add("-1"); pListItemPair.add(null); pNames.add(item.getDescription()); - pStores.add("Kroger"); + pStores.add(storeID2Name.get(storeID)); pPrices.add(df.format(item.getPrice())); pQuantity.add(entry.getQuantity().toString()); pImages.add(item.getImageURL()); pListItemPair.add(entry); } else { - int index = storeHeaderIndex.get("Kroger"); + int index = storeHeaderIndex.get(storeID2Name.get(storeID)); - totalPriceByStore.put("Kroger", totalPriceByStore.get("Kroger") + (item.getPrice().doubleValue() * entry.getQuantity())); - pPrices.set(index, df.format(totalPriceByStore.get("Kroger"))); + totalPriceByStore.put(storeID2Name.get(storeID), totalPriceByStore.get(storeID2Name.get(storeID)) + (item.getPrice().doubleValue() * entry.getQuantity())); + pPrices.set(index, df.format(totalPriceByStore.get(storeID2Name.get(storeID)))); index++; pNames.add(index, item.getDescription()); - pStores.add(index, "Kroger"); + pStores.add(index, storeID2Name.get(storeID)); pPrices.add(index, df.format(item.getPrice())); pQuantity.add(index, entry.getQuantity().toString()); pImages.add(index, item.getImageURL()); @@ -319,21 +341,21 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver { removeItem.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - 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)))); + String storeName = pStores.remove(position); - totalPrice -= (Double.parseDouble(pPrices.get(position)) * - Double.parseDouble(pQuantity.get(position))); + totalPriceByStore.put(storeName, totalPriceByStore.get(storeName) - (Double.parseDouble(pPrices.get(position)) * Integer.parseInt(pQuantity.get(position)))); + pPrices.set(storeHeaderIndex.get(storeName), df.format(totalPriceByStore.get(storeName))); + + totalPrice -= (Double.parseDouble(pPrices.get(position)) * Double.parseDouble(pQuantity.get(position))); tvTotalPrice.setText(String.format("$%.2f", totalPrice)); pNames.remove(position); - pStores.remove(position); + //pStores.remove(position); pPrices.remove(position); pQuantity.remove(position); pImages.remove(position); requestor.deleteObject(pListItemPair.remove(position)); - listView.setAdapter(myAdapter); } }); From cc08b02be124d91dffda71323aa5e4f58d8d7454 Mon Sep 17 00:00:00 2001 From: Aaron Sun Date: Wed, 4 Nov 2020 21:34:38 -0800 Subject: [PATCH 2/2] Shrunk the item name text in shopping list --- Listify/app/src/main/res/layout/activity_listproductentry.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Listify/app/src/main/res/layout/activity_listproductentry.xml b/Listify/app/src/main/res/layout/activity_listproductentry.xml index 96a6fed..b6ac529 100644 --- a/Listify/app/src/main/res/layout/activity_listproductentry.xml +++ b/Listify/app/src/main/res/layout/activity_listproductentry.xml @@ -64,7 +64,7 @@ android:layout_marginBottom="100dp" android:padding="30dp" android:text="Product Name" - android:textSize="20sp" + android:textSize="15sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@+id/imageView" app:layout_constraintTop_toTopOf="parent" />