fixed creating adoption for the backend and implemented adoption to andriod for changes

This commit is contained in:
Alex
2026-04-07 07:27:37 -06:00
parent 77e93a38b7
commit 8c5348dbb6
7 changed files with 105 additions and 36 deletions

View File

@@ -57,7 +57,7 @@ public class AdoptionAdapter extends RecyclerView.Adapter<AdoptionAdapter.Adopti
holder.tvStatus.setText(status);
switch (status) {
case "Approved":
case "Completed":
holder.tvStatus.setBackgroundColor(Color.parseColor("#4CAF50"));
break;
case "Pending":

View File

@@ -10,20 +10,27 @@ public class AdoptionDTO {
private String customerName;
private Long employeeId;
private String employeeName;
private Long sourceStoreId;
private String sourceStoreName;
private String adoptionDate;
private String adoptionStatus;
private BigDecimal adoptionFee;
private String createdAt;
private String updatedAt;
public AdoptionDTO(Long petId, Long customerId, String adoptionDate, String adoptionStatus) {
this(petId, customerId, null, adoptionDate, adoptionStatus);
public AdoptionDTO(Long petId, Long customerId, Long sourceStoreId, String adoptionDate, String adoptionStatus) {
this.petId = petId;
this.customerId = customerId;
this.sourceStoreId = sourceStoreId;
this.adoptionDate = adoptionDate;
this.adoptionStatus = adoptionStatus;
}
public AdoptionDTO(Long petId, Long customerId, Long employeeId, String adoptionDate, String adoptionStatus) {
public AdoptionDTO(Long petId, Long customerId, Long employeeId, Long sourceStoreId, String adoptionDate, String adoptionStatus) {
this.petId = petId;
this.customerId = customerId;
this.employeeId = employeeId;
this.sourceStoreId = sourceStoreId;
this.adoptionDate = adoptionDate;
this.adoptionStatus = adoptionStatus;
}
@@ -56,6 +63,14 @@ public class AdoptionDTO {
return employeeName;
}
public Long getSourceStoreId() {
return sourceStoreId;
}
public String getSourceStoreName() {
return sourceStoreName;
}
public String getAdoptionDate() {
return adoptionDate;
}

View File

@@ -18,6 +18,7 @@ import com.example.petstoremobile.utils.SpinnerUtils;
import com.example.petstoremobile.viewmodels.AdoptionViewModel;
import com.example.petstoremobile.viewmodels.CustomerViewModel;
import com.example.petstoremobile.viewmodels.PetViewModel;
import com.example.petstoremobile.viewmodels.StoreViewModel;
import java.util.*;
@@ -35,15 +36,18 @@ public class AdoptionDetailFragment extends Fragment {
private boolean isEditing = false;
private long preselectedPetId = -1;
private long preselectedCustomerId = -1;
private long preselectedStoreId = -1;
private List<PetDTO> petList = new ArrayList<>();
private List<CustomerDTO> customerList = new ArrayList<>();
private List<StoreDTO> storeList = new ArrayList<>();
private final String[] STATUSES = {"Pending", "Approved", "Rejected"};
private final String[] STATUSES = {"Pending", "Completed", "Cancelled"};
private AdoptionViewModel adoptionViewModel;
private PetViewModel petViewModel;
private CustomerViewModel customerViewModel;
private StoreViewModel storeViewModel;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -51,6 +55,7 @@ public class AdoptionDetailFragment extends Fragment {
adoptionViewModel = new ViewModelProvider(this).get(AdoptionViewModel.class);
petViewModel = new ViewModelProvider(this).get(PetViewModel.class);
customerViewModel = new ViewModelProvider(this).get(CustomerViewModel.class);
storeViewModel = new ViewModelProvider(this).get(StoreViewModel.class);
}
@Override
@@ -107,6 +112,7 @@ public class AdoptionDetailFragment extends Fragment {
private void loadSpinnersData() {
loadPets();
loadCustomers();
loadStores();
}
/**
@@ -152,6 +158,27 @@ public class AdoptionDetailFragment extends Fragment {
preselectedCustomerId, CustomerDTO::getCustomerId);
}
/**
* Loads the list of stores from the API.
*/
private void loadStores() {
storeViewModel.getAllStores(0, 200).observe(getViewLifecycleOwner(), resource -> {
if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
storeList = resource.data.getContent();
refreshStoreSpinner();
}
});
}
/**
* Populates the store selection spinner with data.
*/
private void refreshStoreSpinner() {
SpinnerUtils.populateSpinner(requireContext(), binding.spinnerAdoptionStore, storeList,
StoreDTO::getStoreName, "-- Select Store --",
preselectedStoreId, StoreDTO::getStoreId);
}
/**
* Handles arguments to determine if the fragment is in edit or add mode.
*/
@@ -182,11 +209,13 @@ public class AdoptionDetailFragment extends Fragment {
AdoptionDTO a = resource.data;
preselectedPetId = a.getPetId() != null ? a.getPetId() : -1;
preselectedCustomerId = a.getCustomerId() != null ? a.getCustomerId() : -1;
preselectedStoreId = a.getSourceStoreId() != null ? a.getSourceStoreId() : -1;
binding.etAdoptionDate.setText(a.getAdoptionDate());
SpinnerUtils.setSelectionByValue(binding.spinnerAdoptionStatus, a.getAdoptionStatus());
refreshPetSpinner();
refreshCustomerSpinner();
refreshStoreSpinner();
} else if (resource.status == Resource.Status.ERROR) {
Toast.makeText(getContext(), "Failed to load adoption: " + resource.message, Toast.LENGTH_SHORT).show();
}
@@ -203,6 +232,9 @@ public class AdoptionDetailFragment extends Fragment {
if (binding.spinnerAdoptionPet.getSelectedItemPosition() == 0) {
Toast.makeText(getContext(), "Select a pet", Toast.LENGTH_SHORT).show(); return;
}
if (binding.spinnerAdoptionStore.getSelectedItemPosition() == 0) {
Toast.makeText(getContext(), "Select a store", Toast.LENGTH_SHORT).show(); return;
}
String date = binding.etAdoptionDate.getText().toString().trim();
if (date.isEmpty()) {
Toast.makeText(getContext(), "Select a date", Toast.LENGTH_SHORT).show(); return;
@@ -210,11 +242,13 @@ public class AdoptionDetailFragment extends Fragment {
CustomerDTO customer = customerList.get(binding.spinnerAdoptionCustomer.getSelectedItemPosition() - 1);
PetDTO pet = petList.get(binding.spinnerAdoptionPet.getSelectedItemPosition() - 1);
StoreDTO store = storeList.get(binding.spinnerAdoptionStore.getSelectedItemPosition() - 1);
String status = STATUSES[binding.spinnerAdoptionStatus.getSelectedItemPosition()];
AdoptionDTO dto = new AdoptionDTO(
pet.getPetId(),
customer.getCustomerId(),
store.getStoreId(),
date,
status
);

View File

@@ -95,6 +95,20 @@
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Source Store"
android:textColor="@color/text_dark"
android:textSize="12sp"
android:layout_marginBottom="4dp"/>
<Spinner
android:id="@+id/spinnerAdoptionStore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"/>
<!-- Adoption Date -->
<TextView
android:layout_width="wrap_content"
@@ -165,4 +179,4 @@
</LinearLayout>
</LinearLayout>
</LinearLayout>