Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image loading activity indicator #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Prop | Description | Type | Default
`scrollViewStyle` | Custom style for the `FlatList` component | `object` | `{}`
`onSingleTapConfirmed` | Fired after a single tap | `function`
`onLongPress` | Fired after a long press | `function`
`imageLoadingIndicatorProps` | Props to be passed to the underlying image `ActivityIndicator` component | `object` | React Native `ActivityIndicator` defaults

## Scroll state and events

Expand Down
6 changes: 4 additions & 2 deletions src/Gallery.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { PureComponent } from 'react';
import { View, ViewPropTypes } from 'react-native';
import { View, ViewPropTypes, ActivityIndicator } from 'react-native';
import PropTypes from 'prop-types';
import { createResponder } from './libraries/GestureResponder';
import TransformableImage from './libraries/TransformableImage';
Expand All @@ -13,6 +13,7 @@ export default class Gallery extends PureComponent {
static propTypes = {
...View.propTypes,
images: PropTypes.arrayOf(PropTypes.object),
imageLoadingIndicatorProps: PropTypes.shape(ActivityIndicator.propTypes),
initialPage: PropTypes.number,
scrollViewStyle: ViewPropTypes ? ViewPropTypes.style : View.propTypes.style,
pageMargin: PropTypes.number,
Expand Down Expand Up @@ -225,7 +226,7 @@ export default class Gallery extends PureComponent {
}

renderPage (pageData, pageId) {
const { onViewTransformed, onTransformGestureReleased, errorComponent, imageComponent } = this.props;
const { onViewTransformed, onTransformGestureReleased, errorComponent, imageComponent, imageLoadingIndicatorProps } = this.props;
return (
<TransformableImage
onViewTransformed={((transform) => {
Expand All @@ -240,6 +241,7 @@ export default class Gallery extends PureComponent {
errorComponent={errorComponent}
imageComponent={imageComponent}
image={pageData}
imageLoadingIndicatorProps={imageLoadingIndicatorProps}
/>
);
}
Expand Down
22 changes: 22 additions & 0 deletions src/libraries/ActivityIndicator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import {View, ActivityIndicator} from 'react-native';

const ActivityIndicatorComponent = (props) => {
return (
<View style={{
alignItems: 'center',
justifyContent: 'center',
position: 'absolute',
top: 0,
right: 0,
bottom: 0,
left: 0
}}>
<ActivityIndicator {...props} />
</View>
);
};

ActivityIndicatorComponent.propTypes = ActivityIndicator.propTypes;

export default ActivityIndicatorComponent;
41 changes: 23 additions & 18 deletions src/libraries/TransformableImage/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { PureComponent } from 'react';
import { View, Text, Image, ViewPropTypes } from 'react-native';
import { View, Text, Image, ViewPropTypes, ActivityIndicator } from 'react-native';
import PropTypes from 'prop-types';
import ViewTransformer from '../ViewTransformer';
import ImageLoadingIndicator from '../ActivityIndicator';

export default class TransformableImage extends PureComponent {
static propTypes = {
Expand All @@ -22,7 +23,8 @@ export default class TransformableImage extends PureComponent {
onViewTransformed: PropTypes.func,
imageComponent: PropTypes.func,
resizeMode: PropTypes.string,
errorComponent: PropTypes.func
errorComponent: PropTypes.func,
imageLoadingIndicatorProps: PropTypes.shape(ActivityIndicator.propTypes),
};

static defaultProps = {
Expand Down Expand Up @@ -142,7 +144,7 @@ export default class TransformableImage extends PureComponent {

render () {
const { imageDimensions, viewWidth, viewHeight, error, keyAccumulator, imageLoaded } = this.state;
const { style, image, imageComponent, resizeMode, enableTransform, enableScale, enableTranslate, onTransformGestureReleased, onViewTransformed } = this.props;
const { style, image, imageComponent, resizeMode, enableTransform, enableScale, enableTranslate, onTransformGestureReleased, onViewTransformed, imageLoadingIndicatorProps } = this.props;

let maxScale = 1;
let contentAspectRatio;
Expand Down Expand Up @@ -175,21 +177,24 @@ export default class TransformableImage extends PureComponent {
const content = imageComponent ? imageComponent(imageProps, imageDimensions) : <Image { ...imageProps } />;

return (
<ViewTransformer
ref={'viewTransformer'}
key={'viewTransformer#' + keyAccumulator} // when image source changes, we should use a different node to avoid reusing previous transform state
enableTransform={enableTransform && imageLoaded} // disable transform until image is loaded
enableScale={enableScale}
enableTranslate={enableTranslate}
enableResistance={true}
onTransformGestureReleased={onTransformGestureReleased}
onViewTransformed={onViewTransformed}
maxScale={maxScale}
contentAspectRatio={contentAspectRatio}
onLayout={this.onLayout}
style={style}>
{ error ? this.renderError() : content }
</ViewTransformer>
<View>
<ViewTransformer
ref={'viewTransformer'}
key={'viewTransformer#' + keyAccumulator} // when image source changes, we should use a different node to avoid reusing previous transform state
enableTransform={enableTransform && imageLoaded} // disable transform until image is loaded
enableScale={enableScale}
enableTranslate={enableTranslate}
enableResistance={true}
onTransformGestureReleased={onTransformGestureReleased}
onViewTransformed={onViewTransformed}
maxScale={maxScale}
contentAspectRatio={contentAspectRatio}
onLayout={this.onLayout}
style={style}>
{ error ? this.renderError() : content }
</ViewTransformer>
{!imageLoaded && <ImageLoadingIndicator {...imageLoadingIndicatorProps} />}
</View>
);
}
}
Expand Down