use of br.ufrj.caronae.models.ModelReceivedFromChat in project caronae-android by caronae.
the class FetchReceivedMessagesService method onHandleIntent.
@Override
protected void onHandleIntent(Intent intent) {
rideId = intent.getStringExtra("rideId");
String since = intent.getStringExtra("since");
CaronaeAPI.service(getApplicationContext()).requestChatMsgs(rideId, since).enqueue(new Callback<ModelReceivedFromChat>() {
@Override
public void onResponse(Call<ModelReceivedFromChat> call, Response<ModelReceivedFromChat> response) {
if (response.isSuccessful()) {
ModelReceivedFromChat chatMessagesReceived = response.body();
if (chatMessagesReceived != null && chatMessagesReceived.getMessages().size() != 0) {
final List<ChatMessageReceived> messagesFetched = new ArrayList<>();
List<ChatMessageReceivedFromJson> listMessages = chatMessagesReceived.getMessages();
for (int mensagesNum = 0; mensagesNum < listMessages.size(); mensagesNum++) {
ChatMessageReceived cmr = new ChatMessageReceived(listMessages.get(mensagesNum).getUser().getName(), String.valueOf(listMessages.get(mensagesNum).getUser().getId()), listMessages.get(mensagesNum).getMessage(), rideId, listMessages.get(mensagesNum).getTime());
cmr.setId(Long.parseLong(listMessages.get(mensagesNum).getMessageId()));
messagesFetched.add(cmr);
}
new Thread(new Runnable() {
@Override
public void run() {
for (int messageIndex = 0; messageIndex < messagesFetched.size(); messageIndex++) {
if (!messageAlrealdyExist(messagesFetched.get(messageIndex).getId())) {
messagesFetched.get(messageIndex).save();
}
}
if (SharedPref.getChatActIsForeground()) {
App.getBus().post(messagesFetched.get(0));
}
}
}).start();
} else {
ChatMessageReceived cmr = new ChatMessageReceived();
if (SharedPref.getChatActIsForeground()) {
App.getBus().post(cmr);
}
getApplicationContext().sendBroadcast(new Intent("br.ufrj.caronae.acts.ChatAct.BROADCAST_NEW_MESSAGES_NULL"));
}
} else {
Util.treatResponseFromServer(response);
Util.toast("Erro ao Recuperar mensagem de chat");
Log.e("GetMessages", response.message());
}
}
@Override
public void onFailure(Call<ModelReceivedFromChat> call, Throwable t) {
Util.toast("Erro ao Recuperar mensagem de chat");
Log.e("GetMessages", t.getMessage());
}
});
}
Aggregations