Search in sources :

Example 6 with StoryObject

use of net.iGap.story.StoryObject in project iGap-Android by KianIranian-STDG.

the class HttpUploader method startUpload.

private void startUpload(UploadObject fileObject, final File completedCompressFile) {
    if (fileObject.fileToken != null && completedCompressFile == null) {
        sendMessage(fileObject);
    } else {
        UploadHttpRequest existedRequest = findExistedRequest(fileObject.key);
        if (existedRequest == null) {
            existedRequest = new UploadHttpRequest(fileObject, new UploadHttpRequest.UploadDelegate() {

                @Override
                public void onUploadProgress(UploadObject fileObject) {
                    FileLog.i("HttpUploader " + fileObject.fileToken + " progress -> " + fileObject.progress);
                    G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_PROGRESS, fileObject.key, fileObject.progress, fileObject.fileSize));
                    if (fileObject.onUploadListener != null) {
                        fileObject.onUploadListener.onProgress(String.valueOf(fileObject.messageId), fileObject.progress);
                    }
                }

                @Override
                public void onUploadFinish(UploadObject fileObject) {
                    HelperDataUsage.increaseUploadFiles(fileObject.messageType);
                    HelperDataUsage.progressUpload(fileObject.fileSize, fileObject.messageType);
                    FileLog.i("HttpUploader onUploadFinish " + fileObject.fileToken);
                    UploadHttpRequest req = inProgressUploads.get(fileObject.key);
                    if (req != null) {
                        inProgressUploads.remove(fileObject.key);
                        inProgressCount.decrementAndGet();
                    }
                    if (fileObject.messageObject != null) {
                        if (completedCompressFile != null && completedCompressFile.exists()) {
                            completedCompressFile.delete();
                        }
                        HelperSetAction.sendCancel(fileObject.messageId);
                        DbManager.getInstance().doRealmTransaction(realm -> RealmAttachment.updateToken(fileObject.messageId, fileObject.fileToken));
                        sendMessage(fileObject);
                    }
                    if (fileObject.messageType == ProtoGlobal.RoomMessageType.STORY) {
                        MessageDataStorage.getInstance(AccountManager.selectedAccount).updateStoryFileToken(fileObject.messageId, fileObject.fileToken);
                        if (isMultiUpload || isRoomMultiUpload) {
                            if (MessageDataStorage.getInstance(AccountManager.selectedAccount).getStoryById(AccountManager.getInstance().getCurrentUser().getId(), false).storyObjects.size() == MessageDataStorage.getInstance(AccountManager.selectedAccount).getNotNullTokenStories(AccountManager.getInstance().getCurrentUser().getId(), 0).size()) {
                                List<StoryObject> realmStoryProtos;
                                if (isRoomMultiUpload) {
                                    realmStoryProtos = MessageDataStorage.getInstance(AccountManager.selectedAccount).getStoryByStatus(AccountManager.getInstance().getCurrentUser().getId(), storyRoomIdForUpload, MessageObject.STATUS_SENDING, true, true, new String[] { "createdAt" });
                                } else {
                                    realmStoryProtos = MessageDataStorage.getInstance(AccountManager.selectedAccount).getStoryByStatus(AccountManager.getInstance().getCurrentUser().getId(), 0, MessageObject.STATUS_SENDING, true, false, new String[] { "createdAt" });
                                }
                                if (realmStoryProtos != null && realmStoryProtos.size() > 0) {
                                    List<ProtoStoryUserAddNew.StoryAddRequest> storyObjects = new ArrayList<>();
                                    for (int i = 0; i < realmStoryProtos.size(); i++) {
                                        ProtoStoryUserAddNew.StoryAddRequest.Builder storyAddRequest = ProtoStoryUserAddNew.StoryAddRequest.newBuilder();
                                        storyAddRequest.setToken(realmStoryProtos.get(i).fileToken);
                                        storyAddRequest.setCaption(realmStoryProtos.get(i).caption);
                                        storyObjects.add(storyAddRequest.build());
                                    }
                                    isStoryUploading = false;
                                    if (isRoomMultiUpload) {
                                        MessageController.getInstance(AccountManager.selectedAccount).addMyRoomStory(storyObjects, storyRoomIdForUpload);
                                    } else {
                                        MessageController.getInstance(AccountManager.selectedAccount).addMyStory(storyObjects);
                                    }
                                }
                            }
                        } else {
                            List<StoryObject> realmStoryProtos = MessageDataStorage.getInstance(AccountManager.selectedAccount).getNotNullTokenStories(AccountManager.getInstance().getCurrentUser().getId(), MessageObject.STATUS_SENDING);
                            if (realmStoryProtos != null && realmStoryProtos.size() > 0) {
                                List<ProtoStoryUserAddNew.StoryAddRequest> storyObjects = new ArrayList<>();
                                for (int i = 0; i < realmStoryProtos.size(); i++) {
                                    ProtoStoryUserAddNew.StoryAddRequest.Builder storyAddRequest = ProtoStoryUserAddNew.StoryAddRequest.newBuilder();
                                    storyAddRequest.setToken(realmStoryProtos.get(i).fileToken);
                                    storyAddRequest.setCaption(realmStoryProtos.get(i).caption);
                                    storyObjects.add(storyAddRequest.build());
                                }
                                isStoryUploading = false;
                                if (realmStoryProtos.get(0).isForRoom) {
                                    MessageController.getInstance(AccountManager.selectedAccount).addMyRoomStory(storyObjects, realmStoryProtos.get(0).roomId);
                                } else {
                                    MessageController.getInstance(AccountManager.selectedAccount).addMyStory(storyObjects);
                                }
                            }
                        }
                    }
                    if (fileObject.onUploadListener != null) {
                        fileObject.onUploadListener.onFinish(String.valueOf(fileObject.messageId), fileObject.fileToken);
                    }
                    scheduleNewUpload();
                }

                @Override
                public void onUploadFail(UploadObject fileObject, @Nullable Exception e) {
                    long uploadedBytes = ((fileObject.fileSize / 100) * fileObject.progress);
                    HelperDataUsage.progressUpload(uploadedBytes, fileObject.messageType);
                    FileLog.e("HttpUploader onUploadFail " + fileObject.fileToken, e);
                    UploadHttpRequest req = inProgressUploads.get(fileObject.key);
                    if (req != null) {
                        inProgressUploads.remove(fileObject.key);
                        inProgressCount.decrementAndGet();
                    }
                    scheduleNewUpload();
                    if (inProgressCount.get() == 0)
                        HelperSetAction.sendCancel(fileObject.messageId);
                    makeFailed(fileObject.messageId);
                    if (fileObject.messageType == ProtoGlobal.RoomMessageType.STORY) {
                        MessageDataStorage.getInstance(AccountManager.selectedAccount).updateStorySentStatus(AccountManager.getInstance().getCurrentUser().getId(), false);
                        MessageDataStorage.getInstance(AccountManager.selectedAccount).updateStoryStatus(fileObject.messageId, MessageObject.STATUS_FAILED);
                        G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.STORY_UPLOADED_FAILED, fileObject.messageId));
                    }
                    if (fileObject.onUploadListener != null) {
                        fileObject.onUploadListener.onError(String.valueOf(fileObject.messageId));
                    }
                }
            });
            uploadQueue.add(existedRequest);
            scheduleNewUpload();
        }
    }
}
Also used : UploadHttpRequest(net.iGap.module.upload.UploadHttpRequest) StoryObject(net.iGap.story.StoryObject) ArrayList(java.util.ArrayList) UploadObject(net.iGap.module.upload.UploadObject) Nullable(androidx.annotation.Nullable)

Example 7 with StoryObject

use of net.iGap.story.StoryObject in project iGap-Android by KianIranian-STDG.

the class MyStatusStoryListFragment method sendRoomStory.

private void sendRoomStory(List<String> paths, ArrayList<StructBottomSheet> itemGalleryList, long roomId, int listMode, String roomTitle) {
    G.runOnUiThread(() -> {
        actionButtonsRootView.setVisibility(View.GONE);
        progressBar.setVisibility(View.VISIBLE);
    });
    objectsCounter = 0;
    if (paths.size() > 1) {
        HttpUploader.isRoomMultiUpload = true;
    }
    for (int i = 0; i < paths.size(); i++) {
        long storyId = SUID.id().get();
        long lastUploadedStoryId = storyId + 1L;
        int[] imageDimens = { 0, 0 };
        long attachementId = SUID.id().get();
        imageDimens = AndroidUtils.getImageDimens(paths.get(i));
        RealmAttachment realmAttachment = getMessageDataStorage().createRealmObject(paths.get(i), imageDimens, attachementId);
        StoryObject storyObject = new StoryObject();
        storyObject.isSeen = false;
        storyObject.realmAttachment = realmAttachment;
        storyObject.userId = AccountManager.getInstance().getCurrentUser().getId();
        storyObject.roomId = roomId;
        storyObject.isForRoom = true;
        storyObject.sessionId = AccountManager.getInstance().getCurrentUser().getId();
        storyObject.displayName = roomTitle;
        storyObject.createdAt = System.currentTimeMillis();
        storyObject.caption = itemGalleryList.get(objectsCounter).getText();
        storyObject.status = MessageObject.STATUS_SENDING;
        storyObject.id = lastUploadedStoryId;
        List<StoryObject> realmStories = getMessageDataStorage().getStoryWithIndexSort(storyObject.userId);
        if (realmStories != null && realmStories.size() > 0) {
            storyObject.index = realmStories.get(0).index + 1;
        } else {
            storyObject.index = i;
        }
        storyInLocal.add(storyObject);
        getMessageDataStorage().putStoriesToDatabaseOffline(false, storyObject.userId, storyObject.roomId, storyInLocal, storyObject.displayName, true);
        storyInLocal.remove(0);
        HttpUploader.isStoryUploading = true;
        Uploader.getInstance().upload(UploadObject.createForStory(lastUploadedStoryId, paths.get(i), null, itemGalleryList.get(objectsCounter).getText(), ProtoGlobal.RoomMessageType.STORY));
        objectsCounter++;
        if (objectsCounter == itemGalleryList.size()) {
            G.runOnUiThread(() -> loadStories());
            storyInLocal = new ArrayList<>();
        }
    }
}
Also used : MainStoryObject(net.iGap.story.MainStoryObject) StoryObject(net.iGap.story.StoryObject) RealmAttachment(net.iGap.realm.RealmAttachment)

Example 8 with StoryObject

use of net.iGap.story.StoryObject in project iGap-Android by KianIranian-STDG.

the class StoryViewFragment method onViewCreated.

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (isSingle) {
        if (isForReply) {
            DbManager.getInstance().doRealmTransaction(realm -> {
                RealmStoryProto realmStoryProto = realm.where(RealmStoryProto.class).equalTo("isForReply", false).equalTo("storyId", storyId).findFirst();
                if (realmStoryProto != null) {
                    MainStoryObject mainStoryObject = new MainStoryObject();
                    List<StoryObject> storyObjects = new ArrayList<>();
                    mainStoryObject.userId = realmStoryProto.getUserId();
                    storyObjects.add(StoryObject.create(realmStoryProto));
                    mainStoryObject.displayName = realmStoryProto.getDisplayName();
                    mainStoryObject.profileColor = realmStoryProto.getProfileColor();
                    mainStoryObject.storyObjects = storyObjects;
                    storyResults.add(mainStoryObject);
                }
            });
        } else {
            storyResults.addAll(getMessageDataStorage().getSortedStoryObjectsInMainStoryObject(userId, new String[] { "createdAt" }, new Sort[] { Sort.ASCENDING }));
        }
    } else {
        if (isForRoom && !isForOtherRoom) {
            storyResults.addAll(getMessageDataStorage().getSortedRoomStoryObjectsInMainStoryObject(0, isForOtherRoom, roomId, new String[] { "createdAt" }, new Sort[] { Sort.ASCENDING }));
        } else {
            storyResults.addAll(getMessageDataStorage().getSortedStoryObjectsInMainStoryObject(0, new String[] { "createdAt" }, new Sort[] { Sort.ASCENDING }));
        }
    }
    setUpPager();
}
Also used : StoryObject(net.iGap.story.StoryObject) MainStoryObject(net.iGap.story.MainStoryObject) RealmStoryProto(net.iGap.realm.RealmStoryProto) ArrayList(java.util.ArrayList) Sort(io.realm.Sort) MainStoryObject(net.iGap.story.MainStoryObject)

Example 9 with StoryObject

use of net.iGap.story.StoryObject in project iGap-Android by KianIranian-STDG.

the class RealmStory method setRealmStoryProtos.

public void setRealmStoryProtos(Realm realm, List<StoryObject> stories) {
    boolean isExist = false;
    for (StoryObject igapStory : stories) {
        RealmAttachment realmAttachment;
        RealmStoryProto storyProto;
        if (igapStory.fileToken != null) {
            storyProto = realm.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("fileToken", igapStory.fileToken).findFirst();
        } else {
            storyProto = realm.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("isForReply", false).equalTo("id", igapStory.id).findFirst();
        }
        if (storyProto == null) {
            storyProto = realm.createObject(RealmStoryProto.class);
            setSentAll(false);
            setUploadedAll(false);
        } else {
            isExist = true;
        }
        if (igapStory.realmAttachment != null) {
            storyProto.setFile(igapStory.realmAttachment);
        } else {
            if (!isExist) {
                realmAttachment = RealmAttachment.build(realm, igapStory.file, AttachmentFor.AVATAR, null);
                storyProto.setFile(realmAttachment);
            } else if (isExist && igapStory.file != null) {
                realmAttachment = RealmAttachment.putOrUpdate(realm, SUID.id().get(), storyProto.getFile(), igapStory.file);
                storyProto.setFile(realmAttachment);
            } else {
                realmAttachment = RealmAttachment.build(realm, igapStory.file, AttachmentFor.AVATAR, null);
                storyProto.setFile(realmAttachment);
            }
        }
        storyProto.setCaption(igapStory.caption);
        if (igapStory.storyId == 0) {
            storyProto.setCreatedAt(igapStory.createdAt);
        } else {
            storyProto.setCreatedAt(igapStory.createdAt * 1000L);
        }
        storyProto.setVerified(igapStory.isVerified);
        storyProto.setFileToken(igapStory.fileToken);
        storyProto.setUserId(igapStory.userId);
        storyProto.setStoryId(igapStory.storyId);
        storyProto.setSeen(igapStory.isSeen);
        storyProto.setStatus(igapStory.status);
        storyProto.setId(igapStory.id);
        storyProto.setForReply(false);
        storyProto.setDisplayName(igapStory.displayName);
        storyProto.setProfileColor(igapStory.profileColor);
        storyProto.setRoomId(igapStory.roomId);
        storyProto.setForRoom(igapStory.isForRoom);
        storyProto.setViewCount(igapStory.viewCount);
        storyProto.setSessionId(AccountManager.getInstance().getCurrentUser().getId());
        if (isExist) {
            storyProto.setIndex(storyProto.getIndex());
        } else {
            storyProto.setIndex(igapStory.index);
        }
        if (igapStory.isSeen) {
            setIndexOfSeen(stories.indexOf(igapStory));
        }
        if (getIndexOfSeen() > stories.size()) {
            setIndexOfSeen(0);
        }
        if (isExist) {
            realmStoryProtos.remove(storyProto);
        }
        realmStoryProtos.add(storyProto);
        isExist = false;
    }
    if (realm.where(RealmStoryProto.class).equalTo("userId", getUserId()).equalTo("isForReply", false).equalTo("status", MessageObject.STATUS_SENDING).findAll().size() > 0 || realm.where(RealmStoryProto.class).equalTo("userId", getUserId()).equalTo("isForReply", false).equalTo("status", MessageObject.STATUS_FAILED).findAll().size() > 0) {
        setSentAll(false);
    } else {
        setSentAll(true);
    }
}
Also used : StoryObject(net.iGap.story.StoryObject)

Example 10 with StoryObject

use of net.iGap.story.StoryObject in project iGap-Android by KianIranian-STDG.

the class MessageDataStorage method getNotNullTokenStories.

public List<StoryObject> getNotNullTokenStories(long userId, int status) {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    List<RealmStoryProto> stories = new ArrayList<>();
    List<StoryObject> storyObjects = new ArrayList<>();
    storageQueue.postRunnable(() -> {
        try {
            if (status == 0) {
                stories.addAll(database.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("userId", userId).isNotNull("fileToken").findAll());
            } else {
                stories.addAll(database.where(RealmStoryProto.class).equalTo("sessionId", AccountManager.getInstance().getCurrentUser().getId()).equalTo("isForReply", false).equalTo("userId", userId).equalTo("status", status).isNotNull("fileToken").findAll());
            }
            for (int i = 0; i < stories.size(); i++) {
                storyObjects.add(StoryObject.create(database.copyFromRealm(stories.get(i))));
            }
            countDownLatch.countDown();
        } catch (Exception e) {
            FileLog.e(e);
        } finally {
            countDownLatch.countDown();
        }
    });
    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return storyObjects;
}
Also used : MainStoryObject(net.iGap.story.MainStoryObject) StoryObject(net.iGap.story.StoryObject) RealmStoryProto(net.iGap.realm.RealmStoryProto) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

StoryObject (net.iGap.story.StoryObject)24 MainStoryObject (net.iGap.story.MainStoryObject)22 ArrayList (java.util.ArrayList)18 RealmStoryProto (net.iGap.realm.RealmStoryProto)15 CountDownLatch (java.util.concurrent.CountDownLatch)12 RealmStory (net.iGap.realm.RealmStory)8 Sort (io.realm.Sort)4 List (java.util.List)4 RealmAttachment (net.iGap.realm.RealmAttachment)3 View (android.view.View)2 LinearLayout (android.widget.LinearLayout)2 TextView (android.widget.TextView)2 File (java.io.File)2 IconView (net.iGap.messenger.ui.components.IconView)2 CircleImageView (net.iGap.module.CircleImageView)2 FontIconTextView (net.iGap.module.FontIconTextView)2 MaterialDesignTextView (net.iGap.module.MaterialDesignTextView)2 ProtoStoryUserAddNew (net.iGap.proto.ProtoStoryUserAddNew)2 RealmRegisteredInfo (net.iGap.realm.RealmRegisteredInfo)2 ImageLoadingView (net.iGap.story.liststories.ImageLoadingView)2