|
| 1 | +package edu.sharif.homework1; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.view.LayoutInflater; |
| 5 | +import android.view.View; |
| 6 | +import android.view.ViewGroup; |
| 7 | +import android.widget.Button; |
| 8 | +import android.widget.Toast; |
| 9 | + |
| 10 | +import androidx.annotation.NonNull; |
| 11 | +import androidx.fragment.app.Fragment; |
| 12 | +import androidx.navigation.fragment.NavHostFragment; |
| 13 | + |
| 14 | +import edu.sharif.homework1.databinding.FragmentLoginBinding; |
| 15 | + |
| 16 | +public class LoginFragment extends Fragment { |
| 17 | + |
| 18 | + private FragmentLoginBinding binding; |
| 19 | + |
| 20 | + private String username; |
| 21 | + private String enteredPassword; |
| 22 | + |
| 23 | + @Override |
| 24 | + public View onCreateView( |
| 25 | + LayoutInflater inflater, ViewGroup container, |
| 26 | + Bundle savedInstanceState |
| 27 | + ) { |
| 28 | + |
| 29 | + binding = FragmentLoginBinding.inflate(inflater, container, false); |
| 30 | + return binding.getRoot(); |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { |
| 36 | + super.onViewCreated(view, savedInstanceState); |
| 37 | + |
| 38 | + Button signUpButton = view.findViewById(R.id.button); |
| 39 | + signUpButton.setOnClickListener(view1 -> { |
| 40 | + NavHostFragment.findNavController(LoginFragment.this) |
| 41 | + .navigate(LoginFragmentDirections.actionLoginFragmentToSignupFragment()); |
| 42 | + }); |
| 43 | + |
| 44 | + binding.submitButton.setOnClickListener(view1 -> { |
| 45 | + |
| 46 | + username = binding.usernameText.getText().toString(); |
| 47 | + enteredPassword = binding.passwordText.getText().toString(); |
| 48 | + |
| 49 | + if (username.isEmpty()) { |
| 50 | + Toast.makeText(getContext(), "Please fill username field.", |
| 51 | + Toast.LENGTH_LONG).show(); |
| 52 | + } else { |
| 53 | + User user = User.getUserByUsername(username); |
| 54 | + |
| 55 | + if (user == null) { |
| 56 | + Toast.makeText(getContext(), "there is no user with this username.", |
| 57 | + Toast.LENGTH_LONG).show(); |
| 58 | + } else if (!user.getPassword().equals(enteredPassword)) { |
| 59 | + Toast.makeText(getContext(), "wrong username or password", |
| 60 | + Toast.LENGTH_LONG).show(); |
| 61 | + } else { |
| 62 | + if(user.getClass().equals(Student.class)) |
| 63 | + openStudentPanelFragment(); |
| 64 | + else openProfessorPanelFragment(); |
| 65 | + } |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + private void openStudentPanelFragment() { |
| 72 | + NavHostFragment.findNavController(LoginFragment.this) |
| 73 | + .navigate(LoginFragmentDirections. |
| 74 | + actionLoginFragmentToStudentPanelFragment(username)); |
| 75 | + } |
| 76 | + |
| 77 | + private void openProfessorPanelFragment() { |
| 78 | + NavHostFragment.findNavController(LoginFragment.this) |
| 79 | + .navigate(LoginFragmentDirections. |
| 80 | + actionLoginFragmentToProfessorPanelFragment(username)); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | +} |
0 commit comments