use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method handleRemoveFromThread.
private void handleRemoveFromThread(ChatMessage chatMessage) {
ChatResponse<ResultThread> chatResponse = new ChatResponse<>();
ResultThread resultThread = new ResultThread();
Thread thread = new Thread();
thread.setId(chatMessage.getSubjectId());
resultThread.setThread(thread);
String content = gson.toJson(chatResponse);
if (cache) {
messageDatabaseHelper.deleteThread(chatMessage.getSubjectId());
}
if (sentryResponseLog) {
showLog("RECEIVED_REMOVED_FROM_THREAD", content);
} else {
showLog("RECEIVED_REMOVED_FROM_THREAD");
}
listenerManager.callOnRemovedFromThread(content, chatResponse);
}
use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method addContacts.
/**
* Add list of contacts with their mobile numbers and their cellphoneNumbers
*/
private void addContacts(List<PhoneContact> phoneContacts, String uniqueId) {
ArrayList<String> firstNames = new ArrayList<>();
ArrayList<String> cellphoneNumbers = new ArrayList<>();
ArrayList<String> lastNames = new ArrayList<>();
ArrayList<String> typeCodes = new ArrayList<>();
ArrayList<String> uniqueIds = new ArrayList<>();
ArrayList<String> emails = new ArrayList<>();
for (PhoneContact contact : phoneContacts) {
firstNames.add(contact.getName());
lastNames.add(contact.getLastName());
uniqueIds.add(generateUniqueId());
String phoneNum = String.valueOf(contact.getPhoneNumber());
if (phoneNum.startsWith("9")) {
phoneNum = phoneNum.replaceFirst("9", "09");
}
cellphoneNumbers.add(phoneNum);
emails.add("");
typeCodes.add(getTypeCode());
}
Observable<Response<Contacts>> addContactsObservable;
if (getPlatformHost() != null) {
if (!Util.isNullOrEmpty(getTypeCode())) {
addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers, typeCodes);
} else {
addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers);
}
showLog("Call add contacts " + new Date());
addContactsObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(contactsResponse -> {
showLog(">>> Server Respond " + new Date());
boolean error = false;
if (contactsResponse.body() != null) {
error = contactsResponse.body().getHasError();
showLog(">>>Response: " + contactsResponse.code());
showLog(">>>ReferenceNumber: " + contactsResponse.body().getReferenceNumber());
showLog(">>>Ott: " + contactsResponse.body().getOtt());
}
if (contactsResponse.isSuccessful()) {
if (error) {
captureError(contactsResponse.body().getMessage(), contactsResponse.body().getErrorCode(), uniqueId);
showLog("Error add Contacts: " + contactsResponse.body().getMessage());
} else {
// successful response
Contacts contacts = contactsResponse.body();
ChatResponse<Contacts> chatResponse = new ChatResponse<>();
chatResponse.setResult(contacts);
chatResponse.setUniqueId(uniqueId);
Runnable updatePhoneContactsDBTask = () -> {
try {
boolean result = phoneContactDbHelper.addPhoneContacts(phoneContacts);
if (!result) {
disableCache(() -> phoneContactDbHelper.addPhoneContacts(phoneContacts));
}
} catch (Exception e) {
showErrorLog("Updating Contacts cache failed: " + e.getMessage());
onUnknownException(uniqueId, e);
}
};
Runnable updateCachedContactsTask = () -> {
if (cache) {
try {
// messageDatabaseHelper.saveContacts(chatResponse.getResult().getResult(), getExpireAmount());
dataSource.saveContactsResultFromServer(chatResponse.getResult().getResult());
} catch (Exception e) {
showErrorLog("Saving Contacts Failed: " + e.getMessage());
onUnknownException(uniqueId, e);
}
}
};
new PodThreadManager().addNewTask(updatePhoneContactsDBTask).addNewTask(updateCachedContactsTask).runTasksSynced();
String contactsJson = gson.toJson(chatResponse);
listenerManager.callOnSyncContact(contactsJson, chatResponse);
showLog("SYNC_CONTACT_COMPLETED", contactsJson);
}
} else {
captureError(contactsResponse.message(), contactsResponse.code(), uniqueId);
showLog("Error add Contacts: " + contactsResponse.raw());
}
}, throwable -> captureError(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId));
}
}
use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method handleOutPutBlock.
private void handleOutPutBlock(ChatMessage chatMessage, String messageUniqueId) {
BlockedContact contact = gson.fromJson(chatMessage.getContent(), BlockedContact.class);
ChatResponse<ResultBlock> chatResponse = new ChatResponse<>();
ResultBlock resultBlock = new ResultBlock();
resultBlock.setContact(contact);
chatResponse.setResult(resultBlock);
chatResponse.setErrorCode(0);
chatResponse.setHasError(false);
chatResponse.setUniqueId(chatMessage.getUniqueId());
String jsonBlock = gson.toJson(chatResponse);
if (cache) {
dataSource.saveBlockedContactResultFromServer(contact);
}
if (sentryResponseLog) {
showLog("RECEIVE_BLOCK", jsonBlock);
} else {
showLog("RECEIVE_BLOCK");
}
messageCallbacks.remove(messageUniqueId);
listenerManager.callOnBlock(jsonBlock, chatResponse);
}
use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method uploadFileProgress.
/**
* It uploads file and it shows progress of the file downloading
*/
public String uploadFileProgress(RequestUploadFile request, @Nullable ProgressHandler.onProgressFile handler) {
String uniqueId = generateUniqueId();
if (needReadStoragePermission(request.getActivity())) {
String jsonError = captureError(ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
if (handler != null) {
handler.onError(jsonError, error);
}
return uniqueId;
}
if (!chatReady) {
onChatNotReady(uniqueId);
return uniqueId;
}
if (getPodSpaceServer() == null) {
captureError("File server is null", 0, uniqueId);
return uniqueId;
}
try {
Subscription subscription = PodUploader.uploadPublicToPodSpace(uniqueId, request.getFileUri(), context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, request.isPublic(), new PodUploader.IPodUploadFileToPodSpace() {
@Override
public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length) {
ResultFile resultFile = PodUploader.generateFileUploadResult(response);
FileUpload result = new FileUpload();
result.setResult(resultFile);
ChatResponse<ResultFile> chatResponse = new ChatResponse<>();
resultFile.setUrl(getPodSpaceFileUrl(resultFile.getHashCode()));
showLog("FINISH_UPLOAD_FILE", gson.toJson(resultFile));
chatResponse.setResult(resultFile);
chatResponse.setUniqueId(uniqueId);
if (handler != null) {
handler.onFinish(gson.toJson(chatResponse), result);
}
listenerManager.callOnUploadFile(gson.toJson(resultFile), chatResponse);
}
@Override
public void onFailure(String cause, Throwable t) {
String jsonError = captureError(cause, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId, t);
ErrorOutPut error = new ErrorOutPut(true, cause, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId);
if (handler != null) {
handler.onError(jsonError, error);
}
}
@Override
public void onUploadStarted(String mimeType, File file, long length) {
showLog("UPLOADING_FILE");
}
@Override
public void onProgressUpdate(int progress, int totalBytesSent, int totalBytesToSend) {
if (handler != null) {
handler.onProgressUpdate(progress);
handler.onProgress(uniqueId, progress, totalBytesSent, totalBytesToSend);
}
}
});
initCancelUpload(uniqueId, subscription);
} catch (Exception e) {
String jsonError = captureError(ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId, e);
ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId);
if (handler != null) {
handler.onError(jsonError, error);
}
}
return uniqueId;
}
use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method publishThreadsList.
private String publishThreadsList(String uniqueId, Long finalOffset, ThreadManager.ThreadResponse cacheThreadResponse) {
ChatResponse<ResultThreads> chatResponse = new ChatResponse<>();
List<Thread> threadList = cacheThreadResponse.getThreadList();
chatResponse.setCache(true);
long contentCount = cacheThreadResponse.getContentCount();
ResultThreads resultThreads = new ResultThreads();
resultThreads.setThreads(threadList);
resultThreads.setContentCount(contentCount);
chatResponse.setCache(true);
resultThreads.setHasNext(threadList.size() + finalOffset < contentCount);
resultThreads.setNextOffset(finalOffset + threadList.size());
chatResponse.setResult(resultThreads);
chatResponse.setUniqueId(uniqueId);
String result = gson.toJson(chatResponse);
listenerManager.callOnGetThread(result, chatResponse);
return result;
}
Aggregations