From 102f4e3e88ea7b38ab2de23ded19930542e8a968 Mon Sep 17 00:00:00 2001 From: duguyihou Date: Sat, 31 Aug 2024 20:37:53 +1000 Subject: [PATCH] feat: add custom cacheKey (#331) --- .../main/java/com/turboimage/TurboImageView.kt | 1 + .../java/com/turboimage/TurboImageViewManager.kt | 5 +++++ example/ios/Podfile.lock | 4 ++-- example/src/screens/SuccessScreen.tsx | 2 +- ios/TurboImageView.swift | 15 ++++++++++----- src/types.ts | 1 + 6 files changed, 20 insertions(+), 8 deletions(-) diff --git a/android/src/main/java/com/turboimage/TurboImageView.kt b/android/src/main/java/com/turboimage/TurboImageView.kt index 7ecc19c..f865c9d 100644 --- a/android/src/main/java/com/turboimage/TurboImageView.kt +++ b/android/src/main/java/com/turboimage/TurboImageView.kt @@ -21,6 +21,7 @@ class TurboImageView(private val reactContext: ThemedReactContext) : AppCompatImageView(reactContext) { var uri: String? = null var headers: Headers? = null + var cacheKey: String? = null var cachePolicy: String? = "urlCache" var crossfade: Int? = null var blurhash: String? = null diff --git a/android/src/main/java/com/turboimage/TurboImageViewManager.kt b/android/src/main/java/com/turboimage/TurboImageViewManager.kt index c2c6a58..0baa4bb 100644 --- a/android/src/main/java/com/turboimage/TurboImageViewManager.kt +++ b/android/src/main/java/com/turboimage/TurboImageViewManager.kt @@ -61,6 +61,10 @@ class TurboImageViewManager : SimpleViewManager() { view.load(view.uri, imageLoader) { view.headers?.let { headers(it) } + view.cacheKey?.let { + memoryCacheKey(it) + diskCacheKey(it) + } view.allowHardware?.let { allowHardware(it) } listener(TurboImageListener(view)) view.format?.let { @@ -116,6 +120,7 @@ class TurboImageViewManager : SimpleViewManager() { val uri = source.toHashMap()["uri"] as? String view.uri = uri view.headers = source.toHashMap()["headers"] as? Headers + view.cacheKey = source.toHashMap()["cacheKey"] as? String } @ReactProp(name = "placeholder") diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 5f62b48..eaa0090 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -892,7 +892,7 @@ PODS: - React-debug - react-native-safe-area-context (4.9.0): - React-Core - - react-native-turbo-image (1.12.1): + - react-native-turbo-image (1.16.0): - APNGKit - Gifu - glog @@ -1279,7 +1279,7 @@ SPEC CHECKSUMS: React-logger: 3eb80a977f0d9669468ef641a5e1fabbc50a09ec React-Mapbuffer: 84ea43c6c6232049135b1550b8c60b2faac19fab react-native-safe-area-context: b97eb6f9e3b7f437806c2ce5983f479f8eb5de4b - react-native-turbo-image: 9c39bfdb0909c348f19fe8012c6cda5e20d1846e + react-native-turbo-image: 3948ca0aa3572181607c488df33f17e297921930 React-nativeconfig: b4d4e9901d4cabb57be63053fd2aa6086eb3c85f React-NativeModulesApple: cd26e56d56350e123da0c1e3e4c76cb58a05e1ee React-perflogger: 5f49905de275bac07ac7ea7f575a70611fa988f2 diff --git a/example/src/screens/SuccessScreen.tsx b/example/src/screens/SuccessScreen.tsx index 8d20f78..c6d2d1f 100644 --- a/example/src/screens/SuccessScreen.tsx +++ b/example/src/screens/SuccessScreen.tsx @@ -47,10 +47,10 @@ const SuccessScreen = () => { uri: 'https://placedog.net/100/100?id=121', }} style={{ width: 100, height: 100 }} + cachePolicy="dataCache" onStart={handleStart} onSuccess={handleSuccess} onCompletion={handleCompletion} - onFailure={({ nativeEvent }) => console.log(nativeEvent.error)} placeholder={{ blurhash: 'UBIr4u9}00Rj?yEzxu%LIQ%1%6xt-ks,tAIU', }} diff --git a/ios/TurboImageView.swift b/ios/TurboImageView.swift index b40f78c..463f036 100644 --- a/ios/TurboImageView.swift +++ b/ios/TurboImageView.swift @@ -24,7 +24,7 @@ final class TurboImageView : UIView { return composeProcessors() } - private var urlRequest: URLRequest? + private var imageRequest: ImageRequest? @objc var onStart: RCTDirectEventBlock? @objc var onFailure: RCTDirectEventBlock? @@ -40,9 +40,14 @@ final class TurboImageView : UIView { ]) return } - urlRequest = URLRequest(url: url) + var urlRequest = URLRequest(url: url) if let headers = source?.value(forKey: "headers") as? [String:String] { - urlRequest?.allHTTPHeaderFields = headers + urlRequest.allHTTPHeaderFields = headers + } + if let cacheKey = source?.value(forKey: "cacheKey") as? String { + imageRequest = ImageRequest(urlRequest: urlRequest, userInfo: [.imageIdKey: cacheKey]) + } else { + imageRequest = ImageRequest(urlRequest: urlRequest) } } } @@ -159,8 +164,8 @@ final class TurboImageView : UIView { super.didSetProps(changedProps) defer { - if let urlRequest { - lazyImageView.request = ImageRequest(urlRequest: urlRequest) + if let imageRequest { + lazyImageView.request = imageRequest } } diff --git a/src/types.ts b/src/types.ts index 74c3a54..a8c6016 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,6 +10,7 @@ import type { export type Source = { uri: string; headers?: HeadersInit_ | undefined; + cacheKey?: String | undefined; }; export type IndicatorStyle = 'large' | 'medium';