Add search function

This commit is contained in:
Nezumi
2023-02-15 21:55:25 +07:00
parent ef4fa7ebea
commit 38aa190c0e
3 changed files with 57 additions and 49 deletions
@@ -10,6 +10,7 @@ import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface FoodApi {
@@ -17,13 +18,16 @@ public interface FoodApi {
Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss dd-MM-yyyy").create();
FoodApi apiService = new Retrofit.Builder()
.baseUrl("https://free-food-menus-api-production.up.railway.app/").addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("https://63eb52abbfdd4299674540d4.mockapi.io/api/v1/").addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(FoodApi.class);
@GET("best-foods")
@GET("randomProduct")
Call<List<Food>> bestFoodResponse();
@GET("drinks")
@GET("recommend")
Call<List<Food>> drinksFoodResponse();
@GET("listFood")
Call<List<Food>> listFood(@Query("search") String search);
}
@@ -15,19 +15,32 @@ import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;
import com.capstone.foodify.API.FoodApi;
import com.capstone.foodify.Model.Food.Food;
import com.capstone.foodify.Model.Food.FoodAdapter;
import com.capstone.foodify.R;
import com.paulrybitskyi.persistentsearchview.PersistentSearchView;
import com.paulrybitskyi.persistentsearchview.listeners.OnSearchConfirmedListener;
import com.paulrybitskyi.persistentsearchview.listeners.OnSearchQueryChangeListener;
import com.paulrybitskyi.persistentsearchview.utils.SuggestionCreationUtil;
import com.paulrybitskyi.persistentsearchview.utils.VoiceRecognitionDelegate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class SearchFragment extends Fragment {
private RecyclerView recycler_view_search;
private FoodAdapter foodAdapter;
private PersistentSearchView persistentSearchView;
private static List<Food> listFoodSearch = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@@ -45,40 +58,58 @@ public class SearchFragment extends Fragment {
foodAdapter.setData(listFood);
recycler_view_search.setAdapter(foodAdapter);
persistentSearchView.setOnLeftBtnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(), "Left Btn", Toast.LENGTH_SHORT).show();
}
});
persistentSearchView.setOnClearInputBtnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Handle the clear input button click
Toast.makeText(getContext(), "Clear Btn", Toast.LENGTH_SHORT).show();
}
});
persistentSearchView.setLeftButtonDrawable(R.drawable.ic_search);
// Setting a delegate for the voice recognition input
persistentSearchView.setVoiceRecognitionDelegate(new VoiceRecognitionDelegate(this));
persistentSearchView.setProgressBarEnabled(true);
persistentSearchView.setSuggestionsDisabled(false);
persistentSearchView.setOnSearchConfirmedListener(new OnSearchConfirmedListener() {
@Override
public void onSearchConfirmed(PersistentSearchView searchView, String query) {
// Handle a search confirmation. This is the place where you'd
// want to perform a search against your data provider.
Toast.makeText(getContext(), "setOnSearchConfirmedListener", Toast.LENGTH_SHORT).show();
if(!query.isEmpty())
FoodApi.apiService.listFood(query).enqueue(new Callback<List<Food>>() {
@Override
public void onResponse(Call<List<Food>> call, Response<List<Food>> response) {
List<Food> foodData = response.body();
if(foodData != null) {
listFoodSearch.clear();
for(Food tempFood: foodData){
listFoodSearch.add(tempFood);
}
foodAdapter.setData(listFoodSearch);
recycler_view_search.setAdapter(foodAdapter);
}
}
@Override
public void onFailure(Call<List<Food>> call, Throwable t) {
Toast.makeText(getContext(), "Lỗi" + t, Toast.LENGTH_LONG).show();
}
});
searchView.collapse();
}
});
persistentSearchView.setOnSearchQueryChangeListener(new OnSearchQueryChangeListener() {
@Override
public void onSearchQueryChanged(PersistentSearchView searchView, String oldQuery, String newQuery) {
if(searchView.isInputQueryEmpty()){
foodAdapter.setData(listFood);
recycler_view_search.setAdapter(foodAdapter);
}
}
});
// Disabling the suggestions since they are unused in
// the simple implementation
persistentSearchView.setSuggestionsDisabled(false);
@@ -96,4 +127,5 @@ public class SearchFragment extends Fragment {
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
@@ -4,10 +4,7 @@ public class Food {
private String id;
private String img;
private String name;
private String dsc;
private String price;
private String rate;
private String country;
public String getId() {
return id;
@@ -33,13 +30,6 @@ public class Food {
this.name = name;
}
public String getDsc() {
return dsc;
}
public void setDsc(String dsc) {
this.dsc = dsc;
}
public String getPrice() {
return price;
@@ -49,21 +39,6 @@ public class Food {
this.price = price;
}
public String getRate() {
return rate;
}
public void setRate(String rate) {
this.rate = rate;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
@@ -71,10 +46,7 @@ public class Food {
"id='" + id + '\'' +
", img='" + img + '\'' +
", name='" + name + '\'' +
", dsc='" + dsc + '\'' +
", price='" + price + '\'' +
", rate='" + rate + '\'' +
", country='" + country + '\'' +
'}';
}
}