mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 20:00:50 +00:00
Show list favorite from api
This commit is contained in:
@@ -12,7 +12,10 @@ import java.util.List;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
import retrofit2.http.Body;
|
||||
import retrofit2.http.DELETE;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.POST;
|
||||
import retrofit2.http.Path;
|
||||
import retrofit2.http.Query;
|
||||
|
||||
@@ -55,4 +58,13 @@ public interface FoodApi {
|
||||
|
||||
@GET("district")
|
||||
Call<List<DistrictWardResponse>> districtResponse();
|
||||
|
||||
@GET("wishlist")
|
||||
Call<List<Food>> getListFavoriteFood();
|
||||
|
||||
@POST("wishlist")
|
||||
Call<Food> addFoodToFavorite(@Body Food food);
|
||||
|
||||
@DELETE("wishlist/{id}")
|
||||
Call<Food> removeFoodFromFavorite(@Path("id") int id);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.capstone.foodify.Fragment;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.widget.NestedScrollView;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.recyclerview.widget.DividerItemDecoration;
|
||||
@@ -13,7 +14,9 @@ import androidx.recyclerview.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.capstone.foodify.API.FoodApi;
|
||||
import com.capstone.foodify.ItemTouchHelperListener;
|
||||
import com.capstone.foodify.Model.Food.Food;
|
||||
import com.capstone.foodify.Model.Food.FoodFavoriteAdapter;
|
||||
@@ -22,14 +25,21 @@ import com.capstone.foodify.RecyclerViewItemTouchHelper;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class FavouriteFragment extends Fragment implements ItemTouchHelperListener {
|
||||
|
||||
private RecyclerView recyclerView_favorite_food;
|
||||
private FoodFavoriteAdapter adapter;
|
||||
private List<Food> listFavoriteFood = new ArrayList<>();
|
||||
private final List<Food> listFavoriteFood = new ArrayList<>();
|
||||
|
||||
private NestedScrollView listFavoriteFoodView;
|
||||
@Override
|
||||
@@ -41,12 +51,14 @@ public class FavouriteFragment extends Fragment implements ItemTouchHelperListen
|
||||
recyclerView_favorite_food = rootView.findViewById(R.id.recycler_view_favorite_food);
|
||||
listFavoriteFoodView = rootView.findViewById(R.id.list_favorite_food_view);
|
||||
|
||||
//Get list favorite food
|
||||
getListFavoriteFood();
|
||||
|
||||
//Set layout recyclerview
|
||||
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
|
||||
recyclerView_favorite_food.setLayoutManager(linearLayoutManager);
|
||||
adapter = new FoodFavoriteAdapter();
|
||||
|
||||
listFavoriteFood = getListFavoriteFood();
|
||||
adapter = new FoodFavoriteAdapter(getListFavoriteFood());
|
||||
recyclerView_favorite_food.setAdapter(adapter);
|
||||
|
||||
RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL);
|
||||
recyclerView_favorite_food.addItemDecoration(itemDecoration);
|
||||
@@ -56,14 +68,26 @@ public class FavouriteFragment extends Fragment implements ItemTouchHelperListen
|
||||
return rootView;
|
||||
}
|
||||
|
||||
private List<Food> getListFavoriteFood() {
|
||||
List<Food> listFoodTemp = new ArrayList<>();
|
||||
private void getListFavoriteFood() {
|
||||
//Get data from api
|
||||
FoodApi.apiService.getListFavoriteFood().enqueue(new Callback<List<Food>>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<List<Food>> call, @NonNull Response<List<Food>> response) {
|
||||
List<Food> listFoodData = response.body();
|
||||
if(listFoodData != null){
|
||||
listFavoriteFood.addAll(listFoodData);
|
||||
}
|
||||
Collections.sort(listFavoriteFood);
|
||||
|
||||
for(int i = 1; i <= 20; i++){
|
||||
listFoodTemp.add(new Food(String.valueOf(i), "https://cdn.netspace.edu.vn/images/2022/09/29/hoc-nau-mi-quang-1-1024.jpg", "Food "+ i, "25000", "Shop " + i));
|
||||
}
|
||||
adapter.setData(listFavoriteFood);
|
||||
recyclerView_favorite_food.setAdapter(adapter);
|
||||
}
|
||||
|
||||
return listFoodTemp;
|
||||
@Override
|
||||
public void onFailure(Call<List<Food>> call, Throwable t) {
|
||||
Toast.makeText(getContext(), "Error: " + t, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -75,14 +99,14 @@ public class FavouriteFragment extends Fragment implements ItemTouchHelperListen
|
||||
int indexDelete = viewHolder.getAdapterPosition();
|
||||
|
||||
//Remove Item
|
||||
adapter.removeItem(indexDelete);
|
||||
adapter.removeItem(indexDelete, getContext());
|
||||
|
||||
//Show notification food has been delete
|
||||
Snackbar snackbar = Snackbar.make(listFavoriteFoodView, foodNameDelete + "remove!", Snackbar.LENGTH_LONG);
|
||||
Snackbar snackbar = Snackbar.make(listFavoriteFoodView, foodNameDelete + " remove!", Snackbar.LENGTH_LONG);
|
||||
snackbar.setAction("Undo", new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
adapter.undoItem(foodDelete, indexDelete);
|
||||
adapter.undoItem(foodDelete, indexDelete, getContext());
|
||||
}
|
||||
});
|
||||
snackbar.setActionTextColor(Color.YELLOW);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.capstone.foodify.Model.Food;
|
||||
|
||||
public class Food {
|
||||
public class Food implements Comparable<Food>{
|
||||
private String id;
|
||||
private String img;
|
||||
private String name;
|
||||
@@ -84,4 +84,13 @@ public class Food {
|
||||
public void setShopName(String shopName) {
|
||||
this.shopName = shopName;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int compareTo(Food food) {
|
||||
if(getName().isEmpty() || food.getName().isEmpty()){
|
||||
return 0;
|
||||
}
|
||||
return getName().compareTo(food.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,39 @@
|
||||
package com.capstone.foodify.Model.Food;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
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.cardview.widget.CardView;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.capstone.foodify.API.FoodApi;
|
||||
import com.capstone.foodify.R;
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
|
||||
public class FoodFavoriteAdapter extends RecyclerView.Adapter<FoodFavoriteAdapter.FoodFavoriteViewHolder> {
|
||||
|
||||
private List<Food> listFavoriteFood;
|
||||
|
||||
public FoodFavoriteAdapter(List<Food> listFavoriteFood) {
|
||||
public FoodFavoriteAdapter(){}
|
||||
|
||||
public void setData(List<Food> listFavoriteFood){
|
||||
this.listFavoriteFood = listFavoriteFood;
|
||||
}
|
||||
@NonNull
|
||||
@@ -69,13 +82,49 @@ public class FoodFavoriteAdapter extends RecyclerView.Adapter<FoodFavoriteAdapt
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItem(int index){
|
||||
public void removeItem(int index, Context context){
|
||||
listFavoriteFood.remove(index);
|
||||
notifyItemRemoved(index);
|
||||
|
||||
FoodApi.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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void undoItem(Food food, int index){
|
||||
public void undoItem(Food food, int index, Context context){
|
||||
listFavoriteFood.add(index, food);
|
||||
notifyItemInserted(index);
|
||||
|
||||
FoodApi.apiService.addFoodToFavorite(food).enqueue(new Callback<Food>() {
|
||||
@Override
|
||||
public void onResponse(Call<Food> call, Response<Food> response) {
|
||||
Toast.makeText(context, "Add successful!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Food> call, Throwable t) {
|
||||
Toast.makeText(context, "Error: " + t, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private String convertFromStringToJson(Food foodDelete) throws JSONException {
|
||||
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("id", foodDelete.getId());
|
||||
jsonObject.put("name", foodDelete.getName());
|
||||
jsonObject.put("img", foodDelete.getImg());
|
||||
jsonObject.put("price", foodDelete.getPrice());
|
||||
jsonObject.put("shopName", foodDelete.getShopName());
|
||||
|
||||
return jsonObject.toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user