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;
...@@ -24,7 +27,13 @@ public class UserRegistrationDialogFragment extends DialogFragment { ...@@ -24,7 +27,13 @@ public class UserRegistrationDialogFragment extends DialogFragment {
private String username; private String username;
private String email; private String email;
private String password; private String password;
private TextInputEditText txtUsername;
private TextInputEditText txtEmail;
private TextInputEditText txtPasswd;
private TextInputLayout textInputUsername;
private TextInputLayout textInputEmail;
private TextInputLayout textInputPassword;
private View waitingView;
public UserRegistrationDialogFragment() { public UserRegistrationDialogFragment() {
super(); super();
} }
...@@ -88,9 +97,8 @@ public class UserRegistrationDialogFragment extends DialogFragment { ...@@ -88,9 +97,8 @@ public class UserRegistrationDialogFragment extends DialogFragment {
View dialog = LayoutInflater.from(getContext()) View dialog = LayoutInflater.from(getContext())
.inflate(R.layout.dialog_user_registration, null, false); .inflate(R.layout.dialog_user_registration, null, false);
final TextView txtUsername = (TextView) dialog.findViewById(R.id.editor_username); initViews(dialog);
final TextView txtEmail = (TextView) dialog.findViewById(R.id.editor_email); setUpRxBinders();
final TextView txtPasswd = (TextView) dialog.findViewById(R.id.editor_passwd);
if (!TextUtils.isEmpty(username)) { if (!TextUtils.isEmpty(username)) {
txtUsername.setText(username); txtUsername.setText(username);
...@@ -102,17 +110,19 @@ public class UserRegistrationDialogFragment extends DialogFragment { ...@@ -102,17 +110,19 @@ public class UserRegistrationDialogFragment extends DialogFragment {
txtPasswd.setText(password); txtPasswd.setText(password);
} }
final View waitingView = dialog.findViewById(R.id.waiting);
waitingView.setVisibility(View.GONE); waitingView.setVisibility(View.GONE);
dialog.findViewById(R.id.btn_register_user).setOnClickListener(view -> { dialog.findViewById(R.id.btn_register_user).setOnClickListener(registerButton -> {
view.setEnabled(false); if (checkIfEditTextsEmpty())
return;
registerButton.setEnabled(false);
registerButton.setAlpha(0.5f);
waitingView.setVisibility(View.VISIBLE); waitingView.setVisibility(View.VISIBLE);
username = txtUsername.getText().toString(); username = txtUsername.getText().toString();
email = txtEmail.getText().toString(); email = txtEmail.getText().toString();
password = txtPasswd.getText().toString(); password = txtPasswd.getText().toString();
MethodCallHelper methodCallHelper = new MethodCallHelper(getContext(), hostname); MethodCallHelper methodCallHelper = new MethodCallHelper(getContext(), hostname);
methodCallHelper.registerUser(username, email, password, password) methodCallHelper.registerUser(username, email, password, password)
.onSuccessTask(task -> methodCallHelper.loginWithEmail(email, password)) .onSuccessTask(task -> methodCallHelper.loginWithEmail(email, password))
...@@ -126,7 +136,7 @@ public class UserRegistrationDialogFragment extends DialogFragment { ...@@ -126,7 +136,7 @@ public class UserRegistrationDialogFragment extends DialogFragment {
if (task.isFaulted()) { if (task.isFaulted()) {
Exception exception = task.getError(); Exception exception = task.getError();
showError(exception.getMessage()); showError(exception.getMessage());
view.setEnabled(true); registerButton.setEnabled(true);
waitingView.setVisibility(View.GONE); waitingView.setVisibility(View.GONE);
} }
return null; return null;
...@@ -135,6 +145,51 @@ public class UserRegistrationDialogFragment extends DialogFragment { ...@@ -135,6 +145,51 @@ public class UserRegistrationDialogFragment extends DialogFragment {
return dialog; return dialog;
} }
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);
}
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;
}
private void setUpRxBinders() {
RxTextView.textChanges(txtUsername).subscribe(text -> {
if (!TextUtils.isEmpty(text) && textInputUsername.isErrorEnabled())
textInputUsername.setErrorEnabled(false);
});
RxTextView.textChanges(txtEmail).subscribe(text -> {
if (!TextUtils.isEmpty(text) && textInputEmail.isErrorEnabled())
textInputEmail.setErrorEnabled(false);
});
RxTextView.textChanges(txtPasswd).subscribe(text -> {
if (!TextUtils.isEmpty(text) && textInputPassword.isErrorEnabled())
textInputPassword.setErrorEnabled(false);
});
}
private void showError(String errMessage) { private void showError(String errMessage) {
Toast.makeText(getContext(), errMessage, Toast.LENGTH_SHORT).show(); 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