Skip to content

Commit c984ff6

Browse files
authored
Merge pull request #53 from Priccc/master
Add custom activityIndicator and placeholder image
2 parents 578a19f + 74a08d6 commit c984ff6

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

CachedImage.js

+26-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const flattenStyle = ReactNative.StyleSheet.flatten;
77
const ImageCacheProvider = require('./ImageCacheProvider');
88

99
const {
10+
View,
1011
Image,
1112
ActivityIndicator,
1213
NetInfo,
@@ -33,7 +34,7 @@ const styles = StyleSheet.create({
3334
});
3435

3536
function getImageProps(props) {
36-
return _.omit(props, ['source', 'defaultSource', 'activityIndicatorProps', 'style', 'useQueryParamsInCacheKey', 'renderImage', 'resolveHeaders']);
37+
return _.omit(props, ['source', 'defaultSource', 'fallbackSource', 'LoadingIndicator', 'activityIndicatorProps', 'style', 'useQueryParamsInCacheKey', 'renderImage', 'resolveHeaders']);
3738
}
3839

3940
const CACHED_IMAGE_REF = 'cachedImage';
@@ -151,6 +152,14 @@ const CachedImage = React.createClass({
151152
const source = (this.state.isCacheable && this.state.cachedImagePath) ? {
152153
uri: 'file://' + this.state.cachedImagePath
153154
} : this.props.source;
155+
if (this.props.fallbackSource && !this.state.cachedImagePath) {
156+
return this.props.renderImage({
157+
...props,
158+
key: `${props.key || source.uri}error`,
159+
style,
160+
source: this.props.fallbackSource
161+
});
162+
}
154163
return this.props.renderImage({
155164
...props,
156165
key: props.key || source.uri,
@@ -166,11 +175,20 @@ const CachedImage = React.createClass({
166175
const activityIndicatorProps = _.omit(this.props.activityIndicatorProps, ['style']);
167176
const activityIndicatorStyle = this.props.activityIndicatorProps.style || styles.loader;
168177

178+
const LoadingIndicator = this.props.loadingIndicator;
179+
169180
const source = this.props.defaultSource;
170181

171182
// if the imageStyle has borderRadius it will break the loading image view on android
172183
// so we only show the ActivityIndicator
173184
if (!source || (Platform.OS === 'android' && flattenStyle(imageStyle).borderRadius)) {
185+
if (LoadingIndicator) {
186+
return (
187+
<View style={[imageStyle, activityIndicatorStyle]}>
188+
<LoadingIndicator {...activityIndicatorProps} />
189+
</View>
190+
);
191+
}
174192
return (
175193
<ActivityIndicator
176194
{...activityIndicatorProps}
@@ -184,9 +202,13 @@ const CachedImage = React.createClass({
184202
key: source.uri,
185203
source,
186204
children: (
187-
<ActivityIndicator
188-
{...activityIndicatorProps}
189-
style={activityIndicatorStyle}/>
205+
LoadingIndicator
206+
? <View style={[imageStyle, activityIndicatorStyle]}>
207+
<LoadingIndicator {...activityIndicatorProps} />
208+
</View>
209+
: <ActivityIndicator
210+
{...activityIndicatorProps}
211+
style={activityIndicatorStyle}/>
190212
)
191213
});
192214
}

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ We use [`react-native-fetch-blob`](https://github.com/wkh237/react-native-fetch-
1717
_You should only have to do this once._
1818

1919
react-native link react-native-fetch-blob
20-
20+
2121
Or, if you want to add Android permissions to AndroidManifest.xml automatically, use this one:
2222

2323
RNFB_ANDROID_PERMISSIONS=true react-native link react-native-fetch-blob
@@ -50,6 +50,8 @@ When providing `source={{uri: 'https://example.com/path/to/remote/image.jpg'}}`
5050
* `useQueryParamsInCacheKey` - _array|bool_ an array of keys to use from the `source.uri` query string or a bool value stating whether to use the entire query string or not. **(default: false)**
5151
* `defaultSource` - prop to display a background image while the source image is downloaded. This will work even in android, but will not display background image if there you set borderRadius on this component style prop
5252
* `resolveHeaders` - _function_ when provided, the returned object will be used as the headers object when sending the request to download the image. **(default: () => Promise.resolve({}))**
53+
* `loadingIndicator` - _component_ prop to set custom `ActivityIndicator`.
54+
* `fallbackSource` - prop to set placeholder image. when `source.uri` is null or cached failed, the `fallbackSource` will be display.
5355

5456
### ImageCacheProvider
5557
`ImageCacheProvider` exposes interaction with the cache layer that is used by `CachedImage` so you can use it to prefetch some urls in the background while you app is starting,

0 commit comments

Comments
 (0)