From 01fa5dc5e2825cc36df5fcc4ed82d52de35b0d9f Mon Sep 17 00:00:00 2001 From: alperozturk Date: Thu, 29 Aug 2024 12:29:19 +0200 Subject: [PATCH] Check thumbnail existence and shimmering --- .../android/ui/adapter/OCFileListAdapter.java | 9 ++++ .../owncloud/android/utils/DisplayUtils.java | 52 +++++++++++-------- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java b/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java index 3490e87b3f43..9c5dc23e862d 100644 --- a/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java +++ b/app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java @@ -1015,6 +1015,15 @@ public long getLastTimestamp() { return lastTimestamp; } + @Override + public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { + super.onViewRecycled(holder); + if (holder instanceof ListGridImageViewHolder listGridImageViewHolder) { + LoaderImageView thumbnailShimmer = listGridImageViewHolder.getShimmerThumbnail(); + DisplayUtils.stopShimmer(thumbnailShimmer, listGridImageViewHolder.getThumbnail()); + } + } + @Override public void avatarGenerated(Drawable avatarDrawable, Object callContext) { ((ImageView) callContext).setImageDrawable(avatarDrawable); diff --git a/app/src/main/java/com/owncloud/android/utils/DisplayUtils.java b/app/src/main/java/com/owncloud/android/utils/DisplayUtils.java index 6a8d207ae8a6..cc35cf6d76e9 100644 --- a/app/src/main/java/com/owncloud/android/utils/DisplayUtils.java +++ b/app/src/main/java/com/owncloud/android/utils/DisplayUtils.java @@ -927,12 +927,20 @@ private static void generateNewThumbnail(OCFile file, Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache( ThumbnailsCacheManager.PREFIX_THUMBNAIL + file.getRemoteId()); + if (thumbnail != null) { + // If thumbnail is already in cache, display it immediately + thumbnailView.setImageBitmap(thumbnail); + stopShimmer(shimmerThumbnail, thumbnailView); + return; + } + for (ThumbnailsCacheManager.ThumbnailGenerationTask task : asyncTasks) { if (file.getRemoteId() != null && task.getImageKey() != null && file.getRemoteId().equals(task.getImageKey())) { return; } } + try { final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(thumbnailView, @@ -941,32 +949,34 @@ private static void generateNewThumbnail(OCFile file, asyncTasks, gridView, file.getRemoteId()); - if (thumbnail == null) { - Drawable drawable = MimeTypeUtil.getFileTypeIcon(file.getMimeType(), - file.getFileName(), - context, - viewThemeUtils); - if (drawable == null) { - drawable = ResourcesCompat.getDrawable(context.getResources(), - R.drawable.file_image, - null); - } - if (drawable == null) { - drawable = new ColorDrawable(Color.GRAY); - } - - int px = ThumbnailsCacheManager.getThumbnailDimension(); - thumbnail = BitmapUtils.drawableToBitmap(drawable, px, px); + Drawable drawable = MimeTypeUtil.getFileTypeIcon(file.getMimeType(), + file.getFileName(), + context, + viewThemeUtils); + if (drawable == null) { + drawable = ResourcesCompat.getDrawable(context.getResources(), + R.drawable.file_image, + null); + } + if (drawable == null) { + drawable = new ColorDrawable(Color.GRAY); } + + int px = ThumbnailsCacheManager.getThumbnailDimension(); + thumbnail = BitmapUtils.drawableToBitmap(drawable, px, px); final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable = new ThumbnailsCacheManager.AsyncThumbnailDrawable(context.getResources(), thumbnail, task); - if (shimmerThumbnail != null && shimmerThumbnail.getVisibility() == View.GONE) { - if (gridView) { - configShimmerGridImageSize(shimmerThumbnail, preferences.getGridColumns()); - } - startShimmer(shimmerThumbnail, thumbnailView); + if (shimmerThumbnail != null) { + shimmerThumbnail.postDelayed(() -> { + if (thumbnailView.getDrawable() == null) { + if (gridView) { + configShimmerGridImageSize(shimmerThumbnail, preferences.getGridColumns()); + } + startShimmer(shimmerThumbnail, thumbnailView); + } + }, 100); // 100ms delay, adjust as needed } task.setListener(new ThumbnailsCacheManager.ThumbnailGenerationTask.Listener() {