login and register
This commit is contained in:
@@ -1,17 +1,111 @@
|
||||
package ch.pizzalink.android.activity;
|
||||
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
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.helper.DialogHelper;
|
||||
import ch.pizzalink.android.helper.SharedPrefsHelper;
|
||||
import ch.pizzalink.android.model.responseModel.LoginCustomerResponseModel;
|
||||
import ch.pizzalink.android.view.PizzalinkButton;
|
||||
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_valid_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(){
|
||||
Call<LoginCustomerResponseModel> call = ApiService.apiInterface.login(
|
||||
emailPizzalinkEditText.getText(), passwordPizzalinkEditText.getText());
|
||||
call.enqueue(new Callback<LoginCustomerResponseModel>() {
|
||||
@Override
|
||||
public void onResponse(Call<LoginCustomerResponseModel> call, Response<LoginCustomerResponseModel> response) {
|
||||
if(response.isSuccessful()){
|
||||
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<LoginCustomerResponseModel> call, Throwable t) {
|
||||
DialogHelper.showFailedDialog();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user