Search in sources :

Example 1 with ChatMsgsAdapter

use of br.ufrj.caronae.adapters.ChatMsgsAdapter 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)

Example 2 with ChatMsgsAdapter

use of br.ufrj.caronae.adapters.ChatMsgsAdapter in project caronae-android by caronae.

the class ChatAct method configureActivityWithChatAssets.

private void configureActivityWithChatAssets(ChatAssets chatAssets) {
    context = this;
    color = chatAssets.getColor();
    int colorPressed = Util.getPressedColorbyNormalColor(color);
    lay1.setBackgroundColor(color);
    toolbar.setBackgroundColor(color);
    int bgRes = chatAssets.getBgRes();
    // send_bt.setBackgroundResource(bgRes);
    send_bt.setColorNormal(color);
    send_bt.setColorPressed(colorPressed);
    String neighborhood = chatAssets.getLocation();
    // neighborhood_tv.setText(neighborhood);
    String date = chatAssets.getDate();
    // date_tv.setText(date);
    String time = chatAssets.getTime();
    // time_tv.setText(time);
    headerText.setText(neighborhood + " - " + date + " - " + time);
    chatMsgsList = ChatMessageReceived.find(ChatMessageReceived.class, "ride_id = ?", rideId);
    Collections.sort(chatMsgsList, new ChatMsgComparator());
    chatMsgsAdapter = new ChatMsgsAdapter(chatMsgsList, color);
    chatMsgs_rv.setAdapter(chatMsgsAdapter);
    chatMsgs_rv.setLayoutManager(new LinearLayoutManager(context));
    chatMsgsAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {

        @Override
        public void onChanged() {
            super.onChanged();
            Util.toast("mudou");
        }
    });
    if (!chatMsgsList.isEmpty())
        chatMsgs_rv.scrollToPosition(chatMsgsList.size() - 1);
}
Also used : ChatMsgComparator(br.ufrj.caronae.comparators.ChatMsgComparator) ChatMsgsAdapter(br.ufrj.caronae.adapters.ChatMsgsAdapter) ChatMessageReceived(br.ufrj.caronae.models.ChatMessageReceived) RecyclerView(android.support.v7.widget.RecyclerView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager)

Aggregations

RecyclerView (android.support.v7.widget.RecyclerView)2 ChatMsgsAdapter (br.ufrj.caronae.adapters.ChatMsgsAdapter)2 ChatMessageReceived (br.ufrj.caronae.models.ChatMessageReceived)2 CardView (android.support.v7.widget.CardView)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 View (android.view.View)1 InputMethodManager (android.view.inputmethod.InputMethodManager)1 TextView (android.widget.TextView)1 ChatMsgComparator (br.ufrj.caronae.comparators.ChatMsgComparator)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