mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 13:38:41 +00:00
Add search food by category and name
This commit is contained in:
@@ -57,6 +57,9 @@ public interface FoodApi {
|
||||
@GET("products/categories")
|
||||
Call<Foods> listFoodByCategory(@Query("id") List<Integer> id, @Query("pageNo") int pageNo, @Query("pageSize") int pageSize, @Query("sortBy") String sortBy, @Query("sortDir") String sortDir);
|
||||
|
||||
@GET("products/categoriesAndName")
|
||||
Call<Foods> listFoodByCategoriesAndName(@Query("id") List<Integer> 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<Food> detailFood(@Path("id") String id);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<Food> listFoodSearch = new ArrayList<>();
|
||||
private ChipGroup list_Category;
|
||||
private final List<Integer> 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<Foods>() {
|
||||
private void getListFoodBySearch(int page) {
|
||||
FoodApi.apiService.searchFoodByName(searchQuery, page, LIMIT, "id", "asc").enqueue(new Callback<Foods>() {
|
||||
@Override
|
||||
public void onResponse(Call<Foods> call, Response<Foods> 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<Foods>() {
|
||||
@Override
|
||||
public void onResponse(Call<Foods> call, Response<Foods> response) {
|
||||
searchNotFound.setVisibility(View.GONE);
|
||||
showProgressBarAndHideEndOfListText();
|
||||
|
||||
if(response.code() == 200){
|
||||
LAST_PAGE = response.body().getPage().isLast();
|
||||
loadFoodAdapter(response);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Foods> call, Throwable t) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void getListFoodByCategory(int page) {
|
||||
FoodApi.apiService.listFoodByCategory(statusCategoryChecked, page, LIMIT, "id", "asc").enqueue(new Callback<Foods>() {
|
||||
@Override
|
||||
public void onResponse(Call<Foods> call, Response<Foods> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ public class Food implements Comparable<Food>, Serializable {
|
||||
private List<Category> categories;
|
||||
private List<Image> images;
|
||||
private List<Comment> comments;
|
||||
private int sold;
|
||||
public Food() {
|
||||
}
|
||||
|
||||
@@ -115,6 +116,14 @@ public class Food implements Comparable<Food>, 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()){
|
||||
|
||||
@@ -69,13 +69,13 @@
|
||||
>
|
||||
<TextView
|
||||
android:id="@+id/food_name_text_view"
|
||||
android:textSize="40sp"
|
||||
android:textSize="30sp"
|
||||
android:text="Food Name"
|
||||
android:textColor="@color/black"
|
||||
android:fontFamily="@font/bebas"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="20dp"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_weight="0.8"
|
||||
/>
|
||||
|
||||
@@ -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"
|
||||
>
|
||||
|
||||
<TextView
|
||||
@@ -159,6 +159,15 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:text="235 đánh giá" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/count_sold"
|
||||
android:textSize="13sp"
|
||||
android:layout_marginTop="5dp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAlignment="center"
|
||||
android:text="250 đã bán" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
|
||||
Reference in New Issue
Block a user