Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.pegahackatonclient.Activities;
- import android.content.Intent;
- import android.os.Bundle;
- import android.provider.Settings;
- import android.support.v7.app.AppCompatActivity;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.example.pegahackatonclient.GlobalValues.GlobalValues;
- import com.example.pegahackatonclient.Models.User;
- import com.example.pegahackatonclient.Network.DataService;
- import com.example.pegahackatonclient.Network.RetrofitInstance;
- import com.example.pegahackatonclient.R;
- import okhttp3.Credentials;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public void onResume() {
- super.onResume();
- EditText login = findViewById(R.id.etUsername);
- EditText password = findViewById(R.id.etPassword);
- login.setText("");
- password.setText("");
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- public void login(View view) {
- DataService service = RetrofitInstance.getRetrofitInstance().create(DataService.class);
- final User user = new User();
- EditText login = findViewById(R.id.etUsername);
- final EditText password = findViewById(R.id.etPassword);
- user.setLogin(login.getText().toString());
- user.setPassword(password.getText().toString());
- Call<User> call = service.Login(user);
- call.enqueue(new Callback<User>() {
- @Override
- public void onResponse(Call<User> call, Response<User> response) {
- if(response.isSuccessful())
- {
- GlobalValues.LoggedUser = response.body();
- GlobalValues.LoggedUser.setPassword(password.getText().toString());
- GlobalValues.AuthData = Credentials.basic(response.body().getLogin(), user.getPassword());
- Intent intent = new Intent( getBaseContext() , TabsActivity.class);
- startActivity(intent);
- }
- else
- {
- Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onFailure(Call<User> call, Throwable t) {
- Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
- }
- });
- }
- public void register(View view) {
- Intent intent = new Intent( getBaseContext() , RegistrationActivity.class);
- startActivity(intent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement