Skip to content

Commit f867dde

Browse files
committed
feat: trick codegen to properly pass headers
1 parent bbc247c commit f867dde

File tree

4 files changed

+44
-31
lines changed

4 files changed

+44
-31
lines changed

android/src/main/java/com/dylanvann/fastimage/FastImageViewConverter.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import com.bumptech.glide.signature.ApplicationVersionSignature;
1818
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
1919
import com.facebook.react.bridge.NoSuchKeyException;
20+
import com.facebook.react.bridge.ReadableArray;
2021
import com.facebook.react.bridge.ReadableMap;
21-
import com.facebook.react.bridge.ReadableMapKeySetIterator;
2222

2323
import java.util.HashMap;
2424
import java.util.Map;
@@ -62,15 +62,11 @@ static Headers getHeaders(ReadableMap source) {
6262
Headers headers = Headers.DEFAULT;
6363

6464
if (source.hasKey("headers")) {
65-
ReadableMap headersMap = source.getMap("headers");
66-
ReadableMapKeySetIterator iterator = headersMap.keySetIterator();
6765
LazyHeaders.Builder builder = new LazyHeaders.Builder();
68-
69-
while (iterator.hasNextKey()) {
70-
String header = iterator.nextKey();
71-
String value = headersMap.getString(header);
72-
73-
builder.addHeader(header, value);
66+
ReadableArray headersArray = source.getArray("headers");
67+
for (int i = 0; i < headersArray.size(); i++) {
68+
ReadableMap header = headersArray.getMap(i);
69+
builder.addHeader(header.getString("name"), header.getString("value"));
7470
}
7571

7672
headers = builder.build();

ios/FastImage/FFFastImageViewComponentView.mm

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifdef RCT_NEW_ARCH_ENABLED
22

33
#import "FFFastImageViewComponentView.h"
4+
#import "RCTConvert+FFFastImage.h"
45
#import "FFFastImageView.h"
56

67
#import <React/RCTConversions.h>
@@ -35,46 +36,47 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
3536

3637
const auto &newViewProps = *std::static_pointer_cast<FastImageViewProps const>(props);
3738

38-
NSString *sourceStr = [NSString stringWithCString:newViewProps.source.uri.c_str() encoding:[NSString defaultCStringEncoding]];
39-
NSURL *imageUrl = [[NSURL alloc] initWithString:sourceStr];
40-
FFFastImageSource *imageSource = fastImageView.source;
41-
if(imageSource == NULL){
42-
imageSource = [[FFFastImageSource alloc] init];
43-
}
44-
imageSource.url = imageUrl;
39+
NSMutableDictionary *imageSourcePropsDict = [NSMutableDictionary new];
40+
imageSourcePropsDict[@"uri"] = RCTNSStringFromStringNilIfEmpty(newViewProps.source.uri);
41+
NSMutableDictionary* headers = [[NSMutableDictionary alloc] init];
42+
for (auto & element : newViewProps.source.headers) {
43+
[headers setValue:RCTNSStringFromString(element.value) forKey:RCTNSStringFromString(element.name)];
44+
}
45+
if (headers.count > 0) {
46+
imageSourcePropsDict[@"headers"] = headers;
47+
}
4548

46-
FFFCacheControl cacheControl;
49+
NSString *cacheControl;
4750
switch (newViewProps.source.cache) {
4851
case FastImageViewCache::Web:
49-
cacheControl = FFFCacheControl::FFFCacheControlWeb;
52+
cacheControl = @"web";
5053
break;
5154
case FastImageViewCache::CacheOnly:
52-
cacheControl = FFFCacheControl::FFFCacheControlCacheOnly;
55+
cacheControl = @"cacheOnly";
5356
break;
5457
case FastImageViewCache::Immutable:
5558
default:
56-
cacheControl = FFFCacheControl::FFFCacheControlImmutable;
59+
cacheControl = @"immutable";
5760
break;
5861
}
59-
imageSource.cacheControl = cacheControl;
62+
imageSourcePropsDict[@"cache"] = cacheControl;
6063

61-
FFFPriority priority;
64+
NSString *priority;
6265
switch (newViewProps.source.priority) {
6366
case FastImageViewPriority::Low:
64-
priority = FFFPriority::FFFPriorityLow;
67+
priority = @"low";
6568
break;
6669
case FastImageViewPriority::Normal:
67-
priority = FFFPriority::FFFPriorityNormal;
70+
priority = @"normal";
6871
break;
6972
case FastImageViewPriority::High:
7073
default:
71-
priority = FFFPriority::FFFPriorityHigh;
74+
priority = @"high";
7275
break;
7376
}
77+
imageSourcePropsDict[@"priority"] = priority;
78+
FFFastImageSource *imageSource = [RCTConvert FFFastImageSource:imageSourcePropsDict];
7479

75-
76-
imageSource.priority = priority;
77-
7880
[fastImageView setSource: imageSource];
7981

8082

@@ -99,6 +101,8 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
99101
fastImageView.imageColor = RCTUIColorFromSharedColor(newViewProps.tintColor);
100102

101103
[super updateProps:props oldProps:oldProps];
104+
// this method decides whether to reload the image so we call it after updating the props
105+
// It does not care about the changed props, but
102106
[fastImageView didSetProps:nil];
103107
}
104108

src/FastImageViewNativeComponent.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
2-
import type { ViewProps, ColorValue } from 'react-native';
2+
import type {
3+
ViewProps,
4+
ColorValue,
5+
} from 'react-native';
36
import type {
47
Float,
58
WithDefault,
69
BubblingEventHandler,
710
Int32,
811
} from 'react-native/Libraries/Types/CodegenTypes';
912

10-
type Headers = Readonly<{}>
13+
type Headers = ReadonlyArray<Readonly<{name: string, value: string}>>;
1114
type Priority = WithDefault< 'low' | 'normal' | 'high', 'normal'>
1215
type CacheControl = WithDefault< 'immutable' | 'web' | 'cacheOnly', 'web'>
1316

src/index.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
AccessibilityProps,
1515
ViewProps,
1616
ColorValue,
17+
ImageResolvedAssetSource,
1718
} from 'react-native'
1819
import FastImageView from './FastImageViewNativeComponent';
1920

@@ -194,7 +195,16 @@ function FastImageBase({
194195
)
195196
}
196197

197-
const resolvedSource = Image.resolveAssetSource(source as any)
198+
// this type differs based on the `source` prop passed
199+
const resolvedSource = Image.resolveAssetSource(source as any) as ImageResolvedAssetSource & {headers: any}
200+
if (resolvedSource?.headers) {
201+
// we do it like that to trick codegen
202+
const headersArray: {name: string, value: string}[] = [];
203+
Object.keys(resolvedSource.headers).forEach(key => {
204+
headersArray.push({name: key, value: resolvedSource.headers[key]});
205+
})
206+
resolvedSource.headers = headersArray;
207+
}
198208
const resolvedDefaultSource = resolveDefaultSource(defaultSource)
199209
const resolvedDefaultSourceAsString = resolvedDefaultSource !== null ? String(resolvedDefaultSource) : null;
200210

0 commit comments

Comments
 (0)