Search in sources :

Example 1 with ChatMessageSendResponse

use of br.ufrj.caronae.models.ChatMessageSendResponse in project caronae-android by caronae.

the class ChatAct method sendBt.

@OnClick(R.id.send_bt)
public void sendBt() {
    final String message = msg_et.getText().toString();
    msg_et.setText("");
    if (message.isEmpty())
        return;
    String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US).format(new Date());
    final ChatMessageReceived msg = new ChatMessageReceived(App.getUser().getName(), App.getUser().getDbId() + "", message, rideId, time);
    msg.setId((long) -1);
    chatMsgsList.add(msg);
    final ChatMsgsAdapter adapter = (ChatMsgsAdapter) chatMsgs_rv.getAdapter();
    adapter.notifyItemInserted(chatMsgsList.size() - 1);
    chatMsgs_rv.scrollToPosition(chatMsgsList.size() - 1);
    CaronaeAPI.service(getApplicationContext()).sendChatMsg(rideId, new ChatSendMessageForJson(message)).enqueue(new Callback<ChatMessageSendResponse>() {

        @Override
        public void onResponse(Call<ChatMessageSendResponse> call, Response<ChatMessageSendResponse> response) {
            if (response.isSuccessful()) {
                ChatMessageSendResponse chatMessageSendResponse = response.body();
                Log.i("Message Sent", "Sulcefully Send Chat Messages");
                msg.setId(Long.parseLong(chatMessageSendResponse.getMessageId()));
                chatMsgsList.get(getMessagePositionWithId(chatMsgsList, msg.getId())).setId(Long.parseLong(chatMessageSendResponse.getMessageId()));
                msg.save();
            } else {
                Util.treatResponseFromServer(response);
                Util.toast("Erro ao enviar mensagem de chat, verifique sua conexao");
                chatMsgsList.remove(chatMsgsList.size() - 1);
                chatMsgsAdapter.notifyItemRemoved(chatMsgsList.size());
                msg_et.setText(msg.getMessage());
                /**
                 *********** Esconde o teclado **********
                 */
                View view = ChatAct.this.getCurrentFocus();
                if (view != null) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
                /**
                 ******
                 */
                Log.e("SendMessages", response.message());
            }
        }

        @Override
        public void onFailure(Call<ChatMessageSendResponse> call, Throwable t) {
            Util.toast("Erro ao enviar mensagem de chat, verifique sua conexao");
            if (chatMsgsList.size() > 0) {
                chatMsgsList.remove(chatMsgsList.size() - 1);
                chatMsgsAdapter.notifyItemRemoved(chatMsgsList.size());
                msg_et.setText(msg.getMessage());
            }
            /**
             *********** Esconde o teclado **********
             */
            View view = ChatAct.this.getCurrentFocus();
            if (view != null) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
            /**
             ******
             */
            Log.e("SendMessages", t.getMessage());
        }
    });
}
Also used : ChatMsgsAdapter(br.ufrj.caronae.adapters.ChatMsgsAdapter) ChatSendMessageForJson(br.ufrj.caronae.models.modelsforjson.ChatSendMessageForJson) ChatMessageSendResponse(br.ufrj.caronae.models.ChatMessageSendResponse) ChatMessageReceived(br.ufrj.caronae.models.ChatMessageReceived) InputMethodManager(android.view.inputmethod.InputMethodManager) BindView(butterknife.BindView) View(android.view.View) CardView(android.support.v7.widget.CardView) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) OnClick(butterknife.OnClick)

Aggregations

CardView (android.support.v7.widget.CardView)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 InputMethodManager (android.view.inputmethod.InputMethodManager)1 TextView (android.widget.TextView)1 ChatMsgsAdapter (br.ufrj.caronae.adapters.ChatMsgsAdapter)1 ChatMessageReceived (br.ufrj.caronae.models.ChatMessageReceived)1 ChatMessageSendResponse (br.ufrj.caronae.models.ChatMessageSendResponse)1 ChatSendMessageForJson (br.ufrj.caronae.models.modelsforjson.ChatSendMessageForJson)1 BindView (butterknife.BindView)1 OnClick (butterknife.OnClick)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1