updated search to call api for supplier

This commit is contained in:
Alex
2026-04-07 04:19:51 -06:00
parent 37bd69c6f1
commit 863a85472f
6 changed files with 90 additions and 45 deletions

View File

@@ -18,7 +18,9 @@ public interface SupplierApi {
@GET("api/v1/suppliers")
Call<PageResponse<SupplierDTO>> getAllSuppliers(
@Query("page") int page,
@Query("size") int size
@Query("size") int size,
@Query("q") String query,
@Query("sort") String sort
);
// Get supplier by id

View File

@@ -34,7 +34,6 @@ public class SupplierFragment extends Fragment implements SupplierAdapter.OnSupp
private FragmentSupplierBinding binding;
private List<SupplierDTO> supplierList = new ArrayList<>();
private List<SupplierDTO> filteredList = new ArrayList<>();
private SupplierAdapter adapter;
private SupplierViewModel viewModel;
@@ -58,6 +57,7 @@ public class SupplierFragment extends Fragment implements SupplierAdapter.OnSupp
setupRecyclerView();
setupSearch();
setupSwipeRefresh();
setupFilterToggle();
loadSupplierData();
//Add button to opens the add dialog
@@ -83,6 +83,24 @@ public class SupplierFragment extends Fragment implements SupplierAdapter.OnSupp
binding = null;
}
/**
* Sets up the filter toggle button to show/hide the filter layout.
*/
private void setupFilterToggle() {
binding.btnToggleFilter.setOnClickListener(v -> {
if (binding.layoutFilter.getVisibility() == View.GONE) {
binding.layoutFilter.setVisibility(View.VISIBLE);
binding.btnToggleFilter.setImageResource(android.R.drawable.ic_menu_close_clear_cancel);
} else {
binding.layoutFilter.setVisibility(View.GONE);
binding.btnToggleFilter.setImageResource(android.R.drawable.ic_menu_search);
// Reset search when closing
binding.etSearchSupplier.setText("");
}
});
}
/**
* Configures the search bar for filtering.
*/
@@ -90,32 +108,12 @@ public class SupplierFragment extends Fragment implements SupplierAdapter.OnSupp
binding.etSearchSupplier.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) {
filterSuppliers(s.toString());
loadSupplierData();
}
@Override public void afterTextChanged(Editable s) {}
});
}
/**
* Filters the supplier list based on the search query across company name and contact person.
*/
private void filterSuppliers(String query) {
filteredList.clear();
if (query.isEmpty()) {
filteredList.addAll(supplierList);
} else {
String lower = query.toLowerCase();
for (SupplierDTO s : supplierList) {
if ((s.getSupCompany() != null && s.getSupCompany().toLowerCase().contains(lower))
|| (s.getSupContactFirstName() != null && s.getSupContactFirstName().toLowerCase().contains(lower))
|| (s.getSupContactLastName() != null && s.getSupContactLastName().toLowerCase().contains(lower))) {
filteredList.add(s);
}
}
}
adapter.notifyDataSetChanged();
}
/**
* Sets up the SwipeRefreshLayout to allow manual reloading of supplier data.
*/
@@ -132,7 +130,7 @@ public class SupplierFragment extends Fragment implements SupplierAdapter.OnSupp
//if editing a supplier, add the supplier id to the bundle
if (position != -1) {
SupplierDTO supplier = filteredList.get(position);
SupplierDTO supplier = supplierList.get(position);
args.putLong("supId", supplier.getSupId());
}
@@ -152,8 +150,11 @@ public class SupplierFragment extends Fragment implements SupplierAdapter.OnSupp
* Fetches all supplier data from the server through the ViewModel and updates the UI.
*/
private void loadSupplierData() {
//Load all suppliers from the backend using viewModel
viewModel.getAllSuppliers(0, 100).observe(getViewLifecycleOwner(), resource -> {
String query = binding.etSearchSupplier != null ? binding.etSearchSupplier.getText().toString().trim() : "";
if (query.isEmpty()) query = null;
//Load suppliers from the backend with query and default sort
viewModel.getAllSuppliers(0, 100, query, "supCompany").observe(getViewLifecycleOwner(), resource -> {
if (resource == null) return;
// Check the status to see if the resource is loaded and display the data
@@ -168,7 +169,7 @@ public class SupplierFragment extends Fragment implements SupplierAdapter.OnSupp
if (resource.data != null) {
supplierList.clear();
supplierList.addAll(resource.data.getContent());
filterSuppliers(binding.etSearchSupplier.getText().toString());
adapter.notifyDataSetChanged();
}
break;
case ERROR:
@@ -187,7 +188,7 @@ public class SupplierFragment extends Fragment implements SupplierAdapter.OnSupp
* Initializes the RecyclerView with a layout manager and adapter for displaying suppliers.
*/
private void setupRecyclerView() {
adapter = new SupplierAdapter(filteredList, this);
adapter = new SupplierAdapter(supplierList, this);
binding.recyclerViewSuppliers.setLayoutManager(new LinearLayoutManager(getContext()));
binding.recyclerViewSuppliers.setAdapter(adapter);
}

View File

@@ -107,7 +107,7 @@ public class ProductSupplierDetailFragment extends Fragment {
* Loads the list of suppliers from the API.
*/
private void loadSuppliers() {
supplierViewModel.getAllSuppliers(0, 200).observe(getViewLifecycleOwner(), resource -> {
supplierViewModel.getAllSuppliers(0, 200, null, "supCompany").observe(getViewLifecycleOwner(), resource -> {
if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
supplierList = resource.data.getContent();
refreshSupplierSpinner();

View File

@@ -23,8 +23,8 @@ public class SupplierRepository extends BaseRepository {
/**
* Retrieves a paginated list of all suppliers from the API.
*/
public LiveData<Resource<PageResponse<SupplierDTO>>> getAllSuppliers(int page, int size) {
return executeCall(supplierApi.getAllSuppliers(page, size));
public LiveData<Resource<PageResponse<SupplierDTO>>> getAllSuppliers(int page, int size, String query, String sort) {
return executeCall(supplierApi.getAllSuppliers(page, size, query, sort));
}
/**

View File

@@ -24,8 +24,8 @@ public class SupplierViewModel extends ViewModel {
/**
* Fetches a paginated list of all suppliers.
*/
public LiveData<Resource<PageResponse<SupplierDTO>>> getAllSuppliers(int page, int size) {
return repository.getAllSuppliers(page, size);
public LiveData<Resource<PageResponse<SupplierDTO>>> getAllSuppliers(int page, int size, String query, String sort) {
return repository.getAllSuppliers(page, size, query, sort);
}
/**

View File

@@ -29,27 +29,69 @@
android:contentDescription="Open menu"/>
<TextView
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Suppliers"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"/>
android:textStyle="bold"
android:layout_marginStart="8dp"/>
<ImageButton
android:id="@+id/btnToggleFilter"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@android:drawable/ic_menu_search"
android:background="?attr/selectableItemBackgroundBorderless"
app:tint="@color/white"
android:contentDescription="Toggle filter"/>
</LinearLayout>
<EditText
android:id="@+id/etSearchSupplier"
<LinearLayout
android:id="@+id/layoutFilter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="Search by company or contact name..."
android:inputType="text"
android:drawableStart="@android:drawable/ic_menu_search"
android:drawablePadding="8dp"
android:background="@android:color/white"
android:padding="12dp"
android:textColor="@color/text_dark"/>
android:orientation="vertical"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:visibility="gone"
android:background="@color/primary_dark"
android:elevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="@drawable/bg_search_bar"
android:gravity="center_vertical"
android:paddingStart="12dp"
android:paddingEnd="12dp">
<ImageView
android:layout_width="18dp"
android:layout_height="18dp"
android:src="@android:drawable/ic_menu_search"
android:alpha="0.6"/>
<EditText
android:id="@+id/etSearchSupplier"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Search by company or contact name..."
android:inputType="text"
android:background="@android:color/transparent"
android:textColor="@color/text_dark"
android:textColorHint="#99000000"
android:textSize="14sp"
android:paddingStart="8dp"
android:paddingEnd="8dp"/>
</LinearLayout>
</LinearLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshSupplier"