Merge pull request #106 from ClaytonWWilson/aaron-branch-2

Aaron branch 2
This commit is contained in:
Aaron Sun 2020-11-04 21:38:37 -08:00 committed by GitHub
commit b490945f34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 18 deletions

View File

@ -16,6 +16,7 @@ import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.example.listify.data.Chain;
import com.example.listify.data.Item; import com.example.listify.data.Item;
import com.example.listify.data.List; import com.example.listify.data.List;
import com.example.listify.data.ListEntry; import com.example.listify.data.ListEntry;
@ -57,6 +58,7 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver {
double totalPrice = 0; double totalPrice = 0;
HashMap<Integer, String> storeID2Name = new HashMap<>();
Map<String, Double> totalPriceByStore = new HashMap<>(); Map<String, Double> totalPriceByStore = new HashMap<>();
Map<String, Integer> storeHeaderIndex = new HashMap<>(); Map<String, Integer> storeHeaderIndex = new HashMap<>();
@ -159,34 +161,54 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver {
item = null; item = null;
} }
if(item != null) { if(item != null) {
if(!totalPriceByStore.containsKey("Kroger")) { int storeID = (Integer)(item.getChainID());
totalPriceByStore.put("Kroger", item.getPrice().doubleValue() * entry.getQuantity()); if(!storeID2Name.containsKey(storeID)) {
storeHeaderIndex.put("Kroger", pNames.size()); 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<Chain> 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(""); pStores.add("");
pPrices.add(df.format(totalPriceByStore.get("Kroger"))); pPrices.add(df.format(totalPriceByStore.get(storeID2Name.get(storeID))));
pQuantity.add("-1"); pQuantity.add("-1");
pImages.add("-1"); pImages.add("-1");
pListItemPair.add(null); pListItemPair.add(null);
pNames.add(item.getDescription()); pNames.add(item.getDescription());
pStores.add("Kroger"); pStores.add(storeID2Name.get(storeID));
pPrices.add(df.format(item.getPrice())); pPrices.add(df.format(item.getPrice()));
pQuantity.add(entry.getQuantity().toString()); pQuantity.add(entry.getQuantity().toString());
pImages.add(item.getImageURL()); pImages.add(item.getImageURL());
pListItemPair.add(entry); pListItemPair.add(entry);
} }
else { else {
int index = storeHeaderIndex.get("Kroger"); int index = storeHeaderIndex.get(storeID2Name.get(storeID));
totalPriceByStore.put("Kroger", totalPriceByStore.get("Kroger") + (item.getPrice().doubleValue() * entry.getQuantity())); totalPriceByStore.put(storeID2Name.get(storeID), totalPriceByStore.get(storeID2Name.get(storeID)) + (item.getPrice().doubleValue() * entry.getQuantity()));
pPrices.set(index, df.format(totalPriceByStore.get("Kroger"))); pPrices.set(index, df.format(totalPriceByStore.get(storeID2Name.get(storeID))));
index++; index++;
pNames.add(index, item.getDescription()); pNames.add(index, item.getDescription());
pStores.add(index, "Kroger"); pStores.add(index, storeID2Name.get(storeID));
pPrices.add(index, df.format(item.getPrice())); pPrices.add(index, df.format(item.getPrice()));
pQuantity.add(index, entry.getQuantity().toString()); pQuantity.add(index, entry.getQuantity().toString());
pImages.add(index, item.getImageURL()); pImages.add(index, item.getImageURL());
@ -319,21 +341,21 @@ public class ListPage extends AppCompatActivity implements Requestor.Receiver {
removeItem.setOnClickListener(new View.OnClickListener() { removeItem.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
totalPriceByStore.put("Kroger", totalPriceByStore.get("Kroger") - (Double.parseDouble(pPrices.get(position)) * Integer.parseInt(pQuantity.get(position)))); String storeName = pStores.remove(position);
pPrices.set(storeHeaderIndex.get(pStores.get(position)), df.format(totalPriceByStore.get(pStores.get(position))));
totalPrice -= (Double.parseDouble(pPrices.get(position)) * totalPriceByStore.put(storeName, totalPriceByStore.get(storeName) - (Double.parseDouble(pPrices.get(position)) * Integer.parseInt(pQuantity.get(position))));
Double.parseDouble(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)); tvTotalPrice.setText(String.format("$%.2f", totalPrice));
pNames.remove(position); pNames.remove(position);
pStores.remove(position); //pStores.remove(position);
pPrices.remove(position); pPrices.remove(position);
pQuantity.remove(position); pQuantity.remove(position);
pImages.remove(position); pImages.remove(position);
requestor.deleteObject(pListItemPair.remove(position)); requestor.deleteObject(pListItemPair.remove(position));
listView.setAdapter(myAdapter); listView.setAdapter(myAdapter);
} }
}); });

View File

@ -64,7 +64,7 @@
android:layout_marginBottom="100dp" android:layout_marginBottom="100dp"
android:padding="30dp" android:padding="30dp"
android:text="Product Name" android:text="Product Name"
android:textSize="20sp" android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView" app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />