Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package id.co.rumahcoding.neardeal;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.widget.Button;
- import android.widget.EditText;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import butterknife.OnClick;
- import id.co.rumahcoding.neardeal.responses.LoginResponse;
- import id.co.rumahcoding.neardeal.utils.PopupUtil;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- public class LoginActivity extends AppCompatActivity {
- @BindView(R.id.et_email)
- EditText usernameEditText;
- @BindView(R.id.et_password)
- EditText passwordEditText;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- ButterKnife.bind(this);
- SharedPreferences sharedPref = getSharedPreferences("id.co.rumahcoding.neardeal",
- Context.MODE_PRIVATE);
- boolean isLoggedIn = sharedPref.getBoolean("isLoggedin", false);
- if(isLoggedIn) {
- Intent intent = new Intent(this, MainActivity.class);
- startActivity(intent);
- finish();
- }
- /*SharedPreferences.Editor editor = sharedPref.edit();
- editor.putInt(getKey(mKey), state);
- editor.commit();*/
- }
- @OnClick({R.id.btn_login})
- public void onClick(Button button) {
- PopupUtil.showLoading( this, "", "Please wait....");
- String username = usernameEditText.getText().toString();
- String password = passwordEditText.getText().toString();
- ApiEndPoint apiEndPoint = ApiClient.getClient().create(ApiEndPoint.class);
- Call<LoginResponse> call = apiEndPoint.login(username, password);
- call.enqueue(new Callback<LoginResponse>() {
- @Override
- public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
- PopupUtil.dismissDialog();
- final LoginResponse loginResponse = response.body();
- if (loginResponse.getSuccess()){
- SharedPreferences sharedPref = getSharedPreferences("id.co.rumahcoding.neardeal",
- Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = sharedPref.edit();
- editor.putBoolean("isLoggedin", true);
- editor.commit();
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- startActivity(intent);
- finish();
- }
- });
- }
- else {
- PopupUtil.showMsg(LoginActivity.this, "User dan password salah", PopupUtil.SHORT);
- }
- }
- @Override
- public void onFailure(Call<LoginResponse> call, Throwable t) {
- PopupUtil.dismissDialog();
- t.printStackTrace();
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment