Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.bishal.evader;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.example.bishal.evader.Model.Users;
- import com.example.bishal.evader.Prevalent.Prevalent;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.ValueEventListener;
- import com.rey.material.widget.CheckBox;
- import io.paperdb.Paper;
- public class LoginActivity extends AppCompatActivity
- {
- private EditText InputPhoneNumber, InputPassword;
- private Button LoginButton;
- private ProgressDialog loadingBar;
- private TextView AdminLink, NotAdminLink;
- private String parentDbName = "Users";
- private CheckBox chkBoxRememberMe;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- LoginButton = (Button) findViewById(R.id.login_btn);
- InputPassword = (EditText) findViewById(R.id.login_password_input);
- InputPhoneNumber = (EditText) findViewById(R.id.login_phone_number_input);
- AdminLink = (TextView) findViewById(R.id.admin_panel_link);
- NotAdminLink = (TextView) findViewById(R.id.not_admin_panel_link);
- loadingBar = new ProgressDialog(this);
- chkBoxRememberMe = (CheckBox) findViewById(R.id.remember_me_chkb);
- Paper.init(this);
- LoginButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v)
- {
- LoginUser();
- }
- });
- //admin and users buttons change accordingly if users or admin press them accidentally
- AdminLink.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v)
- {
- LoginButton.setText("Login Admin");
- AdminLink.setVisibility(View.INVISIBLE);
- NotAdminLink.setVisibility(View.VISIBLE);
- parentDbName = "Admins";
- }
- });
- NotAdminLink.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v)
- {
- LoginButton.setText("Login");
- AdminLink.setVisibility(View.VISIBLE);
- NotAdminLink.setVisibility(View.INVISIBLE);
- parentDbName = "Users";
- }
- });
- }
- private void LoginUser()
- {
- String phone = InputPhoneNumber.getText().toString();
- String password = InputPassword.getText().toString();
- if (TextUtils.isEmpty(phone))
- {
- Toast.makeText(this, "Please type your phone number here...", Toast.LENGTH_SHORT).show();
- }
- else if (TextUtils.isEmpty(password))
- {
- Toast.makeText(this, "Please type your password here...", Toast.LENGTH_SHORT).show();
- }
- else
- {
- loadingBar.setTitle("Login Account");
- loadingBar.setMessage("Please wait as the credentials are being checked");
- loadingBar.setCanceledOnTouchOutside(false);
- loadingBar.show();
- AllowAccessToAccount(phone, password);
- }
- }
- private void AllowAccessToAccount(final String phone, final String password)
- {
- //ifthe checkbox for remember me is ticked it will return value
- if (chkBoxRememberMe.isChecked())
- {
- Paper.book().write(Prevalent.UserPhoneKey, phone);
- Paper.book().write(Prevalent.UserPasswordKey, password);
- }
- final DatabaseReference RootRef;
- RootRef = FirebaseDatabase.getInstance().getReference();
- RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot)
- {
- if (dataSnapshot.child(parentDbName).child(phone).exists())
- {
- Users usersData = dataSnapshot.child(parentDbName).child(phone).getValue(Users.class);
- if (usersData.getPhone().equals(phone)) {
- if (usersData.getPassword().equals(password)) {
- if (parentDbName.equals("Admins")) {
- Toast.makeText(LoginActivity.this, "Welcome Admin You are logged in", Toast.LENGTH_SHORT).show();
- loadingBar.dismiss();
- Intent intent = new Intent(LoginActivity.this, AdminCategoryActivity.class);
- startActivity(intent);
- } else if (parentDbName.equals("Users")) {
- Toast.makeText(LoginActivity.this, "You are logged in", Toast.LENGTH_SHORT).show();
- loadingBar.dismiss();
- Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
- Prevalent.currentOnlineUser = usersData;
- startActivity(intent);
- }
- }
- else
- {
- loadingBar.dismiss();
- Toast.makeText(LoginActivity.this, "Your password is incorrect", Toast.LENGTH_SHORT).show();
- }
- }
- }
- else
- {
- Toast.makeText(LoginActivity.this, "Account on this " + phone + " number is not in database", Toast.LENGTH_SHORT).show();
- loadingBar.dismiss();
- Toast.makeText(LoginActivity.this, "Please create an Account idiot", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError)
- {
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment