Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ignoreQueryParamsForCacheKey image option (iOS & Android) #59

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ await clearCache();
| onError | function | | The function to call when an error occurs. The error is passed as the first argument of the function |
| onSuccess | function | | The function to call when the image is successfully loaded |
| grayscale | number | 0 | Filter or transformation that converts the image into shades of gray (0-1). |
| ignoreQueryParamsForCacheKey | boolean | false | Ignore URL query parameters in cache keys |
| allowHardware | boolean | true | Allow hardware rendering (Android only) |
| headers | Record<string, string> | undefined | Pass in headers |
| accessibilityLabel | string | undefined | accessibility label |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.graphics.RectF
import android.graphics.Path
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.util.Base64
import android.view.View
Expand All @@ -18,6 +19,7 @@ import android.widget.ImageView.ScaleType
import androidx.appcompat.widget.AppCompatImageView
import coil.annotation.ExperimentalCoilApi
import coil.imageLoader
import coil.memory.MemoryCache
import coil.request.CachePolicy
import coil.request.ImageRequest
import coil.size.Scale
Expand Down Expand Up @@ -93,7 +95,8 @@ import com.facebook.react.uimanager.events.RCTEventEmitter
val failureImage = options.getString("failureImage")
val grayscale = if (options.hasKey("grayscale")) options.getDouble("grayscale") else 0.0
val allowHardware = if (options.hasKey("allowHardware")) options.getBoolean("allowHardware") else true
val headers = options.getMap("headers")
val headers = options.getMap("headers")
val ignoreQueryParamsForCacheKey = if (options.hasKey("ignoreQueryParamsForCacheKey")) options.getBoolean("ignoreQueryParamsForCacheKey") else false

val borderRadii = BorderRadii(
uniform = if (options.hasKey("borderRadius")) options.getDouble("borderRadius") else 0.0,
Expand Down Expand Up @@ -127,6 +130,20 @@ import com.facebook.react.uimanager.events.RCTEventEmitter
)
} else {
requestBuilder = requestBuilder.data(it)
if (ignoreQueryParamsForCacheKey) {
val uri = Uri.parse(it)
val keyUri = uri.buildUpon().clearQuery().build()
val cacheKey = keyUri.toString()
val memoryCacheKey = MemoryCache.Key(cacheKey)
if (cachePolicy.equals("memory")) {
requestBuilder = requestBuilder
.memoryCacheKey(memoryCacheKey)
} else {
requestBuilder = requestBuilder
.memoryCacheKey(memoryCacheKey)
.diskCacheKey(cacheKey)
}
}
headers?.let {
for (entry in it.entryIterator) {
requestBuilder.setHeader(entry.key, entry.value as String)
Expand Down Expand Up @@ -232,20 +249,6 @@ import com.facebook.react.uimanager.events.RCTEventEmitter

private fun makeThumbHash(view: AppCompatImageView, hash: String): Drawable {
val thumbHash = ThumbHash.thumbHashToRGBA(Base64.decode(hash, Base64.DEFAULT))
val bitmap = Bitmap.createBitmap(thumbHash.width, thumbHash.height, Bitmap.Config.ARGB_8888)
bitmap.setPixels(toIntArray(thumbHash.rgba), 0, thumbHash.width, 0, 0, thumbHash.width, thumbHash.height)
return BitmapDrawable(view.context.resources, bitmap)
}

private fun makeBlurHash(view: AppCompatImageView, hash: String): Drawable {
val bitmap = BlurHashDecoder.decode(hash, 8, 8)
return BitmapDrawable(view.context.resources, bitmap)
}

private fun toIntArray(byteArray: ByteArray): IntArray {
val intArray = IntArray(byteArray.size)
for (i in byteArray.indices) {
intArray[i] = byteArray[i].toInt() and 0xFF
val intArray = IntArray(thumbHash.width * thumbHash.height)
for (i in intArray.indices) {
val r = thumbHash.rgba[i * 4].toInt() and 0xFF
Expand All @@ -259,6 +262,11 @@ import com.facebook.react.uimanager.events.RCTEventEmitter
return BitmapDrawable(view.context.resources, bitmap)
}

private fun makeBlurHash(view: AppCompatImageView, hash: String): Drawable {
val bitmap = BlurHashDecoder.decode(hash, 8, 8)
return BitmapDrawable(view.context.resources, bitmap)
}

companion object {
private val RESIZE_MODE = mapOf(
"contain" to ScaleType.FIT_CENTER,
Expand Down
13 changes: 12 additions & 1 deletion ios/FasterImageViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct ImageOptions: Decodable {
let url: String
let headers: [String: String]?
let grayscale: Double?
let ignoreQueryParamsForCacheKey: Bool?
}

struct BorderRadii {
Expand Down Expand Up @@ -134,12 +135,20 @@ final class FasterImageView: UIView {
}
progressiveLoadingEnabled = options.progressiveLoadingEnabled ?? false
grayscale = options.grayscale ?? 0.0
ignoreQueryParamsForCacheKey = options.ignoreQueryParamsForCacheKey ?? false

if let url = URL(string: options.url) {
var urlRequestFromOptions = URLRequest(url: url)
urlRequestFromOptions.allHTTPHeaderFields = options.headers

urlRequest = urlRequestFromOptions

if ignoreQueryParamsForCacheKey {
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)
components?.query = nil
var url = components?.url?.absoluteString ?? url.absoluteString
lazyImageView.request?.userInfo[.imageIdKey] = url
}
} else {
onError?([
"error": "Expected a valid url but got: \(options.url)",
Expand Down Expand Up @@ -270,6 +279,8 @@ final class FasterImageView: UIView {
}
}

var ignoreQueryParamsForCacheKey = false

// MARK: - Optional Properties

var base64Placeholder: String? {
Expand Down
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type AndroidImageResizeMode =
* @property {number} [borderBottomRightRadius] - Bottom right border radius of the image
* @property {number} [grayscale] - Grayscale value of the image, 0-1
* @property {boolean} [allowHardware] - Allow hardware rendering, defaults to true (Android only)
* @property {boolean} [ignoreQueryParamsForCacheKey] - Ignore query params for cache key, defaults to false
*/
export type ImageOptions = {
blurhash?: string;
Expand All @@ -70,6 +71,7 @@ export type ImageOptions = {
headers?: Record<string, string>;
grayscale?: number;
allowHardware?: boolean;
ignoreQueryParamsForCacheKey?: boolean;
};

/**
Expand Down
Loading