Advertisement
Guest User

Untitled

a guest
Mar 29th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. package com.example.pegahackatonclient.Activities;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.provider.Settings;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12.  
  13. import com.example.pegahackatonclient.GlobalValues.GlobalValues;
  14. import com.example.pegahackatonclient.Models.User;
  15. import com.example.pegahackatonclient.Network.DataService;
  16. import com.example.pegahackatonclient.Network.RetrofitInstance;
  17. import com.example.pegahackatonclient.R;
  18.  
  19. import okhttp3.Credentials;
  20. import retrofit2.Call;
  21. import retrofit2.Callback;
  22. import retrofit2.Response;
  23.  
  24. public class MainActivity extends AppCompatActivity {
  25.  
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_main);
  30. }
  31.  
  32. @Override
  33. public void onResume() {
  34. super.onResume();
  35. EditText login = findViewById(R.id.etUsername);
  36. EditText password = findViewById(R.id.etPassword);
  37.  
  38. login.setText("");
  39. password.setText("");
  40. }
  41.  
  42. @Override
  43. public boolean onCreateOptionsMenu(Menu menu) {
  44. // Inflate the menu; this adds items to the action bar if it is present.
  45. getMenuInflater().inflate(R.menu.menu_main, menu);
  46. return true;
  47. }
  48.  
  49. @Override
  50. public boolean onOptionsItemSelected(MenuItem item) {
  51. // Handle action bar item clicks here. The action bar will
  52. // automatically handle clicks on the Home/Up button, so long
  53. // as you specify a parent activity in AndroidManifest.xml.
  54. int id = item.getItemId();
  55.  
  56. //noinspection SimplifiableIfStatement
  57. if (id == R.id.action_settings) {
  58. return true;
  59. }
  60.  
  61. return super.onOptionsItemSelected(item);
  62. }
  63.  
  64. public void login(View view) {
  65. DataService service = RetrofitInstance.getRetrofitInstance().create(DataService.class);
  66.  
  67. final User user = new User();
  68.  
  69. EditText login = findViewById(R.id.etUsername);
  70. final EditText password = findViewById(R.id.etPassword);
  71.  
  72. user.setLogin(login.getText().toString());
  73. user.setPassword(password.getText().toString());
  74.  
  75. Call<User> call = service.Login(user);
  76.  
  77. call.enqueue(new Callback<User>() {
  78. @Override
  79. public void onResponse(Call<User> call, Response<User> response) {
  80. if(response.isSuccessful())
  81. {
  82. GlobalValues.LoggedUser = response.body();
  83. GlobalValues.LoggedUser.setPassword(password.getText().toString());
  84. GlobalValues.AuthData = Credentials.basic(response.body().getLogin(), user.getPassword());
  85. Intent intent = new Intent( getBaseContext() , TabsActivity.class);
  86. startActivity(intent);
  87. }
  88. else
  89. {
  90. Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
  91. }
  92. }
  93.  
  94. @Override
  95. public void onFailure(Call<User> call, Throwable t) {
  96. Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
  97. }
  98. });
  99. }
  100.  
  101. public void register(View view) {
  102. Intent intent = new Intent( getBaseContext() , RegistrationActivity.class);
  103. startActivity(intent);
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement