Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. package ie.app.barbershop;
  2.  
  3. import android.content.SharedPreferences;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.View;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.widget.Button;
  10. import android.util.Log;
  11. import android.content.Intent;
  12. import android.widget.EditText;
  13. import android.widget.Toast;
  14.  
  15.  
  16.  
  17. public class MainActivity extends AppCompatActivity {
  18.  
  19. private EditText passWordEditText;
  20. private EditText userNameEditText;
  21. private Button loginButton;
  22. private Button registerNowButton;
  23. private SharedPreferences sharedPreferences;
  24. public boolean accessGranted;
  25.  
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28.  
  29. setContentView(R.layout.activity_main);
  30.  
  31. userNameEditText = findViewById(R.id.userNameEditText);
  32. passWordEditText = findViewById(R.id.passWordEditText);
  33. registerNowButton = findViewById(R.id.registerNowButton);
  34. loginButton = findViewById(R.id.loginButton);
  35. sharedPreferences = getSharedPreferences("login", 0);
  36. redirectToHomeIfAllowed();
  37.  
  38. loginButton.setOnClickListener(new View.OnClickListener() {
  39. @Override
  40. public void onClick(View v) {
  41. login();
  42. }
  43. });
  44.  
  45. registerNowButton.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. startActivity(new Intent(MainActivity.this, Register.class));
  49. }
  50. });
  51. }
  52.  
  53. public void redirectToHomeIfAllowed() {
  54.  
  55. if (sharedPreferences.getBoolean("loggedin", false)){
  56. startHomeScreen();
  57. }
  58. }
  59.  
  60. public void register(View v) {
  61. startActivity(new Intent(this, Register.class));
  62. }
  63.  
  64. private void startHomeScreen() {
  65. Intent intent = new Intent(MainActivity.this, MainActivity.class);
  66. MainActivity.this.startActivity(intent);
  67. }
  68.  
  69. public void login() {
  70. String username = userNameEditText.toString();
  71. String password = passWordEditText.toString();
  72.  
  73. String validUsername = sharedPreferences.getString("username", "");
  74. String validPassword = sharedPreferences.getString("password", "");
  75.  
  76. if (username.isEmpty() || password.isEmpty() )
  77. Toast.makeText(this, "You must enter a valid email & password", Toast.LENGTH_SHORT).show();
  78. else if (!userNameEditText.toString().matches(validUsername) || !passWordEditText.toString().matches(validPassword))
  79. Toast.makeText(this, "Unable to validate your email & password", Toast.LENGTH_SHORT).show();
  80. else if (!accessGranted) {
  81. SharedPreferences.Editor editor = sharedPreferences.edit();
  82. editor.putBoolean("loggedin", true);
  83. editor.apply();
  84.  
  85. startHomeScreen();
  86. this.finish();
  87. }
  88. }
  89.  
  90. //Action Bar
  91. @Override
  92. public boolean onCreateOptionsMenu(Menu menu) {
  93. // Inflate the menu; this adds items to the action bar if it is present.
  94. getMenuInflater().inflate(R.menu.menu_main, menu);
  95. return true;
  96. }
  97.  
  98. //When user clicks contact us they are brought to the contact us page
  99. @Override
  100. public boolean onOptionsItemSelected(MenuItem item) {
  101. switch (item.getItemId()) {
  102. case R.id.contactUs:startActivity
  103. (new Intent(this, ContactUs.class));
  104. break;
  105. }
  106.  
  107. return super.onOptionsItemSelected(item);
  108. }
  109.  
  110. public boolean onOptionsItemsSelected(MenuItem item) {
  111. int id = item.getItemId();
  112. if (id == R.id.action_settings) {
  113. return true;
  114. }
  115.  
  116. return super.onOptionsItemSelected(item);
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement