updated search for service to call api

This commit is contained in:
Alex
2026-04-07 04:12:29 -06:00
parent 3d403ee35e
commit 5823152c56
6 changed files with 90 additions and 44 deletions

View File

@@ -18,7 +18,9 @@ public interface ServiceApi {
@GET("api/v1/services")
Call<PageResponse<ServiceDTO>> getAllServices(
@Query("page") int page,
@Query("size") int size
@Query("size") int size,
@Query("q") String query,
@Query("sort") String sort
);
// Get service by id

View File

@@ -34,7 +34,6 @@ public class ServiceFragment extends Fragment implements ServiceAdapter.OnServic
private FragmentServiceBinding binding;
private List<ServiceDTO> serviceList = new ArrayList<>();
private List<ServiceDTO> filteredList = new ArrayList<>();
private ServiceAdapter adapter;
private ServiceViewModel viewModel;
@@ -58,6 +57,7 @@ public class ServiceFragment extends Fragment implements ServiceAdapter.OnServic
setupRecyclerView();
setupSearch();
setupSwipeRefresh();
setupFilterToggle();
loadServiceData();
//Add button to opens the add dialog
@@ -83,6 +83,24 @@ public class ServiceFragment extends Fragment implements ServiceAdapter.OnServic
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.etSearchService.setText("");
}
});
}
/**
* Configures the search bar for filtering.
*/
@@ -90,31 +108,12 @@ public class ServiceFragment extends Fragment implements ServiceAdapter.OnServic
binding.etSearchService.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) {
filterServices(s.toString());
loadServiceData();
}
@Override public void afterTextChanged(Editable s) {}
});
}
/**
* Filters the service list based on the search query across name and description fields.
*/
private void filterServices(String query) {
filteredList.clear();
if (query.isEmpty()) {
filteredList.addAll(serviceList);
} else {
String lower = query.toLowerCase();
for (ServiceDTO s : serviceList) {
if ((s.getServiceName() != null && s.getServiceName().toLowerCase().contains(lower))
|| (s.getServiceDesc() != null && s.getServiceDesc().toLowerCase().contains(lower))) {
filteredList.add(s);
}
}
}
adapter.notifyDataSetChanged();
}
/**
* Sets up the SwipeRefreshLayout to allow manual reloading of service data.
*/
@@ -131,7 +130,7 @@ public class ServiceFragment extends Fragment implements ServiceAdapter.OnServic
//if editing a service, add the service id to the bundle
if (position != -1) {
ServiceDTO service = filteredList.get(position);
ServiceDTO service = serviceList.get(position);
args.putLong("serviceId", service.getServiceId());
}
@@ -150,8 +149,11 @@ public class ServiceFragment extends Fragment implements ServiceAdapter.OnServic
* Fetches all service data from the server through the ViewModel and updates the UI.
*/
private void loadServiceData() {
//Load all services from the backend using viewModel
viewModel.getAllServices(0, 100).observe(getViewLifecycleOwner(), resource -> {
String query = binding.etSearchService != null ? binding.etSearchService.getText().toString().trim() : "";
if (query.isEmpty()) query = null;
//Load services from the backend with query and default sort
viewModel.getAllServices(0, 100, query, "serviceName").observe(getViewLifecycleOwner(), resource -> {
if (resource == null) return;
// Check the status to see if the resource is loaded and display the data
@@ -166,7 +168,7 @@ public class ServiceFragment extends Fragment implements ServiceAdapter.OnServic
if (resource.data != null) {
serviceList.clear();
serviceList.addAll(resource.data.getContent());
filterServices(binding.etSearchService != null ? binding.etSearchService.getText().toString() : "");
adapter.notifyDataSetChanged();
}
break;
case ERROR:
@@ -185,7 +187,7 @@ public class ServiceFragment extends Fragment implements ServiceAdapter.OnServic
* Initializes the RecyclerView with a layout manager and adapter for services.
*/
private void setupRecyclerView() {
adapter = new ServiceAdapter(filteredList, this);
adapter = new ServiceAdapter(serviceList, this);
binding.recyclerViewServices.setLayoutManager(new LinearLayoutManager(getContext()));
binding.recyclerViewServices.setAdapter(adapter);
}

View File

@@ -155,7 +155,7 @@ public class AppointmentDetailFragment extends Fragment {
* Loads the list of services from the API.
*/
private void loadServices() {
serviceViewModel.getAllServices(0, 200).observe(getViewLifecycleOwner(), resource -> {
serviceViewModel.getAllServices(0, 200, null, "serviceName").observe(getViewLifecycleOwner(), resource -> {
if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
serviceList = resource.data.getContent();
refreshServiceSpinner();

View File

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

View File

@@ -24,8 +24,8 @@ public class ServiceViewModel extends ViewModel {
/**
* Fetches a paginated list of all services.
*/
public LiveData<Resource<PageResponse<ServiceDTO>>> getAllServices(int page, int size) {
return repository.getAllServices(page, size);
public LiveData<Resource<PageResponse<ServiceDTO>>> getAllServices(int page, int size, String query, String sort) {
return repository.getAllServices(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="Services"
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/etSearchService"
<LinearLayout
android:id="@+id/layoutFilter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="Search by service name or description..."
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/etSearchService"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Search by service name or description..."
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/swipeRefreshService"