Last active
August 29, 2015 14:14
-
-
Save lgvalle/a83c9ede7d236303eb06 to your computer and use it in GitHub Desktop.
Legacy code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginActivity extends Activity { | |
private EditText mEmailEdit; | |
private EditText mPassword; | |
private Button btnSend; | |
private EditText mPhoneNumber; | |
private TextWatcher watcher; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_login); | |
watcher = new TextWatcher() { | |
@Override | |
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) { | |
} | |
@Override | |
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) { | |
} | |
@Override | |
public void afterTextChanged(final Editable s) { | |
validateAll(); | |
} | |
}; | |
mEmailEdit = (EditText) findViewById(R.id.login_email); | |
mEmailEdit.addTextChangedListener(watcher); | |
mPhoneNumber = (EditText) findViewById(R.id.login_phone_number); | |
mEmailEdit.addTextChangedListener(watcher); | |
mPassword = (EditText) findViewById(R.id.login_password); | |
btnSend = (Button) findViewById(R.id.login_send); | |
btnSend.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
startRequestSend(); | |
} | |
}); | |
} | |
// Validate all form fields | |
private boolean validateAll() { | |
boolean valid; | |
valid = validateEmail(); | |
String password = mPassword.getText().toString(); | |
// password min length == 4 | |
valid &= (password != null && password.length() > 4); | |
buttonState(valid); | |
return valid; | |
} | |
// Enable or disable button | |
private void buttonState(boolean enable) { | |
if (enable) { | |
btnSend.setEnabled(true); | |
} else { | |
btnSend.setEnabled(false); | |
} | |
} | |
private boolean validateEmail() { | |
String email = mEmailEdit.getText().toString(); | |
String phone = mPhoneNumber.getText().toString(); | |
boolean valid = false; | |
if (!TextUtils.isEmpty(email)) { | |
valid = Patterns.EMAIL_ADDRESS.matcher(email).matches(); | |
} | |
if (!TextUtils.isEmpty(phone)) { | |
valid &= Patterns.PHONE.matcher(email).matches(); | |
} | |
return valid; | |
} | |
protected void startRequestSend() { | |
//Start Dialog | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
ProgressDialogFragment.newInstanceRunnable(getFragmentManager(), R.string.dialog_title_loading, null); | |
} | |
}); | |
final String email = mEmailEdit.getText().toString(); | |
final String phone = mPhoneNumber.getText().toString(); | |
final String password = mPassword.getText().toString(); | |
final BaseRequest<UserResponse> request; | |
// Email Request or Phone Request | |
if (!TextUtils.isEmpty(email)) { | |
request = new EmailRequest(email, password); | |
} else { | |
request = new PhoneRequest(phone, password); | |
} | |
// Send request | |
RequestManager.execute(request, request.getRequestCache(), RequestManager.EXPIRATION_TIME, new LoginRequestListener()); | |
} | |
private class LoginRequestListener implements RequestManager.RequestListener { | |
@Override | |
public void onSuccess() { | |
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this); | |
prefs.edit().putBoolean("login_success", true).commit(); | |
launchNextActivity(); | |
} | |
@Override | |
public void onError(int errorCode) { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
ProgressDialogFragment.newInstanceRunnable(getFragmentManager(), R.string.error, "Error on login"); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment