From 440aea9eb810a73fadc5cefa2703bedf0b8f3d27 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 14 Jan 2021 18:50:32 +0100 Subject: [PATCH] use a more advanced cache key by using size too --- .../pro/activities/WidgetConfigureActivity.kt | 4 ++- .../gallery/pro/adapters/DirectoryAdapter.kt | 2 +- .../gallery/pro/adapters/MediaAdapter.kt | 5 +-- .../gallery/pro/extensions/Context.kt | 31 ++++++++++--------- .../gallery/pro/fragments/PhotoFragment.kt | 6 ++-- .../gallery/pro/models/Directory.kt | 3 ++ .../gallery/pro/models/Medium.kt | 13 +++++++- 7 files changed, 41 insertions(+), 23 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/WidgetConfigureActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/WidgetConfigureActivity.kt index ac34d9f3c..4521e64a1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/WidgetConfigureActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/WidgetConfigureActivity.kt @@ -8,6 +8,7 @@ import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.widget.RelativeLayout import android.widget.RemoteViews +import com.bumptech.glide.signature.ObjectKey import com.simplemobiletools.commons.dialogs.ColorPickerDialog import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.ensureBackgroundThread @@ -167,7 +168,8 @@ class WidgetConfigureActivity : SimpleActivity() { val path = directoryDao.getDirectoryThumbnail(folderPath) if (path != null) { runOnUiThread { - loadJpg(path, config_image, config.cropThumbnails, ROUNDED_CORNERS_NONE, 0) + val signature = ObjectKey(System.currentTimeMillis().toString()) + loadJpg(path, config_image, config.cropThumbnails, ROUNDED_CORNERS_NONE, signature) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt index 08b042393..3aa01dc16 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt @@ -710,7 +710,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList ROUNDED_CORNERS_BIG } - activity.loadImage(thumbnailType, directory.tmb, dir_thumbnail, scrollHorizontally, animateGifs, cropThumbnails, roundedCorners, directory.modified) + activity.loadImage(thumbnailType, directory.tmb, dir_thumbnail, scrollHorizontally, animateGifs, cropThumbnails, roundedCorners, directory.getKey()) } dir_pin.beVisibleIf(pinnedFolders.contains(directory.path)) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt index ff0ffd1eb..7f3128cf3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt @@ -540,8 +540,9 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList? = null) { + roundCorners: Int, signature: ObjectKey, skipMemoryCacheAtPaths: ArrayList? = null) { target.isHorizontalScrolling = horizontalScroll if (type == TYPE_IMAGES || type == TYPE_VIDEOS || type == TYPE_RAWS || type == TYPE_PORTRAITS) { if (type == TYPE_IMAGES && path.isPng()) { - loadPng(path, target, cropThumbnails, roundCorners, lastModified, skipMemoryCacheAtPaths) + loadPng(path, target, cropThumbnails, roundCorners, signature, skipMemoryCacheAtPaths) } else { - loadJpg(path, target, cropThumbnails, roundCorners, lastModified, skipMemoryCacheAtPaths) + loadJpg(path, target, cropThumbnails, roundCorners, signature, skipMemoryCacheAtPaths) } } else if (type == TYPE_GIFS) { if (!animateGifs) { - loadStaticGIF(path, target, cropThumbnails, roundCorners, lastModified, skipMemoryCacheAtPaths) + loadStaticGIF(path, target, cropThumbnails, roundCorners, signature, skipMemoryCacheAtPaths) return } @@ -419,12 +420,12 @@ fun Context.loadImage(type: Int, path: String, target: MySquareImageView, horizo target.scaleType = if (cropThumbnails) ImageView.ScaleType.CENTER_CROP else ImageView.ScaleType.FIT_CENTER } catch (e: Exception) { - loadStaticGIF(path, target, cropThumbnails, roundCorners, lastModified, skipMemoryCacheAtPaths) + loadStaticGIF(path, target, cropThumbnails, roundCorners, signature, skipMemoryCacheAtPaths) } catch (e: OutOfMemoryError) { - loadStaticGIF(path, target, cropThumbnails, roundCorners, lastModified, skipMemoryCacheAtPaths) + loadStaticGIF(path, target, cropThumbnails, roundCorners, signature, skipMemoryCacheAtPaths) } } else if (type == TYPE_SVGS) { - loadSVG(path, target, cropThumbnails, roundCorners, lastModified) + loadSVG(path, target, cropThumbnails, roundCorners, signature) } } @@ -449,9 +450,9 @@ fun Context.getPathLocation(path: String): Int { } } -fun Context.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, lastModified: Long, skipMemoryCacheAtPaths: ArrayList? = null) { +fun Context.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, signature: ObjectKey, skipMemoryCacheAtPaths: ArrayList? = null) { val options = RequestOptions() - .signature(path.getFileSignature(lastModified)) + .signature(signature) .skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true) .diskCacheStrategy(DiskCacheStrategy.RESOURCE) .priority(Priority.LOW) @@ -472,9 +473,9 @@ fun Context.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boo builder.into(target) } -fun Context.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, lastModified: Long, skipMemoryCacheAtPaths: ArrayList? = null) { +fun Context.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, signature: ObjectKey, skipMemoryCacheAtPaths: ArrayList? = null) { val options = RequestOptions() - .signature(path.getFileSignature(lastModified)) + .signature(signature) .skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true) .priority(Priority.LOW) .diskCacheStrategy(DiskCacheStrategy.RESOURCE) @@ -494,9 +495,9 @@ fun Context.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boo builder.into(target) } -fun Context.loadStaticGIF(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, lastModified: Long, skipMemoryCacheAtPaths: ArrayList? = null) { +fun Context.loadStaticGIF(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, signature: ObjectKey, skipMemoryCacheAtPaths: ArrayList? = null) { val options = RequestOptions() - .signature(path.getFileSignature(lastModified)) + .signature(signature) .skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true) .priority(Priority.LOW) .diskCacheStrategy(DiskCacheStrategy.RESOURCE) @@ -516,10 +517,10 @@ fun Context.loadStaticGIF(path: String, target: MySquareImageView, cropThumbnail builder.into(target) } -fun Context.loadSVG(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, lastModified: Long) { +fun Context.loadSVG(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, signature: ObjectKey) { target.scaleType = if (cropThumbnails) ImageView.ScaleType.CENTER_CROP else ImageView.ScaleType.FIT_CENTER - val options = RequestOptions().signature(path.getFileSignature(lastModified)) + val options = RequestOptions().signature(signature) var builder = Glide.with(applicationContext) .`as`(PictureDrawable::class.java) .listener(SvgSoftwareLayerSetter()) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt index 7114cfdcf..e80dd20dd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt @@ -410,7 +410,7 @@ class PhotoFragment : ViewPagerFragment() { private fun loadWithGlide(path: String, addZoomableView: Boolean) { val priority = if (mIsFragmentVisible) Priority.IMMEDIATE else Priority.NORMAL val options = RequestOptions() - .signature(getFilePathToShow().getFileSignature(mMedium.modified)) + .signature(mMedium.getKey()) .format(DecodeFormat.PREFER_ARGB_8888) .priority(priority) .diskCacheStrategy(DiskCacheStrategy.RESOURCE) @@ -450,7 +450,7 @@ class PhotoFragment : ViewPagerFragment() { val picasso = Picasso.get() .load(pathToLoad) .centerInside() - .stableKey(mMedium.path.getFileKey(mMedium.modified)) + .stableKey(mMedium.getSignature()) .resize(mScreenWidth, mScreenHeight) if (mCurrentRotationDegrees != 0) { @@ -617,7 +617,7 @@ class PhotoFragment : ViewPagerFragment() { val minTileDpi = if (showHighestQuality) -1 else getMinTileDpi() val bitmapDecoder = object : DecoderFactory { - override fun make() = MyGlideImageDecoder(rotation, mMedium.getSignature()) + override fun make() = MyGlideImageDecoder(rotation, mMedium.getKey()) } val regionDecoder = object : DecoderFactory { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Directory.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Directory.kt index 1be81a8bf..5f51940e6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Directory.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Directory.kt @@ -2,6 +2,7 @@ package com.simplemobiletools.gallery.pro.models import android.content.Context import androidx.room.* +import com.bumptech.glide.signature.ObjectKey import com.simplemobiletools.commons.extensions.formatDate import com.simplemobiletools.commons.extensions.formatSize import com.simplemobiletools.commons.helpers.* @@ -39,4 +40,6 @@ data class Directory( fun areFavorites() = path == FAVORITES fun isRecycleBin() = path == RECYCLE_BIN + + fun getKey() = ObjectKey("$path-$modified") } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Medium.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Medium.kt index d392bc1a1..0041b0e28 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Medium.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/models/Medium.kt @@ -15,6 +15,7 @@ import com.simplemobiletools.commons.helpers.SORT_BY_NAME import com.simplemobiletools.commons.helpers.SORT_BY_PATH import com.simplemobiletools.commons.helpers.SORT_BY_SIZE import com.simplemobiletools.gallery.pro.helpers.* +import java.io.File import java.io.Serializable import java.util.* @@ -91,5 +92,15 @@ data class Medium( return calendar.timeInMillis.toString() } - fun getSignature() = ObjectKey("$path-$modified-$size") + fun getSignature(): String { + val lastModified = if (modified > 1) { + modified + } else { + File(path).lastModified() + } + + return "$path-$lastModified-$size" + } + + fun getKey() = ObjectKey(getSignature()) }