Search in sources :

Example 1 with ResultMessage

use of com.fanap.podchat.model.ResultMessage in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method handleSeen.

private void handleSeen(ChatMessage chatMessage, String messageUniqueId, long threadId) {
    if (threadCallbacks.containsKey(threadId)) {
        ArrayList<Callback> callbacks = threadCallbacks.get(threadId);
        if (callbacks != null) {
            for (Callback callback : callbacks) {
                if (messageUniqueId.equals(callback.getUniqueId())) {
                    int indexUnique = callbacks.indexOf(callback);
                    while (indexUnique > -1) {
                        if (callbacks.get(indexUnique).isSeen()) {
                            ResultMessage resultMessage = gson.fromJson(chatMessage.getContent(), ResultMessage.class);
                            if (callbacks.get(indexUnique).isDelivery()) {
                                ChatResponse<ResultMessage> chatResponse = new ChatResponse<>();
                                chatResponse.setErrorMessage("");
                                chatResponse.setErrorCode(0);
                                chatResponse.setHasError(false);
                                chatResponse.setUniqueId(callback.getUniqueId());
                                chatResponse.setResult(resultMessage);
                                String json = gson.toJson(chatResponse);
                                listenerManager.callOnDeliveryMessage(json, chatResponse);
                                Callback callbackUpdateSent = new Callback();
                                callbackUpdateSent.setSent(callback.isSent());
                                callbackUpdateSent.setDelivery(false);
                                callbackUpdateSent.setSeen(callback.isSeen());
                                callbackUpdateSent.setUniqueId(callback.getUniqueId());
                                callbacks.set(indexUnique, callbackUpdateSent);
                                threadCallbacks.put(threadId, callbacks);
                                if (sentryResponseLog) {
                                    showLog("RECEIVED_DELIVERED_MESSAGE", json);
                                } else {
                                    showLog("RECEIVED_DELIVERED_MESSAGE");
                                }
                            }
                            ChatResponse<ResultMessage> chatResponse = new ChatResponse<>();
                            chatResponse.setErrorMessage("");
                            chatResponse.setErrorCode(0);
                            chatResponse.setHasError(false);
                            chatResponse.setUniqueId(callback.getUniqueId());
                            chatResponse.setResult(resultMessage);
                            String json = gson.toJson(chatResponse);
                            listenerManager.callOnSeenMessage(json, chatResponse);
                            callbacks.remove(indexUnique);
                            threadCallbacks.put(threadId, callbacks);
                            if (sentryResponseLog) {
                                showLog("RECEIVED_SEEN_MESSAGE", json);
                            } else {
                                showLog("RECEIVED_SEEN_MESSAGE");
                            }
                        }
                        indexUnique--;
                    }
                    break;
                }
            }
        }
    }
}
Also used : Callback(com.fanap.podchat.util.Callback) ChatResponse(com.fanap.podchat.model.ChatResponse) ResultMessage(com.fanap.podchat.model.ResultMessage)

Example 2 with ResultMessage

use of com.fanap.podchat.model.ResultMessage in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method handleDelivery.

private void handleDelivery(ChatMessage chatMessage, String messageUniqueId, long threadId) {
    try {
        if (threadCallbacks.containsKey(threadId)) {
            ArrayList<Callback> callbacks = threadCallbacks.get(threadId);
            if (callbacks != null) {
                for (Callback callback : callbacks) {
                    if (messageUniqueId.equals(callback.getUniqueId())) {
                        int indexUnique = callbacks.indexOf(callback);
                        while (indexUnique > -1) {
                            if (callbacks.get(indexUnique).isDelivery()) {
                                ChatResponse<ResultMessage> chatResponse = new ChatResponse<>();
                                ResultMessage resultMessage = gson.fromJson(chatMessage.getContent(), ResultMessage.class);
                                chatResponse.setErrorMessage("");
                                chatResponse.setErrorCode(0);
                                chatResponse.setHasError(false);
                                chatResponse.setUniqueId(callback.getUniqueId());
                                chatResponse.setResult(resultMessage);
                                String json = gson.toJson(chatResponse);
                                listenerManager.callOnDeliveryMessage(json, chatResponse);
                                Callback callbackUpdateSent = new Callback();
                                callbackUpdateSent.setSent(callback.isSent());
                                callbackUpdateSent.setDelivery(false);
                                callbackUpdateSent.setSeen(callback.isSeen());
                                callbackUpdateSent.setUniqueId(callback.getUniqueId());
                                callbacks.set(indexUnique, callbackUpdateSent);
                                threadCallbacks.put(threadId, callbacks);
                                if (sentryResponseLog) {
                                    showLog("RECEIVED_DELIVERED_MESSAGE", json);
                                } else {
                                    showLog("RECEIVED_DELIVERED_MESSAGE");
                                }
                            }
                            indexUnique--;
                        }
                        break;
                    }
                }
            }
        }
    } catch (Exception e) {
        showErrorLog(e.getMessage());
        onUnknownException(chatMessage.getUniqueId(), e);
    }
}
Also used : Callback(com.fanap.podchat.util.Callback) ChatResponse(com.fanap.podchat.model.ChatResponse) ResultMessage(com.fanap.podchat.model.ResultMessage) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException)

Example 3 with ResultMessage

use of com.fanap.podchat.model.ResultMessage in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method handleSent.

private void handleSent(ChatMessage chatMessage, String messageUniqueId, long threadId) {
    if (cache) {
        dataSource.deleteWaitQueueWithUniqueId(messageUniqueId);
    } else {
        waitQList.remove(messageUniqueId);
    }
    boolean found = false;
    try {
        if (threadCallbacks.containsKey(threadId)) {
            ArrayList<Callback> callbacks = threadCallbacks.get(threadId);
            if (callbacks != null) {
                for (Callback callback : callbacks) {
                    if (messageUniqueId.equals(callback.getUniqueId())) {
                        if (callback.isSent()) {
                            found = true;
                            ChatResponse<ResultMessage> chatResponse = new ChatResponse<>();
                            ResultMessage resultMessage = new ResultMessage();
                            chatResponse.setErrorCode(0);
                            chatResponse.setErrorMessage("");
                            chatResponse.setHasError(false);
                            chatResponse.setUniqueId(callback.getUniqueId());
                            chatResponse.setSubjectId(chatMessage.getSubjectId());
                            resultMessage.setConversationId(chatMessage.getSubjectId());
                            try {
                                resultMessage.setMessageId(Long.parseLong(chatMessage.getContent()));
                            } catch (NumberFormatException e) {
                                captureError(new PodChatException(e.getMessage(), messageUniqueId, getToken()));
                                resultMessage.setMessageId(0);
                            }
                            chatResponse.setResult(resultMessage);
                            String json = gson.toJson(chatResponse);
                            listenerManager.callOnSentMessage(json, chatResponse);
                            runOnUIThread(() -> {
                                if (handlerSend.get(callback.getUniqueId()) != null) {
                                    ChatHandler handler = handlerSend.get(callback.getUniqueId());
                                    if (handler != null) {
                                        handler.onSentResult(chatMessage.getContent());
                                    }
                                }
                            });
                            Callback callbackUpdateSent = new Callback();
                            callbackUpdateSent.setSent(false);
                            callbackUpdateSent.setDelivery(callback.isDelivery());
                            callbackUpdateSent.setSeen(callback.isSeen());
                            callbackUpdateSent.setUniqueId(callback.getUniqueId());
                            callbacks.set(callbacks.indexOf(callback), callbackUpdateSent);
                            threadCallbacks.put(threadId, callbacks);
                            if (sentryResponseLog) {
                                showLog("RECEIVED_SENT_MESSAGE", json);
                            } else {
                                showLog("RECEIVED_SENT_MESSAGE");
                            }
                        }
                        break;
                    }
                }
            }
        }
        if (!found) {
            ChatResponse<ResultMessage> chatResponse = new ChatResponse<>();
            ResultMessage resultMessage = new ResultMessage();
            chatResponse.setErrorCode(0);
            chatResponse.setErrorMessage("");
            chatResponse.setHasError(false);
            chatResponse.setUniqueId(messageUniqueId);
            chatResponse.setSubjectId(chatMessage.getSubjectId());
            resultMessage.setConversationId(chatMessage.getSubjectId());
            resultMessage.setMessageId(Long.parseLong(chatMessage.getContent()));
            chatResponse.setResult(resultMessage);
            String json = gson.toJson(chatResponse);
            listenerManager.callOnSentMessage(json, chatResponse);
            // Log.d("MTAG", "threadid isn't in callbacks!");
            runOnUIThread(() -> {
                if (handlerSend.get(messageUniqueId) != null) {
                    handlerSend.get(messageUniqueId).onSentResult(chatMessage.getContent());
                }
            });
            Callback callbackUpdateSent = new Callback();
            callbackUpdateSent.setSent(false);
            callbackUpdateSent.setUniqueId(messageUniqueId);
            if (sentryResponseLog) {
                showLog("RECEIVED_SENT_MESSAGE", json);
            } else {
                showLog("RECEIVED_SENT_MESSAGE");
            }
        }
    } catch (Throwable e) {
        showErrorLog(e.getMessage());
        onUnknownException(chatMessage.getUniqueId(), e);
    }
}
Also used : Callback(com.fanap.podchat.util.Callback) PodChatException(com.fanap.podchat.util.PodChatException) ChatResponse(com.fanap.podchat.model.ChatResponse) ResultMessage(com.fanap.podchat.model.ResultMessage)

Aggregations

ChatResponse (com.fanap.podchat.model.ChatResponse)3 ResultMessage (com.fanap.podchat.model.ResultMessage)3 Callback (com.fanap.podchat.util.Callback)3 PodChatException (com.fanap.podchat.util.PodChatException)2 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 SentryException (io.sentry.core.protocol.SentryException)1 IOException (java.io.IOException)1 JSONException (org.json.JSONException)1