Commit d12d3c06 authored by Aniket's avatar Aniket

adds error messages to sign up dialog edit texts

parent 6f80a3c4
...@@ -4,14 +4,17 @@ import android.app.Dialog; ...@@ -4,14 +4,17 @@ import android.app.Dialog;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.util.Patterns; import android.util.Patterns;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.jakewharton.rxbinding2.widget.RxTextView;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
...@@ -20,122 +23,174 @@ import chat.rocket.android.helper.TextUtils; ...@@ -20,122 +23,174 @@ import chat.rocket.android.helper.TextUtils;
* Dialog for user registration. * Dialog for user registration.
*/ */
public class UserRegistrationDialogFragment extends DialogFragment { public class UserRegistrationDialogFragment extends DialogFragment {
private String hostname; private String hostname;
private String username; private String username;
private String email; private String email;
private String password; private String password;
private TextInputEditText txtUsername;
public UserRegistrationDialogFragment() { private TextInputEditText txtEmail;
super(); private TextInputEditText txtPasswd;
} private TextInputLayout textInputUsername;
private TextInputLayout textInputEmail;
/** private TextInputLayout textInputPassword;
* build UserRegistrationDialogFragment with auto-detect email/username. private View waitingView;
*/ public UserRegistrationDialogFragment() {
public static UserRegistrationDialogFragment create(String hostname, super();
String usernameOrEmail, String password) {
if (Patterns.EMAIL_ADDRESS.matcher(usernameOrEmail).matches()) {
return create(hostname, null, usernameOrEmail, password);
} else {
return create(hostname, usernameOrEmail, null, password);
} }
}
/**
/** * build UserRegistrationDialogFragment with auto-detect email/username.
* build UserRegistrationDialogFragment. */
*/ public static UserRegistrationDialogFragment create(String hostname,
public static UserRegistrationDialogFragment create(String hostname, String usernameOrEmail, String password) {
String username, String email, if (Patterns.EMAIL_ADDRESS.matcher(usernameOrEmail).matches()) {
String password) { return create(hostname, null, usernameOrEmail, password);
Bundle args = new Bundle(); } else {
args.putString("hostname", hostname); return create(hostname, usernameOrEmail, null, password);
if (!TextUtils.isEmpty(username)) { }
args.putString("username", username);
} }
if (!TextUtils.isEmpty(email)) {
args.putString("email", email); /**
* build UserRegistrationDialogFragment.
*/
public static UserRegistrationDialogFragment create(String hostname,
String username, String email,
String password) {
Bundle args = new Bundle();
args.putString("hostname", hostname);
if (!TextUtils.isEmpty(username)) {
args.putString("username", username);
}
if (!TextUtils.isEmpty(email)) {
args.putString("email", email);
}
if (!TextUtils.isEmpty(password)) {
args.putString("password", password);
}
UserRegistrationDialogFragment dialog = new UserRegistrationDialogFragment();
dialog.setArguments(args);
return dialog;
} }
if (!TextUtils.isEmpty(password)) {
args.putString("password", password); @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
hostname = args.getString("hostname");
username = args.getString("username");
email = args.getString("email");
password = args.getString("password");
}
} }
UserRegistrationDialogFragment dialog = new UserRegistrationDialogFragment();
dialog.setArguments(args); @NonNull
return dialog; @Override
} public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getContext(), R.style.AppTheme_Dialog)
@Override .setView(createDialogView())
public void onCreate(@Nullable Bundle savedInstanceState) { .create();
super.onCreate(savedInstanceState);
Bundle args = getArguments();
if (args != null) {
hostname = args.getString("hostname");
username = args.getString("username");
email = args.getString("email");
password = args.getString("password");
} }
}
private View createDialogView() {
@NonNull View dialog = LayoutInflater.from(getContext())
@Override .inflate(R.layout.dialog_user_registration, null, false);
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getContext(), R.style.AppTheme_Dialog) initViews(dialog);
.setView(createDialogView()) setUpRxBinders();
.create();
} if (!TextUtils.isEmpty(username)) {
txtUsername.setText(username);
private View createDialogView() { }
View dialog = LayoutInflater.from(getContext()) if (!TextUtils.isEmpty(email)) {
.inflate(R.layout.dialog_user_registration, null, false); txtEmail.setText(email);
}
final TextView txtUsername = (TextView) dialog.findViewById(R.id.editor_username); if (!TextUtils.isEmpty(password)) {
final TextView txtEmail = (TextView) dialog.findViewById(R.id.editor_email); txtPasswd.setText(password);
final TextView txtPasswd = (TextView) dialog.findViewById(R.id.editor_passwd); }
if (!TextUtils.isEmpty(username)) { waitingView.setVisibility(View.GONE);
txtUsername.setText(username);
dialog.findViewById(R.id.btn_register_user).setOnClickListener(registerButton -> {
if (checkIfEditTextsEmpty())
return;
registerButton.setEnabled(false);
registerButton.setAlpha(0.5f);
waitingView.setVisibility(View.VISIBLE);
username = txtUsername.getText().toString();
email = txtEmail.getText().toString();
password = txtPasswd.getText().toString();
MethodCallHelper methodCallHelper = new MethodCallHelper(getContext(), hostname);
methodCallHelper.registerUser(username, email, password, password)
.onSuccessTask(task -> methodCallHelper.loginWithEmail(email, password))
.onSuccessTask(task -> methodCallHelper.setUsername(username)) //TODO: should prompt!
.onSuccessTask(task -> methodCallHelper.joinDefaultChannels())
.onSuccessTask(task -> {
dismiss();
return task;
})
.continueWith(task -> {
if (task.isFaulted()) {
Exception exception = task.getError();
showError(exception.getMessage());
registerButton.setEnabled(true);
waitingView.setVisibility(View.GONE);
}
return null;
});
});
return dialog;
} }
if (!TextUtils.isEmpty(email)) {
txtEmail.setText(email); private void initViews(View dialog) {
txtUsername = dialog.findViewById(R.id.editor_username);
txtEmail = dialog.findViewById(R.id.editor_email);
txtPasswd = dialog.findViewById(R.id.editor_passwd);
textInputEmail = dialog.findViewById(R.id.text_input_email);
textInputUsername = dialog.findViewById(R.id.text_input_username);
textInputPassword = dialog.findViewById(R.id.text_input_passwd);
waitingView = dialog.findViewById(R.id.waiting);
} }
if (!TextUtils.isEmpty(password)) {
txtPasswd.setText(password); private boolean checkIfEditTextsEmpty() {
boolean check = false;
if (TextUtils.isEmpty(txtEmail.getText().toString())) {
textInputEmail.setError("Enter an email address");
textInputEmail.setErrorEnabled(true);
check = true;
}
if (TextUtils.isEmpty(txtUsername.getText().toString())) {
textInputUsername.setError("Enter a username");
textInputUsername.setErrorEnabled(true);
check = true;
}
if (TextUtils.isEmpty(txtPasswd.getText().toString())) {
textInputPassword.setError("Enter a password");
textInputPassword.setErrorEnabled(true);
check = true;
}
return check;
} }
final View waitingView = dialog.findViewById(R.id.waiting); private void setUpRxBinders() {
waitingView.setVisibility(View.GONE); RxTextView.textChanges(txtUsername).subscribe(text -> {
if (!TextUtils.isEmpty(text) && textInputUsername.isErrorEnabled())
dialog.findViewById(R.id.btn_register_user).setOnClickListener(view -> { textInputUsername.setErrorEnabled(false);
view.setEnabled(false); });
waitingView.setVisibility(View.VISIBLE); RxTextView.textChanges(txtEmail).subscribe(text -> {
if (!TextUtils.isEmpty(text) && textInputEmail.isErrorEnabled())
username = txtUsername.getText().toString(); textInputEmail.setErrorEnabled(false);
email = txtEmail.getText().toString(); });
password = txtPasswd.getText().toString(); RxTextView.textChanges(txtPasswd).subscribe(text -> {
if (!TextUtils.isEmpty(text) && textInputPassword.isErrorEnabled())
MethodCallHelper methodCallHelper = new MethodCallHelper(getContext(), hostname); textInputPassword.setErrorEnabled(false);
methodCallHelper.registerUser(username, email, password, password) });
.onSuccessTask(task -> methodCallHelper.loginWithEmail(email, password)) }
.onSuccessTask(task -> methodCallHelper.setUsername(username)) //TODO: should prompt!
.onSuccessTask(task -> methodCallHelper.joinDefaultChannels()) private void showError(String errMessage) {
.onSuccessTask(task -> { Toast.makeText(getContext(), errMessage, Toast.LENGTH_SHORT).show();
dismiss(); }
return task;
})
.continueWith(task -> {
if (task.isFaulted()) {
Exception exception = task.getError();
showError(exception.getMessage());
view.setEnabled(true);
waitingView.setVisibility(View.GONE);
}
return null;
});
});
return dialog;
}
private void showError(String errMessage) {
Toast.makeText(getContext(), errMessage, Toast.LENGTH_SHORT).show();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment