added petspecies spinner
This commit is contained in:
@@ -30,6 +30,7 @@ import com.example.petstoremobile.utils.SpinnerUtils;
|
||||
import com.example.petstoremobile.utils.UIUtils;
|
||||
import com.example.petstoremobile.viewmodels.PetDetailViewModel;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -116,13 +117,16 @@ public class PetDetailFragment extends Fragment {
|
||||
|
||||
private void savePet() {
|
||||
if (!InputValidator.isNotEmpty(binding.etPetName, "Pet Name")) return;
|
||||
if (!InputValidator.isNotEmpty(binding.etPetSpecies, "Species")) return;
|
||||
if (!InputValidator.isSpinnerSelected(binding.spinnerPetSpecies, "Species")) return;
|
||||
if (!InputValidator.isNotEmpty(binding.etPetBreed, "Breed")) return;
|
||||
if (!InputValidator.isPositiveInteger(binding.etPetAge, "Age")) return;
|
||||
if (!InputValidator.isPositiveDecimal(binding.etPetPrice, "Price")) return;
|
||||
|
||||
String name = binding.etPetName.getText().toString().trim();
|
||||
String species = binding.etPetSpecies.getText().toString().trim();
|
||||
List<DropdownDTO> speciesOptions = viewModel.getSpeciesList().getValue();
|
||||
String species = (speciesOptions != null && binding.spinnerPetSpecies.getSelectedItemPosition() > 0)
|
||||
? speciesOptions.get(binding.spinnerPetSpecies.getSelectedItemPosition() - 1).getLabel()
|
||||
: "";
|
||||
String breed = binding.etPetBreed.getText().toString().trim();
|
||||
int age = Integer.parseInt(binding.etPetAge.getText().toString().trim());
|
||||
double price = Double.parseDouble(binding.etPetPrice.getText().toString().trim());
|
||||
@@ -218,7 +222,6 @@ public class PetDetailFragment extends Fragment {
|
||||
if (resource.status == Resource.Status.SUCCESS && resource.data != null) {
|
||||
PetDTO p = resource.data;
|
||||
binding.etPetName.setText(p.getPetName());
|
||||
binding.etPetSpecies.setText(p.getPetSpecies());
|
||||
binding.etPetBreed.setText(p.getPetBreed());
|
||||
binding.etPetAge.setText(String.valueOf(p.getPetAge()));
|
||||
if (p.getPetPrice() != null) {
|
||||
@@ -257,6 +260,11 @@ public class PetDetailFragment extends Fragment {
|
||||
private void setupSpinner() {
|
||||
SpinnerUtils.setupStringSpinner(requireContext(), binding.spinnerPetStatus, new String[]{});
|
||||
|
||||
SpinnerUtils.setOnIndexSelectedListener(binding.spinnerPetSpecies, p -> {
|
||||
if (isUpdatingUI) return;
|
||||
viewModel.onSpeciesSelected(p);
|
||||
});
|
||||
|
||||
binding.spinnerCustomer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
@@ -306,7 +314,7 @@ public class PetDetailFragment extends Fragment {
|
||||
binding.btnDeletePet.setVisibility(state.isDeleteVisible ? View.VISIBLE : View.GONE);
|
||||
binding.btnSavePet.setText(state.saveButtonText);
|
||||
|
||||
UIUtils.setViewsEnabled(state.isSpeciesEnabled, binding.etPetSpecies);
|
||||
UIUtils.setViewsEnabled(state.isSpeciesEnabled, binding.spinnerPetSpecies);
|
||||
UIUtils.setViewsEnabled(state.isBreedEnabled, binding.etPetBreed);
|
||||
UIUtils.setViewsEnabled(state.isCustomerEnabled, binding.spinnerCustomer);
|
||||
UIUtils.setViewsEnabled(state.isStoreEnabled, binding.spinnerStore);
|
||||
@@ -317,6 +325,13 @@ public class PetDetailFragment extends Fragment {
|
||||
updateCustomerSpinnerSelection(state.selectedCustomerId);
|
||||
updateStoreSpinnerSelection(state.selectedStoreId);
|
||||
|
||||
List<DropdownDTO> species = viewModel.getSpeciesList().getValue();
|
||||
if (species != null) {
|
||||
SpinnerUtils.populateSpinner(requireContext(), binding.spinnerPetSpecies, species,
|
||||
DropdownDTO::getLabel, "-- Select Species --", null, DropdownDTO::getId);
|
||||
SpinnerUtils.setSelectionByValue(binding.spinnerPetSpecies, state.selectedSpecies);
|
||||
}
|
||||
|
||||
if (!state.isCustomerEnabled && binding.spinnerCustomer.getSelectedItemPosition() != 0) {
|
||||
binding.spinnerCustomer.setSelection(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user