Skip to content

Commit

Permalink
#26: fixed some cache choosing and scaling mechanisms
Browse files Browse the repository at this point in the history
  • Loading branch information
programmerr47 committed Jan 11, 2020
1 parent ed9062c commit 24a3373
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions library/src/main/java/com/programmerr47/phroom/Phroom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ 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
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

Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 24a3373

Please sign in to comment.