From f8d39f61af813a9db6e283e1c9b4056dfd56573e Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 13 Jan 2019 21:16:20 +0100 Subject: [PATCH] improve cache clearing at rotating a file --- .../gallery/pro/extensions/Activity.kt | 34 ++++++++++--------- .../gallery/pro/extensions/String.kt | 6 ++-- .../gallery/pro/fragments/PhotoFragment.kt | 1 + 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt index d07d0f197..3ef475fe5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt @@ -27,6 +27,7 @@ import com.simplemobiletools.gallery.pro.dialogs.PickDirectoryDialog import com.simplemobiletools.gallery.pro.helpers.NOMEDIA import com.simplemobiletools.gallery.pro.helpers.RECYCLE_BIN import com.simplemobiletools.gallery.pro.interfaces.MediumDao +import com.squareup.picasso.Picasso import java.io.File import java.io.FileOutputStream import java.io.InputStream @@ -406,22 +407,11 @@ fun BaseSimpleActivity.saveRotatedImageToFile(oldPath: String, newPath: String, copyFile(tmpPath, newPath) scanPathRecursively(newPath) toast(R.string.file_saved) - - if (config.keepLastModified) { - File(newPath).setLastModified(oldLastModified) - updateLastModified(newPath, oldLastModified) - } + fileRotatedSuccessfully(newPath, oldLastModified) it.flush() it.close() callback.invoke() - - // we cannot refresh a specific image in Glide Cache, so just clear it all - val glide = Glide.get(applicationContext) - glide.clearDiskCache() - runOnUiThread { - glide.clearMemory() - } } } catch (e: OutOfMemoryError) { toast(R.string.out_of_memory_error) @@ -438,10 +428,7 @@ fun Activity.tryRotateByExif(path: String, degrees: Int, callback: () -> Unit): val file = File(path) val oldLastModified = file.lastModified() if (saveImageRotation(path, degrees)) { - if (config.keepLastModified) { - file.setLastModified(oldLastModified) - updateLastModified(path, oldLastModified) - } + fileRotatedSuccessfully(path, oldLastModified) callback.invoke() toast(R.string.file_saved) true @@ -454,6 +441,21 @@ fun Activity.tryRotateByExif(path: String, degrees: Int, callback: () -> Unit): } } +fun Activity.fileRotatedSuccessfully(path: String, lastModified: Long) { + if (config.keepLastModified) { + File(path).setLastModified(lastModified) + updateLastModified(path, lastModified) + } + + Picasso.get().invalidate(path.getFileKey()) + // we cannot refresh a specific image in Glide Cache, so just clear it all + val glide = Glide.get(applicationContext) + glide.clearDiskCache() + runOnUiThread { + glide.clearMemory() + } +} + fun BaseSimpleActivity.copyFile(source: String, destination: String) { var inputStream: InputStream? = null var out: OutputStream? = null diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt index ee08503bf..556f2c2f0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/String.kt @@ -6,9 +6,11 @@ import com.simplemobiletools.commons.helpers.OTG_PATH import java.io.File import java.io.IOException -fun String.getFileSignature(): ObjectKey { +fun String.getFileSignature() = ObjectKey(getFileKey()) + +fun String.getFileKey(): String { val file = File(this) - return ObjectKey("${file.absolutePath}${file.lastModified()}") + return "${file.absolutePath}${file.lastModified()}" } fun String.isThisOrParentIncluded(includedPaths: MutableSet) = includedPaths.any { startsWith(it, true) } 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 4e35d0604..867c54936 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 @@ -336,6 +336,7 @@ class PhotoFragment : ViewPagerFragment() { val picasso = Picasso.get() .load(pathToLoad) .centerInside() + .stableKey(mMedium.path.getFileKey()) .resize(mScreenWidth, mScreenHeight) if (degrees != 0) {