mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 13:38:41 +00:00
Add Category tag to Search Fragment
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.capstone.foodify.API;
|
package com.capstone.foodify.API;
|
||||||
|
|
||||||
|
import com.capstone.foodify.Model.Category.Category;
|
||||||
import com.capstone.foodify.Model.Food.Food;
|
import com.capstone.foodify.Model.Food.Food;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
@@ -10,6 +11,7 @@ import retrofit2.Call;
|
|||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
import retrofit2.converter.gson.GsonConverterFactory;
|
import retrofit2.converter.gson.GsonConverterFactory;
|
||||||
import retrofit2.http.GET;
|
import retrofit2.http.GET;
|
||||||
|
import retrofit2.http.Path;
|
||||||
import retrofit2.http.Query;
|
import retrofit2.http.Query;
|
||||||
|
|
||||||
|
|
||||||
@@ -30,4 +32,10 @@ public interface FoodApi {
|
|||||||
|
|
||||||
@GET("listFood")
|
@GET("listFood")
|
||||||
Call<List<Food>> listFood(@Query("search") String search);
|
Call<List<Food>> listFood(@Query("search") String search);
|
||||||
|
|
||||||
|
@GET("Category")
|
||||||
|
Call<List<Category>> listCategory();
|
||||||
|
|
||||||
|
@GET("Category/{id}/food")
|
||||||
|
Call<List<Food>> listFoodByCategory(@Path("id") String id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import android.view.LayoutInflater;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.view.inputmethod.InputMethodManager;
|
import android.view.inputmethod.InputMethodManager;
|
||||||
|
import android.widget.CompoundButton;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
@@ -15,10 +16,13 @@ import androidx.recyclerview.widget.GridLayoutManager;
|
|||||||
import androidx.recyclerview.widget.RecyclerView;
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
|
||||||
import com.capstone.foodify.API.FoodApi;
|
import com.capstone.foodify.API.FoodApi;
|
||||||
|
import com.capstone.foodify.Model.Category.Category;
|
||||||
import com.capstone.foodify.Model.Food.Food;
|
import com.capstone.foodify.Model.Food.Food;
|
||||||
import com.capstone.foodify.Model.Food.FoodAdapter;
|
import com.capstone.foodify.Model.Food.FoodAdapter;
|
||||||
import com.capstone.foodify.Model.Food.FoodSearchAdapter;
|
import com.capstone.foodify.Model.Food.FoodSearchAdapter;
|
||||||
import com.capstone.foodify.R;
|
import com.capstone.foodify.R;
|
||||||
|
import com.google.android.material.chip.Chip;
|
||||||
|
import com.google.android.material.chip.ChipGroup;
|
||||||
import com.paulrybitskyi.persistentsearchview.PersistentSearchView;
|
import com.paulrybitskyi.persistentsearchview.PersistentSearchView;
|
||||||
import com.paulrybitskyi.persistentsearchview.listeners.OnSearchConfirmedListener;
|
import com.paulrybitskyi.persistentsearchview.listeners.OnSearchConfirmedListener;
|
||||||
import com.paulrybitskyi.persistentsearchview.listeners.OnSearchQueryChangeListener;
|
import com.paulrybitskyi.persistentsearchview.listeners.OnSearchQueryChangeListener;
|
||||||
@@ -35,9 +39,9 @@ public class SearchFragment extends Fragment {
|
|||||||
|
|
||||||
private RecyclerView recycler_view_search;
|
private RecyclerView recycler_view_search;
|
||||||
private FoodSearchAdapter foodAdapter;
|
private FoodSearchAdapter foodAdapter;
|
||||||
private PersistentSearchView persistentSearchView;
|
private static final List<Food> listFoodSearch = new ArrayList<>();
|
||||||
|
private ChipGroup list_Category;
|
||||||
private static List<Food> listFoodSearch = new ArrayList<>();
|
private final List<String> statusCategoryChecked = new ArrayList<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
@@ -45,11 +49,14 @@ public class SearchFragment extends Fragment {
|
|||||||
View view = inflater.inflate(R.layout.fragment_search, container, false);
|
View view = inflater.inflate(R.layout.fragment_search, container, false);
|
||||||
|
|
||||||
//Init Component
|
//Init Component
|
||||||
|
PersistentSearchView persistentSearchView = view.findViewById(R.id.search_view);
|
||||||
recycler_view_search = view.findViewById(R.id.recycler_view_search);
|
recycler_view_search = view.findViewById(R.id.recycler_view_search);
|
||||||
persistentSearchView = view.findViewById(R.id.search_view);
|
list_Category = view.findViewById(R.id.list_category);
|
||||||
|
|
||||||
foodAdapter = new FoodSearchAdapter(getContext());
|
foodAdapter = new FoodSearchAdapter(getContext());
|
||||||
|
|
||||||
|
getListCategory();
|
||||||
|
|
||||||
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
|
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
|
||||||
recycler_view_search.setLayoutManager(gridLayoutManager);
|
recycler_view_search.setLayoutManager(gridLayoutManager);
|
||||||
|
|
||||||
@@ -57,7 +64,6 @@ public class SearchFragment extends Fragment {
|
|||||||
recycler_view_search.setAdapter(foodAdapter);
|
recycler_view_search.setAdapter(foodAdapter);
|
||||||
|
|
||||||
persistentSearchView.setLeftButtonDrawable(R.drawable.ic_search);
|
persistentSearchView.setLeftButtonDrawable(R.drawable.ic_search);
|
||||||
|
|
||||||
persistentSearchView.setOnSearchConfirmedListener(new OnSearchConfirmedListener() {
|
persistentSearchView.setOnSearchConfirmedListener(new OnSearchConfirmedListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -68,17 +74,7 @@ public class SearchFragment extends Fragment {
|
|||||||
FoodApi.apiService.listFood(query).enqueue(new Callback<List<Food>>() {
|
FoodApi.apiService.listFood(query).enqueue(new Callback<List<Food>>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<List<Food>> call, Response<List<Food>> response) {
|
public void onResponse(Call<List<Food>> call, Response<List<Food>> response) {
|
||||||
List<Food> foodData = response.body();
|
loadFoodAdapter(response);
|
||||||
|
|
||||||
if(foodData != null) {
|
|
||||||
listFoodSearch.clear();
|
|
||||||
for(Food tempFood: foodData){
|
|
||||||
listFoodSearch.add(tempFood);
|
|
||||||
}
|
|
||||||
foodAdapter.setData(listFoodSearch);
|
|
||||||
recycler_view_search.setAdapter(foodAdapter);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -86,6 +82,8 @@ public class SearchFragment extends Fragment {
|
|||||||
Toast.makeText(getContext(), "Lỗi" + t, Toast.LENGTH_LONG).show();
|
Toast.makeText(getContext(), "Lỗi" + t, Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Toast.makeText(getContext(), "Category checked: " + statusCategoryChecked.toString(), Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
searchView.collapse();
|
searchView.collapse();
|
||||||
@@ -106,4 +104,69 @@ public class SearchFragment extends Fragment {
|
|||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void loadFoodAdapter(Response<List<Food>> response) {
|
||||||
|
List<Food> foodData = response.body();
|
||||||
|
|
||||||
|
if(foodData != null) {
|
||||||
|
listFoodSearch.clear();
|
||||||
|
listFoodSearch.addAll(foodData);
|
||||||
|
foodAdapter.setData(listFoodSearch);
|
||||||
|
recycler_view_search.setAdapter(foodAdapter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void getListCategory() {
|
||||||
|
LayoutInflater inflater = LayoutInflater.from(getContext());
|
||||||
|
|
||||||
|
FoodApi.apiService.listCategory().enqueue(new Callback<List<Category>>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
|
||||||
|
List<Category> categoriesData = response.body();
|
||||||
|
|
||||||
|
if(categoriesData != null){
|
||||||
|
for(Category tempCategory: categoriesData){
|
||||||
|
Chip chip = (Chip) inflater.inflate(R.layout.chip_item, null, false);
|
||||||
|
chip.setText(tempCategory.getName());
|
||||||
|
chip.setEnabled(true);
|
||||||
|
|
||||||
|
chip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
|
||||||
|
loadFoodByCategory(tempCategory.getId());
|
||||||
|
if(b) {
|
||||||
|
statusCategoryChecked.add(tempCategory.getId());
|
||||||
|
} else {
|
||||||
|
statusCategoryChecked.remove(tempCategory.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
list_Category.addView(chip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<List<Category>> call, Throwable t) {
|
||||||
|
Toast.makeText(getContext(), "Error: " + t , Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadFoodByCategory(String id) {
|
||||||
|
|
||||||
|
FoodApi.apiService.listFoodByCategory(id).enqueue(new Callback<List<Food>>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(Call<List<Food>> call, Response<List<Food>> response) {
|
||||||
|
loadFoodAdapter(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<List<Food>> call, Throwable t) {
|
||||||
|
Toast.makeText(getContext(), "Error: " + t, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.capstone.foodify.Model.Category;
|
||||||
|
|
||||||
|
public class Category {
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:text="Example"
|
||||||
|
android:id="@+id/chip_item"
|
||||||
|
style="@style/Widget.MaterialComponents.Chip.Choice"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
</com.google.android.material.chip.Chip>
|
||||||
@@ -19,13 +19,21 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content" />
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
<com.google.android.material.chip.ChipGroup
|
||||||
|
android:id="@+id/list_category"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/search_view"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
<androidx.recyclerview.widget.RecyclerView
|
<androidx.recyclerview.widget.RecyclerView
|
||||||
android:id="@+id/recycler_view_search"
|
android:id="@+id/recycler_view_search"
|
||||||
tools:listitem="@layout/list_foods"
|
tools:listitem="@layout/list_foods"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginStart="10dp"
|
android:layout_marginStart="10dp"
|
||||||
app:layout_constraintTop_toBottomOf="@id/search_view"
|
app:layout_constraintTop_toBottomOf="@id/list_category"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in New Issue
Block a user