@Override protected void onResume() { super.onResume(); updateBaseGraph(); if (selectedItems != null) { deleteAndQuantityButtonsEnabled(); } else { deleteAndQuantityButtonsDisabled(); } // Get the previous history for the user // if it exists, then pass it to Graph labels to draw goals if (pcart.getCalories() != 0.0f) { advisor.setPastCart(pcart); } advisor.setCurrCart(ccartTotals); advisor.setRecDailyValues(totalRDV); advisor.setDays(days); LayoutInflater inflater = getLayoutInflater(); ViewGroup toastRoot = (ViewGroup) findViewById(R.id.toast_layout_root); ViewGroup toastRoot2 = (ViewGroup) findViewById(R.id.toast_layout_root2); View layout = inflater.inflate(R.layout.toast_layout, toastRoot); View layout2 = inflater.inflate(R.layout.toast_layout2, toastRoot2); advisor.giveNegativeDialogAdvice(CartActivity.this, layout); advisor.givePositiveDialogAdvice(CartActivity.this, layout2); }
public void performAction(View view) { PreviousHistory cTotals = getCartTotalsFor(currentUsername); if (cTotals.getCalories() != 0) { showCheckoutDialog(cTotals); } else { Toast noItemsInCart = Toast.makeText( CartActivity.this, "You have no items in your cart yet!", Toast.LENGTH_SHORT); noItemsInCart.show(); } }
/** UPDATE BASE GRAPH with new Cart totals */ private void updateBaseGraph() { // get an updated version of the adb on resume each time adb = new AccountDatabaseHelper(this); ccart = adb.getAllGroceryItemsOf(currentUsername); int groceryCount = adb.getGroceryCountFor(currentUsername); if (groceryCount == 1) { added.setText(groceryCount + " ITEM IN CART"); } else if (groceryCount == 0 || groceryCount > 1) { added.setText(groceryCount + " ITEMS IN CART"); } adb.close(); if (ccart != null) { setupItemDrawer(); } passedIntent = getIntent(); results = passedIntent.getStringExtra("results"); int check = passedIntent.getIntExtra("check", 0); if (check == 1) { Log.d("CartActivity", "This is results " + results); if (results != null && !results.equals("e")) { updateGraphWithSelected(results); } else { Toast noData = Toast.makeText( CartActivity.this, "Sorry, we couldn't find the nutrition data for this item", Toast.LENGTH_LONG); noData.show(); } } ccartTotals = getCartTotalsFor(currentUsername); totalRDV = getRDVTotalsFor(currentUsername); if (pcart.getCalories() != 0.0f) { graph.getRatiosWithPCart(ccartTotals, totalRDV, pcart); } else { graph.getRatiosWithoutPCart(ccartTotals, totalRDV); } graph.postInvalidate(); graphLabels.setDays(days); graphLabels.postInvalidate(); }
/** TOTAL VALUES Add up all of the values of the current_cart items */ public PreviousHistory getCartTotalsFor(String username) { NutritionDatabaseHelper ndb = new NutritionDatabaseHelper(this); AccountDatabaseHelper adb = new AccountDatabaseHelper(this); List<GroceryItem> allGItems = adb.getAllGroceryItemsOf(username); PreviousHistory cartTotals = new PreviousHistory(); if (allGItems != null) { cartTotals.setId(-1); cartTotals.setUsername(username); for (int i = 0; i < allGItems.size(); i++) { // retrieve the grocery item from the array GroceryItem tempGrocery = allGItems.get(i); // get the Quantity of the item int quantity = tempGrocery.getQuantity(); // locate the item in the nutrition database Item tempItem = ndb.getItem(tempGrocery.getItemName()); // add the totals to the current cartTotal, remembering // to multiply by the quantity! cartTotals.setCalories(cartTotals.getCalories() + (tempItem.getCalories() * quantity)); cartTotals.setProtein(cartTotals.getProtein() + (tempItem.getProtein() * quantity)); cartTotals.setFat(cartTotals.getFat() + (tempItem.getFat() * quantity)); cartTotals.setCarbohydrate( cartTotals.getCarbohydrate() + (tempItem.getCarbohydrate() * quantity)); cartTotals.setFiber(cartTotals.getFiber() + (tempItem.getFiber() * quantity)); cartTotals.setSugar(cartTotals.getSugar() + (tempItem.getSugar() * quantity)); cartTotals.setCalcium(cartTotals.getCalcium() + (tempItem.getCalcium() * quantity)); cartTotals.setIron(cartTotals.getIron() + (tempItem.getIron() * quantity)); cartTotals.setMagnesium(cartTotals.getMagnesium() + (tempItem.getMagnesium() * quantity)); cartTotals.setPotassium(cartTotals.getPotassium() + (tempItem.getPotassium() * quantity)); cartTotals.setSodium(cartTotals.getSodium() + (tempItem.getSodium() * quantity)); cartTotals.setZinc(cartTotals.getZinc() + (tempItem.getZinc() * quantity)); cartTotals.setVitC(cartTotals.getVitC() + (tempItem.getVitC() * quantity)); cartTotals.setVitB6(cartTotals.getVitB6() + (tempItem.getVitB6() * quantity)); cartTotals.setVitB12(cartTotals.getVitB12() + (tempItem.getVitB12() * quantity)); cartTotals.setVitA(cartTotals.getVitA() + (tempItem.getVitA() * quantity)); cartTotals.setVitE(cartTotals.getVitE() + (tempItem.getVitE() * quantity)); cartTotals.setVitD(cartTotals.getVitD() + (tempItem.getVitD() * quantity)); cartTotals.setVitK(cartTotals.getVitK() + (tempItem.getVitK() * quantity)); cartTotals.setFatSat(cartTotals.getFatSat() + (tempItem.getFatSat() * quantity)); cartTotals.setFatMono(cartTotals.getFatMono() + (tempItem.getFatMono() * quantity)); cartTotals.setFatPoly(cartTotals.getFatPoly() + (tempItem.getFatPoly() * quantity)); cartTotals.setCholesterol( cartTotals.getCholesterol() + (tempItem.getCholesterol() * quantity)); cartTotals.setDays(days); } // Log.d("Created: ", "Cart Total for : " + cartTotals.getUsername() // + "Total Calories: " + cartTotals.getCalories()); adb.close(); ndb.close(); return cartTotals; } ndb.close(); adb.close(); PreviousHistory emptyHistory = new PreviousHistory(); emptyHistory.setUsername(currentUsername); return emptyHistory; }