From 24a3373e536821725132b907bec2fca4a3bd7cde Mon Sep 17 00:00:00 2001 From: Mikhail Spitsin Date: Sun, 12 Jan 2020 00:13:51 +0400 Subject: [PATCH] #26: fixed some cache choosing and scaling mechanisms --- library/src/main/java/com/programmerr47/phroom/Phroom.kt | 9 +++++---- .../java/com/programmerr47/phroom/caching/MemoryCache.kt | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/library/src/main/java/com/programmerr47/phroom/Phroom.kt b/library/src/main/java/com/programmerr47/phroom/Phroom.kt index 7a89be3..8067106 100644 --- a/library/src/main/java/com/programmerr47/phroom/Phroom.kt +++ b/library/src/main/java/com/programmerr47/phroom/Phroom.kt @@ -15,7 +15,6 @@ import com.programmerr47.phroom.targets.LogTarget import com.programmerr47.phroom.targets.MainThreadTarget import com.programmerr47.phroom.targets.Target import com.programmerr47.phroom.targets.ViewTarget -import java.io.BufferedInputStream import java.io.File import java.io.InputStream import java.net.URL @@ -23,6 +22,7 @@ import java.util.WeakHashMap import java.util.concurrent.ExecutorService import java.util.concurrent.Executors import java.util.concurrent.Future +import kotlin.math.max import kotlin.math.min import kotlin.math.sqrt @@ -151,7 +151,7 @@ private class UrlTask( @WorkerThread private fun fetchUrl(spec: BitmapSpec) { runCatching { - diskCache.get(spec.url) ?: BitmapFactory.decodeStream(BufferedInputStream(URL(spec.url).content as InputStream)).also { + diskCache.get(spec.url) ?: BitmapFactory.decodeStream(URL(spec.url).content as InputStream).also { diskCache.put(spec.url, it) } } @@ -176,13 +176,14 @@ private class UrlTask( } @WorkerThread + //pick a scale factor with preserving the aspect ratio private fun scaleFactor(bitmap: Bitmap, spec: BitmapSpec): Float { if (bitmap.width <= spec.tWidth || bitmap.height <= spec.tHeight) return 1f - return min( + return min(1f, max( spec.tWidth.toFloat() / bitmap.width, spec.tHeight.toFloat() / bitmap.height - ) + )) } @WorkerThread diff --git a/library/src/main/java/com/programmerr47/phroom/caching/MemoryCache.kt b/library/src/main/java/com/programmerr47/phroom/caching/MemoryCache.kt index d2e69b7..994c95c 100644 --- a/library/src/main/java/com/programmerr47/phroom/caching/MemoryCache.kt +++ b/library/src/main/java/com/programmerr47/phroom/caching/MemoryCache.kt @@ -74,7 +74,7 @@ internal class LruMemoryCache( //thing is overkill right now return lru[it] to false } else { - if (it.url in originMap) { + if (originMap[it.url] == it) { return lru[it] to false }