Search in sources :

Example 1 with OnGroupUpdateUsername

use of net.iGap.observers.interfaces.OnGroupUpdateUsername in project iGap-Android by KianIranian-STDG.

the class FragmentGroupProfile method setUsername.

// TODO: change this and create custom dialog and handle request in it and ...
// TODO: because no time be this and this code is not correct and should be change it
public void setUsername() {
    final LinearLayout layoutUserName = new LinearLayout(getContext());
    layoutUserName.setOrientation(LinearLayout.VERTICAL);
    final View viewUserName = new View(getContext());
    LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
    final TextInputLayout inputUserName = new TextInputLayout(getContext());
    final MEditText edtUserName = new MEditText(getContext());
    edtUserName.setHint(getString(R.string.group_title_set_username));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        edtUserName.setTextDirection(View.TEXT_DIRECTION_LTR);
    }
    edtUserName.setTypeface(ResourcesCompat.getFont(edtUserName.getContext(), R.font.main_font));
    edtUserName.setTextSize(TypedValue.COMPLEX_UNIT_PX, getContext().getResources().getDimension(R.dimen.dp14));
    // TODO: fixed this and this will set viewModel
    if (viewModel.isPopup) {
        edtUserName.setText(Config.IGAP_LINK_PREFIX);
    } else {
        edtUserName.setText(Config.IGAP_LINK_PREFIX + viewModel.linkUsername);
    }
    edtUserName.setTextColor(getContext().getResources().getColor(R.color.text_edit_text));
    edtUserName.setHintTextColor(getContext().getResources().getColor(R.color.hint_edit_text));
    edtUserName.setPadding(0, 8, 0, 8);
    edtUserName.setSingleLine(true);
    inputUserName.addView(edtUserName);
    inputUserName.addView(viewUserName, viewParams);
    viewUserName.setBackgroundColor(getContext().getResources().getColor(R.color.line_edit_text));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        edtUserName.setBackground(getContext().getResources().getDrawable(android.R.color.transparent));
    }
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutUserName.addView(inputUserName, layoutParams);
    ProgressBar progressBar = new ProgressBar(getContext());
    LinearLayout.LayoutParams progParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    progParams.gravity = Gravity.CENTER;
    progressBar.setLayoutParams(progParams);
    progressBar.setIndeterminate(true);
    progressBar.setVisibility(View.GONE);
    layoutUserName.addView(progressBar);
    final MaterialDialog dialog = new MaterialDialog.Builder(getContext()).title(R.string.st_username).positiveText(R.string.save).customView(layoutUserName, true).widgetColor(new Theme().getAccentColor(getContext())).negativeText(R.string.B_cancel).build();
    final View positive = dialog.getActionButton(DialogAction.POSITIVE);
    positive.setEnabled(false);
    edtUserName.setSelection((edtUserName.getText().toString().length()));
    G.handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            edtUserName.requestFocus();
            InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (inputMethodManager != null) {
                inputMethodManager.showSoftInput(edtUserName, InputMethodManager.SHOW_IMPLICIT);
            }
        }
    }, 100);
    edtUserName.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (!editable.toString().startsWith(Config.IGAP_LINK_PREFIX)) {
                edtUserName.setText(Config.IGAP_LINK_PREFIX);
                Selection.setSelection(edtUserName.getText(), edtUserName.getText().length());
            } else {
                Selection.setSelection(edtUserName.getText(), edtUserName.getText().length());
            }
            if (HelperString.regexCheckUsername(editable.toString().replace(Config.IGAP_LINK_PREFIX, ""))) {
                if (getRequestManager().isUserLogin()) {
                    String userName = edtUserName.getText().toString().replace(Config.IGAP_LINK_PREFIX, "");
                    new RequestGroupCheckUsername().GroupCheckUsername(viewModel.roomId, userName, new OnGroupCheckUsername() {

                        @Override
                        public void onGroupCheckUsername(final ProtoGroupCheckUsername.GroupCheckUsernameResponse.Status status) {
                            G.handler.post(new Runnable() {

                                @Override
                                public void run() {
                                    if (status == ProtoGroupCheckUsername.GroupCheckUsernameResponse.Status.AVAILABLE) {
                                        positive.setEnabled(true);
                                        inputUserName.setErrorEnabled(true);
                                        inputUserName.setError("");
                                    } else if (status == ProtoGroupCheckUsername.GroupCheckUsernameResponse.Status.INVALID) {
                                        positive.setEnabled(false);
                                        inputUserName.setErrorEnabled(true);
                                        inputUserName.setError(getString(R.string.INVALID));
                                    } else if (status == ProtoGroupCheckUsername.GroupCheckUsernameResponse.Status.TAKEN) {
                                        positive.setEnabled(false);
                                        inputUserName.setErrorEnabled(true);
                                        inputUserName.setError(getString(R.string.TAKEN));
                                    } else if (status == ProtoGroupCheckUsername.GroupCheckUsernameResponse.Status.OCCUPYING_LIMIT_EXCEEDED) {
                                        positive.setEnabled(false);
                                        inputUserName.setErrorEnabled(true);
                                        inputUserName.setError("" + getString(R.string.OCCUPYING_LIMIT_EXCEEDED));
                                    }
                                }
                            });
                        }

                        @Override
                        public void onError(int majorCode, int minorCode) {
                            if (majorCode == 5) {
                                positive.setEnabled(false);
                                inputUserName.setErrorEnabled(true);
                                inputUserName.setError(getString(R.string.network_error));
                            } else {
                                positive.setEnabled(false);
                                inputUserName.setErrorEnabled(true);
                                inputUserName.setError(getString(R.string.server_error));
                            }
                        }
                    });
                } else {
                    positive.setEnabled(false);
                    inputUserName.setErrorEnabled(true);
                    inputUserName.setError(getString(R.string.network_error));
                }
            } else {
                positive.setEnabled(false);
                inputUserName.setErrorEnabled(true);
                inputUserName.setError(getString(R.string.INVALID));
            }
        }
    });
    positive.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            hideKeyboard();
            String userName = edtUserName.getText().toString().replace(Config.IGAP_LINK_PREFIX, "");
            if (getRequestManager().isUserLogin()) {
                progressBar.setVisibility(View.VISIBLE);
                positive.setEnabled(false);
                new RequestGroupUpdateUsername().groupUpdateUsername(viewModel.roomId, userName, new OnGroupUpdateUsername() {

                    @Override
                    public void onGroupUpdateUsername(final long roomId, final String username) {
                        G.handler.post(new Runnable() {

                            @Override
                            public void run() {
                                positive.setEnabled(true);
                                progressBar.setVisibility(View.GONE);
                                viewModel.isPrivate = false;
                                dialog.dismiss();
                                viewModel.linkUsername = username;
                                viewModel.setTextGroupLik();
                            }
                        });
                    }

                    @Override
                    public void onError(final int majorCode, int minorCode, final int time) {
                        G.handler.post(new Runnable() {

                            @Override
                            public void run() {
                                positive.setEnabled(true);
                                progressBar.setVisibility(View.GONE);
                                switch(majorCode) {
                                    case 5:
                                        HelperError.showSnackMessage(getString(R.string.wallet_error_server), false);
                                    case 368:
                                        if (dialog.isShowing())
                                            dialog.dismiss();
                                        dialogWaitTime(R.string.GROUP_UPDATE_USERNAME_UPDATE_LOCK, time, majorCode);
                                        break;
                                }
                            }
                        });
                    }
                });
            } else {
                progressBar.setVisibility(View.GONE);
                HelperError.showSnackMessage(getString(R.string.wallet_error_server), false);
            }
        }
    });
    edtUserName.setOnFocusChangeListener((view, b) -> {
        if (b) {
            viewUserName.setBackgroundColor(new Theme().getAccentColor(getContext()));
        } else {
            viewUserName.setBackgroundColor(getContext().getResources().getColor(R.color.line_edit_text));
        }
    });
    // check each word with server
    dialog.setOnDismissListener(dialog1 -> hideKeyboard());
    dialog.show();
}
Also used : RequestGroupUpdateUsername(net.iGap.request.RequestGroupUpdateUsername) InputMethodManager(android.view.inputmethod.InputMethodManager) HelperString(net.iGap.helper.HelperString) OnGroupUpdateUsername(net.iGap.observers.interfaces.OnGroupUpdateUsername) ProtoGroupCheckUsername(net.iGap.proto.ProtoGroupCheckUsername) OnGroupCheckUsername(net.iGap.observers.interfaces.OnGroupCheckUsername) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) TextInputLayout(com.google.android.material.textfield.TextInputLayout) ProgressBar(android.widget.ProgressBar) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) MEditText(net.iGap.module.MEditText) View(android.view.View) TextView(android.widget.TextView) CircleImageView(net.iGap.module.CircleImageView) RequestGroupCheckUsername(net.iGap.request.RequestGroupCheckUsername) Theme(net.iGap.module.Theme) LinearLayout(android.widget.LinearLayout)

Aggregations

Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 View (android.view.View)1 InputMethodManager (android.view.inputmethod.InputMethodManager)1 LinearLayout (android.widget.LinearLayout)1 ProgressBar (android.widget.ProgressBar)1 TextView (android.widget.TextView)1 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)1 TextInputLayout (com.google.android.material.textfield.TextInputLayout)1 HelperString (net.iGap.helper.HelperString)1 CircleImageView (net.iGap.module.CircleImageView)1 MEditText (net.iGap.module.MEditText)1 Theme (net.iGap.module.Theme)1 OnGroupCheckUsername (net.iGap.observers.interfaces.OnGroupCheckUsername)1 OnGroupUpdateUsername (net.iGap.observers.interfaces.OnGroupUpdateUsername)1 ProtoGroupCheckUsername (net.iGap.proto.ProtoGroupCheckUsername)1 RequestGroupCheckUsername (net.iGap.request.RequestGroupCheckUsername)1 RequestGroupUpdateUsername (net.iGap.request.RequestGroupUpdateUsername)1