use of com.example.first_responder_app.databinding.FragmentEditUserBinding in project FirstResponse by mattpost1700.
the class EditUserFragment method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// binding fragment with nav_map by using navHostFragment, throw this block of code in there and that allows you to switch to other fragments
FragmentEditUserBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_edit_user, container, false);
NavHostFragment navHostFragment = (NavHostFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
// TODO: navCont created for side bar(still need to be implemented)
NavController navController = navHostFragment.getNavController();
// sharedViewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
firestoreDatabase = new FirestoreDatabase();
db = firestoreDatabase.getDb();
activeUser = (ActiveUser) getActivity();
if (activeUser != null) {
user = activeUser.getActive();
}
Spinner rankSpinner = binding.userRank;
ranksAndIds = new HashMap<>();
populateRanks(rankSpinner);
populateEditTexts(binding.userFirstName, binding.userLastName, binding.userPhone, binding.userAddress);
rankSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
rankSpinner.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// can leave this empty
}
});
binding.saveButton.setOnClickListener(v -> {
String firstName = binding.userFirstName.getText().toString();
String lastName = binding.userLastName.getText().toString();
String phone = binding.userPhone.getText().toString();
String address = binding.userAddress.getText().toString();
String id = "";
if (activeUser != null) {
UsersDataModel user = activeUser.getActive();
if (user != null) {
id = user.getDocumentId();
}
}
String rankName = binding.userRank.getSelectedItem().toString();
String rankID = ranksAndIds.get(rankName);
if (id != null) {
String errorMsg = "";
if (!firestoreDatabase.validateName(firstName)) {
errorMsg = "First name has invalid format";
} else if (!firestoreDatabase.validateName(lastName)) {
errorMsg = "Last name has invalid format";
} else if (!firestoreDatabase.validatePhone(phone)) {
errorMsg = "Phone number has invalid format";
}
if (errorMsg.equals("")) {
// TODO: await
firestoreDatabase.editUser(firstName, lastName, rankID, phone, address, id, getActivity());
user.setFirst_name(firstName);
user.setLast_name(lastName);
user.setRank_id(rankID);
user.setPhone_number(phone);
user.setAddress(address);
UserViewModel userViewModel = new ViewModelProvider(this).get(UserViewModel.class);
userViewModel.setUserDataModel(user);
NavDirections action = EditUserFragmentDirections.actionEditUserFragmentToUserFragment();
Navigation.findNavController(binding.getRoot()).navigate(action);
} else {
Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_SHORT).show();
}
} else {
Log.d("User", "No active user found");
}
});
return binding.getRoot();
}
Aggregations