Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. public class AuthenticationActivity extends BaseActivity implements View.OnClickListener {
  2.  
  3. EditText username, password;
  4. Button signInButton, registerButton;
  5.  
  6. public static void startActivity(Activity startingActivity) {
  7. startingActivity.startActivity(new Intent(startingActivity, AuthenticationActivity.class));
  8. startingActivity.finish();
  9. }
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. Hasura.initialise(this);
  15. setContentView(R.layout.activity_authentication);
  16.  
  17. username = (EditText) findViewById(R.id.username);
  18. password = (EditText) findViewById(R.id.password);
  19. signInButton = (Button) findViewById(R.id.signInButton);
  20. registerButton = (Button) findViewById(R.id.registerButton);
  21.  
  22. signInButton.setOnClickListener(this);
  23. registerButton.setOnClickListener(this);
  24.  
  25. if (Hasura.getUserSessionId() != null) {
  26. ToDoActivity.startActivity(this);
  27. }
  28.  
  29. }
  30.  
  31. @Override
  32. public void onClick(View v) {
  33. switch (v.getId()) {
  34. case R.id.signInButton:
  35. handleLogin();
  36. break;
  37. case R.id.registerButton:
  38. handleRegistration();
  39. break;
  40. }
  41. }
  42.  
  43. private void handleLogin() {
  44. showProgressIndicator();
  45. Hasura.auth.login(new AuthRequest(username.getText().toString(), password.getText().toString())).enqueue(new Callback<AuthResponse>() {
  46. @Override
  47. public void onResponse(Call<AuthResponse> call, Response<AuthResponse> response) {
  48. hideProgressIndicator();
  49. if (response.isSuccessful()) {
  50. Hasura.setSession(response.body());
  51. ToDoActivity.startActivity(AuthenticationActivity.this);
  52. } else {
  53. try {
  54. MessageResponse messageResponse = new Gson().fromJson(response.errorBody().string(), MessageResponse.class);
  55. Toast.makeText(AuthenticationActivity.this, messageResponse.getMessage(), Toast.LENGTH_LONG).show();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. }
  61.  
  62. @Override
  63. public void onFailure(Call<AuthResponse> call, Throwable t) {
  64. hideProgressIndicator();
  65. Toast.makeText(AuthenticationActivity.this, "Something went wrong, please ensure that you have a working internet connection", Toast.LENGTH_LONG).show();
  66. }
  67. });
  68. }
  69.  
  70. private void handleRegistration() {
  71. showProgressIndicator();
  72. Hasura.auth.register(new AuthRequest(username.getText().toString(), password.getText().toString())).enqueue(new Callback<AuthResponse>() {
  73. @Override
  74. public void onResponse(Call<AuthResponse> call, Response<AuthResponse> response) {
  75. hideProgressIndicator();
  76. if (response.isSuccessful()) {
  77. Hasura.setSession(response.body());
  78. ToDoActivity.startActivity(AuthenticationActivity.this);
  79. } else {
  80. try {
  81. MessageResponse messageResponse = new Gson().fromJson(response.errorBody().string(), MessageResponse.class);
  82. Toast.makeText(AuthenticationActivity.this, messageResponse.getMessage(), Toast.LENGTH_LONG).show();
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. }
  88.  
  89. @Override
  90. public void onFailure(Call<AuthResponse> call, Throwable t) {
  91. hideProgressIndicator();
  92. Toast.makeText(AuthenticationActivity.this, "Something went wrong, please ensure that you have a working internet connection", Toast.LENGTH_LONG).show();
  93. }
  94. });
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement