use a more advanced cache key by using size too

This commit is contained in:
tibbi 2021-01-14 18:50:32 +01:00
parent 60e0a68c1c
commit 440aea9eb8
7 changed files with 41 additions and 23 deletions

View file

@ -8,6 +8,7 @@ import android.graphics.drawable.ColorDrawable
import android.os.Bundle import android.os.Bundle
import android.widget.RelativeLayout import android.widget.RelativeLayout
import android.widget.RemoteViews import android.widget.RemoteViews
import com.bumptech.glide.signature.ObjectKey
import com.simplemobiletools.commons.dialogs.ColorPickerDialog import com.simplemobiletools.commons.dialogs.ColorPickerDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
@ -167,7 +168,8 @@ class WidgetConfigureActivity : SimpleActivity() {
val path = directoryDao.getDirectoryThumbnail(folderPath) val path = directoryDao.getDirectoryThumbnail(folderPath)
if (path != null) { if (path != null) {
runOnUiThread { 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)
} }
} }
} }

View file

@ -710,7 +710,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList<Directo
else -> ROUNDED_CORNERS_BIG else -> 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)) dir_pin.beVisibleIf(pinnedFolders.contains(directory.path))

View file

@ -540,8 +540,9 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
} }
val roundedCorners = if (isListViewType) ROUNDED_CORNERS_SMALL else ROUNDED_CORNERS_NONE val roundedCorners = if (isListViewType) ROUNDED_CORNERS_SMALL else ROUNDED_CORNERS_NONE
if (loadImageInstantly) { if (loadImageInstantly) {
activity.loadImage(medium.type, path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails, roundedCorners, medium.modified, activity.loadImage(medium.type, path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails, roundedCorners, medium.getKey(),
rotatedImagePaths) rotatedImagePaths)
} else { } else {
medium_thumbnail.setImageDrawable(null) medium_thumbnail.setImageDrawable(null)
@ -550,7 +551,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
val isVisible = visibleItemPaths.contains(medium.path) val isVisible = visibleItemPaths.contains(medium.path)
if (isVisible) { if (isVisible) {
activity.loadImage(medium.type, path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails, roundedCorners, activity.loadImage(medium.type, path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails, roundedCorners,
medium.modified, rotatedImagePaths) medium.getKey(), rotatedImagePaths)
} }
}, IMAGE_LOAD_DELAY) }, IMAGE_LOAD_DELAY)
} }

View file

@ -18,6 +18,7 @@ import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.signature.ObjectKey
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.R
@ -398,17 +399,17 @@ fun Context.getFolderNameFromPath(path: String): String {
} }
fun Context.loadImage(type: Int, path: String, target: MySquareImageView, horizontalScroll: Boolean, animateGifs: Boolean, cropThumbnails: Boolean, fun Context.loadImage(type: Int, path: String, target: MySquareImageView, horizontalScroll: Boolean, animateGifs: Boolean, cropThumbnails: Boolean,
roundCorners: Int, lastModified: Long, skipMemoryCacheAtPaths: ArrayList<String>? = null) { roundCorners: Int, signature: ObjectKey, skipMemoryCacheAtPaths: ArrayList<String>? = null) {
target.isHorizontalScrolling = horizontalScroll target.isHorizontalScrolling = horizontalScroll
if (type == TYPE_IMAGES || type == TYPE_VIDEOS || type == TYPE_RAWS || type == TYPE_PORTRAITS) { if (type == TYPE_IMAGES || type == TYPE_VIDEOS || type == TYPE_RAWS || type == TYPE_PORTRAITS) {
if (type == TYPE_IMAGES && path.isPng()) { if (type == TYPE_IMAGES && path.isPng()) {
loadPng(path, target, cropThumbnails, roundCorners, lastModified, skipMemoryCacheAtPaths) loadPng(path, target, cropThumbnails, roundCorners, signature, skipMemoryCacheAtPaths)
} else { } else {
loadJpg(path, target, cropThumbnails, roundCorners, lastModified, skipMemoryCacheAtPaths) loadJpg(path, target, cropThumbnails, roundCorners, signature, skipMemoryCacheAtPaths)
} }
} else if (type == TYPE_GIFS) { } else if (type == TYPE_GIFS) {
if (!animateGifs) { if (!animateGifs) {
loadStaticGIF(path, target, cropThumbnails, roundCorners, lastModified, skipMemoryCacheAtPaths) loadStaticGIF(path, target, cropThumbnails, roundCorners, signature, skipMemoryCacheAtPaths)
return 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 target.scaleType = if (cropThumbnails) ImageView.ScaleType.CENTER_CROP else ImageView.ScaleType.FIT_CENTER
} catch (e: Exception) { } catch (e: Exception) {
loadStaticGIF(path, target, cropThumbnails, roundCorners, lastModified, skipMemoryCacheAtPaths) loadStaticGIF(path, target, cropThumbnails, roundCorners, signature, skipMemoryCacheAtPaths)
} catch (e: OutOfMemoryError) { } catch (e: OutOfMemoryError) {
loadStaticGIF(path, target, cropThumbnails, roundCorners, lastModified, skipMemoryCacheAtPaths) loadStaticGIF(path, target, cropThumbnails, roundCorners, signature, skipMemoryCacheAtPaths)
} }
} else if (type == TYPE_SVGS) { } 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<String>? = null) { fun Context.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, signature: ObjectKey, skipMemoryCacheAtPaths: ArrayList<String>? = null) {
val options = RequestOptions() val options = RequestOptions()
.signature(path.getFileSignature(lastModified)) .signature(signature)
.skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true) .skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE) .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.priority(Priority.LOW) .priority(Priority.LOW)
@ -472,9 +473,9 @@ fun Context.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boo
builder.into(target) builder.into(target)
} }
fun Context.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, lastModified: Long, skipMemoryCacheAtPaths: ArrayList<String>? = null) { fun Context.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, signature: ObjectKey, skipMemoryCacheAtPaths: ArrayList<String>? = null) {
val options = RequestOptions() val options = RequestOptions()
.signature(path.getFileSignature(lastModified)) .signature(signature)
.skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true) .skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true)
.priority(Priority.LOW) .priority(Priority.LOW)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE) .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
@ -494,9 +495,9 @@ fun Context.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boo
builder.into(target) builder.into(target)
} }
fun Context.loadStaticGIF(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, lastModified: Long, skipMemoryCacheAtPaths: ArrayList<String>? = null) { fun Context.loadStaticGIF(path: String, target: MySquareImageView, cropThumbnails: Boolean, roundCorners: Int, signature: ObjectKey, skipMemoryCacheAtPaths: ArrayList<String>? = null) {
val options = RequestOptions() val options = RequestOptions()
.signature(path.getFileSignature(lastModified)) .signature(signature)
.skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true) .skipMemoryCache(skipMemoryCacheAtPaths?.contains(path) == true)
.priority(Priority.LOW) .priority(Priority.LOW)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE) .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
@ -516,10 +517,10 @@ fun Context.loadStaticGIF(path: String, target: MySquareImageView, cropThumbnail
builder.into(target) 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 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) var builder = Glide.with(applicationContext)
.`as`(PictureDrawable::class.java) .`as`(PictureDrawable::class.java)
.listener(SvgSoftwareLayerSetter()) .listener(SvgSoftwareLayerSetter())

View file

@ -410,7 +410,7 @@ class PhotoFragment : ViewPagerFragment() {
private fun loadWithGlide(path: String, addZoomableView: Boolean) { private fun loadWithGlide(path: String, addZoomableView: Boolean) {
val priority = if (mIsFragmentVisible) Priority.IMMEDIATE else Priority.NORMAL val priority = if (mIsFragmentVisible) Priority.IMMEDIATE else Priority.NORMAL
val options = RequestOptions() val options = RequestOptions()
.signature(getFilePathToShow().getFileSignature(mMedium.modified)) .signature(mMedium.getKey())
.format(DecodeFormat.PREFER_ARGB_8888) .format(DecodeFormat.PREFER_ARGB_8888)
.priority(priority) .priority(priority)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE) .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
@ -450,7 +450,7 @@ class PhotoFragment : ViewPagerFragment() {
val picasso = Picasso.get() val picasso = Picasso.get()
.load(pathToLoad) .load(pathToLoad)
.centerInside() .centerInside()
.stableKey(mMedium.path.getFileKey(mMedium.modified)) .stableKey(mMedium.getSignature())
.resize(mScreenWidth, mScreenHeight) .resize(mScreenWidth, mScreenHeight)
if (mCurrentRotationDegrees != 0) { if (mCurrentRotationDegrees != 0) {
@ -617,7 +617,7 @@ class PhotoFragment : ViewPagerFragment() {
val minTileDpi = if (showHighestQuality) -1 else getMinTileDpi() val minTileDpi = if (showHighestQuality) -1 else getMinTileDpi()
val bitmapDecoder = object : DecoderFactory<ImageDecoder> { val bitmapDecoder = object : DecoderFactory<ImageDecoder> {
override fun make() = MyGlideImageDecoder(rotation, mMedium.getSignature()) override fun make() = MyGlideImageDecoder(rotation, mMedium.getKey())
} }
val regionDecoder = object : DecoderFactory<ImageRegionDecoder> { val regionDecoder = object : DecoderFactory<ImageRegionDecoder> {

View file

@ -2,6 +2,7 @@ package com.simplemobiletools.gallery.pro.models
import android.content.Context import android.content.Context
import androidx.room.* import androidx.room.*
import com.bumptech.glide.signature.ObjectKey
import com.simplemobiletools.commons.extensions.formatDate import com.simplemobiletools.commons.extensions.formatDate
import com.simplemobiletools.commons.extensions.formatSize import com.simplemobiletools.commons.extensions.formatSize
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.*
@ -39,4 +40,6 @@ data class Directory(
fun areFavorites() = path == FAVORITES fun areFavorites() = path == FAVORITES
fun isRecycleBin() = path == RECYCLE_BIN fun isRecycleBin() = path == RECYCLE_BIN
fun getKey() = ObjectKey("$path-$modified")
} }

View file

@ -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_PATH
import com.simplemobiletools.commons.helpers.SORT_BY_SIZE import com.simplemobiletools.commons.helpers.SORT_BY_SIZE
import com.simplemobiletools.gallery.pro.helpers.* import com.simplemobiletools.gallery.pro.helpers.*
import java.io.File
import java.io.Serializable import java.io.Serializable
import java.util.* import java.util.*
@ -91,5 +92,15 @@ data class Medium(
return calendar.timeInMillis.toString() 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())
} }