Skip to content

Commit

Permalink
feat: indicate failed image request, resolves #27
Browse files Browse the repository at this point in the history
  • Loading branch information
just4fun committed May 13, 2018
1 parent 5fec35d commit b60049d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
37 changes: 26 additions & 11 deletions src/components/ProgressImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react';
import {
View,
Image,
Text,
ActivityIndicator,
TouchableHighlight
} from 'react-native';
Expand All @@ -25,7 +26,8 @@ export default class ProgressImage extends Component {
super(props);

this.state = {
isLoading: false
isLoading: false,
isLoadError: false
};
}

Expand Down Expand Up @@ -70,22 +72,35 @@ export default class ProgressImage extends Component {

render() {
let { style, thumbUri, originalUri } = this.props;
let { isLoading } = this.state;
let { isLoading, isLoadError } = this.state;

return (
<TouchableHighlight
underlayColor={colors.underlay}
onPress={() => SafariView.show(originalUri)}>
<View style={[styles.image, style]}>
<Image
source={{ uri: thumbUri }}
defaultSource={require('../images/image_default.png')}
onLoadStart={() => this.setState({ isLoading: true })}
onLoadEnd={() => this.setState({ isLoading: false })}
resizeMode={'contain'}
// onLayout={event => this.handleLayout(event)}
style={[styles.image, style]} />
{isLoading && <ActivityIndicator style={styles.indicator} />}
{isLoadError &&
<View>
<Image
defaultSource={require('../images/image_fail.png')}
resizeMode={'contain'}
style={[styles.image, style]} />
<Text style={styles.text}>图片加载失败或图片已失效</Text>
</View>
||
<View>
<Image
source={{ uri: thumbUri }}
defaultSource={require('../images/image_default.png')}
onLoadStart={() => this.setState({ isLoading: true })}
onLoadEnd={() => this.setState({ isLoading: false })}
onError={() => this.setState({ isLoadError: true })}
resizeMode={'contain'}
// onLayout={event => this.handleLayout(event)}
style={[styles.image, style]} />
{isLoading && <ActivityIndicator style={styles.indicator} />}
</View>
}
</View>
</TouchableHighlight>
);
Expand Down
Binary file added src/images/image_fail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 10 additions & 2 deletions src/styles/components/_ProgressImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
StyleSheet,
Dimensions
} from 'react-native';
import colors from '../common/_colors';

const window = Dimensions.get('window');
const IMAGE_HEIGHT = 250;
Expand All @@ -13,7 +14,14 @@ export default StyleSheet.create({
indicator: {
position: 'absolute',
top: IMAGE_HEIGHT / 2,
// `20` is width for ActivityIndicator.
left: window.width / 2 - 20
// window.width / 2 - (width of ActivityIndicator / 2 + margin of image)
left: window.width / 2 - (20 / 2 + 10)
},
text: {
position: 'absolute',
top: IMAGE_HEIGHT / 2 + 60,
// window.width / 2 - (width of `图片加载失败或图片已失效` / 2 + margin of image)
left: window.width / 2 - (168 / 2 + 10),
color: colors.mainField
}
});

0 comments on commit b60049d

Please sign in to comment.