updated search to call api for supplier
This commit is contained in:
@@ -18,7 +18,9 @@ public interface SupplierApi {
|
|||||||
@GET("api/v1/suppliers")
|
@GET("api/v1/suppliers")
|
||||||
Call<PageResponse<SupplierDTO>> getAllSuppliers(
|
Call<PageResponse<SupplierDTO>> getAllSuppliers(
|
||||||
@Query("page") int page,
|
@Query("page") int page,
|
||||||
@Query("size") int size
|
@Query("size") int size,
|
||||||
|
@Query("q") String query,
|
||||||
|
@Query("sort") String sort
|
||||||
);
|
);
|
||||||
|
|
||||||
// Get supplier by id
|
// Get supplier by id
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ public class SupplierFragment extends Fragment implements SupplierAdapter.OnSupp
|
|||||||
|
|
||||||
private FragmentSupplierBinding binding;
|
private FragmentSupplierBinding binding;
|
||||||
private List<SupplierDTO> supplierList = new ArrayList<>();
|
private List<SupplierDTO> supplierList = new ArrayList<>();
|
||||||
private List<SupplierDTO> filteredList = new ArrayList<>();
|
|
||||||
private SupplierAdapter adapter;
|
private SupplierAdapter adapter;
|
||||||
private SupplierViewModel viewModel;
|
private SupplierViewModel viewModel;
|
||||||
|
|
||||||
@@ -58,6 +57,7 @@ public class SupplierFragment extends Fragment implements SupplierAdapter.OnSupp
|
|||||||
setupRecyclerView();
|
setupRecyclerView();
|
||||||
setupSearch();
|
setupSearch();
|
||||||
setupSwipeRefresh();
|
setupSwipeRefresh();
|
||||||
|
setupFilterToggle();
|
||||||
loadSupplierData();
|
loadSupplierData();
|
||||||
|
|
||||||
//Add button to opens the add dialog
|
//Add button to opens the add dialog
|
||||||
@@ -83,6 +83,24 @@ public class SupplierFragment extends Fragment implements SupplierAdapter.OnSupp
|
|||||||
binding = null;
|
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.
|
* Configures the search bar for filtering.
|
||||||
*/
|
*/
|
||||||
@@ -90,32 +108,12 @@ public class SupplierFragment extends Fragment implements SupplierAdapter.OnSupp
|
|||||||
binding.etSearchSupplier.addTextChangedListener(new TextWatcher() {
|
binding.etSearchSupplier.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) {
|
||||||
filterSuppliers(s.toString());
|
loadSupplierData();
|
||||||
}
|
}
|
||||||
@Override public void afterTextChanged(Editable s) {}
|
@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.
|
* 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 editing a supplier, add the supplier id to the bundle
|
||||||
if (position != -1) {
|
if (position != -1) {
|
||||||
SupplierDTO supplier = filteredList.get(position);
|
SupplierDTO supplier = supplierList.get(position);
|
||||||
args.putLong("supId", supplier.getSupId());
|
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.
|
* Fetches all supplier data from the server through the ViewModel and updates the UI.
|
||||||
*/
|
*/
|
||||||
private void loadSupplierData() {
|
private void loadSupplierData() {
|
||||||
//Load all suppliers from the backend using viewModel
|
String query = binding.etSearchSupplier != null ? binding.etSearchSupplier.getText().toString().trim() : "";
|
||||||
viewModel.getAllSuppliers(0, 100).observe(getViewLifecycleOwner(), resource -> {
|
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;
|
if (resource == null) return;
|
||||||
|
|
||||||
// Check the status to see if the resource is loaded and display the data
|
// 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) {
|
if (resource.data != null) {
|
||||||
supplierList.clear();
|
supplierList.clear();
|
||||||
supplierList.addAll(resource.data.getContent());
|
supplierList.addAll(resource.data.getContent());
|
||||||
filterSuppliers(binding.etSearchSupplier.getText().toString());
|
adapter.notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ERROR:
|
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.
|
* Initializes the RecyclerView with a layout manager and adapter for displaying suppliers.
|
||||||
*/
|
*/
|
||||||
private void setupRecyclerView() {
|
private void setupRecyclerView() {
|
||||||
adapter = new SupplierAdapter(filteredList, this);
|
adapter = new SupplierAdapter(supplierList, this);
|
||||||
binding.recyclerViewSuppliers.setLayoutManager(new LinearLayoutManager(getContext()));
|
binding.recyclerViewSuppliers.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||||
binding.recyclerViewSuppliers.setAdapter(adapter);
|
binding.recyclerViewSuppliers.setAdapter(adapter);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ public class ProductSupplierDetailFragment extends Fragment {
|
|||||||
* Loads the list of suppliers from the API.
|
* Loads the list of suppliers from the API.
|
||||||
*/
|
*/
|
||||||
private void loadSuppliers() {
|
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) {
|
if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
|
||||||
supplierList = resource.data.getContent();
|
supplierList = resource.data.getContent();
|
||||||
refreshSupplierSpinner();
|
refreshSupplierSpinner();
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ public class SupplierRepository extends BaseRepository {
|
|||||||
/**
|
/**
|
||||||
* Retrieves a paginated list of all suppliers from the API.
|
* Retrieves a paginated list of all suppliers from the API.
|
||||||
*/
|
*/
|
||||||
public LiveData<Resource<PageResponse<SupplierDTO>>> getAllSuppliers(int page, int size) {
|
public LiveData<Resource<PageResponse<SupplierDTO>>> getAllSuppliers(int page, int size, String query, String sort) {
|
||||||
return executeCall(supplierApi.getAllSuppliers(page, size));
|
return executeCall(supplierApi.getAllSuppliers(page, size, query, sort));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ public class SupplierViewModel extends ViewModel {
|
|||||||
/**
|
/**
|
||||||
* Fetches a paginated list of all suppliers.
|
* Fetches a paginated list of all suppliers.
|
||||||
*/
|
*/
|
||||||
public LiveData<Resource<PageResponse<SupplierDTO>>> getAllSuppliers(int page, int size) {
|
public LiveData<Resource<PageResponse<SupplierDTO>>> getAllSuppliers(int page, int size, String query, String sort) {
|
||||||
return repository.getAllSuppliers(page, size);
|
return repository.getAllSuppliers(page, size, query, sort);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -29,27 +29,69 @@
|
|||||||
android:contentDescription="Open menu"/>
|
android:contentDescription="Open menu"/>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="match_parent"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
android:text="Suppliers"
|
android:text="Suppliers"
|
||||||
android:textColor="@color/white"
|
android:textColor="@color/white"
|
||||||
android:textSize="20sp"
|
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>
|
</LinearLayout>
|
||||||
|
|
||||||
<EditText
|
<LinearLayout
|
||||||
android:id="@+id/etSearchSupplier"
|
android:id="@+id/layoutFilter"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_margin="8dp"
|
android:orientation="vertical"
|
||||||
android:hint="Search by company or contact name..."
|
android:paddingStart="12dp"
|
||||||
android:inputType="text"
|
android:paddingEnd="12dp"
|
||||||
android:drawableStart="@android:drawable/ic_menu_search"
|
android:paddingTop="10dp"
|
||||||
android:drawablePadding="8dp"
|
android:paddingBottom="10dp"
|
||||||
android:background="@android:color/white"
|
android:visibility="gone"
|
||||||
android:padding="12dp"
|
android:background="@color/primary_dark"
|
||||||
android:textColor="@color/text_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
|
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||||
android:id="@+id/swipeRefreshSupplier"
|
android:id="@+id/swipeRefreshSupplier"
|
||||||
|
|||||||
Reference in New Issue
Block a user