changed filtering and search in pets to use api calls

This commit is contained in:
Alex
2026-04-07 02:46:00 -06:00
parent 003c7ec58a
commit 6b3979c68f
6 changed files with 30 additions and 43 deletions

View File

@@ -20,11 +20,16 @@ public interface PetApi {
// endpoint for downloading the pet's image file
String PET_IMAGE_PATH = "api/v1/pets/%d/image";
// Get all pets
// Get all pets with filters
@GET("api/v1/pets")
Call<PageResponse<PetDTO>> getAllPets(
@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

View File

@@ -38,7 +38,6 @@ import dagger.hilt.android.AndroidEntryPoint;
public class PetFragment extends Fragment implements PetAdapter.OnPetClickListener {
private FragmentPetBinding binding;
private List<PetDTO> petList = new ArrayList<>();
private List<PetDTO> filteredList = new ArrayList<>();
private PetAdapter adapter;
private PetViewModel viewModel;
@@ -103,7 +102,7 @@ public class PetFragment extends Fragment implements PetAdapter.OnPetClickListen
binding.etSearchPet.addTextChangedListener(new TextWatcher() {
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override public void onTextChanged(CharSequence s, int start, int before, int count) {
filterPets();
loadPetData();
}
@Override public void afterTextChanged(Editable s) {}
});
@@ -121,7 +120,7 @@ public class PetFragment extends Fragment implements PetAdapter.OnPetClickListen
binding.spinnerStatus.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
filterPets();
loadPetData();
}
@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.
*/
@@ -165,7 +140,7 @@ public class PetFragment extends Fragment implements PetAdapter.OnPetClickListen
*/
private void openPetProfile(int position) {
Bundle args = new Bundle();
PetDTO pet = filteredList.get(position);
PetDTO pet = petList.get(position);
args.putLong("petId", pet.getPetId());
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() {
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;
switch (resource.status) {
@@ -202,7 +184,7 @@ public class PetFragment extends Fragment implements PetAdapter.OnPetClickListen
if (resource.data != null) {
petList.clear();
petList.addAll(resource.data.getContent());
filterPets();
adapter.notifyDataSetChanged();
}
break;
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.
*/
private void setupRecyclerView() {
adapter = new PetAdapter(filteredList, this);
adapter = new PetAdapter(petList, this);
adapter.setBaseUrl(baseUrl);
binding.recyclerViewPets.setLayoutManager(new LinearLayoutManager(getContext()));
binding.recyclerViewPets.setAdapter(adapter);

View File

@@ -113,7 +113,7 @@ public class AdoptionDetailFragment extends Fragment {
* Loads the list of pets from the API.
*/
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) {
petList = resource.data.getContent();
refreshPetSpinner();

View File

@@ -134,7 +134,7 @@ public class AppointmentDetailFragment extends Fragment {
* Loads the list of pets from the ViewModel.
*/
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) {
petList = resource.data.getContent();
refreshPetSpinner();

View File

@@ -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) {
return executeCall(petApi.getAllPets(page, 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, query, status, species, storeId, sort));
}
/**

View File

@@ -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) {
return repository.getAllPets(page, 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, query, status, species, storeId, sort);
}
/**