mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 20:00:50 +00:00
Auto update total price in basket
This commit is contained in:
@@ -121,12 +121,10 @@ public class FoodDetailActivity extends AppCompatActivity {
|
||||
@SuppressLint("SetTextI18n")
|
||||
@Override
|
||||
public void onValueChanged(int i) {
|
||||
Locale locale = new Locale("vi", "VN");
|
||||
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
|
||||
|
||||
if(totalPrice >= 0)
|
||||
totalPrice = price * i;
|
||||
add_to_basket_button.setText("Add to basket - " + fmt.format(totalPrice));
|
||||
add_to_basket_button.setText("Add to basket - " + Common.changeCurrencyUnit(totalPrice));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -156,8 +154,8 @@ public class FoodDetailActivity extends AppCompatActivity {
|
||||
add_to_basket_button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Common.LIST_BASKET_FOOD.add(new Basket(food.getId(), imageFood.get(0), food.getName(), food.getPrice(), food.getShopName(),
|
||||
String.valueOf(horizontalQuantitizer.getValue())));
|
||||
Common.LIST_BASKET_FOOD.add(new Basket(food.getId(), imageFood.get(0), food.getName(), food.getPrice(), "Shop Name",
|
||||
String.valueOf(horizontalQuantitizer.getValue()), "0"));
|
||||
Toast.makeText(FoodDetailActivity.this, "Thêm vào giỏ hàng thành công!", Toast.LENGTH_SHORT).show();
|
||||
startActivity(new Intent(FoodDetailActivity.this, MainActivity.class));
|
||||
}
|
||||
@@ -215,19 +213,16 @@ public class FoodDetailActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
private void initData(){
|
||||
Locale locale = new Locale("vi", "VN");
|
||||
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
|
||||
|
||||
price = Float.parseFloat(food.getPrice());
|
||||
|
||||
foodName_txt.setText(food.getName());
|
||||
shopName_txt.setText("Shop Name");
|
||||
price_txt.setText(fmt.format(Float.parseFloat(food.getPrice())));
|
||||
price_txt.setText(Common.changeCurrencyUnit(Float.parseFloat(food.getPrice())));
|
||||
discount_txt.setText("-" + food.getDiscount() + "%");
|
||||
description_content_txt.setText(food.getDescription());
|
||||
countRating_txt.setText(food.getReviewCount() + " rating");
|
||||
|
||||
add_to_basket_button.setText("ADD TO BASKET - " + fmt.format(0));
|
||||
add_to_basket_button.setText("ADD TO BASKET - " + Common.changeCurrencyUnit(0));
|
||||
|
||||
List<SlideModel> slideModels = new ArrayList<>();
|
||||
if(imageFood != null){
|
||||
|
||||
@@ -2,9 +2,17 @@ package com.capstone.foodify;
|
||||
|
||||
import com.capstone.foodify.Model.Basket.Basket;
|
||||
|
||||
import java.text.NumberFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class Common {
|
||||
public static List<Basket> LIST_BASKET_FOOD = new ArrayList<>();
|
||||
|
||||
public static String changeCurrencyUnit(float price){
|
||||
Locale locale = new Locale("vi", "VN");
|
||||
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
|
||||
return fmt.format(price);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.recyclerview.widget.DividerItemDecoration;
|
||||
@@ -31,6 +32,7 @@ public class BasketFragment extends Fragment implements ItemTouchHelperListener
|
||||
private List<Basket> listBasketFood = new ArrayList<>();
|
||||
private RelativeLayout listBasketFoodView;
|
||||
|
||||
public TextView total;
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
@@ -40,7 +42,11 @@ public class BasketFragment extends Fragment implements ItemTouchHelperListener
|
||||
//Init Component
|
||||
recyclerView_basket_food = view.findViewById(R.id.recycler_view_basket_food);
|
||||
listBasketFoodView = view.findViewById(R.id.list_basket_food_view);
|
||||
adapter = new BasketAdapter();
|
||||
adapter = new BasketAdapter(this);
|
||||
total = view.findViewById(R.id.total_text_view);
|
||||
|
||||
//Init data
|
||||
total.setText(Common.changeCurrencyUnit(0));
|
||||
|
||||
//Get list food in basket
|
||||
getListBasketFood();
|
||||
@@ -59,6 +65,7 @@ public class BasketFragment extends Fragment implements ItemTouchHelperListener
|
||||
}
|
||||
|
||||
private void getListBasketFood(){
|
||||
//Get list food from basket
|
||||
listBasketFood = Common.LIST_BASKET_FOOD;
|
||||
adapter.setData(listBasketFood);
|
||||
recyclerView_basket_food.setAdapter(adapter);
|
||||
|
||||
@@ -7,14 +7,16 @@ public class Basket {
|
||||
private String price;
|
||||
private String shopName;
|
||||
private String quantity;
|
||||
private String discount;
|
||||
|
||||
public Basket(String id, String img, String name, String price, String shopName, String quantity) {
|
||||
public Basket(String id, String img, String name, String price, String shopName, String quantity, String discount) {
|
||||
this.id = id;
|
||||
this.img = img;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
this.shopName = shopName;
|
||||
this.quantity = quantity;
|
||||
this.discount = discount;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
@@ -64,4 +66,11 @@ public class Basket {
|
||||
public void setQuantity(String quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
public String getDiscount() {
|
||||
return discount;
|
||||
}
|
||||
|
||||
public void setDiscount(String discount) {
|
||||
this.discount = discount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,24 +8,28 @@ import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.capstone.foodify.API.FoodApi;
|
||||
import com.capstone.foodify.Model.Food.Food;
|
||||
import com.capstone.foodify.Common;
|
||||
import com.capstone.foodify.Fragment.BasketFragment;
|
||||
import com.capstone.foodify.R;
|
||||
import com.cepheuen.elegantnumberbutton.view.ElegantNumberButton;
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
import java.text.NumberFormat;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class BasketAdapter extends RecyclerView.Adapter<BasketAdapter.BasketViewHolder> {
|
||||
|
||||
public List<Basket> listBasketFood;
|
||||
public List<Basket> listBasketFood;
|
||||
|
||||
public BasketAdapter() {
|
||||
private final BasketFragment basketFragment;
|
||||
|
||||
public BasketAdapter(BasketFragment basketFragment) {
|
||||
this.basketFragment = basketFragment;
|
||||
}
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
@@ -51,19 +55,34 @@ public class BasketAdapter extends RecyclerView.Adapter<BasketAdapter.BasketView
|
||||
//Init data
|
||||
holder.foodName.setText(foodBasket.getName());
|
||||
holder.shopName.setText(foodBasket.getShopName());
|
||||
holder.price.setText(foodBasket.getPrice());
|
||||
holder.price.setText(Common.changeCurrencyUnit(Float.parseFloat(foodBasket.getPrice())));
|
||||
Picasso.get().load(foodBasket.getImg()).into(holder.imageView);
|
||||
holder.quantity.setNumber(foodBasket.getQuantity());
|
||||
|
||||
//Calculate total price
|
||||
calculateTotalPrice();
|
||||
|
||||
//Update quantity food when change quantity number
|
||||
holder.quantity.setOnValueChangeListener(new ElegantNumberButton.OnValueChangeListener() {
|
||||
@Override
|
||||
public void onValueChange(ElegantNumberButton view, int oldValue, int newValue) {
|
||||
foodBasket.setQuantity(String.valueOf(newValue));
|
||||
|
||||
//Update total price
|
||||
calculateTotalPrice();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void calculateTotalPrice() {
|
||||
float total = 0;
|
||||
|
||||
for(Basket foodTemp: listBasketFood){
|
||||
total += (Float.parseFloat(foodTemp.getPrice())) * (Float.parseFloat(foodTemp.getQuantity())) * (100 - Long.parseLong(foodTemp.getDiscount()))/100;
|
||||
}
|
||||
basketFragment.total.setText(Common.changeCurrencyUnit(total));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
if(listBasketFood != null)
|
||||
@@ -73,7 +92,7 @@ public class BasketAdapter extends RecyclerView.Adapter<BasketAdapter.BasketView
|
||||
|
||||
public static class BasketViewHolder extends RecyclerView.ViewHolder{
|
||||
|
||||
TextView foodName, shopName, price;
|
||||
TextView foodName, shopName, price, total;
|
||||
public LinearLayout layoutForeGround;
|
||||
ImageView imageView;
|
||||
ElegantNumberButton quantity;
|
||||
@@ -87,6 +106,7 @@ public class BasketAdapter extends RecyclerView.Adapter<BasketAdapter.BasketView
|
||||
price = itemView.findViewById(R.id.price_text_view);
|
||||
imageView = itemView.findViewById(R.id.image_view);
|
||||
quantity = itemView.findViewById(R.id.quantity);
|
||||
total = itemView.findViewById(R.id.total_text_view);
|
||||
|
||||
layoutForeGround = itemView.findViewById(R.id.layout_foreground);
|
||||
}
|
||||
@@ -95,10 +115,14 @@ public class BasketAdapter extends RecyclerView.Adapter<BasketAdapter.BasketView
|
||||
public void removeItem(int index, Context context){
|
||||
listBasketFood.remove(index);
|
||||
notifyItemRemoved(index);
|
||||
|
||||
calculateTotalPrice();
|
||||
}
|
||||
|
||||
public void undoItem(Basket food, int index, Context context){
|
||||
listBasketFood.add(index, food);
|
||||
notifyItemInserted(index);
|
||||
|
||||
calculateTotalPrice();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.capstone.foodify.Activity.FoodDetailActivity;
|
||||
import com.capstone.foodify.Common;
|
||||
import com.capstone.foodify.R;
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
@@ -49,9 +50,6 @@ public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.FoodViewHolder
|
||||
return;
|
||||
}
|
||||
|
||||
Locale locale = new Locale("vi", "VN");
|
||||
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
|
||||
|
||||
String foodName = food.getName();
|
||||
if(foodName.length() >= 30){
|
||||
StringBuilder stringBuilder = new StringBuilder(food.getName());
|
||||
@@ -61,7 +59,7 @@ public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.FoodViewHolder
|
||||
holder.name.setText(foodName);
|
||||
}
|
||||
Picasso.get().load(food.getImg()).into(holder.image);
|
||||
holder.price.setText(fmt.format(Float.parseFloat(food.getPrice())));
|
||||
holder.price.setText(Common.changeCurrencyUnit(Float.parseFloat(food.getPrice())));
|
||||
|
||||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,7 @@ import android.widget.TextView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.capstone.foodify.Common;
|
||||
import com.capstone.foodify.R;
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
@@ -57,10 +58,7 @@ public class FoodSearchAdapter extends RecyclerView.Adapter<FoodSearchAdapter.Fo
|
||||
Picasso.get().load(food.getImg()).into(holder.image);
|
||||
|
||||
|
||||
Locale locale = new Locale("vi", "VN");
|
||||
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
|
||||
|
||||
holder.price.setText(fmt.format(Float.parseFloat(food.getPrice())));
|
||||
holder.price.setText(Common.changeCurrencyUnit(Float.parseFloat(food.getPrice())));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -55,7 +55,7 @@
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/total"
|
||||
android:id="@+id/total_text_view"
|
||||
android:text="$10,000"
|
||||
android:fontFamily="@font/opensans"
|
||||
android:textSize="24sp"
|
||||
|
||||
Reference in New Issue
Block a user