Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- 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.rengwuxian.materialedittext.MaterialEditText;
- import sonu.enigma.Common.Common;
- import sonu.enigma.Model.User;
- import static sonu.enigma.R.drawable.l;
- public class MainActivity extends AppCompatActivity {
- MaterialEditText edtNewUser, edtNewPassword, edtNewEmail; //for Sign Up
- MaterialEditText edtUser, edtPassword; //for Sign In
- Button btnSignUp, btnSignIn;
- FirebaseDatabase database;
- DatabaseReference users;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //Firebase
- database=FirebaseDatabase.getInstance();
- users=database.getReference("Users");
- edtUser=(MaterialEditText)findViewById(R.id.edtUserName);
- edtPassword=(MaterialEditText)findViewById(R.id.edtPassword);
- btnSignIn=(Button)findViewById(R.id.btn_sign_in);
- btnSignUp=(Button)findViewById(R.id.btn_sign_up);
- btnSignUp.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showSignUpDialog();
- }
- });
- btnSignIn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- signIn(edtUser.getText().toString(), edtPassword.getText().toString());
- }
- });
- }
- private void signIn(final String user, final String pwd) {
- users.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- if (dataSnapshot.child(user).exists()){
- if (!user.isEmpty()){
- User login=dataSnapshot.child(user).getValue(User.class);
- if (login.getPassword().equals(pwd)){
- Intent homeActivity = new Intent(MainActivity.this, Home.class);
- Common.CurrentUser=login;
- startActivity(homeActivity);
- finish();
- }
- else {
- Toast.makeText(MainActivity.this, "Wrong Password", Toast.LENGTH_SHORT).show();
- }
- }
- else {
- Toast.makeText(MainActivity.this, "Please enter your User name", Toast.LENGTH_SHORT).show();
- }
- }
- else{
- Toast.makeText(MainActivity.this, "User is not exists!", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- }
- private void showSignUpDialog() {
- AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
- alertDialog.setTitle("Sign Up");
- alertDialog.setMessage("Kindly, fill the information!");
- LayoutInflater inflater= this.getLayoutInflater();
- View sign_up_layout= inflater.inflate(R.layout.sign_up_layout, null);
- edtNewUser=(MaterialEditText)sign_up_layout.findViewById(R.id.edtNewUserName);
- edtNewEmail=(MaterialEditText)sign_up_layout.findViewById(R.id.edtNewEmail);
- edtNewPassword=(MaterialEditText)sign_up_layout.findViewById(R.id.edtNewPassword);
- alertDialog.setView(sign_up_layout);
- alertDialog.setIcon(R.drawable.ic_account_circle_black_24dp);
- alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- final User user= new User(edtNewUser.getText().toString(),
- edtNewPassword.getText().toString(),
- edtNewEmail.getText().toString());
- users.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- if(dataSnapshot.child(user.getUserName()).exists())
- Toast.makeText(MainActivity.this, "User already exists!", Toast.LENGTH_SHORT).show();
- else {
- users.child(user.getUserName())
- .setValue(user);
- Toast.makeText(MainActivity.this, "User Registration Successful!", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- dialogInterface.dismiss();
- }
- });
- alertDialog.show();
- }
- }
- `][1]
- `package sonu.enigma.Model;
- /**
- * Created by PAWAN on 01-10-2017.
- */
- public class User {
- private String userName;
- private String password;
- private String email;
- public User(){
- }
- public User(String userName, String password, String email){
- this.userName=userName;
- this.password=password;
- this.email =email;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- }
- `
- [
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement