Search in sources :

Example 1 with FragmentEditUserBinding

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();
}
Also used : UsersDataModel(com.example.first_responder_app.dataModels.UsersDataModel) Spinner(android.widget.Spinner) NavController(androidx.navigation.NavController) View(android.view.View) AdapterView(android.widget.AdapterView) NavDirections(androidx.navigation.NavDirections) EditUserViewModel(com.example.first_responder_app.viewModels.EditUserViewModel) UserViewModel(com.example.first_responder_app.viewModels.UserViewModel) FragmentEditUserBinding(com.example.first_responder_app.databinding.FragmentEditUserBinding) AdapterView(android.widget.AdapterView) NavHostFragment(androidx.navigation.fragment.NavHostFragment) FirestoreDatabase(com.example.first_responder_app.FirestoreDatabase) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Aggregations

View (android.view.View)1 AdapterView (android.widget.AdapterView)1 Spinner (android.widget.Spinner)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 NavController (androidx.navigation.NavController)1 NavDirections (androidx.navigation.NavDirections)1 NavHostFragment (androidx.navigation.fragment.NavHostFragment)1 FirestoreDatabase (com.example.first_responder_app.FirestoreDatabase)1 UsersDataModel (com.example.first_responder_app.dataModels.UsersDataModel)1 FragmentEditUserBinding (com.example.first_responder_app.databinding.FragmentEditUserBinding)1 EditUserViewModel (com.example.first_responder_app.viewModels.EditUserViewModel)1 UserViewModel (com.example.first_responder_app.viewModels.UserViewModel)1