changed filtering and search in pets to use api calls
This commit is contained in:
@@ -20,11 +20,16 @@ public interface PetApi {
|
|||||||
// endpoint for downloading the pet's image file
|
// endpoint for downloading the pet's image file
|
||||||
String PET_IMAGE_PATH = "api/v1/pets/%d/image";
|
String PET_IMAGE_PATH = "api/v1/pets/%d/image";
|
||||||
|
|
||||||
// Get all pets
|
// Get all pets with filters
|
||||||
@GET("api/v1/pets")
|
@GET("api/v1/pets")
|
||||||
Call<PageResponse<PetDTO>> getAllPets(
|
Call<PageResponse<PetDTO>> getAllPets(
|
||||||
@Query("page") int page,
|
@Query("page") int page,
|
||||||
@Query("size") int size
|
@Query("size") int size,
|
||||||
|
@Query("q") String query,
|
||||||
|
@Query("status") String status,
|
||||||
|
@Query("species") String species,
|
||||||
|
@Query("storeId") Long storeId,
|
||||||
|
@Query("sort") String sort
|
||||||
);
|
);
|
||||||
|
|
||||||
// Get pet by id
|
// Get pet by id
|
||||||
|
|||||||
@@ -38,10 +38,9 @@ import dagger.hilt.android.AndroidEntryPoint;
|
|||||||
public class PetFragment extends Fragment implements PetAdapter.OnPetClickListener {
|
public class PetFragment extends Fragment implements PetAdapter.OnPetClickListener {
|
||||||
private FragmentPetBinding binding;
|
private FragmentPetBinding binding;
|
||||||
private List<PetDTO> petList = new ArrayList<>();
|
private List<PetDTO> petList = new ArrayList<>();
|
||||||
private List<PetDTO> filteredList = new ArrayList<>();
|
|
||||||
private PetAdapter adapter;
|
private PetAdapter adapter;
|
||||||
private PetViewModel viewModel;
|
private PetViewModel viewModel;
|
||||||
|
|
||||||
@Inject @Named("baseUrl") String baseUrl;
|
@Inject @Named("baseUrl") String baseUrl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -103,7 +102,7 @@ public class PetFragment extends Fragment implements PetAdapter.OnPetClickListen
|
|||||||
binding.etSearchPet.addTextChangedListener(new TextWatcher() {
|
binding.etSearchPet.addTextChangedListener(new TextWatcher() {
|
||||||
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||||
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
|
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||||
filterPets();
|
loadPetData();
|
||||||
}
|
}
|
||||||
@Override public void afterTextChanged(Editable s) {}
|
@Override public void afterTextChanged(Editable s) {}
|
||||||
});
|
});
|
||||||
@@ -121,7 +120,7 @@ public class PetFragment extends Fragment implements PetAdapter.OnPetClickListen
|
|||||||
binding.spinnerStatus.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
binding.spinnerStatus.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||||
filterPets();
|
loadPetData();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -129,30 +128,6 @@ public class PetFragment extends Fragment implements PetAdapter.OnPetClickListen
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Filters the pet list based on both the search query and the selected status.
|
|
||||||
*/
|
|
||||||
private void filterPets() {
|
|
||||||
String query = binding.etSearchPet.getText().toString().toLowerCase();
|
|
||||||
String selectedStatus = binding.spinnerStatus.getSelectedItem().toString();
|
|
||||||
|
|
||||||
filteredList.clear();
|
|
||||||
for (PetDTO p : petList) {
|
|
||||||
boolean matchesSearch = query.isEmpty() ||
|
|
||||||
p.getPetName().toLowerCase().contains(query) ||
|
|
||||||
p.getPetSpecies().toLowerCase().contains(query) ||
|
|
||||||
p.getPetBreed().toLowerCase().contains(query);
|
|
||||||
|
|
||||||
boolean matchesStatus = selectedStatus.equals("All Statuses") ||
|
|
||||||
p.getPetStatus().equalsIgnoreCase(selectedStatus);
|
|
||||||
|
|
||||||
if (matchesSearch && matchesStatus) {
|
|
||||||
filteredList.add(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
adapter.notifyDataSetChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets up the SwipeRefreshLayout to allow manual re-fetching of pet data.
|
* Sets up the SwipeRefreshLayout to allow manual re-fetching of pet data.
|
||||||
*/
|
*/
|
||||||
@@ -165,7 +140,7 @@ public class PetFragment extends Fragment implements PetAdapter.OnPetClickListen
|
|||||||
*/
|
*/
|
||||||
private void openPetProfile(int position) {
|
private void openPetProfile(int position) {
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
PetDTO pet = filteredList.get(position);
|
PetDTO pet = petList.get(position);
|
||||||
args.putLong("petId", pet.getPetId());
|
args.putLong("petId", pet.getPetId());
|
||||||
|
|
||||||
NavHostFragment.findNavController(this).navigate(R.id.nav_pet_profile, args);
|
NavHostFragment.findNavController(this).navigate(R.id.nav_pet_profile, args);
|
||||||
@@ -187,10 +162,17 @@ public class PetFragment extends Fragment implements PetAdapter.OnPetClickListen
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches all pet data from the server via the ViewModel and updates the UI.
|
* Fetches pet data from the server with filters and updates the UI.
|
||||||
*/
|
*/
|
||||||
private void loadPetData() {
|
private void loadPetData() {
|
||||||
viewModel.getAllPets(0, 100).observe(getViewLifecycleOwner(), resource -> {
|
String query = binding.etSearchPet.getText().toString().trim();
|
||||||
|
String status = binding.spinnerStatus.getSelectedItem().toString();
|
||||||
|
|
||||||
|
if (status.equals("All Statuses")) {
|
||||||
|
status = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
viewModel.getAllPets(0, 100, query, status, null, null, "petName").observe(getViewLifecycleOwner(), resource -> {
|
||||||
if (resource == null) return;
|
if (resource == null) return;
|
||||||
|
|
||||||
switch (resource.status) {
|
switch (resource.status) {
|
||||||
@@ -202,7 +184,7 @@ public class PetFragment extends Fragment implements PetAdapter.OnPetClickListen
|
|||||||
if (resource.data != null) {
|
if (resource.data != null) {
|
||||||
petList.clear();
|
petList.clear();
|
||||||
petList.addAll(resource.data.getContent());
|
petList.addAll(resource.data.getContent());
|
||||||
filterPets();
|
adapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ERROR:
|
case ERROR:
|
||||||
@@ -218,7 +200,7 @@ public class PetFragment extends Fragment implements PetAdapter.OnPetClickListen
|
|||||||
* Initializes the RecyclerView with a layout manager and adapter for displaying pets.
|
* Initializes the RecyclerView with a layout manager and adapter for displaying pets.
|
||||||
*/
|
*/
|
||||||
private void setupRecyclerView() {
|
private void setupRecyclerView() {
|
||||||
adapter = new PetAdapter(filteredList, this);
|
adapter = new PetAdapter(petList, this);
|
||||||
adapter.setBaseUrl(baseUrl);
|
adapter.setBaseUrl(baseUrl);
|
||||||
binding.recyclerViewPets.setLayoutManager(new LinearLayoutManager(getContext()));
|
binding.recyclerViewPets.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||||
binding.recyclerViewPets.setAdapter(adapter);
|
binding.recyclerViewPets.setAdapter(adapter);
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ public class AdoptionDetailFragment extends Fragment {
|
|||||||
* Loads the list of pets from the API.
|
* Loads the list of pets from the API.
|
||||||
*/
|
*/
|
||||||
private void loadPets() {
|
private void loadPets() {
|
||||||
petViewModel.getAllPets(0, 200).observe(getViewLifecycleOwner(), resource -> {
|
petViewModel.getAllPets(0, 200, null, null, null, null, "petName").observe(getViewLifecycleOwner(), resource -> {
|
||||||
if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
|
if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
|
||||||
petList = resource.data.getContent();
|
petList = resource.data.getContent();
|
||||||
refreshPetSpinner();
|
refreshPetSpinner();
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ public class AppointmentDetailFragment extends Fragment {
|
|||||||
* Loads the list of pets from the ViewModel.
|
* Loads the list of pets from the ViewModel.
|
||||||
*/
|
*/
|
||||||
private void loadPets() {
|
private void loadPets() {
|
||||||
petViewModel.getAllPets(0, 200).observe(getViewLifecycleOwner(), resource -> {
|
petViewModel.getAllPets(0, 200, null, null, null, null, "petName").observe(getViewLifecycleOwner(), resource -> {
|
||||||
if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
|
if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
|
||||||
petList = resource.data.getContent();
|
petList = resource.data.getContent();
|
||||||
refreshPetSpinner();
|
refreshPetSpinner();
|
||||||
|
|||||||
@@ -23,10 +23,10 @@ public class PetRepository extends BaseRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves a paginated list of all pets from the API.
|
* Retrieves a paginated list of pets from the API with optional filters.
|
||||||
*/
|
*/
|
||||||
public LiveData<Resource<PageResponse<PetDTO>>> getAllPets(int page, int size) {
|
public LiveData<Resource<PageResponse<PetDTO>>> getAllPets(int page, int size, String query, String status, String species, Long storeId, String sort) {
|
||||||
return executeCall(petApi.getAllPets(page, size));
|
return executeCall(petApi.getAllPets(page, size, query, status, species, storeId, sort));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -23,10 +23,10 @@ public class PetViewModel extends ViewModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches a paginated list of all pets.
|
* Fetches a paginated list of pets with filters.
|
||||||
*/
|
*/
|
||||||
public LiveData<Resource<PageResponse<PetDTO>>> getAllPets(int page, int size) {
|
public LiveData<Resource<PageResponse<PetDTO>>> getAllPets(int page, int size, String query, String status, String species, Long storeId, String sort) {
|
||||||
return repository.getAllPets(page, size);
|
return repository.getAllPets(page, size, query, status, species, storeId, sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user