Advertisement
Guest User

Untitled

a guest
Oct 8th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. public static List<User> userList;
  2. private EditText editTextPhoneNumber;
  3. private EditText editTextPassword;
  4. private Button buttonLogIn;
  5. private DatabaseReference databaseUsers;
  6.  
  7. public static List<User> listSameChildren;
  8. public static User currentUser;
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_log_in);
  14.  
  15. databaseUsers = FirebaseDatabase.getInstance().getReference("Users");
  16.  
  17. userList = new ArrayList<>();
  18.  
  19. editTextPhoneNumber = (EditText) findViewById(R.id.editTextPhone);
  20. editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  21. buttonLogIn = (Button) findViewById(R.id.buttonLogIn);
  22.  
  23. buttonLogIn.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View view) {
  26. if (credentialsFound())
  27. {
  28. logIn();
  29. }
  30. }
  31. });
  32.  
  33. }
  34.  
  35. protected void onStart() {
  36. super.onStart();
  37.  
  38. databaseUsers.addValueEventListener(new ValueEventListener() {
  39. @Override
  40. public void onDataChange(DataSnapshot dataSnapshot) {
  41. userList.clear();
  42. for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
  43. User user = userSnapshot.getValue(User.class);
  44. userList.add(user);
  45. Log.i("YO", userList.get(0).getCredentials());
  46. }
  47. }
  48.  
  49. @Override
  50. public void onCancelled(DatabaseError databaseError) {
  51. Log.i("YO", "Failed to post");
  52. }
  53.  
  54. });
  55. }
  56.  
  57. private boolean credentialsFound()
  58. {
  59. String phoneNumber = editTextPhoneNumber.getText().toString().trim();
  60. String password = editTextPassword.getText().toString().trim();
  61.  
  62. String familyKey;
  63.  
  64. for (int i = 0; i < userList.size(); i++)
  65. {
  66. if (phoneNumber.equals(userList.get(i).getUserPhoneNumber()))
  67. {
  68. if (password.equals(userList.get(i).getUserPassword()))
  69. {
  70. currentUser = userList.get(i);
  71. familyKey = userList.get(i).getFamilyKey();
  72. findChildrenOfFamily(familyKey);
  73. return true;
  74. }
  75. }
  76. }
  77. Toast.makeText(this, "Enter the correct information!", Toast.LENGTH_LONG).show();
  78. return false;
  79. }
  80.  
  81. private void logIn()
  82. {
  83. Toast.makeText(this, "It worked!", Toast.LENGTH_LONG).show();
  84. if (currentUser.isUserIsParent())
  85. {
  86. Intent intent = new Intent(this, ParentMainActivity.class);
  87. startActivity(intent);
  88. }
  89. else if (!currentUser.isUserIsParent())
  90. {
  91. Intent intent = new Intent(this, ChildMainActivity.class);
  92. startActivity(intent);
  93. }
  94. }
  95.  
  96. private List<User> findChildrenOfFamily(String familyKey)
  97. {
  98. List<User> listSameFamily = new ArrayList<>();
  99. listSameChildren = new ArrayList<>();
  100. for (int i = 0; i < userList.size(); i++)
  101. {
  102. if (familyKey.equals(userList.get(i).getFamilyKey()))
  103. {
  104. listSameFamily.add(userList.get(i));
  105. }
  106. }
  107.  
  108. for (int i = 0; i < listSameFamily.size(); i++)
  109. {
  110. if (!listSameFamily.get(i).isUserIsParent())
  111. {
  112. listSameChildren.add(listSameFamily.get(i));
  113. }
  114. }
  115.  
  116. return listSameChildren;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement