use of pinkpanthers.pinkshelters.Model.UniqueKeyError in project pink-panthers by MrTrai.
the class Registration method registerButton.
/**
* Register user with:
* Name
* Password
* Email
* User Types
*
* @param view the current view that the register button is on
*/
public void registerButton(@SuppressWarnings("unused") View view) {
Boolean noName;
Boolean noUsername;
Boolean noPass;
Boolean noEmail;
Boolean noType;
String empty = "";
Editable nameText = name.getText();
String isValidName = nameText.toString();
TextView missingName = findViewById(R.id.missingName);
if (isValidName.equals(empty)) {
// missing name
missingName.setVisibility(View.VISIBLE);
noName = false;
} else {
missingName.setVisibility(View.INVISIBLE);
noName = true;
}
Editable emailText = email.getText();
String isValidEmail = emailText.toString();
isValidEmail = isValidEmail.toLowerCase();
TextView missingEmail = findViewById(R.id.missingEmail);
if (isValidEmail.equals(empty) || !isValidEmail.contains("@")) {
// missing email or "@" sign
missingEmail.setVisibility(View.VISIBLE);
noEmail = false;
} else {
missingEmail.setVisibility(View.INVISIBLE);
noEmail = true;
}
Editable usernameText = username.getText();
String isValidUsername = usernameText.toString();
isValidUsername = isValidUsername.toLowerCase();
TextView missingUsername = findViewById(R.id.missingUsername);
if (isValidUsername.length() < 6) {
// username cannot be less than 6 characters
missingUsername.setVisibility(View.VISIBLE);
noUsername = false;
} else {
missingUsername.setVisibility(View.INVISIBLE);
noUsername = true;
}
Editable passwordText = password.getText();
String isValidPassword = passwordText.toString();
TextView missingPassword = findViewById(R.id.missingPassword);
if (isValidPassword.length() < 6) {
// password cannot be less than 6 characters
missingPassword.setVisibility(View.VISIBLE);
noPass = false;
} else {
missingPassword.setVisibility(View.INVISIBLE);
noPass = true;
}
String isValidType = (String) userTypes.getSelectedItem();
TextView missingUserType = findViewById(R.id.missingUserType);
if (isValidType.equals(empty)) {
// missing user type
missingUserType.setVisibility(View.VISIBLE);
noType = false;
} else {
missingUserType.setVisibility(View.INVISIBLE);
noType = true;
}
Boolean missingAnything = noName && noEmail && noPass && noUsername && noType;
if (missingAnything) {
try {
Account newAccount = db.createAccount(isValidType, isValidUsername, isValidPassword, isValidName, isValidEmail);
sendEmail(isValidEmail);
db.logAction(newAccount, "account_created");
Intent loginPageIntent = new Intent(this, LoginActivity.class);
startActivity(loginPageIntent);
} catch (UniqueKeyError e) {
TextView duplicate = findViewById(R.id.duplicate);
duplicate.setVisibility(View.VISIBLE);
}
}
}
Aggregations