Search in sources :

Example 61 with ChatResponse

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

the class ChatCore method syncContact.

/**
 * First we get the contact from server then at the respond of that
 *
 * @param activity its for check the permission of reading the phone contact
 *                 {@link #getPhoneContact(Context, String, OnContactLoaded)}
 */
public String syncContact(Activity activity) {
    showLog(">>> Start Syncing... " + new Date());
    String uniqueId = generateUniqueId();
    if (Permission.Check_READ_CONTACTS(activity)) {
        if (chatReady) {
            getPhoneContact(getContext(), uniqueId, phoneContacts -> {
                if (phoneContacts.size() > 0) {
                    if (sentryResponseLog) {
                        showLog(">>> Synchronizing " + phoneContacts.size() + " with server at " + new Date());
                    } else {
                        showLog(">>> Synchronizing");
                    }
                    handleAddContacts(uniqueId, phoneContacts);
                } else {
                    showLog(">>> No New Contact Found. Everything is synced ");
                    ChatResponse<Contacts> chatResponse = new ChatResponse<>();
                    chatResponse.setUniqueId(uniqueId);
                    Contacts contacts = new Contacts();
                    contacts.setCount(0);
                    contacts.setResult(new ArrayList<>());
                    chatResponse.setResult(contacts);
                    listenerManager.callOnSyncContact(gson.toJson(chatResponse), chatResponse);
                    if (log)
                        showLog("SYNC_CONTACT_COMPLETED");
                }
            });
        } else {
            onChatNotReady(uniqueId);
        }
    } else {
        String jsonError = captureError(ChatConstant.ERROR_READ_CONTACT_PERMISSION, ChatConstant.ERROR_CODE_READ_CONTACT_PERMISSION, uniqueId);
        Permission.Request_READ_CONTACTS(activity, READ_CONTACTS_CODE);
        if (log)
            Log.e(TAG, jsonError);
    }
    return uniqueId;
}
Also used : Contacts(com.fanap.podchat.model.Contacts) ChatResponse(com.fanap.podchat.model.ChatResponse) Date(java.util.Date)

Example 62 with ChatResponse

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

the class ChatCore method updateContact.

/**
 * Update contacts
 * all of the params all required to update
 */
public String updateContact(long userId, String firstName, String lastName, String cellphoneNumber, String email) {
    String uniqueId = generateUniqueId();
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("uniqueId", uniqueId);
    jsonObject.addProperty("id", userId);
    jsonObject.addProperty("tokenIssuer", 1);
    jsonObject.addProperty("firstName", firstName);
    jsonObject.addProperty("lastName", lastName);
    jsonObject.addProperty("cellphoneNumber", cellphoneNumber);
    jsonObject.addProperty("email", email);
    showLog("SEND_UPDATE_CONTACT", getJsonForLog(jsonObject));
    if (Util.isNullOrEmpty(firstName)) {
        firstName = "";
    }
    if (Util.isNullOrEmpty(lastName)) {
        lastName = "";
    }
    if (Util.isNullOrEmpty(cellphoneNumber)) {
        cellphoneNumber = "";
    }
    if (Util.isNullOrEmpty(email)) {
        email = "";
    }
    if (chatReady) {
        Observable<Response<UpdateContact>> updateContactObservable;
        if (Util.isNullOrEmpty(getTypeCode())) {
            updateContactObservable = contactApi.updateContact(getToken(), 1, userId, firstName, lastName, email, uniqueId, cellphoneNumber);
        } else {
            updateContactObservable = contactApi.updateContact(getToken(), 1, userId, firstName, lastName, email, uniqueId, cellphoneNumber, getTypeCode());
        }
        updateContactObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(response -> {
            UpdateContact updateContact = response.body();
            if (updateContact != null && response.isSuccessful()) {
                if (response.body() != null) {
                    if (!response.body().getHasError()) {
                        ChatResponse<ResultUpdateContact> chatResponse = new ChatResponse<>();
                        chatResponse.setUniqueId(uniqueId);
                        ResultUpdateContact resultUpdateContact = new ResultUpdateContact();
                        if (!Util.isNullOrEmpty(updateContact.getCount())) {
                            resultUpdateContact.setContentCount(updateContact.getCount());
                        }
                        resultUpdateContact.setContacts(updateContact.getResult());
                        chatResponse.setResult(resultUpdateContact);
                        String json = gson.toJson(chatResponse);
                        listenerManager.callOnUpdateContact(json, chatResponse);
                        if (cache) {
                            // messageDatabaseHelper.saveContact(updateContact.getResult().get(0), getExpireAmount());
                            dataSource.saveContactsResultFromServer(updateContact.getResult());
                        }
                        showLog("RECEIVE_UPDATE_CONTACT", json);
                    } else {
                        String errorMsg = response.body().getMessage();
                        int errorCodeMsg = response.body().getErrorCode();
                        errorMsg = errorMsg != null ? errorMsg : "";
                        captureError(errorMsg, errorCodeMsg, uniqueId);
                    }
                }
            }
        }, (Throwable throwable) -> {
            if (throwable != null) {
                captureError(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId, throwable);
            }
        });
    } else {
        captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
    }
    return uniqueId;
}
Also used : ChatResponse(com.fanap.podchat.model.ChatResponse) Response(retrofit2.Response) ChatResponse(com.fanap.podchat.model.ChatResponse) JsonObject(com.google.gson.JsonObject) UpdateContact(com.fanap.podchat.mainmodel.UpdateContact) RequestUpdateContact(com.fanap.podchat.requestobject.RequestUpdateContact) ResultUpdateContact(com.fanap.podchat.model.ResultUpdateContact) ResultUpdateContact(com.fanap.podchat.model.ResultUpdateContact)

Example 63 with ChatResponse

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

the class ChatCore method onThreadLastMessageUpdated.

private void onThreadLastMessageUpdated(Thread thread, String uniqueId) {
    ResultThread resultThread = new ResultThread();
    resultThread.setThread(thread);
    ChatResponse<ResultThread> chatResponse = new ChatResponse<>();
    chatResponse.setResult(resultThread);
    chatResponse.setUniqueId(uniqueId);
    chatResponse.setSubjectId(thread.getId());
    if (sentryResponseLog) {
        showLog("THREAD_INFO_UPDATED", chatResponse.getJson());
    } else {
        showLog("THREAD_INFO_UPDATED");
    }
    if (cache) {
        dataSource.saveThreadResultFromCache(thread);
    }
    listenerManager.callOnThreadInfoUpdated(chatResponse.getJson(), chatResponse);
}
Also used : ChatResponse(com.fanap.podchat.model.ChatResponse) ResultThread(com.fanap.podchat.model.ResultThread)

Example 64 with ChatResponse

use of com.fanap.podchat.model.ChatResponse 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 65 with ChatResponse

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

the class ChatCore method handleOnGetUserInfo.

/**
 * Its check the Failed Queue {@link #checkMessageQueue()} to send all the message that is waiting to be send.
 */
private void handleOnGetUserInfo(ChatMessage chatMessage, String messageUniqueId, Callback callback) {
    if (callback.isResult()) {
        // if there is a key its ok if not it will go for the key and then chat ready
        ChatResponse<ResultUserInfo> chatResponse = new ChatResponse<>();
        UserInfo userInfo = gson.fromJson(chatMessage.getContent(), UserInfo.class);
        setUserId(userInfo.getId());
        setChatReady("CHAT_READY", true);
        userInfoResponse = true;
        // add user info for sentry
        setSentryUser(userInfo);
        String userInfoJson = reformatUserInfo(chatMessage, chatResponse, userInfo);
        if (sentryResponseLog) {
            showLog("RECEIVE_USER_INFO", userInfoJson);
        } else {
            showLog("RECEIVE_USER_INFO");
        }
        pingWithDelay();
        messageCallbacks.remove(messageUniqueId);
        listenerManager.callOnUserInfo(userInfoJson, chatResponse);
    }
}
Also used : ResultUserInfo(com.fanap.podchat.model.ResultUserInfo) ChatResponse(com.fanap.podchat.model.ChatResponse) ResultUserInfo(com.fanap.podchat.model.ResultUserInfo) UserInfo(com.fanap.podchat.mainmodel.UserInfo)

Aggregations

ChatResponse (com.fanap.podchat.model.ChatResponse)162 ChatListener (com.fanap.podchat.chat.ChatListener)38 Test (org.junit.Test)38 ArrayList (java.util.ArrayList)35 Thread (com.fanap.podchat.mainmodel.Thread)29 JsonSyntaxException (com.google.gson.JsonSyntaxException)29 ResultHistory (com.fanap.podchat.model.ResultHistory)28 LargeTest (android.support.test.filters.LargeTest)27 RequestThread (com.fanap.podchat.requestobject.RequestThread)27 PodChatException (com.fanap.podchat.util.PodChatException)26 MessageVO (com.fanap.podchat.mainmodel.MessageVO)25 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)23 IOException (java.io.IOException)23 JSONException (org.json.JSONException)22 ResultThreads (com.fanap.podchat.model.ResultThreads)21 RequestGetHistory (com.fanap.podchat.requestobject.RequestGetHistory)21 SentryException (io.sentry.core.protocol.SentryException)21 FlakyTest (android.support.test.filters.FlakyTest)19 MediumTest (android.support.test.filters.MediumTest)19 Activity (android.app.Activity)17