Search in sources :

Example 26 with ChatMessage

use of com.yellowmessenger.sdk.models.db.ChatMessage 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)

Example 27 with ChatMessage

use of com.yellowmessenger.sdk.models.db.ChatMessage in project yellowmessenger-sdk by yellowmessenger.

the class ChatListAdapter method getMessageView.

private View getMessageView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    final ChatMessage chatMessage = values.get(position);
    MessageViewHolder messageViewHolder = null;
    if (view == null) {
        view = inflater.inflate(R.layout.chat_list_item, parent, false);
        messageViewHolder = new MessageViewHolder();
        messageViewHolder.message = (TextView) view.findViewById(R.id.message);
        messageViewHolder.timestamp = (TextView) view.findViewById(R.id.timestamp);
        view.setTag(messageViewHolder);
    } else {
        messageViewHolder = (MessageViewHolder) view.getTag();
    }
    messageViewHolder.message.setText(chatMessage.getMessage());
    try {
        messageViewHolder.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 : ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) ParseException(java.text.ParseException) ImageView(android.widget.ImageView) HorizontalScrollView(android.widget.HorizontalScrollView) View(android.view.View) TextView(android.widget.TextView) Date(java.util.Date)

Example 28 with ChatMessage

use of com.yellowmessenger.sdk.models.db.ChatMessage in project yellowmessenger-sdk by yellowmessenger.

the class ChatListAdapter method getProductView.

private View getProductView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    final ChatMessage chatMessage = values.get(position);
    ProductViewHolder productViewHolder = null;
    if (view == null) {
        view = inflater.inflate(R.layout.chat_list_item_product, parent, false);
        productViewHolder = new ProductViewHolder();
        productViewHolder.timestamp = (TextView) view.findViewById(R.id.timestamp);
        productViewHolder.productName = (TextView) view.findViewById(R.id.product_name);
        productViewHolder.productPrice = (TextView) view.findViewById(R.id.product_price);
        productViewHolder.productPriceOriginal = (TextView) view.findViewById(R.id.product_price_original);
        productViewHolder.productImageLayout = view.findViewById(R.id.product_image_layout);
        productViewHolder.productPriceDiscount = (TextView) view.findViewById(R.id.product_price_discount);
        productViewHolder.productDescription = (TextView) view.findViewById(R.id.product_description);
        productViewHolder.buy = view.findViewById(R.id.buy_layout);
        productViewHolder.productActions = view.findViewById(R.id.product_actions);
        productViewHolder.imageView = (ImageView) view.findViewById(R.id.product_image);
        productViewHolder.buyButton = (TextView) view.findViewById(R.id.buy_button);
        productViewHolder.productDetails = view.findViewById(R.id.product_details);
        view.setTag(productViewHolder);
    } else {
        productViewHolder = (ProductViewHolder) view.getTag();
    }
    productViewHolder.productActions.setVisibility(GONE);
    productViewHolder.timestamp.setVisibility(View.VISIBLE);
    try {
        productViewHolder.timestamp.setText(DateUtils.getRelativeTimeSpanString(format.parse(values.get(position).getTimestamp()).getTime(), (new Date()).getTime(), DateUtils.FORMAT_ABBREV_RELATIVE).toString());
    } catch (ParseException e) {
        e.printStackTrace();
    }
    ChatResponse chatResponse = chatMessage.getChatResponse();
    if (chatResponse != null) {
        final Product product = chatResponse.getProduct();
        if (product != null) {
            productViewHolder.imageView.setVisibility(GONE);
            if (product.getName() != null) {
                productViewHolder.productName.setVisibility(View.VISIBLE);
                productViewHolder.productName.setText(product.getName());
                productViewHolder.productDetails.setVisibility(View.VISIBLE);
            } else {
                productViewHolder.productName.setVisibility(GONE);
                productViewHolder.productDetails.setVisibility(GONE);
            }
            if (product.getPrice() != null) {
                productViewHolder.productPrice.setText(Html.fromHtml(product.getPrice()));
                productViewHolder.productPrice.setVisibility(View.VISIBLE);
            } else {
                productViewHolder.productPrice.setVisibility(GONE);
            }
            if (product.getPriceOriginal() != null && !product.getPriceOriginal().equals(product.getPrice())) {
                productViewHolder.productPriceOriginal.setText(Html.fromHtml(product.getPriceOriginal()));
                productViewHolder.productPriceOriginal.setPaintFlags(productViewHolder.productPriceOriginal.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                productViewHolder.productPriceOriginal.setVisibility(View.VISIBLE);
                if (product.getDiscount() != null) {
                    productViewHolder.productPriceDiscount.setText(Html.fromHtml(product.getDiscount()));
                    productViewHolder.productPriceDiscount.setVisibility(View.VISIBLE);
                }
            } else {
                productViewHolder.productPriceOriginal.setVisibility(GONE);
                productViewHolder.productPriceDiscount.setVisibility(GONE);
            }
            if (product.getDescription() != null) {
                productViewHolder.productDescription.setVisibility(View.VISIBLE);
                productViewHolder.productDescription.setText(Html.fromHtml(product.getDescription()));
            } else {
                productViewHolder.productDescription.setVisibility(GONE);
            }
            if (product.getUrl() != null) {
                productViewHolder.productActions.setVisibility(View.VISIBLE);
                final String buyUrl = product.getUrl();
                final String buyTitle = product.getAction();
                productViewHolder.buyButton.setText(Html.fromHtml(product.getAction()));
                productViewHolder.buy.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                    }
                });
            } else {
                productViewHolder.productActions.setVisibility(GONE);
            }
            if (product.getImage() != null) {
                productViewHolder.imageView.setVisibility(View.VISIBLE);
                DrawableManager.getInstance(context).fetchDrawableOnThread(product.getImage(), productViewHolder.imageView);
                final String imageUrl = product.getImage();
                productViewHolder.imageView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                    /*Intent intent = new Intent(context, ImageActivity.class);
                            Bundle bundle = new Bundle();

                            bundle.putStringArrayList("urls", new ArrayList<>(Collections.singletonList(imageUrl)));
                            intent.putExtras(bundle);
                            ((Activity) context).startActivityForResult(intent, 0);*/
                    }
                });
            }
        }
    }
    return view;
}
Also used : ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) ChatResponse(com.yellowmessenger.sdk.models.ChatResponse) Product(com.yellowmessenger.sdk.models.Product) ParseException(java.text.ParseException) ImageView(android.widget.ImageView) HorizontalScrollView(android.widget.HorizontalScrollView) View(android.view.View) TextView(android.widget.TextView) Date(java.util.Date)

Example 29 with ChatMessage

use of com.yellowmessenger.sdk.models.db.ChatMessage in project yellowmessenger-sdk by yellowmessenger.

the class ChatListAdapter method getLocationView.

private View getLocationView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    final ChatMessage chatMessage = values.get(position);
    LocationViewHolder locationViewHolder = null;
    if (view == null) {
        view = inflater.inflate(R.layout.chat_list_item_location, parent, false);
        locationViewHolder = new LocationViewHolder();
        locationViewHolder.mapView = (ImageView) view.findViewById(R.id.map_view);
        locationViewHolder.timestamp = (TextView) view.findViewById(R.id.timestamp);
        view.setTag(locationViewHolder);
    } else {
        locationViewHolder = (LocationViewHolder) view.getTag();
    }
    try {
        String markers = "color:red%7Clabel:A%7C" + chatMessage.getChatResponse().getLocation().getLat() + "," + chatMessage.getChatResponse().getLocation().getLng();
        String url = "http://maps.google.com/maps/api/staticmap?center=" + chatMessage.getChatResponse().getLocation().getLat() + "," + chatMessage.getChatResponse().getLocation().getLng() + "&scale=2&markers=" + markers + "&zoom=15&size=280x140&sensor=false";
        DrawableManager.getInstance(getContext()).fetchDrawableOnThread(url, locationViewHolder.mapView);
        String timestamp = DateUtils.getRelativeTimeSpanString(format.parse(values.get(position).getTimestamp()).getTime(), (new Date()).getTime(), DateUtils.FORMAT_ABBREV_RELATIVE).toString() + " - " + (chatMessage.getAcknowledged() ? "Sent" : "Sending..");
        locationViewHolder.timestamp.setText(timestamp);
        locationViewHolder.mapView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String uri = String.format(Locale.ENGLISH, "geo:%f,%f?q=%f,%f(%s)", chatMessage.getChatResponse().getLocation().getLat(), chatMessage.getChatResponse().getLocation().getLng(), chatMessage.getChatResponse().getLocation().getLat(), chatMessage.getChatResponse().getLocation().getLng(), chatMessage.getChatResponse().getLocation().getName() != null ? chatMessage.getChatResponse().getLocation().getName() : "");
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
                context.startActivity(intent);
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    return view;
}
Also used : ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) Intent(android.content.Intent) ImageView(android.widget.ImageView) HorizontalScrollView(android.widget.HorizontalScrollView) View(android.view.View) TextView(android.widget.TextView) Date(java.util.Date) ParseException(java.text.ParseException)

Aggregations

ChatMessage (com.yellowmessenger.sdk.models.db.ChatMessage)29 SendMessageEvent (com.yellowmessenger.sdk.events.SendMessageEvent)13 View (android.view.View)7 HorizontalScrollView (android.widget.HorizontalScrollView)7 ImageView (android.widget.ImageView)7 TextView (android.widget.TextView)7 ParseException (java.text.ParseException)7 Date (java.util.Date)7 Subscribe (org.greenrobot.eventbus.Subscribe)5 Intent (android.content.Intent)4 ChatResponse (com.yellowmessenger.sdk.models.ChatResponse)3 Question (com.yellowmessenger.sdk.models.Question)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 SmackException (org.jivesoftware.smack.SmackException)3 JSONObject (org.json.JSONObject)3 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)3 Paint (android.graphics.Paint)2 Bundle (android.os.Bundle)2 ViewGroup (android.view.ViewGroup)2 MessageReceivedEvent (com.yellowmessenger.sdk.events.MessageReceivedEvent)2