added an api connection to Users in Andriod

NOTE Will have to change backend so staffs can access other staffs
This commit is contained in:
Alex
2026-04-07 07:46:22 -06:00
parent 8c5348dbb6
commit 7c0ab0bfae
6 changed files with 123 additions and 4 deletions

View File

@@ -0,0 +1,13 @@
package com.example.petstoremobile.api;
import com.example.petstoremobile.dtos.PageResponse;
import com.example.petstoremobile.dtos.UserDTO;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface UserApi {
@GET("api/v1/users")
Call<PageResponse<UserDTO>> getUsers(@Query("role") String role, @Query("page") int page, @Query("size") int size);
}

View File

@@ -173,4 +173,10 @@ public class NetworkModule {
public static CategoryApi provideCategoryApi(Retrofit retrofit) {
return retrofit.create(CategoryApi.class);
}
}
@Provides
@Singleton
public static UserApi provideUserApi(Retrofit retrofit) {
return retrofit.create(UserApi.class);
}
}

View File

@@ -21,6 +21,7 @@ import com.example.petstoremobile.viewmodels.CustomerViewModel;
import com.example.petstoremobile.viewmodels.PetViewModel;
import com.example.petstoremobile.viewmodels.ServiceViewModel;
import com.example.petstoremobile.viewmodels.StoreViewModel;
import com.example.petstoremobile.viewmodels.UserViewModel;
import java.util.*;
@@ -40,11 +41,13 @@ public class AppointmentDetailFragment extends Fragment {
private long preselectedServiceId = -1;
private long preselectedCustomerId = -1;
private long preselectedStoreId = -1;
private long preselectedStaffId = -1;
private List<PetDTO> petList = new ArrayList<>();
private List<ServiceDTO> serviceList = new ArrayList<>();
private List<CustomerDTO> customerList = new ArrayList<>();
private List<StoreDTO> storeList = new ArrayList<>();
private List<UserDTO> staffList = new ArrayList<>();
private final Integer[] HOURS = {9,10,11,12,13,14,15,16,17};
private final Integer[] MINUTES = {0,15,30,45};
@@ -54,6 +57,7 @@ public class AppointmentDetailFragment extends Fragment {
private ServiceViewModel serviceViewModel;
private StoreViewModel storeViewModel;
private CustomerViewModel customerViewModel;
private UserViewModel userViewModel;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -63,6 +67,7 @@ public class AppointmentDetailFragment extends Fragment {
serviceViewModel = new ViewModelProvider(this).get(ServiceViewModel.class);
storeViewModel = new ViewModelProvider(this).get(StoreViewModel.class);
customerViewModel = new ViewModelProvider(this).get(CustomerViewModel.class);
userViewModel = new ViewModelProvider(this).get(UserViewModel.class);
}
@Override
@@ -128,6 +133,7 @@ public class AppointmentDetailFragment extends Fragment {
loadServices();
loadCustomers();
loadStores();
loadStaff();
}
/**
@@ -215,6 +221,27 @@ public class AppointmentDetailFragment extends Fragment {
preselectedStoreId, StoreDTO::getStoreId);
}
/**
* Loads the list of staff from the API.
*/
private void loadStaff() {
userViewModel.getUsers("STAFF", 0, 100).observe(getViewLifecycleOwner(), resource -> {
if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
staffList = resource.data.getContent();
refreshStaffSpinner();
}
});
}
/**
* Populates the staff selection spinner.
*/
private void refreshStaffSpinner() {
SpinnerUtils.populateSpinner(requireContext(), binding.spinnerStaff, staffList,
UserDTO::getFullName, "-- Select Staff --",
preselectedStaffId, UserDTO::getId);
}
/**
* Handles arguments to determine if the fragment is in edit or add mode.
*/
@@ -247,6 +274,7 @@ public class AppointmentDetailFragment extends Fragment {
preselectedServiceId = (a.getServiceId() != null) ? a.getServiceId() : -1;
preselectedCustomerId = (a.getCustomerId() != null) ? a.getCustomerId() : -1;
preselectedStoreId = (a.getStoreId() != null) ? a.getStoreId() : -1;
preselectedStaffId = (a.getEmployeeId() != null) ? a.getEmployeeId() : -1;
binding.etAppointmentDate.setText(a.getAppointmentDate());
@@ -276,6 +304,7 @@ public class AppointmentDetailFragment extends Fragment {
refreshServiceSpinner();
refreshCustomerSpinner();
refreshStoreSpinner();
refreshStaffSpinner();
} else if (resource.status == Resource.Status.ERROR) {
Toast.makeText(getContext(), "Failed to load appointment: " + resource.message, Toast.LENGTH_SHORT).show();
}
@@ -307,6 +336,11 @@ public class AppointmentDetailFragment extends Fragment {
StoreDTO store = storeList.get(binding.spinnerStore.getSelectedItemPosition() - 1);
PetDTO pet = petList.get(binding.spinnerPet.getSelectedItemPosition() - 1);
ServiceDTO service = serviceList.get(binding.spinnerService.getSelectedItemPosition() - 1);
Long employeeId = null;
if (binding.spinnerStaff.getSelectedItemPosition() > 0) {
employeeId = staffList.get(binding.spinnerStaff.getSelectedItemPosition() - 1).getId();
}
String time = String.format("%02d:%02d",
HOURS[binding.spinnerHour.getSelectedItemPosition()],
@@ -346,6 +380,7 @@ public class AppointmentDetailFragment extends Fragment {
customer.getCustomerId(),
store.getStoreId(),
service.getServiceId(),
employeeId,
date,
time,
status,

View File

@@ -0,0 +1,26 @@
package com.example.petstoremobile.repositories;
import androidx.lifecycle.LiveData;
import com.example.petstoremobile.api.UserApi;
import com.example.petstoremobile.dtos.PageResponse;
import com.example.petstoremobile.dtos.UserDTO;
import com.example.petstoremobile.utils.Resource;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class UserRepository extends BaseRepository {
private final UserApi userApi;
@Inject
public UserRepository(UserApi userApi) {
super("UserRepository");
this.userApi = userApi;
}
public LiveData<Resource<PageResponse<UserDTO>>> getUsers(String role, int page, int size) {
return executeCall(userApi.getUsers(role, page, size));
}
}

View File

@@ -0,0 +1,27 @@
package com.example.petstoremobile.viewmodels;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import com.example.petstoremobile.dtos.PageResponse;
import com.example.petstoremobile.dtos.UserDTO;
import com.example.petstoremobile.repositories.UserRepository;
import com.example.petstoremobile.utils.Resource;
import javax.inject.Inject;
import dagger.hilt.android.lifecycle.HiltViewModel;
@HiltViewModel
public class UserViewModel extends ViewModel {
private final UserRepository userRepository;
@Inject
public UserViewModel(UserRepository userRepository) {
this.userRepository = userRepository;
}
public LiveData<Resource<PageResponse<UserDTO>>> getUsers(String role, int page, int size) {
return userRepository.getUsers(role, page, size);
}
}

View File

@@ -124,15 +124,27 @@
android:textSize="12sp"
android:layout_marginBottom="4dp"/>
<Spinner
android:id="@+id/spinnerService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"/>
<!-- Staff -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Staff"
android:textColor="@color/text_dark"
android:textSize="12sp"
android:layout_marginBottom="4dp"/>
<Spinner
android:id="@+id/spinnerStaff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"/>
<!-- Appointment Date -->
<TextView