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());
}
});
}
Aggregations