119 lines
4.3 KiB
Java
119 lines
4.3 KiB
Java
package ch.pizzalink.android.activity;
|
|
|
|
import android.content.Intent;
|
|
import android.graphics.Color;
|
|
import android.os.Bundle;
|
|
import android.text.Spannable;
|
|
import android.text.SpannableString;
|
|
import android.text.style.ForegroundColorSpan;
|
|
import android.view.View;
|
|
import android.widget.Button;
|
|
import android.widget.TextView;
|
|
|
|
import butterknife.BindString;
|
|
import butterknife.BindView;
|
|
import butterknife.ButterKnife;
|
|
import butterknife.OnClick;
|
|
import ch.pizzalink.android.R;
|
|
import ch.pizzalink.android.api.ApiErrorUtils;
|
|
import ch.pizzalink.android.api.ApiService;
|
|
import ch.pizzalink.android.api.ResponseObject;
|
|
import ch.pizzalink.android.helper.DialogHelper;
|
|
import ch.pizzalink.android.helper.SharedPrefsHelper;
|
|
import ch.pizzalink.android.model.UserModel;
|
|
import ch.pizzalink.android.view.PizzalinkEditText;
|
|
import retrofit2.Call;
|
|
import retrofit2.Callback;
|
|
import retrofit2.Response;
|
|
|
|
public class LoginActivity extends BaseActivity {
|
|
|
|
@BindView(R.id.emailPizzalinkEditText) PizzalinkEditText emailPizzalinkEditText;
|
|
@BindView(R.id.passwordPizzalinkEditText) PizzalinkEditText passwordPizzalinkEditText;
|
|
@BindView(R.id.loginButton) Button loginButton;
|
|
@BindView(R.id.registerTextView) TextView registerTextView;
|
|
|
|
@BindString(R.string.not_have_an_accaount) String notHaveAnAccountText;
|
|
@BindString(R.string.register_text) String registerText;
|
|
@BindString(R.string.alert_fill_all_fields) String fillAllFieldsText;
|
|
@BindString(R.string.alert_invalid_email) String validEmailText;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_login);
|
|
ButterKnife.bind(this);
|
|
initViews();
|
|
}
|
|
|
|
@OnClick({R.id.loginButton, R.id.registerTextView})
|
|
protected void onClick(View view){
|
|
switch (view.getId()){
|
|
case R.id.loginButton:
|
|
if(checkFields())
|
|
login();
|
|
break;
|
|
case R.id.registerTextView:
|
|
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void initViews(){
|
|
initRegisterTextView();
|
|
/*
|
|
emailPizzalinkEditText.getEditText().setText("aytaccici@gmail.com");
|
|
passwordPizzalinkEditText.getEditText().setText("3522625");
|
|
*/
|
|
}
|
|
|
|
private void initRegisterTextView(){
|
|
Spannable wordtoSpan = new SpannableString(notHaveAnAccountText + " " + registerText);
|
|
wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 16, wordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
registerTextView.setText(wordtoSpan);
|
|
}
|
|
|
|
private boolean checkFields(){
|
|
|
|
if(emailPizzalinkEditText.isEmpty() || passwordPizzalinkEditText.isEmpty()){
|
|
DialogHelper.showAlertDialog(this, fillAllFieldsText);
|
|
return false;
|
|
}
|
|
|
|
if(!emailPizzalinkEditText.isEmail()){
|
|
DialogHelper.showAlertDialog(this, validEmailText);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private void login(){
|
|
DialogHelper.showLoadingDialog();
|
|
Call<ResponseObject<UserModel>> call = ApiService.apiInterface.login(
|
|
emailPizzalinkEditText.getText(), passwordPizzalinkEditText.getText());
|
|
call.enqueue(new Callback<ResponseObject<UserModel>>() {
|
|
@Override
|
|
public void onResponse(Call<ResponseObject<UserModel>> call, Response<ResponseObject<UserModel>> response) {
|
|
DialogHelper.hideLoadingDialog();
|
|
if(response.isSuccessful() &&
|
|
response.body().getData() != null &&
|
|
response.body().isSuccess()){
|
|
SharedPrefsHelper.saveUser(response.body().getData());
|
|
SharedPrefsHelper.saveCustomerToken(response.body().getData().getToken());
|
|
SharedPrefsHelper.setCustomerLoggedIn(true);
|
|
startActivity(new Intent(LoginActivity.this, MainActivity.class));
|
|
}
|
|
else
|
|
ApiErrorUtils.parseError(response);
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(Call<ResponseObject<UserModel>> call, Throwable t) {
|
|
DialogHelper.hideLoadingDialog();
|
|
DialogHelper.showFailedDialog();
|
|
}
|
|
});
|
|
}
|
|
}
|