I have a code that adds a user to the database, getting the username from the dialog.My fragment implements the interface with the onInputSend method, which is called by the dialog when the user clicks the desired button. Here is the code that adds the user to the database. I want to hear criticism of my code.
UsersActivity
public void onInputSend(@NonNull InputTextAlertDialog dialog, @NotNull String input) {
userActionManager.isEmptyUser(input)
.doOnSubscribe(disposable -> disposables.add(disposable))
.subscribe(empty -> insertUser(dialog, input, empty));
}
private void insertUser(InputTextAlertDialog dialog, String userName, boolean isEmpty) {
if (isEmpty) {
if (!userName.trim().isEmpty()) {
userActionManager.insertUser(new User(userName));
dialog.cancel();
} else {
dialog.setErrorMessage(getString(R.string.user_not_have_name));
}
} else {
dialog.setErrorMessage(getString(R.string.user_already_exists));
}
}
InputTextAlertDialog
class InputTextAlertDialog(context: Context) : BaseAlertDialog(context) {
var onInputOkListener: OnInputOkListener? = null
private var input: EditText? = null
private var error: TextView? = null
var colorError: Int = Color.RED
set(colorError: Int) {
field = colorError
error!!.setTextColor(colorError)
}
init {
error = view.findViewById(R.id.error_text)
input = view.findViewById(R.id.input)
error!!.setTextColor(colorError)
}
fun setErrorMessage(errorMessage: String?) {
error!!.text = errorMessage
input!!.backgroundTintList = ColorStateList.valueOf(colorError);
}
override fun initOkClickListener() {
ok.setOnClickListener { v: View? ->
ok()
}
}
override fun ok() {
onInputOkListener?.onInputSend(this, input!!.text.toString())
}
override fun getLayout(): Int {
return R.layout.input_text_layout
}
interface OnInputOkListener {
fun onInputSend(dialog: InputTextAlertDialog, input: String)
}
}
BaseAlertDialog
public abstract class BaseAlertDialog {
AlertDialog alertDialog;
protected Button cancel;
protected Button ok;
protected View view;
private AlertDialog.Builder builder;
protected Context context;
BaseAlertDialog(Context context) {
this.context = context;
initDialog(context);
initViews(view);
initCancelClickListener();
initOkClickListener();
}
private void initDialog(Context context) {
if (getLayout() != 0) {
view = LayoutInflater.from(context).inflate(getLayout(), null);
} else {
throw new RuntimeException("In getLayout () you should return the layout id");
}
builder = new AlertDialog.Builder(context);
builder.setView(view);
alertDialog = builder.create();
}
private void initViews(View view) {
cancel = view.findViewById(R.id.cancel);
ok = view.findViewById(R.id.ok);
}
private void initCancelClickListener() {
cancel.setOnClickListener(v -> {
alertDialog.cancel();
});
}
protected void initOkClickListener() {
if (ok != null) {
ok.setOnClickListener(v -> {
ok();
alertDialog.cancel();
});
}
}
public void cancel(){
alertDialog.cancel();
}
public void show(){
alertDialog.show();
}
abstract void ok();
abstract int getLayout();
}
input
orerror
would be absent? \$\endgroup\$ – tieskedh May 6 '20 at 13:47findViewById
always find the view, or would it be ok if it didn't? \$\endgroup\$ – tieskedh May 6 '20 at 14:07