Search in sources :

Example 1 with RealmAttachment

use of net.iGap.realm.RealmAttachment in project iGap-Android by KianIranian-STDG.

the class MessageDataStorage method deleteFileFromStorage.

public void deleteFileFromStorage(MessageObject message, DatabaseDelegate delegate) {
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "deleteFileFromStorage: " + message.id);
        try {
            database.beginTransaction();
            RealmAttachment attachment = database.where(RealmAttachment.class).equalTo("token", message.attachment.token).findFirst();
            if (attachment != null) {
                attachment.setLocalFilePath("");
            }
            database.commitTransaction();
            G.runOnUiThread(() -> delegate.run(null));
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : RealmAttachment(net.iGap.realm.RealmAttachment)

Example 2 with RealmAttachment

use of net.iGap.realm.RealmAttachment in project iGap-Android by KianIranian-STDG.

the class MessageDataStorage method setAttachmentFilePath.

public void setAttachmentFilePath(String cacheId, String absolutePath, boolean isThumb) {
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "setAttachmentFilePath: " + cacheId + " " + absolutePath);
        try {
            database.beginTransaction();
            RealmResults<RealmAttachment> attachments = database.where(RealmAttachment.class).equalTo("cacheId", cacheId).findAll();
            for (RealmAttachment attachment : attachments) {
                if (isThumb) {
                    attachment.setLocalThumbnailPath(absolutePath);
                } else {
                    attachment.setLocalFilePath(absolutePath);
                }
            }
            database.commitTransaction();
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : RealmAttachment(net.iGap.realm.RealmAttachment)

Example 3 with RealmAttachment

use of net.iGap.realm.RealmAttachment in project iGap-Android by KianIranian-STDG.

the class AdapterChatBackground method onBindViewHolder.

@Override
public void onBindViewHolder(@NotNull RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof ViewHolderImage) {
        if (position == 0) {
            ((ViewHolderImage) holder).messageProgress.setVisibility(View.GONE);
            ((ViewHolderImage) holder).imageView.setImageResource(R.drawable.add_chat_background_setting);
        } else {
            ((ViewHolderImage) holder).messageProgress.setVisibility(View.VISIBLE);
            ((ViewHolderImage) holder).imageView.setImageDrawable(null);
            StructWallpaper wallpaper = mList.get(position - 1);
            String path = "";
            if (wallpaper.getWallpaperType() == FragmentChatBackground.WallpaperType.proto) {
                RealmAttachment pf = wallpaper.getProtoWallpaper().getFile();
                // final String path = G.context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) + "/"  + pf.getCacheId() + "_" + pf.getName();
                path = pf.getLocalFilePath() != null ? pf.getLocalFilePath() : "";
                File file = new File(path);
                if (file.exists()) {
                    G.imageLoader.displayImage(AndroidUtils.suitablePath(path), ((ViewHolderImage) holder).imageView);
                } else {
                    path = pf.getLocalThumbnailPath() != null ? pf.getLocalThumbnailPath() : "";
                    file = new File(path);
                    if (file.exists()) {
                        G.imageLoader.displayImage(AndroidUtils.suitablePath(path), ((ViewHolderImage) holder).imageView);
                    } else {
                        DownloadObject downloadObject = DownloadObject.createForRoomMessage(AttachmentObject.create(pf), ProtoGlobal.RoomMessageType.IMAGE.getNumber());
                        Downloader.getInstance(AccountManager.selectedAccount).download(downloadObject, arg -> {
                            if (arg.status == Status.SUCCESS && arg.data != null) {
                                String filepath = arg.data.getFilePath();
                                String fileToken = arg.data.getToken();
                                if (!(new File(filepath).exists())) {
                                    HelperLog.getInstance().setErrorLog(new Exception("File Dont Exist After Download !!" + filepath));
                                }
                                DbManager.getInstance().doRealmTransaction(realm -> {
                                    for (RealmAvatar realmAvatar1 : realm.where(RealmAvatar.class).equalTo("file.token", fileToken).findAll()) {
                                        realmAvatar1.getFile().setLocalFilePath(filepath);
                                    }
                                });
                                G.runOnUiThread(() -> G.handler.post(() -> {
                                    if (((ViewHolderImage) holder).imageView != null) {
                                        G.imageLoader.displayImage(AndroidUtils.suitablePath(filepath), ((ViewHolderImage) holder).imageView);
                                    }
                                }));
                            }
                        });
                    }
                }
            } else {
                G.imageLoader.displayImage(AndroidUtils.suitablePath(wallpaper.getPath()), ((ViewHolderImage) holder).imageView);
            }
            String bigImagePath;
            bigImagePath = path;
            if (new File(bigImagePath).exists()) {
                ((ViewHolderImage) holder).messageProgress.setVisibility(View.GONE);
            } else {
                ((ViewHolderImage) holder).messageProgress.setVisibility(View.VISIBLE);
                startDownload(position - 1, ((ViewHolderImage) holder).messageProgress);
            }
        }
        holder.itemView.setOnClickListener(v -> {
            if (position == 0) {
                onImageClick.onAddImageClick();
            } else {
                onImageClick.onClick(type, holder.getAdapterPosition() - 1);
            }
        });
    } else if (holder instanceof ViewHolderSolid) {
        ((ViewHolderSolid) holder).cardView.setCardBackgroundColor(Color.parseColor(solidColorList.get(position)));
        holder.itemView.setOnClickListener(v -> onImageClick.onClick(type, holder.getAdapterPosition()));
    }
}
Also used : AttachmentObject(net.iGap.structs.AttachmentObject) AndroidUtils(net.iGap.module.AndroidUtils) AppUtils(net.iGap.module.AppUtils) Environment(android.os.Environment) AppCompatImageView(androidx.appcompat.widget.AppCompatImageView) AccountManager(net.iGap.module.accountManager.AccountManager) ProtoFileDownload(net.iGap.proto.ProtoFileDownload) ChatBackgroundViewModel(net.iGap.viewmodel.ChatBackgroundViewModel) MessageProgress(net.iGap.messageprogress.MessageProgress) G(net.iGap.G) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) CardView(androidx.cardview.widget.CardView) HelperDownloadFile(net.iGap.helper.HelperDownloadFile) Status(net.iGap.module.downloader.Status) LayoutInflater(android.view.LayoutInflater) OnProgress(net.iGap.messageprogress.OnProgress) Downloader(net.iGap.module.downloader.Downloader) ViewGroup(android.view.ViewGroup) File(java.io.File) Color(android.graphics.Color) RealmAvatar(net.iGap.realm.RealmAvatar) HelperLog(net.iGap.helper.HelperLog) List(java.util.List) StructWallpaper(net.iGap.module.StructWallpaper) ProtoGlobal(net.iGap.proto.ProtoGlobal) R(net.iGap.R) NotNull(org.jetbrains.annotations.NotNull) DbManager(net.iGap.module.accountManager.DbManager) FragmentChatBackground(net.iGap.fragments.FragmentChatBackground) DownloadObject(net.iGap.module.downloader.DownloadObject) RealmAttachment(net.iGap.realm.RealmAttachment) StructWallpaper(net.iGap.module.StructWallpaper) DownloadObject(net.iGap.module.downloader.DownloadObject) RealmAttachment(net.iGap.realm.RealmAttachment) HelperDownloadFile(net.iGap.helper.HelperDownloadFile) File(java.io.File) RealmAvatar(net.iGap.realm.RealmAvatar)

Example 4 with RealmAttachment

use of net.iGap.realm.RealmAttachment 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 5 with RealmAttachment

use of net.iGap.realm.RealmAttachment in project iGap-Android by KianIranian-STDG.

the class FragmentChat method openMessage.

private void openMessage(MessageObject messageObject) {
    String filePath = null;
    String token = messageObject.forwardedMessage != null ? messageObject.forwardedMessage.getAttachment().token : messageObject.getAttachment().token;
    RealmAttachment realmAttachment = DbManager.getInstance().doRealmTask(realm -> {
        return realm.where(RealmAttachment.class).equalTo("token", token).findFirst();
    });
    if (realmAttachment != null) {
        filePath = realmAttachment.getLocalFilePath();
    } else if (messageObject.getAttachment() != null) {
        filePath = messageObject.getAttachment().filePath;
    }
    if (filePath == null) {
        filePath = messageObject.getCacheFile(false);
    }
    if (filePath == null || filePath.length() == 0) {
        return;
    }
    Intent intent = HelperMimeType.appropriateProgram(filePath);
    if (intent != null) {
        try {
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(context, R.string.can_not_support_file, Toast.LENGTH_SHORT).show();
        }
    } else {
        Toast.makeText(context, R.string.can_not_open_file, Toast.LENGTH_SHORT).show();
    }
}
Also used : Intent(android.content.Intent) RealmString(net.iGap.realm.RealmString) HelperString(net.iGap.helper.HelperString) RealmAttachment(net.iGap.realm.RealmAttachment) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException)

Aggregations

RealmAttachment (net.iGap.realm.RealmAttachment)17 File (java.io.File)7 HelperString (net.iGap.helper.HelperString)5 DownloadObject (net.iGap.module.downloader.DownloadObject)4 Gson (com.google.gson.Gson)3 List (java.util.List)3 HelperDownloadFile (net.iGap.helper.HelperDownloadFile)3 RealmAdditional (net.iGap.realm.RealmAdditional)3 RealmAvatar (net.iGap.realm.RealmAvatar)3 Intent (android.content.Intent)2 ArrayList (java.util.ArrayList)2 StructBottomSheet (net.iGap.module.structs.StructBottomSheet)2 RealmRoom (net.iGap.realm.RealmRoom)2 RealmRoomMessage (net.iGap.realm.RealmRoomMessage)2 RealmStickerItem (net.iGap.realm.RealmStickerItem)2 RealmString (net.iGap.realm.RealmString)2 RealmWallpaper (net.iGap.realm.RealmWallpaper)2 MainStoryObject (net.iGap.story.MainStoryObject)2 StoryObject (net.iGap.story.StoryObject)2 MessageObject (net.iGap.structs.MessageObject)2