Search in sources :

Example 16 with ChatMessage

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

the class ChatListAdapter method getQuestionView.

private View getQuestionView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    final ChatMessage chatMessage = values.get(position);
    QuestionViewHolder questionViewHolder = null;
    if (view == null) {
        view = inflater.inflate(R.layout.chat_list_item_question, parent, false);
        questionViewHolder = new QuestionViewHolder();
        questionViewHolder.message = (TextView) view.findViewById(R.id.message);
        questionViewHolder.timestamp = (TextView) view.findViewById(R.id.timestamp);
        questionViewHolder.actionButton = view.findViewById(R.id.action_button);
        questionViewHolder.actionText = (TextView) view.findViewById(R.id.action_text);
        for (int i = 0; i < 10; i++) {
            View optionView = inflater.inflate(R.layout.options_view, parent, false);
            ((ViewGroup) view.findViewById(R.id.persistentOptions)).addView(optionView);
            questionViewHolder.persistentOptions.add(optionView);
        }
        view.setTag(questionViewHolder);
    } else {
        questionViewHolder = (QuestionViewHolder) view.getTag();
    }
    final Question question = chatMessage.getChatResponse().getQuestion();
    questionViewHolder.message.setText(chatMessage.getChatResponse().getQuestion().getQuestion());
    if (question.getAction() != null) {
        questionViewHolder.actionButton.setVisibility(View.VISIBLE);
        questionViewHolder.actionText.setText(question.getAction().getLabel());
        questionViewHolder.actionButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ((ChatActivity) context).sendOptionEvent(new SendOptionEvent(question.getAction()));
            }
        });
    } else {
        questionViewHolder.actionButton.setVisibility(GONE);
    }
    for (View optionView : questionViewHolder.persistentOptions) {
        optionView.setVisibility(GONE);
        optionView.findViewById(R.id.button_1).setVisibility(GONE);
        optionView.findViewById(R.id.button_2).setVisibility(GONE);
        optionView.findViewById(R.id.button_3).setVisibility(GONE);
    }
    if (question.isPersistentOptions() && question.getOptions() != null && question.getOptions().size() > 0) {
        for (int i = 0; i < question.getOptions().size(); i++) {
            View optionView = questionViewHolder.persistentOptions.get(i / 3);
            optionView.setVisibility(View.VISIBLE);
            int buttonNumber = i % 3;
            TextView button = null;
            if (buttonNumber == 0) {
                button = (TextView) optionView.findViewById(R.id.button_1);
            } else if (buttonNumber == 1) {
                button = (TextView) optionView.findViewById(R.id.button_2);
            } else {
                button = (TextView) optionView.findViewById(R.id.button_3);
            }
            final Option option = question.getOptions().get(i);
            button.setVisibility(View.VISIBLE);
            button.setText(question.getOptions().get(i).getLabel());
            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((ChatActivity) context).sendOptionEvent(new SendOptionEvent(option));
                }
            });
        }
    }
    try {
        questionViewHolder.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) ViewGroup(android.view.ViewGroup) SendOptionEvent(com.yellowmessenger.sdk.events.SendOptionEvent) ImageView(android.widget.ImageView) HorizontalScrollView(android.widget.HorizontalScrollView) View(android.view.View) TextView(android.widget.TextView) Paint(android.graphics.Paint) Date(java.util.Date) Question(com.yellowmessenger.sdk.models.Question) TextView(android.widget.TextView) Option(com.yellowmessenger.sdk.models.Option) ParseException(java.text.ParseException)

Example 17 with ChatMessage

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

the class ChatActivity method sendOptionEvent.

public void sendOptionEvent(SendOptionEvent sendOptionEvent) {
    optionsLayout.setVisibility(View.GONE);
    optionsLayout.removeAllViews();
    ChatMessage chatMessage = new ChatMessage(username, sendOptionEvent.getOption().getLabel(), name, true);
    addMessage(chatMessage);
    chatMessage.setMessageValue(sendOptionEvent.getOption().getValue());
    EventBus.getDefault().post(new SendMessageEvent(chatMessage));
}
Also used : ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) SendMessageEvent(com.yellowmessenger.sdk.events.SendMessageEvent)

Example 18 with ChatMessage

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

the class ChatActivity method onEvent.

@Subscribe
public void onEvent(AudioCompleteEvent event) {
    try {
        JSONObject jsonObject = new JSONObject(event.getResponse());
        ChatMessage chatMessage = new ChatMessage(username, jsonObject.getJSONArray("transcriptions").getJSONObject(0).getString("utf_text"), name, true);
        EventBus.getDefault().post(new SendMessageEvent(chatMessage));
        addMessage(chatMessage);
        dialog.hide();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(org.json.JSONObject) ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) SendMessageEvent(com.yellowmessenger.sdk.events.SendMessageEvent) JSONException(org.json.JSONException) Subscribe(org.greenrobot.eventbus.Subscribe)

Example 19 with ChatMessage

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

the class ChatActivity method sendMessage.

public void sendMessage(View view) {
    String message = editText.getText().toString();
    if (!message.trim().equals("")) {
        ChatMessage chatMessage = new ChatMessage(username, message, name, true);
        EventBus.getDefault().post(new SendMessageEvent(chatMessage));
        addMessage(chatMessage);
        editText.setText("");
        optionsLayout.removeAllViews();
    }
}
Also used : ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) SendMessageEvent(com.yellowmessenger.sdk.events.SendMessageEvent)

Example 20 with ChatMessage

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

the class RecyclerChatActivity method sendActionEvent.

public void sendActionEvent(SendActionEvent sendActionEvent) {
    ChatMessage chatMessage = new ChatMessage(username, sendActionEvent.getAction().getTitle(), name, true);
    addMessage(chatMessage);
    chatMessage.setMessageValue(sendActionEvent.getAction().getText() != null ? sendActionEvent.getAction().getText() : sendActionEvent.getAction().getTitle());
    EventBus.getDefault().post(new SendMessageEvent(chatMessage));
}
Also used : ChatMessage(com.yellowmessenger.sdk.models.db.ChatMessage) SendMessageEvent(com.yellowmessenger.sdk.events.SendMessageEvent)

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