forked from kfiroo/react-native-cached-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CachedImage.js
232 lines (197 loc) · 7.12 KB
/
CachedImage.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
'use strict';
const _ = require('lodash');
const React = require('react');
const ReactNative = require('react-native');
const PropTypes = require('prop-types');
const ImageCacheManagerOptionsPropTypes = require('./ImageCacheManagerOptionsPropTypes');
const flattenStyle = ReactNative.StyleSheet.flatten;
const ImageCacheManager = require('./ImageCacheManager');
const {
View,
ImageBackground,
ActivityIndicator,
NetInfo,
Platform,
StyleSheet,
} = ReactNative;
const styles = StyleSheet.create({
image: {
backgroundColor: 'transparent'
},
loader: {
backgroundColor: 'transparent',
},
loaderPlaceholder: {
backgroundColor: 'transparent',
alignItems: 'center',
justifyContent: 'center'
}
});
function getImageProps(props) {
return _.omit(props, ['source', 'defaultSource', 'fallbackSource', 'LoadingIndicator', 'activityIndicatorProps', 'style', 'useQueryParamsInCacheKey', 'renderImage', 'resolveHeaders']);
}
const CACHED_IMAGE_REF = 'cachedImage';
class CachedImage extends React.Component {
static propTypes = {
renderImage: PropTypes.func.isRequired,
activityIndicatorProps: PropTypes.object.isRequired,
// ImageCacheManager options
...ImageCacheManagerOptionsPropTypes,
};
static defaultProps = {
renderImage: props => (<ImageBackground imageStyle={props.style} ref={CACHED_IMAGE_REF} {...props} />),
activityIndicatorProps: {},
};
static contextTypes = {
getImageCacheManager: PropTypes.func,
};
constructor(props) {
super(props);
this._isMounted = false;
this.state = {
isCacheable: true,
cachedImagePath: null,
networkAvailable: true
};
this.getImageCacheManagerOptions = this.getImageCacheManagerOptions.bind(this);
this.getImageCacheManager = this.getImageCacheManager.bind(this);
this.safeSetState = this.safeSetState.bind(this);
this.handleConnectivityChange = this.handleConnectivityChange.bind(this);
this.processSource = this.processSource.bind(this);
this.renderLoader = this.renderLoader.bind(this);
}
componentWillMount() {
this._isMounted = true;
NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectivityChange);
// initial
NetInfo.isConnected.fetch()
.then(isConnected => {
this.safeSetState({
networkAvailable: isConnected
});
});
this.processSource(this.props.source);
}
componentWillUnmount() {
this._isMounted = false;
NetInfo.isConnected.removeEventListener('connectionChange', this.handleConnectivityChange);
}
componentWillReceiveProps(nextProps) {
if (!_.isEqual(this.props.source, nextProps.source)) {
this.processSource(nextProps.source);
}
}
setNativeProps(nativeProps) {
try {
this.refs[CACHED_IMAGE_REF].setNativeProps(nativeProps);
} catch (e) {
console.error(e);
}
}
getImageCacheManagerOptions() {
return _.pick(this.props, _.keys(ImageCacheManagerOptionsPropTypes));
}
getImageCacheManager() {
// try to get ImageCacheManager from context
if (this.context && this.context.getImageCacheManager) {
return this.context.getImageCacheManager();
}
// create a new one if context is not available
const options = this.getImageCacheManagerOptions();
return ImageCacheManager(options);
}
safeSetState(newState) {
if (!this._isMounted) {
return;
}
return this.setState(newState);
}
handleConnectivityChange(isConnected) {
this.safeSetState({
networkAvailable: isConnected
});
}
processSource(source) {
const url = _.get(source, ['uri'], null);
const options = this.getImageCacheManagerOptions();
const imageCacheManager = this.getImageCacheManager();
imageCacheManager.downloadAndCacheUrl(url, options)
.then(cachedImagePath => {
this.safeSetState({
cachedImagePath
});
})
.catch(err => {
// console.warn(err);
this.safeSetState({
cachedImagePath: null,
isCacheable: false
});
});
}
render() {
if (this.state.isCacheable && !this.state.cachedImagePath) {
return this.renderLoader();
}
const props = getImageProps(this.props);
const style = this.props.style || styles.image;
const source = (this.state.isCacheable && this.state.cachedImagePath) ? {
uri: 'file://' + this.state.cachedImagePath
} : this.props.source;
if (this.props.fallbackSource && !this.state.cachedImagePath) {
return this.props.renderImage({
...props,
key: `${props.key || source.uri}error`,
style,
source: this.props.fallbackSource
});
}
return this.props.renderImage({
...props,
key: props.key || source.uri,
style,
source
});
}
renderLoader() {
const imageProps = getImageProps(this.props);
const imageStyle = [this.props.style, styles.loaderPlaceholder];
const activityIndicatorProps = _.omit(this.props.activityIndicatorProps, ['style']);
const activityIndicatorStyle = this.props.activityIndicatorProps.style || styles.loader;
const LoadingIndicator = this.props.loadingIndicator;
const source = this.props.defaultSource;
// if the imageStyle has borderRadius it will break the loading image view on android
// so we only show the ActivityIndicator
if (!source || (Platform.OS === 'android' && flattenStyle(imageStyle).borderRadius)) {
if (LoadingIndicator) {
return (
<View style={[imageStyle, activityIndicatorStyle]}>
<LoadingIndicator {...activityIndicatorProps} />
</View>
);
}
return (
<ActivityIndicator
{...activityIndicatorProps}
style={[imageStyle, activityIndicatorStyle]}/>
);
}
// otherwise render an image with the defaultSource with the ActivityIndicator on top of it
return this.props.renderImage({
...imageProps,
style: imageStyle,
key: source.uri,
source,
children: (
LoadingIndicator
? <View style={[imageStyle, activityIndicatorStyle]}>
<LoadingIndicator {...activityIndicatorProps} />
</View>
: <ActivityIndicator
{...activityIndicatorProps}
style={activityIndicatorStyle}/>
)
});
}
}
module.exports = CachedImage;