Search in sources :

Example 21 with ChatResponse

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

the class MessageDatabaseHelper method getThreadHistory.

public Observable<ChatResponse<ResultHistory>> getThreadHistory(@NonNull History history, long threadId) {
    return rx.Observable.create(subscriber -> {
        List<MessageVO> messageVOS = new ArrayList<>();
        List<CacheMessageVO> cacheMessageVOS;
        long fromTime = history.getFromTime();
        long fromTimeNanos = history.getFromTimeNanos();
        long toTime = history.getToTime();
        long toTimeNanos = history.getToTimeNanos();
        long messageId = history.getId();
        long offset = history.getOffset();
        long count = history.getCount();
        int messageType = history.getMessageType();
        String query = history.getQuery();
        String order = history.getOrder();
        offset = offset >= 0 ? offset : 0;
        count = count > 0 ? count : 50;
        if (Util.isNullOrEmpty(order)) {
            order = "desc";
        }
        String rawQuery = "SELECT * FROM CacheMessageVO WHERE threadVoId =" + threadId;
        rawQuery = addMessageIdIfExist(messageId, rawQuery);
        rawQuery = addFromTimeIfExist(fromTime, fromTimeNanos, rawQuery);
        rawQuery = addToTimeIfExist(toTime, toTimeNanos, rawQuery);
        rawQuery = addQueryIfExist(query, rawQuery);
        rawQuery = addMessageTypeIfExist(messageType, rawQuery);
        long contentCount = messageDao.getHistoryContentCount(new SimpleSQLiteQuery(rawQuery.replaceFirst("SELECT \\* ", "SELECT COUNT(ID) ")));
        rawQuery = addOrderAndLimitAndOffset(offset, count, order, rawQuery);
        SupportSQLiteQuery sqLiteQuery = new SimpleSQLiteQuery(rawQuery);
        cacheMessageVOS = messageDao.getRawHistory(sqLiteQuery);
        prepareMessageVOs(messageVOS, cacheMessageVOS);
        List<Sending> sendingList = getAllSendingQueueByThreadId(threadId);
        List<Uploading> uploadingList = getAllUploadingQueueByThreadId(threadId);
        List<Failed> failedList = getAllWaitQueueCacheByThreadId(threadId);
        ChatResponse<ResultHistory> chatResponse = new ChatResponse<>();
        chatResponse.setCache(true);
        ResultHistory resultHistory = new ResultHistory();
        resultHistory.setHistory(messageVOS);
        resultHistory.setNextOffset(history.getOffset() + messageVOS.size());
        resultHistory.setContentCount(contentCount);
        if (messageVOS.size() + history.getOffset() < contentCount) {
            resultHistory.setHasNext(true);
        } else {
            resultHistory.setHasNext(false);
        }
        resultHistory.setHistory(messageVOS);
        resultHistory.setSending(sendingList);
        resultHistory.setUploadingQueue(uploadingList);
        resultHistory.setFailed(failedList);
        chatResponse.setErrorCode(0);
        chatResponse.setHasError(false);
        chatResponse.setErrorMessage("");
        chatResponse.setResult(resultHistory);
        chatResponse.setCache(true);
        chatResponse.setSubjectId(threadId);
        subscriber.onNext(chatResponse);
    });
}
Also used : Sending(com.fanap.podchat.cachemodel.queue.Sending) SupportSQLiteQuery(android.arch.persistence.db.SupportSQLiteQuery) Failed(com.fanap.podchat.cachemodel.queue.Failed) ArrayList(java.util.ArrayList) Uploading(com.fanap.podchat.cachemodel.queue.Uploading) ResultHistory(com.fanap.podchat.model.ResultHistory) SimpleSQLiteQuery(android.arch.persistence.db.SimpleSQLiteQuery) ChatResponse(com.fanap.podchat.model.ChatResponse) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO) PinMessageVO(com.fanap.podchat.mainmodel.PinMessageVO) MessageVO(com.fanap.podchat.mainmodel.MessageVO) GapMessageVO(com.fanap.podchat.cachemodel.GapMessageVO) CacheMessageVO(com.fanap.podchat.cachemodel.CacheMessageVO)

Example 22 with ChatResponse

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

the class MemoryDataSource method createHistoryResponse.

private MessageManager.HistoryResponse createHistoryResponse(List<MessageVO> messageVOS, long threadId) {
    ChatResponse<ResultHistory> response = new ChatResponse<>();
    ResultHistory result = new ResultHistory();
    result.setHistory(messageVOS);
    result.setContentCount(contactContentCount);
    // TODO: 9/14/2020 fix here
    result.setHasNext(messageVOS.size() < contactContentCount);
    result.setNextOffset(messageVOS.size());
    result.setFailed(getThreadFailedMessages(threadId));
    result.setSending(getThreadSendingMessages(threadId));
    result.setUploadingQueue(getThreadUploadingMessage(threadId));
    response.setResult(result);
    return new MessageManager.HistoryResponse(response, MEMORY);
}
Also used : ChatResponse(com.fanap.podchat.model.ChatResponse) ResultHistory(com.fanap.podchat.model.ResultHistory)

Example 23 with ChatResponse

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

the class ChatActivity method downloadFile.

private void downloadFile() {
    String url = "https://core.pod.ir/nzh/image?imageId=222808&hashCode=16c3cd2b93f-0.527719303638482";
    // image
    // long imageId = 404143;
    long imageId = 68349;
    long fileId = 68684;
    // String imageHashCode = "17024463974-0.5588549950419339";
    String imageHashCode = "16feadc7bf1-0.5248624596970302";
    String fileHashCode = "17077360d4b-0.2366487166443898";
    RequestGetImage requestGetImage = new RequestGetImage.Builder(imageId, imageHashCode, true).build();
    RequestGetFile requestGetFile = new RequestGetFile.Builder(fileId, fileHashCode, true).build();
    RequestGetPodSpaceFile rePod = new RequestGetPodSpaceFile.Builder("GAUPE1ZRK76VOXBS").build();
    RequestGetPodSpaceImage rePodImage = new RequestGetPodSpaceImage.Builder("613Q7WCCEXZ1DGY5").setQuality(0.45f).build();
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, REQUEST_WRITE_EXTERNAL_STORAGE);
        return;
    }
    downloadingId = presenter.downloadFile(rePodImage, new ProgressHandler.IDownloadFile() {

        @Override
        public void onProgressUpdate(String uniqueId, long bytesDownloaded, long totalBytesToDownload) {
            Log.e("DOWNLOAD", "IN ACTIVITY: " + "Downloaded: " + bytesDownloaded + " Left: " + totalBytesToDownload);
        }

        @Override
        public void onProgressUpdate(String uniqueId, int progress) {
            Log.e("DOWNLOAD", "IN ACTIVITY: " + "Progress: " + progress);
        }

        @Override
        public void onError(String uniqueId, String error, String url) {
            Log.e("DOWNLOAD", "IN ACTIVITY: ERROR :(((");
        }

        @Override
        public void onLowFreeSpace(String uniqueId, String url) {
            Log.e("DOWNLOAD", "Low Space...");
        }

        @Override
        public void onFileReady(ChatResponse<ResultDownloadFile> response) {
            Log.e("DOWNLOAD", "IN ACTIVITY: Finish File!!!!");
            Log.e("DOWNLOAD", "File name: " + response.getResult().getFile().getName());
            Log.e("DOWNLOAD", "Uri " + response.getResult().getUri());
            Log.e("DOWNLOAD", "File Exist " + response.getResult().getFile().exists());
            if (response.getResult().getFile().exists()) {
                try {
                    Bitmap v = BitmapFactory.decodeFile(response.getResult().getFile().getAbsolutePath());
                    runOnUiThread(() -> {
                    // imageMap.setImageBitmap(v)
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("DOWNLOAD", "Not Image");
                }
            }
        }
    });
// downloadingId = presenter.downloadFile(requestGetImage, new ProgressHandler.IDownloadFile() {
// 
// 
// @Override
// public void onProgressUpdate(String uniqueId, int bytesDownloaded, int totalBytesToDownload) {
// Log.e("DOWNLOAD", "IN ACTIVITY: " + "Downloaded: " + bytesDownloaded + " Left: " + totalBytesToDownload);
// 
// }
// 
// @Override
// public void onProgressUpdate(String uniqueId, int progress) {
// 
// }
// 
// @Override
// public void onError(String uniqueId, String error, String url) {
// Log.e("DOWNLOAD", "IN ACTIVITY: ERROR :(((");
// 
// }
// 
// @Override
// public void onLowFreeSpace(String uniqueId, String url) {
// Log.e("DOWNLOAD", "Low Space...");
// 
// }
// 
// @Override
// public void onFileReady(ChatResponse<ResultDownloadFile> response) {
// Log.e("DOWNLOAD", "IN ACTIVITY: Finish File!!!!");
// Log.e("DOWNLOAD", "File name: " + response.getResult().getFile().getName());
// Log.e("DOWNLOAD", "Uri " + response.getResult().getUri());
// Log.e("DOWNLOAD", "File Exist " + response.getResult().getFile().exists());
// 
// 
// if (response.getResult().getFile().exists()) {
// 
// 
// try {
// Bitmap v = BitmapFactory.decodeFile(response.getResult().getFile().getAbsolutePath());
// runOnUiThread(() -> imageMap.setImageBitmap(v));
// } catch (Exception e) {
// e.printStackTrace();
// }
// 
// }
// 
// 
// }
// 
// });
}
Also used : RequestGetFile(com.fanap.podchat.requestobject.RequestGetFile) GsonBuilder(com.google.gson.GsonBuilder) RequestGetImage(com.fanap.podchat.requestobject.RequestGetImage) RequestGetPodSpaceImage(com.fanap.podchat.requestobject.RequestGetPodSpaceImage) Bitmap(android.graphics.Bitmap) ChatResponse(com.fanap.podchat.model.ChatResponse) RequestGetPodSpaceFile(com.fanap.podchat.requestobject.RequestGetPodSpaceFile)

Example 24 with ChatResponse

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

the class ChatActivity method sendFileMessage.

private void sendFileMessage(String[] fileUnique) {
    RequestFileMessage request = new RequestFileMessage.Builder(ChatActivity.this, TEST_THREAD_ID, getUri(), // constructor
    TextMessageType.Constants.POD_SPACE_PICTURE).description("test file message").setUserGroupHash(TEST_THREAD_HASH).build();
    fileUnique[0] = presenter.sendFileMessage(request, new ProgressHandler.sendFileMessage() {

        @Override
        public void onProgressUpdate(String uniqueId, int progress, int totalBytesSent, int totalBytesToSend) {
            Log.e("SFM", "Sending files message: " + progress + " * " + totalBytesSent + " * " + totalBytesToSend);
        }

        @Override
        public void onFinishImage(String json, ChatResponse<ResultImageFile> chatResponse) {
            Log.e("SFM", "onFinishImage");
        }

        @Override
        public void onFinishFile(String json, ChatResponse<ResultFile> chatResponse) {
            Log.e("SFM", "onFinishFile");
        }

        @Override
        public void onError(String jsonError, ErrorOutPut error) {
            Log.e("SFM", "onError");
        }
    });
// fileUnique[0] = presenter.sendFileMessage(
// ChatActivity.this,
// ChatActivity.this,
// "test file message",
// TEST_THREAD_ID,
// getUri(),
// getMetaData(),
// TextMessageType.Constants.PICTURE,
// new ProgressHandler.sendFileMessage() {
// @Override
// public void onProgressUpdate(String uniqueId, int progress, int totalBytesSent, int totalBytesToSend) {
// 
// Log.e("SFM", "Sending files message: " + progress + " * " + totalBytesSent + " * " + totalBytesToSend);
// }
// 
// @Override
// public void onFinishImage(String json, ChatResponse<ResultImageFile> chatResponse) {
// 
// Log.e("SFM", "onFinishImage");
// 
// }
// 
// @Override
// public void onFinishFile(String json, ChatResponse<ResultFile> chatResponse) {
// 
// Log.e("SFM", "onFinishFile");
// 
// }
// 
// @Override
// public void onError(String jsonError, ErrorOutPut error) {
// 
// Log.e("SFM", "onError");
// 
// 
// }
// });
}
Also used : ChatResponse(com.fanap.podchat.model.ChatResponse) ErrorOutPut(com.fanap.podchat.model.ErrorOutPut) RequestFileMessage(com.fanap.podchat.requestobject.RequestFileMessage)

Example 25 with ChatResponse

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

the class MessagesCacheTest method sendReplyMessageAndCheckCache.

@Test
public void sendReplyMessageAndCheckCache() {
    populateThreadsListFromServerOnly();
    populateMessagesFromServer();
    assert threads.size() > 0;
    assert threadMessagesList.size() > 0;
    Thread thread = threads.get(0);
    MessageVO message = threadMessagesList.get(0);
    ChatListener mTestListener = Mockito.mock(ChatListener.class);
    chat.setListener(mTestListener);
    String replyTXT = "Reply to " + message.getMessage();
    RequestReplyMessage repRequest = new RequestReplyMessage.Builder(replyTXT, thread.getId(), message.getId(), TextMessageType.Constants.TEXT).build();
    chat.replyMessage(repRequest, null);
    Mockito.verify(mTestListener, Mockito.after(2000).atLeastOnce()).onNewMessage(Mockito.any(), Mockito.any());
    RequestGetHistory requestGetHistory = new RequestGetHistory.Builder(thread.getId()).build();
    chat.getHistory(requestGetHistory, null);
    Mockito.verify(mTestListener, Mockito.after(2000).atLeastOnce()).onGetHistory(Mockito.any(), Mockito.argThat((ChatResponse<ResultHistory> response) -> response.isCache() && response.getResult().getHistory().stream().anyMatch(messageVO -> messageVO.getMessage().equals(replyTXT))));
}
Also used : ResultThreads(com.fanap.podchat.model.ResultThreads) Context(android.content.Context) ResultNewMessage(com.fanap.podchat.model.ResultNewMessage) RequestConnect(com.fanap.podchat.requestobject.RequestConnect) BeforeClass(org.junit.BeforeClass) RequestForwardMessage(com.fanap.podchat.requestobject.RequestForwardMessage) Mock(org.mockito.Mock) Date(java.util.Date) Thread(com.fanap.podchat.mainmodel.Thread) RunWith(org.junit.runner.RunWith) ChatPresenter(com.example.chat.application.chatexample.ChatPresenter) ChatContract(com.example.chat.application.chatexample.ChatContract) ChatResponse(com.fanap.podchat.model.ChatResponse) ArrayList(java.util.ArrayList) R(com.fanap.podchat.example.R) ResultHistory(com.fanap.podchat.model.ResultHistory) Looper(android.os.Looper) BaseApplication(com.example.chat.application.chatexample.BaseApplication) ChatActivity(com.example.chat.application.chatexample.ChatActivity) RequestThread(com.fanap.podchat.requestobject.RequestThread) RequestMessage(com.fanap.podchat.requestobject.RequestMessage) Before(org.junit.Before) RequestGetHistory(com.fanap.podchat.requestobject.RequestGetHistory) CHAT_READY(com.fanap.podchat.util.ChatStateType.ChatSateConstant.CHAT_READY) ActivityTestRule(android.support.test.rule.ActivityTestRule) Test(org.junit.Test) InstrumentationRegistry(android.support.test.InstrumentationRegistry) AndroidJUnit4(android.support.test.runner.AndroidJUnit4) ChatListener(com.fanap.podchat.chat.ChatListener) MessageVO(com.fanap.podchat.mainmodel.MessageVO) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) APP_ID(com.example.chat.application.chatexample.ChatActivity.APP_ID) Rule(org.junit.Rule) TextMessageType(com.fanap.podchat.util.TextMessageType) Assert(org.junit.Assert) Activity(android.app.Activity) Chat(com.fanap.podchat.chat.Chat) Collections(java.util.Collections) RequestReplyMessage(com.fanap.podchat.requestobject.RequestReplyMessage) RequestGetHistory(com.fanap.podchat.requestobject.RequestGetHistory) RequestReplyMessage(com.fanap.podchat.requestobject.RequestReplyMessage) ChatListener(com.fanap.podchat.chat.ChatListener) MessageVO(com.fanap.podchat.mainmodel.MessageVO) ResultHistory(com.fanap.podchat.model.ResultHistory) Thread(com.fanap.podchat.mainmodel.Thread) RequestThread(com.fanap.podchat.requestobject.RequestThread) Test(org.junit.Test)

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