Skip to content

Commit

Permalink
Fix scroll lagging for auto-upload files. (NMC-2589)
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder-tsys committed Feb 14, 2024
1 parent 6617084 commit 26a71b9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import java.net.URLEncoder;
import java.util.List;

import androidx.annotation.DimenRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
Expand Down Expand Up @@ -170,9 +171,19 @@ protected Void doInBackground(File... params) {
* @return int
*/
public static int getThumbnailDimension() {
return getThumbnailDimension(R.dimen.file_icon_size_grid);
}

/**
* Converts size of file icon from dp to pixel
* this function is required for custom thumbnail sizes
* @param thumbnailDimension dimension to be converted
* @return int
*/
public static int getThumbnailDimension(@DimenRes int thumbnailDimension) {
// Converts dp to pixel
Resources r = MainApp.getAppContext().getResources();
return Math.round(r.getDimension(R.dimen.file_icon_size_grid));
return Math.round(r.getDimension(thumbnailDimension));
}

/**
Expand Down Expand Up @@ -827,14 +838,25 @@ private enum Type {IMAGE, VIDEO}
private String mImageKey;
private final Context mContext;
private final ViewThemeUtils viewThemeUtils;
@DimenRes
private final int thumbnailDimension;

public MediaThumbnailGenerationTask(ImageView imageView,
Context context,
ViewThemeUtils viewThemeUtils) {
this(imageView, context, R.dimen.file_icon_size_grid, viewThemeUtils);
}

// constructor to generate thumbnails for the requested size
public MediaThumbnailGenerationTask(ImageView imageView,
Context context,
@DimenRes int thumbnailDimension,
ViewThemeUtils viewThemeUtils) {
// Use a WeakReference to ensure the ImageView can be garbage collected
mImageViewReference = new WeakReference<>(imageView);
mContext = context;
this.viewThemeUtils = viewThemeUtils;
this.thumbnailDimension = thumbnailDimension;
}

@Override
Expand All @@ -849,9 +871,9 @@ protected Bitmap doInBackground(Object... params) {
}

if (MimeTypeUtil.isImage(mFile)) {
thumbnail = doFileInBackground(mFile, Type.IMAGE);
thumbnail = doFileInBackground(mFile, Type.IMAGE, thumbnailDimension);
} else if (MimeTypeUtil.isVideo(mFile)) {
thumbnail = doFileInBackground(mFile, Type.VIDEO);
thumbnail = doFileInBackground(mFile, Type.VIDEO, thumbnailDimension);
}
}
} // the app should never break due to a problem with thumbnails
Expand Down Expand Up @@ -897,7 +919,7 @@ protected void onPostExecute(Bitmap bitmap) {
}
}

private Bitmap doFileInBackground(File file, Type type) {
private Bitmap doFileInBackground(File file, Type type, @DimenRes int thumbnailDimension) {
final String imageKey;

if (mImageKey != null) {
Expand All @@ -913,7 +935,7 @@ private Bitmap doFileInBackground(File file, Type type) {
if (thumbnail == null) {

if (Type.IMAGE == type) {
int px = getThumbnailDimension();
int px = getThumbnailDimension(thumbnailDimension);

Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromFile(file.getAbsolutePath(), px, px);

Expand All @@ -939,7 +961,7 @@ private Bitmap doFileInBackground(File file, Type type) {

if (thumbnail != null) {
// Scale down bitmap if too large.
int px = getThumbnailDimension();
int px = getThumbnailDimension(thumbnailDimension);
int width = thumbnail.getWidth();
int height = thumbnail.getHeight();
int max = Math.max(width, height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ public void onBindViewHolder(SectionedViewHolder commonHolder, int section, int
ThumbnailsCacheManager.MediaThumbnailGenerationTask task =
new ThumbnailsCacheManager.MediaThumbnailGenerationTask(holder.binding.thumbnail,
context,
// due to 512dp(NMC) thumb size there was scroll lagging in auto upload
// so for auto upload we have to use different thumb size (NMC-2589)
R.dimen.auto_upload_file_thumb_size,
viewThemeUtils);

ThumbnailsCacheManager.AsyncMediaThumbnailDrawable asyncDrawable =
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-sw600dp/dims.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
<resources>
<integer name="media_grid_width">6</integer>
<dimen name="file_icon_size_grid">512dp</dimen>
<dimen name="auto_upload_file_thumb_size">512dp</dimen>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/dims.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<dimen name="synced_folders_item_type_layout_right_end_margin">24dp</dimen>
<dimen name="synced_folders_recycler_view_layout_margin">-3dp</dimen>
<dimen name="synced_folders_control_width">80dp</dimen>
<dimen name="auto_upload_file_thumb_size">128dp</dimen>
<dimen name="bottom_sheet_text_size">16sp</dimen>
<dimen name="permission_dialog_text_size">18sp</dimen>
<dimen name="button_corner_radius">24dp</dimen>
Expand Down

0 comments on commit 26a71b9

Please sign in to comment.