mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 20:00:50 +00:00
Add favorite food
This commit is contained in:
@@ -56,7 +56,13 @@ public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.FoodViewHolder
|
||||
} else {
|
||||
holder.name.setText(foodName);
|
||||
}
|
||||
Picasso.get().load(food.getImages().get(0).getImageUrl()).into(holder.image);
|
||||
|
||||
//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 = 0;
|
||||
|
||||
@@ -13,8 +13,12 @@ import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.capstone.foodify.API.FoodApiToken;
|
||||
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 java.util.List;
|
||||
|
||||
@@ -25,8 +29,11 @@ import retrofit2.Response;
|
||||
public class FoodFavoriteAdapter extends RecyclerView.Adapter<FoodFavoriteAdapter.FoodFavoriteViewHolder> {
|
||||
|
||||
private List<Food> listFavoriteFood;
|
||||
private Context context;
|
||||
|
||||
public FoodFavoriteAdapter(){}
|
||||
public FoodFavoriteAdapter(Context context){
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void setData(List<Food> listFavoriteFood){
|
||||
this.listFavoriteFood = listFavoriteFood;
|
||||
@@ -45,10 +52,67 @@ public class FoodFavoriteAdapter extends RecyclerView.Adapter<FoodFavoriteAdapt
|
||||
if(food == null)
|
||||
return;
|
||||
|
||||
// holder.foodName.setText(food.getName());
|
||||
// holder.shopName.setText(food.getShopName());
|
||||
// holder.price.setText(food.getPrice());
|
||||
// Picasso.get().load(food.getImg()).into(holder.imageView);
|
||||
holder.foodName.setText(food.getName());
|
||||
holder.shopName.setText(food.getShop().getName());
|
||||
|
||||
//Check value discountPercent
|
||||
float cost = 0;
|
||||
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));
|
||||
|
||||
//When food does not have image
|
||||
if(food.getImages().size() == 0){
|
||||
holder.imageView.setImageResource(R.drawable.default_image_food);
|
||||
} else {
|
||||
Picasso.get().load(food.getImages().get(0).getImageUrl()).into(holder.imageView);
|
||||
}
|
||||
|
||||
//Set event basket icon
|
||||
holder.basket_icon.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Basket foodInBasket = Common.getFoodExistInBasket(food.getId());
|
||||
if(foodInBasket == null){
|
||||
//If item is not exist in basket
|
||||
|
||||
//Check food image is null or not
|
||||
String imageTemp = null;
|
||||
if(food.getImages().size() > 0)
|
||||
imageTemp = food.getImages().get(0).getImageUrl();
|
||||
|
||||
Common.LIST_BASKET_FOOD.add(new Basket(food.getId(), imageTemp, food.getName(), food.getCost(), food.getShop().getName(),
|
||||
"1", food.getDiscountPercent()));
|
||||
} else {
|
||||
//If item is exist in basket
|
||||
|
||||
for(int i = 0; i < Common.LIST_BASKET_FOOD.size(); i++){
|
||||
String foodId = Common.LIST_BASKET_FOOD.get(i).getId();
|
||||
//Find foodId exist
|
||||
if(foodId.equals(food.getId())){
|
||||
//Get quantity food from basket
|
||||
String quantity = Common.LIST_BASKET_FOOD.get(i).getQuantity();
|
||||
|
||||
//Change quantity food from basket
|
||||
int quantityInt = Integer.parseInt(quantity) + 1;
|
||||
Common.LIST_BASKET_FOOD.get(i).setQuantity(String.valueOf(quantityInt));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Toast.makeText(context, "Đã thêm " + food.getName() + " vào giỏ hàng!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -61,9 +125,9 @@ public class FoodFavoriteAdapter extends RecyclerView.Adapter<FoodFavoriteAdapt
|
||||
|
||||
public static class FoodFavoriteViewHolder extends RecyclerView.ViewHolder{
|
||||
|
||||
TextView foodName, shopName, price;
|
||||
TextView foodName, shopName, price, discount;
|
||||
public LinearLayout layoutForeGround;
|
||||
ImageView imageView;
|
||||
ImageView imageView, basket_icon;
|
||||
|
||||
public FoodFavoriteViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
@@ -72,42 +136,45 @@ public class FoodFavoriteAdapter extends RecyclerView.Adapter<FoodFavoriteAdapt
|
||||
shopName = itemView.findViewById(R.id.shop_name_text_view);
|
||||
price = itemView.findViewById(R.id.price_text_view);
|
||||
imageView = itemView.findViewById(R.id.image_view);
|
||||
discount = itemView.findViewById(R.id.discount);
|
||||
|
||||
basket_icon = itemView.findViewById(R.id.basket_icon);
|
||||
|
||||
layoutForeGround = itemView.findViewById(R.id.layout_foreground);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItem(int index, Context context){
|
||||
public void removeItem(Food food, int index, Context context){
|
||||
listFavoriteFood.remove(index);
|
||||
notifyItemRemoved(index);
|
||||
|
||||
// FoodApiToken.apiService.removeFoodFromFavorite(index).enqueue(new Callback<Food>() {
|
||||
// @Override
|
||||
// public void onResponse(Call<Food> call, Response<Food> response) {
|
||||
// Toast.makeText(context, "Remove successful!", Toast.LENGTH_SHORT).show();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onFailure(Call<Food> call, Throwable t) {
|
||||
// Toast.makeText(context, "Error: " + t, Toast.LENGTH_SHORT).show();
|
||||
// }
|
||||
// });
|
||||
FoodApiToken.apiService.removeFoodFromFavorite(Common.CURRENT_USER.getId(), Integer.parseInt(food.getId())).enqueue(new Callback<Food>() {
|
||||
@Override
|
||||
public void onResponse(Call<Food> call, Response<Food> response) {
|
||||
Toast.makeText(context, "Đã xoá " + food.getName() + " khỏi danh sách yêu thích!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Food> call, Throwable t) {
|
||||
Toast.makeText(context, "Oops! Đã có lỗi, vui lòng thử lại sau!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void undoItem(Food food, int index, Context context){
|
||||
listFavoriteFood.add(index, food);
|
||||
notifyItemInserted(index);
|
||||
|
||||
// FoodApiToken.apiService.addFoodToFavorite(food.getId()).enqueue(new Callback<Food>() {
|
||||
// @Override
|
||||
// public void onResponse(@NonNull Call<Food> call, @NonNull Response<Food> response) {
|
||||
// Toast.makeText(context, "Add successful!", Toast.LENGTH_SHORT).show();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onFailure(@NonNull Call<Food> call, @NonNull Throwable t) {
|
||||
// Toast.makeText(context, "Error: " + t, Toast.LENGTH_SHORT).show();
|
||||
// }
|
||||
// });
|
||||
FoodApiToken.apiService.addFoodToFavorite(Common.CURRENT_USER.getId(), Integer.parseInt(food.getId())).enqueue(new Callback<Food>() {
|
||||
@Override
|
||||
public void onResponse(Call<Food> call, Response<Food> response) {
|
||||
Toast.makeText(context, "Đã thêm " + food.getName() + " vào danh sách yêu thích!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Food> call, Throwable t) {
|
||||
Toast.makeText(context, "Oops! Đã có lỗi, vui lòng thử lại sau!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user