Skip to content

Commit

Permalink
feat: add custom cacheKey (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
duguyihou committed Aug 31, 2024
1 parent 9f3a04a commit 102f4e3
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions android/src/main/java/com/turboimage/TurboImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions android/src/main/java/com/turboimage/TurboImageViewManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class TurboImageViewManager : SimpleViewManager<TurboImageView>() {

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 {
Expand Down Expand Up @@ -116,6 +120,7 @@ class TurboImageViewManager : SimpleViewManager<TurboImageView>() {
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")
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example/src/screens/SuccessScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ const SuccessScreen = () => {
uri: 'https://placedog.net/100/100?id=121',
}}
style={{ width: 100, height: 100 }}

Check warning on line 49 in example/src/screens/SuccessScreen.tsx

View workflow job for this annotation

GitHub Actions / lint

Inline 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',
}}
Expand Down
15 changes: 10 additions & 5 deletions ios/TurboImageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
export type Source = {
uri: string;
headers?: HeadersInit_ | undefined;
cacheKey?: String | undefined;
};

export type IndicatorStyle = 'large' | 'medium';
Expand Down

0 comments on commit 102f4e3

Please sign in to comment.