Skip to content

Commit b6446a0

Browse files
committed
Login menu added
1 parent 2f58f53 commit b6446a0

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".LoginFragment">
8+
9+
10+
<LinearLayout
11+
android:id="@+id/linearLayout2"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:orientation="vertical"
15+
app:layout_constraintBottom_toBottomOf="parent"
16+
app:layout_constraintEnd_toEndOf="parent"
17+
app:layout_constraintStart_toStartOf="parent"
18+
app:layout_constraintTop_toTopOf="parent">
19+
20+
<EditText
21+
android:id="@+id/username_text"
22+
android:layout_width="match_parent"
23+
android:layout_height="wrap_content"
24+
android:ems="10"
25+
android:hint="@string/UserName"
26+
android:inputType="text" />
27+
28+
<EditText
29+
android:id="@+id/password_text"
30+
android:layout_width="match_parent"
31+
android:layout_height="wrap_content"
32+
android:ems="10"
33+
android:hint="@string/password"
34+
android:inputType="textPassword" />
35+
36+
<Button
37+
android:id="@+id/submit_button"
38+
android:layout_width="match_parent"
39+
android:layout_height="wrap_content"
40+
android:text="@string/login" />
41+
</LinearLayout>
42+
43+
<Button
44+
android:id="@+id/button"
45+
android:layout_width="wrap_content"
46+
android:layout_height="wrap_content"
47+
android:text="don't have an account? sign up"
48+
app:layout_constraintBottom_toBottomOf="parent"
49+
app:layout_constraintEnd_toEndOf="parent"
50+
app:layout_constraintHorizontal_bias="0.496"
51+
app:layout_constraintStart_toStartOf="parent"
52+
app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
53+
app:layout_constraintVertical_bias="0.459" />
54+
55+
56+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)