Search in sources :

Example 1 with RealmAvatar

use of net.iGap.realm.RealmAvatar 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 2 with RealmAvatar

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

the class FragmentiGapMap method avatarMark.

public static Drawable avatarMark(long userId, MarkerColor markerColor) {
    Bitmap bitmap = DbManager.getInstance().doRealmTask(realm -> {
        String pathName = "";
        Bitmap bitmap1 = null;
        for (RealmAvatar avatar : realm.where(RealmAvatar.class).equalTo("ownerId", userId).findAll().sort("id", Sort.DESCENDING)) {
            if (avatar.getFile() != null) {
                pathName = avatar.getFile().getLocalFilePath();
                if (pathName == null) {
                    pathName = avatar.getFile().getLocalThumbnailPath();
                }
                break;
            }
        }
        if (pathName == null || pathName.isEmpty()) {
            bitmap1 = getInitials(realm, userId);
        } else {
            try {
                File imgFile = new File(pathName);
                bitmap1 = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            } catch (OutOfMemoryError e) {
                try {
                    File imgFile = new File(pathName);
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 2;
                    bitmap1 = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
                } catch (OutOfMemoryError e1) {
                    try {
                        File imgFile = new File(pathName);
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inSampleSize = 4;
                        bitmap1 = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
                    } catch (OutOfMemoryError e2) {
                        e2.printStackTrace();
                    }
                }
            }
            if (bitmap1 == null) {
                bitmap1 = getInitials(realm, userId);
            }
        }
        return bitmap1;
    });
    return new BitmapDrawable(G.context.getResources(), drawAvatar(bitmap, markerColor, userId == AccountManager.getInstance().getCurrentUser().getId()));
}
Also used : Bitmap(android.graphics.Bitmap) BitmapFactory(android.graphics.BitmapFactory) BitmapDrawable(android.graphics.drawable.BitmapDrawable) File(java.io.File) RealmAvatar(net.iGap.realm.RealmAvatar)

Example 3 with RealmAvatar

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

the class CallService method getAvatarBitmap.

private Bitmap getAvatarBitmap() {
    return DbManager.getInstance().doRealmTask(realm -> {
        String avatarPath = null;
        RealmAvatar realmAvatarPath = RealmAvatar.getLastAvatar(userId, realm);
        if (realmAvatarPath != null) {
            if (realmAvatarPath.getFile().isFileExistsOnLocal()) {
                avatarPath = realmAvatarPath.getFile().getLocalFilePath();
            } else if (realmAvatarPath.getFile().isThumbnailExistsOnLocal()) {
                avatarPath = realmAvatarPath.getFile().getLocalThumbnailPath();
            }
        }
        if (avatarPath != null) {
            try {
                BitmapFactory.Options option = new BitmapFactory.Options();
                option.outHeight = 64;
                option.outWidth = 64;
                return BitmapFactory.decodeFile(avatarPath, option);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    });
}
Also used : BitmapFactory(android.graphics.BitmapFactory) RealmAvatar(net.iGap.realm.RealmAvatar)

Example 4 with RealmAvatar

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

the class UserProfileViewModel method getProfileWallpaper.

private void getProfileWallpaper(Realm realm) {
    try {
        RealmWallpaper realmWallpaper = realm.where(RealmWallpaper.class).equalTo("type", ProtoInfoWallpaper.InfoWallpaper.Type.PROFILE_WALLPAPER_VALUE).findFirst();
        if (realmWallpaper != null) {
            if (realmWallpaper.getWallPaperList() != null && realmWallpaper.getWallPaperList().size() > 0) {
                RealmAttachment pf = realmWallpaper.getWallPaperList().get(realmWallpaper.getWallPaperList().size() - 1).getFile();
                String bigImagePath = G.context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).getAbsolutePath() + "/" + pf.getCacheId() + "_" + DownloadObject.extractMime(pf.getName());
                if (!new File(bigImagePath).exists()) {
                    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(db -> {
                                for (RealmAvatar realmAvatar1 : db.where(RealmAvatar.class).equalTo("file.token", fileToken).findAll()) {
                                    realmAvatar1.getFile().setLocalFilePath(filepath);
                                }
                            });
                            changeUserProfileWallpaperPath.postValue(bigImagePath);
                        }
                    });
                } else {
                    changeUserProfileWallpaperPath.postValue(bigImagePath);
                }
            } else {
                getProfileWallpaperFromServer();
            }
        } else {
            getProfileWallpaperFromServer();
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        e.printStackTrace();
    } catch (NullPointerException e2) {
        e2.printStackTrace();
    } catch (Exception e3) {
        e3.printStackTrace();
    }
}
Also used : RealmWallpaper(net.iGap.realm.RealmWallpaper) DownloadObject(net.iGap.module.downloader.DownloadObject) RealmAttachment(net.iGap.realm.RealmAttachment) HelperString(net.iGap.helper.HelperString) HelperDownloadFile(net.iGap.helper.HelperDownloadFile) File(java.io.File) RealmAvatar(net.iGap.realm.RealmAvatar)

Example 5 with RealmAvatar

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

the class MessageDataStorage method putUserAvatar.

public void putUserAvatar(long roomId, ProtoGlobal.Avatar avatar) {
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "putUserAvatar: " + roomId);
        try {
            database.beginTransaction();
            RealmAvatar realmAvatar = database.where(RealmAvatar.class).equalTo("id", avatar.getId()).findFirst();
            if (realmAvatar == null) {
                realmAvatar = database.createObject(RealmAvatar.class, avatar.getId());
            }
            realmAvatar.setOwnerId(roomId);
            realmAvatar.setFile(RealmAttachment.build(database, avatar.getFile(), AttachmentFor.AVATAR, null));
            database.commitTransaction();
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : RealmAvatar(net.iGap.realm.RealmAvatar)

Aggregations

RealmAvatar (net.iGap.realm.RealmAvatar)7 File (java.io.File)5 DownloadObject (net.iGap.module.downloader.DownloadObject)4 BitmapFactory (android.graphics.BitmapFactory)3 HelperDownloadFile (net.iGap.helper.HelperDownloadFile)3 RealmAttachment (net.iGap.realm.RealmAttachment)3 Bitmap (android.graphics.Bitmap)2 G (net.iGap.G)2 HelperLog (net.iGap.helper.HelperLog)2 OnProgress (net.iGap.messageprogress.OnProgress)2 AndroidUtils (net.iGap.module.AndroidUtils)2 AccountManager (net.iGap.module.accountManager.AccountManager)2 DbManager (net.iGap.module.accountManager.DbManager)2 Downloader (net.iGap.module.downloader.Downloader)2 Status (net.iGap.module.downloader.Status)2 ProtoGlobal (net.iGap.proto.ProtoGlobal)2 Color (android.graphics.Color)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Environment (android.os.Environment)1 Handler (android.os.Handler)1