mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 20:00:50 +00:00
139 lines
4.7 KiB
Java
139 lines
4.7 KiB
Java
package com.capstone.foodify.Adapter;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.ImageView;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import com.capstone.foodify.Activity.FoodDetailActivity;
|
|
import com.capstone.foodify.Common;
|
|
import com.capstone.foodify.Model.Basket;
|
|
import com.capstone.foodify.Model.Food;
|
|
import com.capstone.foodify.R;
|
|
import com.squareup.picasso.Picasso;
|
|
import com.thecode.aestheticdialogs.AestheticDialog;
|
|
import com.thecode.aestheticdialogs.DialogAnimation;
|
|
import com.thecode.aestheticdialogs.DialogStyle;
|
|
import com.thecode.aestheticdialogs.DialogType;
|
|
|
|
import java.util.List;
|
|
|
|
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.FoodViewHolder>{
|
|
|
|
private List<Food> listFood;
|
|
private Context context;
|
|
private Activity activity;
|
|
|
|
public FoodAdapter(Context context){
|
|
this.context = context;
|
|
}
|
|
|
|
|
|
public FoodAdapter() {}
|
|
public void setData(List<Food> list) {
|
|
this.listFood = list;
|
|
notifyDataSetChanged();
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public FoodViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_food, parent, false);
|
|
return new FoodViewHolder(view);
|
|
}
|
|
|
|
@Override
|
|
public void onBindViewHolder(@NonNull FoodViewHolder holder, int position) {
|
|
Food food = listFood.get(position);
|
|
if(food == null){
|
|
return;
|
|
}
|
|
|
|
holder.name.setText(food.getName());
|
|
|
|
//When food does not have image
|
|
if(food.getImages().size() == 0){
|
|
holder.image.setImageResource(R.drawable.default_image_food);
|
|
} else {
|
|
Picasso.get().load(food.getImages().get(0).getImageUrl()).into(holder.image);
|
|
}
|
|
|
|
//Check value discountPercent
|
|
float cost;
|
|
if(food.getDiscountPercent() > 0){
|
|
|
|
//Calculate final cost when apply discountPercent
|
|
cost = food.getCost() - (food.getCost() * food.getDiscountPercent()/100);
|
|
|
|
//Show discountPercent value on screen
|
|
holder.discount.setVisibility(View.VISIBLE);
|
|
holder.discount.setText("-" + food.getDiscountPercent()+ "%");
|
|
} else {
|
|
cost = food.getCost();
|
|
}
|
|
|
|
holder.price.setText(Common.changeCurrencyUnit(cost));
|
|
|
|
holder.basket_icon.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
if(Common.FINAL_SHOP == null && Common.LIST_BASKET_FOOD.size() == 0){
|
|
Common.FINAL_SHOP = food.getShop().getName();
|
|
}
|
|
|
|
if(Common.FINAL_SHOP.equals(food.getShop().getName())){
|
|
Common.addFoodToBasket(food);
|
|
Toast.makeText(context, "Đã thêm " + food.getName() + " vào giỏ hàng!", Toast.LENGTH_SHORT).show();
|
|
} else {
|
|
new AestheticDialog.Builder((Activity) context, DialogStyle.FLAT, DialogType.INFO)
|
|
.setTitle("Thông báo!")
|
|
.setMessage("Bạn chỉ có thể đặt những món ăn cùng cửa hàng, xin vui lòng kiểm tra lại giỏ hàng!")
|
|
.setAnimation(DialogAnimation.SHRINK)
|
|
.setCancelable(true)
|
|
.show();
|
|
}
|
|
}
|
|
});
|
|
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
Intent intent = new Intent(context, FoodDetailActivity.class);
|
|
intent.putExtra("FoodId", food.getId());
|
|
context.startActivity(intent);
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
if(listFood != null) {
|
|
return listFood.size();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public class FoodViewHolder extends RecyclerView.ViewHolder{
|
|
|
|
private ImageView image, basket_icon;
|
|
private TextView name, price, discount;
|
|
|
|
public FoodViewHolder(@NonNull View itemView) {
|
|
super(itemView);
|
|
|
|
image = itemView.findViewById(R.id.image_view);
|
|
name = itemView.findViewById(R.id.title);
|
|
price = itemView.findViewById(R.id.price);
|
|
discount = itemView.findViewById(R.id.discount);
|
|
basket_icon = itemView.findViewById(R.id.basket_icon);
|
|
}
|
|
}
|
|
}
|