Search in sources :

Example 1 with SendActionEvent

use of com.yellowmessenger.sdk.events.SendActionEvent in project yellowmessenger-sdk by yellowmessenger.

the class ChatListAdapter method getSearchView.

private View getSearchView(int position, View convertView, ViewGroup parent, boolean portraitMode) {
    View view = convertView;
    final ChatMessage chatMessage = values.get(position);
    SearchViewHolder searchViewHolder = null;
    if (view == null) {
        view = inflater.inflate(R.layout.chat_list_item_search, parent, false);
        searchViewHolder = new SearchViewHolder();
        searchViewHolder.timestamp = (TextView) view.findViewById(R.id.timestamp);
        searchViewHolder.message = (TextView) view.findViewById(R.id.message);
        for (int i = 0; i < 10; i++) {
            View productView = inflater.inflate(portraitMode ? R.layout.list_product_portrait_view : R.layout.list_product_view, parent, false);
            ((ViewGroup) view.findViewById(R.id.productsView)).addView(productView);
            searchViewHolder.productsLayouts.add(productView);
        }
        searchViewHolder.scrollView = (HorizontalScrollView) view.findViewById(R.id.scrollView);
        view.setTag(searchViewHolder);
    } else {
        searchViewHolder = (SearchViewHolder) view.getTag();
    }
    searchViewHolder.scrollView.scrollTo(0, 0);
    final ChatResponse chatResponse = chatMessage.getChatResponse();
    if (chatResponse.getSearchResults().getMessage() != null) {
        searchViewHolder.message.setVisibility(View.VISIBLE);
        searchViewHolder.message.setText(chatResponse.getSearchResults().getMessage());
    } else {
        searchViewHolder.message.setVisibility(GONE);
    }
    int productSize = chatResponse.getSearchResults().getProducts() != null ? chatResponse.getSearchResults().getProducts().size() : 0;
    final List<Product> products = chatResponse.getSearchResults().getProducts();
    if (productSize == 0) {
        for (int i = 0; i < 10; i++) {
            searchViewHolder.productsLayouts.get(i).setVisibility(GONE);
        }
    }
    for (int i = 0; i < 10; i++) {
        final int index = i;
        if (productSize > i) {
            searchViewHolder.productsLayouts.get(i).setVisibility(View.VISIBLE);
            searchViewHolder.productsLayouts.get(i).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (products.get(index).getJsonUrl() != null) {
                        ((ChatActivity) context).openProductView(products.get(index));
                    }
                }
            });
            ((TextView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_name)).setText(products.get(i).getName());
            if (products.get(i).getPrice() != null) {
                searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_price).setVisibility(View.VISIBLE);
                ((TextView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_price)).setText(Html.fromHtml(products.get(i).getPrice()));
            } else {
                searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_price).setVisibility(GONE);
            }
            if (products.get(i).getPriceOriginal() != null && !products.get(i).getPriceOriginal().equals(products.get(i).getPrice())) {
                ((TextView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_price_original)).setText(Html.fromHtml(products.get(i).getPriceOriginal()));
                ((TextView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_price_original)).setPaintFlags(((TextView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_price_original)).getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_price_original).setVisibility(View.VISIBLE);
                if (products.get(i).getDiscount() != null) {
                    ((TextView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_discount)).setText(Html.fromHtml(products.get(i).getDiscount()));
                    searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_discount).setVisibility(View.VISIBLE);
                }
            } else {
                searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_price_original).setVisibility(GONE);
                searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_discount).setVisibility(GONE);
            }
            if (products.get(i).getDescription() != null) {
                searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_description).setVisibility(View.VISIBLE);
                ((TextView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_description)).setText(Html.fromHtml(products.get(i).getDescription()));
            } else {
                searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_description).setVisibility(GONE);
            }
            TextView buttonPrimary = (TextView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_action_button);
            TextView buttonSecondary = (TextView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_action_button_secondary);
            TextView button1 = (TextView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_action_button_1);
            TextView button2 = (TextView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_action_button_2);
            TextView button3 = (TextView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_action_button_3);
            searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_details).setVisibility(View.VISIBLE);
            if (products.get(i).getActions() != null && products.get(i).getActions().size() > 0) {
                if (products.get(i).getActions().size() > 2 || chatResponse.getSearchResults().isSelection()) {
                    button1.setVisibility(View.GONE);
                    button2.setVisibility(View.GONE);
                    button3.setVisibility(View.GONE);
                    for (int x = 0; x < products.get(i).getActions().size(); x++) {
                        final Action action = products.get(i).getActions().get(x);
                        switch(x) {
                            case 0:
                                button1.setVisibility(View.VISIBLE);
                                button1.setText(action.getTitle());
                                button1.setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        if (action.getUrl() != null) {
                                            Intent i = new Intent(Intent.ACTION_VIEW);
                                            i.setData(Uri.parse(action.getUrl()));
                                            context.startActivity(i);
                                        } else {
                                            ((ChatActivity) context).sendActionEvent(new SendActionEvent(action));
                                        }
                                    }
                                });
                                break;
                            case 1:
                                button2.setVisibility(View.VISIBLE);
                                button2.setText(action.getTitle());
                                button2.setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        if (action.getUrl() != null) {
                                            Intent i = new Intent(Intent.ACTION_VIEW);
                                            i.setData(Uri.parse(action.getUrl()));
                                            context.startActivity(i);
                                        } else {
                                            ((ChatActivity) context).sendActionEvent(new SendActionEvent(action));
                                        }
                                    }
                                });
                                break;
                            case 2:
                                button3.setVisibility(View.VISIBLE);
                                button3.setText(action.getTitle());
                                button3.setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        if (action.getUrl() != null) {
                                            Intent i = new Intent(Intent.ACTION_VIEW);
                                            i.setData(Uri.parse(action.getUrl()));
                                            context.startActivity(i);
                                        } else {
                                            ((ChatActivity) context).sendActionEvent(new SendActionEvent(action));
                                        }
                                    }
                                });
                                break;
                        }
                        if (x == 2) {
                            break;
                        }
                    }
                    buttonPrimary.setVisibility(GONE);
                    buttonSecondary.setVisibility(GONE);
                } else {
                    button1.setVisibility(View.GONE);
                    button2.setVisibility(View.GONE);
                    button3.setVisibility(View.GONE);
                    buttonPrimary.setVisibility(View.VISIBLE);
                    buttonPrimary.setText(products.get(i).getActions().get(0).getTitle());
                    final Action action = products.get(i).getActions().get(0);
                    buttonPrimary.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if (action.getUrl() != null) {
                                Intent i = new Intent(Intent.ACTION_VIEW);
                                i.setData(Uri.parse(action.getUrl()));
                                context.startActivity(i);
                            } else {
                                ((ChatActivity) context).sendActionEvent(new SendActionEvent(action));
                            }
                        }
                    });
                    if (products.get(i).getActions().size() > 1) {
                        buttonSecondary.setVisibility(View.VISIBLE);
                        buttonSecondary.setText(products.get(i).getActions().get(1).getTitle());
                        final Action actionSecondary = products.get(i).getActions().get(1);
                        buttonSecondary.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                if (actionSecondary.getUrl() != null) {
                                    Intent i = new Intent(Intent.ACTION_VIEW);
                                    i.setData(Uri.parse(actionSecondary.getUrl()));
                                    context.startActivity(i);
                                } else {
                                    ((ChatActivity) context).sendActionEvent(new SendActionEvent(actionSecondary));
                                }
                            }
                        });
                    } else {
                        buttonSecondary.setVisibility(GONE);
                    }
                }
            } else {
                buttonPrimary.setVisibility(GONE);
                buttonSecondary.setVisibility(GONE);
            }
            int detailsMarginBottom = products.get(i).getActions() != null ? (products.get(i).getActions().size() > 1 ? margin42 * 2 : (products.get(i).getActions().size() > 0) ? margin42 : 0) : 0;
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(0, 0, 0, detailsMarginBottom);
            searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_details).setLayoutParams(layoutParams);
            if (products.get(i).getImage() != null) {
                DrawableManager.getInstance(context).fetchDrawableOnThread(products.get(i).getImage(), ((ImageView) searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_image)));
            } else {
                searchViewHolder.productsLayouts.get(i).findViewById(R.id.product_image).setVisibility(GONE);
            }
        } else {
            searchViewHolder.productsLayouts.get(i).setVisibility(GONE);
        }
    }
    try {
        searchViewHolder.timestamp.setText(DateUtils.getRelativeTimeSpanString(format.parse(values.get(position).getTimestamp()).getTime(), (new Date()).getTime(), DateUtils.FORMAT_ABBREV_RELATIVE).toString());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return view;
}
Also used : Action(com.yellowmessenger.sdk.models.Action) ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) ViewGroup(android.view.ViewGroup) Product(com.yellowmessenger.sdk.models.Product) Intent(android.content.Intent) SendActionEvent(com.yellowmessenger.sdk.events.SendActionEvent) ImageView(android.widget.ImageView) HorizontalScrollView(android.widget.HorizontalScrollView) View(android.view.View) TextView(android.widget.TextView) Paint(android.graphics.Paint) Date(java.util.Date) ChatResponse(com.yellowmessenger.sdk.models.ChatResponse) RelativeLayout(android.widget.RelativeLayout) TextView(android.widget.TextView) ImageView(android.widget.ImageView) ParseException(java.text.ParseException)

Aggregations

Intent (android.content.Intent)1 Paint (android.graphics.Paint)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 HorizontalScrollView (android.widget.HorizontalScrollView)1 ImageView (android.widget.ImageView)1 RelativeLayout (android.widget.RelativeLayout)1 TextView (android.widget.TextView)1 SendActionEvent (com.yellowmessenger.sdk.events.SendActionEvent)1 Action (com.yellowmessenger.sdk.models.Action)1 ChatResponse (com.yellowmessenger.sdk.models.ChatResponse)1 Product (com.yellowmessenger.sdk.models.Product)1 ChatMessage (com.yellowmessenger.sdk.models.db.ChatMessage)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1