Search in sources :

Example 1 with ChatResponse

use of com.yellowmessenger.sdk.models.ChatResponse in project yellowmessenger-sdk by yellowmessenger.

the class XMPPService method processMessage.

private void processMessage(String sender, String message) {
    String name = PreferencesManager.getInstance(getApplicationContext()).getBusinessName(sender);
    ChatResponse chatResponse = null;
    try {
        chatResponse = gson.fromJson(message, ChatResponse.class);
    } catch (Exception e) {
    // e.printStackTrace();
    }
    if (chatResponse != null) {
        if (chatResponse.getTyping() != null) {
            if (XMPPService.this.username != null && sender.toLowerCase().equals(XMPPService.this.username.toLowerCase())) {
                Log.d("Event posting: ", message);
                EventBus.getDefault().post(new TypingEvent(sender, chatResponse.getTyping()));
            }
        }
        if (chatResponse.isValid()) {
            ChatMessage chatMessage = new ChatMessage(sender, message, sender, false);
            chatMessage.save();
            if (XMPPService.this.username != null && sender.toLowerCase().equals(XMPPService.this.username.toLowerCase())) {
                Log.d("Event posting: ", message);
                EventBus.getDefault().post(new MessageReceivedEvent(chatMessage));
            }
        }
        if (chatResponse.getTyping() == null && !(XMPPService.this.username != null && sender.toLowerCase().equals(XMPPService.this.username.toLowerCase()))) {
            notifyMessage(sender, name, "...");
        }
    } else {
        processXMPPMessage(sender, name, message);
    }
}
Also used : TypingEvent(com.yellowmessenger.sdk.events.TypingEvent) ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) ChatResponse(com.yellowmessenger.sdk.models.ChatResponse) SmackException(org.jivesoftware.smack.SmackException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageReceivedEvent(com.yellowmessenger.sdk.events.MessageReceivedEvent)

Example 2 with ChatResponse

use of com.yellowmessenger.sdk.models.ChatResponse in project yellowmessenger-sdk by yellowmessenger.

the class ProductFragment method loadProduct.

public void loadProduct() {
    JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.GET, product.getJsonUrl(), null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            String responseString = response.toString();
            ChatResponse chatResponse = gson.fromJson(responseString, ChatResponse.class);
            if (chatResponse.getProduct() != null) {
                ProductFragment.this.setProduct(chatResponse.getProduct());
                showProduct();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    stringRequest.setRetryPolicy(retryPolicy);
    // Add the request to the RequestQueue.
    queue.add(stringRequest);
}
Also used : Response(com.android.volley.Response) ChatResponse(com.yellowmessenger.sdk.models.ChatResponse) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) ChatResponse(com.yellowmessenger.sdk.models.ChatResponse) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 3 with ChatResponse

use of com.yellowmessenger.sdk.models.ChatResponse 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 4 with ChatResponse

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

Aggregations

ChatResponse (com.yellowmessenger.sdk.models.ChatResponse)4 ChatMessage (com.yellowmessenger.sdk.models.db.ChatMessage)3 View (android.view.View)2 HorizontalScrollView (android.widget.HorizontalScrollView)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 Product (com.yellowmessenger.sdk.models.Product)2 ParseException (java.text.ParseException)2 Date (java.util.Date)2 Intent (android.content.Intent)1 Paint (android.graphics.Paint)1 ViewGroup (android.view.ViewGroup)1 RelativeLayout (android.widget.RelativeLayout)1 Response (com.android.volley.Response)1 VolleyError (com.android.volley.VolleyError)1 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)1 MessageReceivedEvent (com.yellowmessenger.sdk.events.MessageReceivedEvent)1 SendActionEvent (com.yellowmessenger.sdk.events.SendActionEvent)1 TypingEvent (com.yellowmessenger.sdk.events.TypingEvent)1 Action (com.yellowmessenger.sdk.models.Action)1