Search in sources :

Example 1 with EnumSelectionCallback

use of com.onesignal.sdktest.callback.EnumSelectionCallback in project OneSignal-Android-SDK by OneSignal.

the class Dialog method createSendOutcomeAlertDialog.

public void createSendOutcomeAlertDialog(final String content) {
    final View sendOutcomeAlertDialogView = layoutInflater.inflate(R.layout.send_outcome_alert_dialog_layout, null, false);
    final CardView sendOutcomeDialogTitleCardView = sendOutcomeAlertDialogView.findViewById(R.id.send_outcome_alert_dialog_selection_card_view);
    final RelativeLayout sendOutcomeDialogTitleRelativeLayout = sendOutcomeAlertDialogView.findViewById(R.id.send_outcome_alert_dialog_selection_relative_layout);
    final TextView sendOutcomeDialogTitleTextView = sendOutcomeAlertDialogView.findViewById(R.id.send_outcome_alert_dialog_selection_text_view);
    final ImageView sendOutcomeDialogTitleArrowImageView = sendOutcomeAlertDialogView.findViewById(R.id.send_outcome_alert_dialog_selection_arrow_image_view);
    final RecyclerView sendOutcomeDialogSelectionRecyclerView = sendOutcomeAlertDialogView.findViewById(R.id.send_outcome_alert_dialog_selection_recycler_view);
    final LinearLayout sendOutcomeDialogSelectionContentLinearLayout = sendOutcomeAlertDialogView.findViewById(R.id.send_outcome_alert_dialog_content_linear_layout);
    final TextInputLayout sendOutcomeDialogNameTextInputLayout = sendOutcomeAlertDialogView.findViewById(R.id.send_outcome_alert_dialog_name_text_input_layout);
    final EditText sendOutcomeDialogNameEditText = sendOutcomeAlertDialogView.findViewById(R.id.send_outcome_alert_dialog_name_edit_text);
    final TextInputLayout sendOutcomeDialogValueTextInputLayout = sendOutcomeAlertDialogView.findViewById(R.id.send_outcome_alert_dialog_value_text_input_layout);
    final EditText sendOutcomeDialogValueEditText = sendOutcomeAlertDialogView.findViewById(R.id.send_outcome_alert_dialog_value_edit_text);
    final ProgressBar sendOutcomeDialogProgressBar = sendOutcomeAlertDialogView.findViewById(R.id.send_outcome_alert_dialog_progress_bar);
    sendOutcomeDialogNameTextInputLayout.setHint("Name");
    sendOutcomeDialogValueTextInputLayout.setHint("Value");
    sendOutcomeDialogTitleTextView.setText(content);
    font.applyFont(sendOutcomeDialogTitleTextView, font.saralaBold);
    font.applyFont(sendOutcomeDialogNameTextInputLayout, font.saralaBold);
    font.applyFont(sendOutcomeDialogValueTextInputLayout, font.saralaBold);
    sendOutcomeDialogTitleCardView.setCardElevation(8f);
    recyclerViewBuilder.setupRecyclerView(sendOutcomeDialogSelectionRecyclerView, 3, false, true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
    sendOutcomeDialogSelectionRecyclerView.setLayoutManager(linearLayoutManager);
    EnumSelectionRecyclerViewAdapter enumSelectionRecyclerViewAdapter = new EnumSelectionRecyclerViewAdapter(context, OutcomeEvent.values(), new EnumSelectionCallback() {

        @Override
        public void onSelection(String title) {
            int nameVisibility = View.GONE;
            int valueVisibility = View.GONE;
            OutcomeEvent outcomeEvent = OutcomeEvent.enumFromTitleString(title);
            if (outcomeEvent == null) {
                Drawable arrow = context.getResources().getDrawable(R.drawable.ic_chevron_down_white_48dp);
                sendOutcomeDialogTitleArrowImageView.setImageDrawable(arrow);
                sendOutcomeDialogTitleCardView.setCardElevation(0f);
                sendOutcomeDialogSelectionRecyclerView.setVisibility(View.GONE);
                sendOutcomeDialogSelectionContentLinearLayout.setVisibility(View.GONE);
                sendOutcomeDialogNameEditText.setVisibility(nameVisibility);
                sendOutcomeDialogValueTextInputLayout.setVisibility(valueVisibility);
                return;
            }
            switch(outcomeEvent) {
                case OUTCOME:
                case UNIQUE_OUTCOME:
                    nameVisibility = View.VISIBLE;
                    break;
                case OUTCOME_WITH_VALUE:
                    nameVisibility = View.VISIBLE;
                    valueVisibility = View.VISIBLE;
                    break;
            }
            sendOutcomeDialogTitleTextView.setText(outcomeEvent.getTitle());
            Drawable arrow = context.getResources().getDrawable(R.drawable.ic_chevron_down_white_48dp);
            sendOutcomeDialogTitleArrowImageView.setImageDrawable(arrow);
            sendOutcomeDialogTitleCardView.setCardElevation(0f);
            sendOutcomeDialogSelectionRecyclerView.setVisibility(View.GONE);
            sendOutcomeDialogSelectionContentLinearLayout.setVisibility(View.VISIBLE);
            sendOutcomeDialogNameTextInputLayout.setVisibility(nameVisibility);
            sendOutcomeDialogNameEditText.setVisibility(nameVisibility);
            sendOutcomeDialogValueTextInputLayout.setVisibility(valueVisibility);
            sendOutcomeDialogValueEditText.setVisibility(valueVisibility);
        }
    });
    sendOutcomeDialogSelectionRecyclerView.setAdapter(enumSelectionRecyclerViewAdapter);
    sendOutcomeDialogTitleRelativeLayout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean showMenu = sendOutcomeDialogSelectionRecyclerView.getVisibility() == View.GONE;
            Drawable arrow = context.getResources().getDrawable(showMenu ? R.drawable.ic_chevron_up_white_48dp : R.drawable.ic_chevron_down_white_48dp);
            int menuVisibility = showMenu ? View.VISIBLE : View.GONE;
            int contentVisibility = showMenu ? View.GONE : View.VISIBLE;
            float shadow = showMenu ? 8f : 0f;
            sendOutcomeDialogTitleArrowImageView.setImageDrawable(arrow);
            sendOutcomeDialogTitleCardView.setCardElevation(shadow);
            sendOutcomeDialogSelectionRecyclerView.setVisibility(menuVisibility);
            sendOutcomeDialogSelectionContentLinearLayout.setVisibility(contentVisibility);
            int nameVisibility = View.GONE;
            int valueVisibility = View.GONE;
            String selectedTitle = sendOutcomeDialogTitleTextView.getText().toString();
            OutcomeEvent outcomeEvent = OutcomeEvent.enumFromTitleString(selectedTitle);
            if (outcomeEvent == null) {
                sendOutcomeDialogSelectionContentLinearLayout.setVisibility(View.GONE);
                return;
            }
            if (!showMenu) {
                switch(outcomeEvent) {
                    case OUTCOME:
                    case UNIQUE_OUTCOME:
                        nameVisibility = View.VISIBLE;
                        break;
                    case OUTCOME_WITH_VALUE:
                        nameVisibility = View.VISIBLE;
                        valueVisibility = View.VISIBLE;
                        break;
                }
            }
            sendOutcomeDialogSelectionContentLinearLayout.setVisibility(nameVisibility);
            sendOutcomeDialogNameEditText.setVisibility(nameVisibility);
            sendOutcomeDialogValueTextInputLayout.setVisibility(valueVisibility);
            sendOutcomeDialogValueEditText.setVisibility(valueVisibility);
        }
    });
    final CustomAlertDialogBuilder sendOutcomeAlertDialog = new CustomAlertDialogBuilder(context, sendOutcomeAlertDialogView);
    sendOutcomeAlertDialog.setView(sendOutcomeAlertDialogView);
    sendOutcomeAlertDialog.setIsCancelable(true);
    sendOutcomeAlertDialog.setCanceledOnTouchOutside(false);
    sendOutcomeAlertDialog.setPositiveButton(Text.BUTTON_SEND, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(final DialogInterface dialog, int which) {
            toggleUpdateAlertDialogAttributes(true);
            String selectedTitle = sendOutcomeDialogTitleTextView.getText().toString();
            OutcomeEvent outcomeEvent = OutcomeEvent.enumFromTitleString(selectedTitle);
            if (outcomeEvent == null) {
                toaster.makeCustomViewToast("Please select an outcome type!", ToastType.ERROR);
                toggleUpdateAlertDialogAttributes(false);
                return;
            }
            String name = sendOutcomeDialogNameEditText.getText().toString().trim();
            String value = sendOutcomeDialogValueEditText.getText().toString().trim();
            if (name.isEmpty()) {
                toaster.makeCustomViewToast("Please enter an outcome name!", ToastType.ERROR);
                toggleUpdateAlertDialogAttributes(false);
                return;
            }
            switch(outcomeEvent) {
                case OUTCOME:
                    OneSignal.sendOutcome(name, new OneSignal.OutcomeCallback() {

                        @Override
                        public void onSuccess(@Nullable OSOutcomeEvent outcomeEvent) {
                            ((Activity) context).runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    toggleUpdateAlertDialogAttributes(false);
                                    dialog.dismiss();
                                }
                            });
                        }
                    });
                    break;
                case UNIQUE_OUTCOME:
                    OneSignal.sendUniqueOutcome(name, new OneSignal.OutcomeCallback() {

                        @Override
                        public void onSuccess(@Nullable OSOutcomeEvent outcomeEvent) {
                            ((Activity) context).runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    toggleUpdateAlertDialogAttributes(false);
                                    dialog.dismiss();
                                }
                            });
                        }
                    });
                    break;
                case OUTCOME_WITH_VALUE:
                    if (value.isEmpty()) {
                        toaster.makeCustomViewToast("Please enter an outcome value!", ToastType.ERROR);
                        toggleUpdateAlertDialogAttributes(false);
                        return;
                    }
                    OneSignal.sendOutcomeWithValue(name, Float.parseFloat(value), new OneSignal.OutcomeCallback() {

                        @Override
                        public void onSuccess(@Nullable OSOutcomeEvent outcomeEvent) {
                            ((Activity) context).runOnUiThread(new Runnable() {

                                @Override
                                public void run() {
                                    toggleUpdateAlertDialogAttributes(false);
                                    dialog.dismiss();
                                }
                            });
                        }
                    });
                    break;
            }
            InterfaceUtil.hideKeyboardFrom(context, sendOutcomeAlertDialogView);
        }

        private void toggleUpdateAlertDialogAttributes(boolean disableAttributes) {
            int progressVisibility = disableAttributes ? View.VISIBLE : View.GONE;
            sendOutcomeDialogProgressBar.setVisibility(progressVisibility);
            int buttonVisibility = disableAttributes ? View.GONE : View.VISIBLE;
            sendOutcomeAlertDialog.getPositiveButtonElement().setVisibility(buttonVisibility);
            sendOutcomeAlertDialog.getNegativeButtonElement().setVisibility(buttonVisibility);
            sendOutcomeAlertDialog.getPositiveButtonElement().setEnabled(!disableAttributes);
            sendOutcomeAlertDialog.getNegativeButtonElement().setEnabled(!disableAttributes);
            sendOutcomeAlertDialog.setIsCancelable(!disableAttributes);
        }
    }).setNegativeButton(Text.BUTTON_CANCEL, null);
    sendOutcomeAlertDialog.show();
}
Also used : OneSignal(com.onesignal.OneSignal) DialogInterface(android.content.DialogInterface) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) OutcomeEvent(com.onesignal.sdktest.type.OutcomeEvent) OSOutcomeEvent(com.onesignal.OSOutcomeEvent) TextView(android.widget.TextView) CustomAlertDialogBuilder(com.onesignal.sdktest.ui.CustomAlertDialogBuilder) ImageView(android.widget.ImageView) TextInputLayout(com.google.android.material.textfield.TextInputLayout) ProgressBar(android.widget.ProgressBar) EditText(android.widget.EditText) OSOutcomeEvent(com.onesignal.OSOutcomeEvent) CardView(androidx.cardview.widget.CardView) Drawable(android.graphics.drawable.Drawable) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) CardView(androidx.cardview.widget.CardView) TextView(android.widget.TextView) EnumSelectionCallback(com.onesignal.sdktest.callback.EnumSelectionCallback) RelativeLayout(android.widget.RelativeLayout) RecyclerView(androidx.recyclerview.widget.RecyclerView) LinearLayout(android.widget.LinearLayout) EnumSelectionRecyclerViewAdapter(com.onesignal.sdktest.adapter.EnumSelectionRecyclerViewAdapter)

Aggregations

DialogInterface (android.content.DialogInterface)1 Drawable (android.graphics.drawable.Drawable)1 View (android.view.View)1 EditText (android.widget.EditText)1 ImageView (android.widget.ImageView)1 LinearLayout (android.widget.LinearLayout)1 ProgressBar (android.widget.ProgressBar)1 RelativeLayout (android.widget.RelativeLayout)1 TextView (android.widget.TextView)1 CardView (androidx.cardview.widget.CardView)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 TextInputLayout (com.google.android.material.textfield.TextInputLayout)1 OSOutcomeEvent (com.onesignal.OSOutcomeEvent)1 OneSignal (com.onesignal.OneSignal)1 EnumSelectionRecyclerViewAdapter (com.onesignal.sdktest.adapter.EnumSelectionRecyclerViewAdapter)1 EnumSelectionCallback (com.onesignal.sdktest.callback.EnumSelectionCallback)1 OutcomeEvent (com.onesignal.sdktest.type.OutcomeEvent)1 CustomAlertDialogBuilder (com.onesignal.sdktest.ui.CustomAlertDialogBuilder)1