Advertisement
Guest User

Untitled

a guest
Oct 5th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. import android.content.DialogInterface;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.support.v7.app.AlertDialog;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.Toast;
  10.  
  11. import com.google.firebase.database.DataSnapshot;
  12. import com.google.firebase.database.DatabaseError;
  13. import com.google.firebase.database.DatabaseReference;
  14. import com.google.firebase.database.FirebaseDatabase;
  15. import com.google.firebase.database.ValueEventListener;
  16. import com.rengwuxian.materialedittext.MaterialEditText;
  17.  
  18. import sonu.enigma.Common.Common;
  19. import sonu.enigma.Model.User;
  20.  
  21. import static sonu.enigma.R.drawable.l;
  22.  
  23. public class MainActivity extends AppCompatActivity {
  24. MaterialEditText edtNewUser, edtNewPassword, edtNewEmail; //for Sign Up
  25. MaterialEditText edtUser, edtPassword; //for Sign In
  26. Button btnSignUp, btnSignIn;
  27. FirebaseDatabase database;
  28. DatabaseReference users;
  29.  
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  35.  
  36. //Firebase
  37. database=FirebaseDatabase.getInstance();
  38. users=database.getReference("Users");
  39. edtUser=(MaterialEditText)findViewById(R.id.edtUserName);
  40. edtPassword=(MaterialEditText)findViewById(R.id.edtPassword);
  41. btnSignIn=(Button)findViewById(R.id.btn_sign_in);
  42. btnSignUp=(Button)findViewById(R.id.btn_sign_up);
  43.  
  44. btnSignUp.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. showSignUpDialog();
  48. }
  49. });
  50. btnSignIn.setOnClickListener(new View.OnClickListener() {
  51. @Override
  52. public void onClick(View v) {
  53. signIn(edtUser.getText().toString(), edtPassword.getText().toString());
  54. }
  55. });
  56.  
  57.  
  58. }
  59.  
  60. private void signIn(final String user, final String pwd) {
  61. users.addListenerForSingleValueEvent(new ValueEventListener() {
  62. @Override
  63. public void onDataChange(DataSnapshot dataSnapshot) {
  64. if (dataSnapshot.child(user).exists()){
  65. if (!user.isEmpty()){
  66. User login=dataSnapshot.child(user).getValue(User.class);
  67. if (login.getPassword().equals(pwd)){
  68. Intent homeActivity = new Intent(MainActivity.this, Home.class);
  69.  
  70. Common.CurrentUser=login;
  71.  
  72. startActivity(homeActivity);
  73. finish();
  74.  
  75. }
  76. else {
  77. Toast.makeText(MainActivity.this, "Wrong Password", Toast.LENGTH_SHORT).show();
  78. }
  79.  
  80. }
  81. else {
  82. Toast.makeText(MainActivity.this, "Please enter your User name", Toast.LENGTH_SHORT).show();
  83.  
  84. }
  85. }
  86. else{
  87. Toast.makeText(MainActivity.this, "User is not exists!", Toast.LENGTH_SHORT).show();
  88.  
  89. }
  90. }
  91.  
  92. @Override
  93. public void onCancelled(DatabaseError databaseError) {
  94.  
  95. }
  96. });
  97.  
  98. }
  99.  
  100. private void showSignUpDialog() {
  101. AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
  102. alertDialog.setTitle("Sign Up");
  103. alertDialog.setMessage("Kindly, fill the information!");
  104. LayoutInflater inflater= this.getLayoutInflater();
  105. View sign_up_layout= inflater.inflate(R.layout.sign_up_layout, null);
  106. edtNewUser=(MaterialEditText)sign_up_layout.findViewById(R.id.edtNewUserName);
  107. edtNewEmail=(MaterialEditText)sign_up_layout.findViewById(R.id.edtNewEmail);
  108. edtNewPassword=(MaterialEditText)sign_up_layout.findViewById(R.id.edtNewPassword);
  109.  
  110. alertDialog.setView(sign_up_layout);
  111. alertDialog.setIcon(R.drawable.ic_account_circle_black_24dp);
  112.  
  113. alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
  114. @Override
  115. public void onClick(DialogInterface dialogInterface, int i) {
  116. dialogInterface.dismiss();
  117.  
  118. }
  119. });
  120.  
  121. alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
  122. @Override
  123. public void onClick(DialogInterface dialogInterface, int i) {
  124. final User user= new User(edtNewUser.getText().toString(),
  125. edtNewPassword.getText().toString(),
  126. edtNewEmail.getText().toString());
  127.  
  128. users.addListenerForSingleValueEvent(new ValueEventListener() {
  129. @Override
  130. public void onDataChange(DataSnapshot dataSnapshot) {
  131. if(dataSnapshot.child(user.getUserName()).exists())
  132. Toast.makeText(MainActivity.this, "User already exists!", Toast.LENGTH_SHORT).show();
  133. else {
  134. users.child(user.getUserName())
  135. .setValue(user);
  136. Toast.makeText(MainActivity.this, "User Registration Successful!", Toast.LENGTH_SHORT).show();
  137. }
  138.  
  139. }
  140.  
  141. @Override
  142. public void onCancelled(DatabaseError databaseError) {
  143.  
  144. }
  145. });
  146. dialogInterface.dismiss();
  147. }
  148. });
  149. alertDialog.show();
  150. }
  151. }
  152. `][1]
  153.  
  154. `package sonu.enigma.Model;
  155.  
  156. /**
  157. * Created by PAWAN on 01-10-2017.
  158. */
  159.  
  160. public class User {
  161. private String userName;
  162. private String password;
  163. private String email;
  164. public User(){
  165.  
  166. }
  167. public User(String userName, String password, String email){
  168. this.userName=userName;
  169. this.password=password;
  170. this.email =email;
  171.  
  172.  
  173. }
  174.  
  175. public String getUserName() {
  176. return userName;
  177. }
  178.  
  179. public void setUserName(String userName) {
  180. this.userName = userName;
  181. }
  182.  
  183. public String getPassword() {
  184. return password;
  185. }
  186.  
  187. public void setPassword(String password) {
  188. this.password = password;
  189. }
  190.  
  191. public String getEmail() {
  192. return email;
  193. }
  194.  
  195. public void setEmail(String email) {
  196. this.email = email;
  197. }
  198. }
  199. `
  200.  
  201.  
  202. [
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement