forked from DylanVann/react-native-fast-image
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathFFFastImageViewComponentView.mm
More file actions
146 lines (125 loc) · 4.77 KB
/
FFFastImageViewComponentView.mm
File metadata and controls
146 lines (125 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifdef RCT_NEW_ARCH_ENABLED
#import "FFFastImageViewComponentView.h"
#import "RCTConvert+FFFastImage.h"
#import "FFFastImageView.h"
#import <React/RCTConversions.h>
#import <React/RCTFabricComponentsPlugins.h>
#import <react/renderer/components/rnfastimage/ComponentDescriptors.h>
#import <react/renderer/components/rnfastimage/Props.h>
#import <react/renderer/components/rnfastimage/EventEmitters.h>
using namespace facebook::react;
@implementation FFFastImageViewComponentView
{
FFFastImageView *fastImageView;
BOOL _shouldPostponeUpdate;
}
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<FastImageViewComponentDescriptor>();
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const FastImageViewProps>();
_props = defaultProps;
fastImageView = [[FFFastImageView alloc] initWithFrame:self.bounds];
self.contentView = fastImageView;
}
return self;
}
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
const auto &newViewProps = *std::static_pointer_cast<FastImageViewProps const>(props);
NSMutableDictionary *imageSourcePropsDict = [NSMutableDictionary new];
imageSourcePropsDict[@"uri"] = RCTNSStringFromStringNilIfEmpty(newViewProps.source.uri);
NSMutableDictionary* headers = [[NSMutableDictionary alloc] init];
for (auto & element : newViewProps.source.headers) {
[headers setValue:RCTNSStringFromString(element.value) forKey:RCTNSStringFromString(element.name)];
}
if (headers.count > 0) {
imageSourcePropsDict[@"headers"] = headers;
}
NSString *cacheControl;
switch (newViewProps.source.cache) {
case FastImageViewCache::Web:
cacheControl = @"web";
break;
case FastImageViewCache::CacheOnly:
cacheControl = @"cacheOnly";
break;
case FastImageViewCache::Immutable:
default:
cacheControl = @"immutable";
break;
}
imageSourcePropsDict[@"cache"] = cacheControl;
NSString *priority;
switch (newViewProps.source.priority) {
case FastImageViewPriority::Low:
priority = @"low";
break;
case FastImageViewPriority::Normal:
priority = @"normal";
break;
case FastImageViewPriority::High:
default:
priority = @"high";
break;
}
imageSourcePropsDict[@"priority"] = priority;
FFFastImageSource *imageSource = [RCTConvert FFFastImageSource:imageSourcePropsDict];
[fastImageView setSource: imageSource];
NSString *defaultSource = RCTNSStringFromStringNilIfEmpty(newViewProps.defaultSource);
[fastImageView setDefaultSource:defaultSource];
RCTResizeMode resizeMode;
switch (newViewProps.resizeMode) {
case FastImageViewResizeMode::Contain:
resizeMode = RCTResizeMode::RCTResizeModeContain;
break;
case FastImageViewResizeMode::Stretch:
resizeMode = RCTResizeMode::RCTResizeModeStretch;
break;
case FastImageViewResizeMode::Center:
resizeMode = RCTResizeMode::RCTResizeModeCenter;
break;
case FastImageViewResizeMode::Cover:
default:
resizeMode = RCTResizeMode::RCTResizeModeCover;
break;
}
[fastImageView setResizeMode:resizeMode];
fastImageView.imageColor = RCTUIColorFromSharedColor(newViewProps.tintColor);
[super updateProps:props oldProps:oldProps];
// this method decides whether to reload the image based on changed props
// so we call it after updating the props. If the _eventEmitter is not present yet,
// we postpone the update till it is set in `updateEventEmitter` since we want to send
// events to JS.
if (!_eventEmitter) {
_shouldPostponeUpdate = YES;
} else {
_shouldPostponeUpdate = NO;
[fastImageView didSetProps:nil];
}
}
- (void)updateEventEmitter:(const facebook::react::EventEmitter::Shared &)eventEmitter
{
[super updateEventEmitter:eventEmitter];
assert(std::dynamic_pointer_cast<FastImageViewEventEmitter const>(eventEmitter));
[fastImageView setEventEmitter:std::static_pointer_cast<FastImageViewEventEmitter const>(eventEmitter)];
if (_shouldPostponeUpdate) {
// we do the update here since it is the moment we can send events to JS
[fastImageView didSetProps:nil];
}
}
- (void)prepareForRecycle
{
[super prepareForRecycle];
fastImageView = [[FFFastImageView alloc] initWithFrame:self.bounds];
self.contentView = fastImageView;
}
@end
Class<RCTComponentViewProtocol> FastImageViewCls(void)
{
return FFFastImageViewComponentView.class;
}
#endif