diff --git a/app/src/main/java/com/capstone/foodify/API/FoodApi.java b/app/src/main/java/com/capstone/foodify/API/FoodApi.java index da7bb0a..a10d49e 100644 --- a/app/src/main/java/com/capstone/foodify/API/FoodApi.java +++ b/app/src/main/java/com/capstone/foodify/API/FoodApi.java @@ -57,6 +57,9 @@ public interface FoodApi { @GET("products/categories") Call listFoodByCategory(@Query("id") List id, @Query("pageNo") int pageNo, @Query("pageSize") int pageSize, @Query("sortBy") String sortBy, @Query("sortDir") String sortDir); + @GET("products/categoriesAndName") + Call listFoodByCategoriesAndName(@Query("id") List id, @Query("name") String name, @Query("pageNo") int pageNo, @Query("pageSize") int pageSize, @Query("sortBy") String sortBy, @Query("sortDir") String sortDir); + @GET("products/{id}") Call detailFood(@Path("id") String id); diff --git a/app/src/main/java/com/capstone/foodify/Activity/FoodDetailActivity.java b/app/src/main/java/com/capstone/foodify/Activity/FoodDetailActivity.java index 01f59f3..e761ccc 100644 --- a/app/src/main/java/com/capstone/foodify/Activity/FoodDetailActivity.java +++ b/app/src/main/java/com/capstone/foodify/Activity/FoodDetailActivity.java @@ -80,7 +80,7 @@ public class FoodDetailActivity extends AppCompatActivity { private String foodId = ""; private Food food; private TextView foodName_txt, shopName_txt, discount_txt, price_txt, countRating_txt, description_content_txt, endOfListText, - userNameComment, contentComment; + userNameComment, contentComment, count_sold_txt; private ConstraintLayout content_view; private Button add_to_basket_button; private float totalPrice, price; @@ -123,6 +123,7 @@ public class FoodDetailActivity extends AppCompatActivity { user_comment_layout = findViewById(R.id.user_comment_layout); edt_button = findViewById(R.id.edit_button); list_category = findViewById(R.id.list_category); + count_sold_txt = findViewById(R.id.count_sold); } @Override protected void onCreate(Bundle savedInstanceState) { @@ -474,6 +475,12 @@ public class FoodDetailActivity extends AppCompatActivity { countRating_txt.setText(food.getReviewCount() + " đánh giá"); } + if(food.getSold() == 0){ + count_sold_txt.setText("Chưa có lượt bán nào!"); + } else { + count_sold_txt.setText(food.getSold() + " đã bán"); + } + //Init category for(Category category: food.getCategories()){ Chip chip = (Chip) inflater.inflate(R.layout.chip_item, null, false); diff --git a/app/src/main/java/com/capstone/foodify/Fragment/SearchFragment.java b/app/src/main/java/com/capstone/foodify/Fragment/SearchFragment.java index 3fbb8a0..3b4a8ea 100644 --- a/app/src/main/java/com/capstone/foodify/Fragment/SearchFragment.java +++ b/app/src/main/java/com/capstone/foodify/Fragment/SearchFragment.java @@ -34,6 +34,7 @@ import java.util.List; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; +import retrofit2.http.GET; public class SearchFragment extends Fragment { @@ -44,6 +45,7 @@ public class SearchFragment extends Fragment { private static final int LIST_FOOD = -1; private static final int SEARCH_FOOD = 0; private static final int LIST_FOOD_BY_CATEGORY = 2; + private static final int LIST_FOOD_BY_CATEGORY_AND_NAME = 3; private String searchQuery = ""; private LinearLayout searchNotFound; private RecyclerView recycler_view_search; @@ -54,6 +56,7 @@ public class SearchFragment extends Fragment { private static final List listFoodSearch = new ArrayList<>(); private ChipGroup list_Category; private final List statusCategoryChecked = new ArrayList<>(); + private SearchView searchView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -61,7 +64,7 @@ public class SearchFragment extends Fragment { View view = inflater.inflate(R.layout.fragment_search, container, false); //Init Component - SearchView searchView = view.findViewById(R.id.search_view); + searchView = view.findViewById(R.id.search_view); recycler_view_search = view.findViewById(R.id.recycler_view_search); list_Category = view.findViewById(R.id.list_category); nestedScrollView = view.findViewById(R.id.search_fragment); @@ -101,32 +104,32 @@ public class SearchFragment extends Fragment { //Hide UI search not found searchNotFound.setVisibility(View.GONE); - //Set action code - ACTION_CODE = SEARCH_FOOD; - CURRENT_PAGE = 0; + listFoodSearch.clear(); showProgressBarAndHideEndOfListText(); if (!query.isEmpty()) { + searchQuery = query; - getListFoodBySearch(searchQuery, CURRENT_PAGE); + + //Search food with name and category + if(statusCategoryChecked.size() != 0){ + ACTION_CODE = LIST_FOOD_BY_CATEGORY_AND_NAME; + CURRENT_PAGE = 0; + + getListFoodByCategoryAndName(0); + } else { + //Set action code + ACTION_CODE = SEARCH_FOOD; + CURRENT_PAGE = 0; + + getListFoodBySearch(CURRENT_PAGE); + } } return true; } @Override public boolean onQueryTextChange(String newText) { - if (searchView.getQuery().length() == 0) { - listFoodSearch.clear(); - - CURRENT_PAGE = 0; - ACTION_CODE = LIST_FOOD; - showProgressBarAndHideEndOfListText(); - - //Hide UI search not found - searchNotFound.setVisibility(View.GONE); - - getListFood(0); - } return true; } }); @@ -145,8 +148,8 @@ public class SearchFragment extends Fragment { } - private void getListFoodBySearch(String query, int page) { - FoodApi.apiService.searchFoodByName(query, page, LIMIT, "id", "asc").enqueue(new Callback() { + private void getListFoodBySearch(int page) { + FoodApi.apiService.searchFoodByName(searchQuery, page, LIMIT, "id", "asc").enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { //Init total page and total elements value @@ -213,17 +216,40 @@ public class SearchFragment extends Fragment { listFoodSearch.clear(); CURRENT_PAGE = 0; showProgressBarAndHideEndOfListText(); + searchNotFound.setVisibility(View.GONE); //Check category list null or not if(statusCategoryChecked.size() == 0){ - //If null, return list food - ACTION_CODE = LIST_FOOD; - getListFood(0); + //Check query text + if(!searchQuery.isEmpty()){ + //Set action code + ACTION_CODE = SEARCH_FOOD; + CURRENT_PAGE = 0; + + getListFoodBySearch(CURRENT_PAGE); + + } else { + //If null, return list food + ACTION_CODE = LIST_FOOD; + getListFood(0); + } } else { - //If have category check, return list food by category - ACTION_CODE = LIST_FOOD_BY_CATEGORY; - getListFoodByCategory(CURRENT_PAGE); + //If have category check + if(!searchQuery.isEmpty()){ + //Return list food by name and category + + ACTION_CODE = LIST_FOOD_BY_CATEGORY_AND_NAME; + CURRENT_PAGE = 0; + + getListFoodByCategoryAndName(0); + } else { + //Return list food by category + + ACTION_CODE = LIST_FOOD_BY_CATEGORY; + getListFoodByCategory(CURRENT_PAGE); + } + } } }); @@ -242,13 +268,33 @@ public class SearchFragment extends Fragment { }); } + private void getListFoodByCategoryAndName(int page){ + FoodApi.apiService.listFoodByCategoriesAndName(statusCategoryChecked, searchQuery, page, LIMIT, "id", "asc").enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + searchNotFound.setVisibility(View.GONE); + showProgressBarAndHideEndOfListText(); + + if(response.code() == 200){ + LAST_PAGE = response.body().getPage().isLast(); + loadFoodAdapter(response); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + } + private void getListFoodByCategory(int page) { FoodApi.apiService.listFoodByCategory(statusCategoryChecked, page, LIMIT, "id", "asc").enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { searchNotFound.setVisibility(View.GONE); - end_of_list_text_view.setVisibility(View.GONE); + showProgressBarAndHideEndOfListText(); //Init total page and total elements value LAST_PAGE = response.body().getPage().isLast(); @@ -291,7 +337,7 @@ public class SearchFragment extends Fragment { } case SEARCH_FOOD:{ if(!LAST_PAGE){ - getListFoodBySearch(searchQuery, ++CURRENT_PAGE); + getListFoodBySearch(++CURRENT_PAGE); } else { hideProgressBarAndShowEndOfListText(); } @@ -305,6 +351,14 @@ public class SearchFragment extends Fragment { } break; } + case LIST_FOOD_BY_CATEGORY_AND_NAME:{ + if(!LAST_PAGE){ + getListFoodByCategoryAndName(++CURRENT_PAGE); + } else { + hideProgressBarAndShowEndOfListText(); + } + break; + } } } diff --git a/app/src/main/java/com/capstone/foodify/Model/Food.java b/app/src/main/java/com/capstone/foodify/Model/Food.java index 1e4929e..fd4526a 100644 --- a/app/src/main/java/com/capstone/foodify/Model/Food.java +++ b/app/src/main/java/com/capstone/foodify/Model/Food.java @@ -16,6 +16,7 @@ public class Food implements Comparable, Serializable { private List categories; private List images; private List comments; + private int sold; public Food() { } @@ -115,6 +116,14 @@ public class Food implements Comparable, Serializable { this.comments = comments; } + public int getSold() { + return sold; + } + + public void setSold(int sold) { + this.sold = sold; + } + @Override public int compareTo(Food food) { if(getName().isEmpty() || food.getName().isEmpty()){ diff --git a/app/src/main/res/layout/activity_food_detail.xml b/app/src/main/res/layout/activity_food_detail.xml index 8e2d43a..4fb1071 100644 --- a/app/src/main/res/layout/activity_food_detail.xml +++ b/app/src/main/res/layout/activity_food_detail.xml @@ -69,13 +69,13 @@ > @@ -84,7 +84,7 @@ android:layout_height="60dp" android:layout_weight="0.2" android:orientation="vertical" - android:layout_gravity="bottom" + android:layout_gravity="center_vertical" > + +