mirror of
https://github.com/status-im/react-native-fast-image.git
synced 2025-02-23 03:38:16 +00:00
Make property names consistent and add example.
This commit is contained in:
parent
438fd11dc1
commit
8cafd670bf
@ -1,21 +1,27 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { StyleSheet, Text, TouchableOpacity } from 'react-native'
|
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native'
|
||||||
|
|
||||||
const Button = ({ text, onPress }) => (
|
const Button = ({ text, onPress }) => (
|
||||||
<TouchableOpacity onPress={onPress}>
|
<TouchableOpacity onPress={onPress}>
|
||||||
<Text style={styles.button}>{text}</Text>
|
<View style={styles.button}>
|
||||||
|
<Text style={styles.text}>{text}</Text>
|
||||||
|
</View>
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
)
|
)
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
const styles = StyleSheet.create({
|
||||||
button: {
|
button: {
|
||||||
backgroundColor: 'black',
|
backgroundColor: 'black',
|
||||||
color: 'white',
|
|
||||||
margin: 5,
|
margin: 5,
|
||||||
padding: 5,
|
height: 44,
|
||||||
paddingLeft: 10,
|
paddingLeft: 10,
|
||||||
paddingRight: 10,
|
paddingRight: 10,
|
||||||
borderRadius: 2,
|
borderRadius: 10,
|
||||||
|
alignItems: 'center',
|
||||||
|
justifyContent: 'center',
|
||||||
|
},
|
||||||
|
text: {
|
||||||
|
color: 'white',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -6,23 +6,32 @@ import Section from './Section'
|
|||||||
import FeatureText from './FeatureText'
|
import FeatureText from './FeatureText'
|
||||||
import uuid from 'uuid/v4'
|
import uuid from 'uuid/v4'
|
||||||
import Button from './Button'
|
import Button from './Button'
|
||||||
|
import { createImageProgress } from 'react-native-image-progress'
|
||||||
|
|
||||||
const IMAGE_URL =
|
const IMAGE_URL =
|
||||||
'https://cdn-images-1.medium.com/max/1600/1*-CY5bU4OqiJRox7G00sftw.gif'
|
'https://cdn-images-1.medium.com/max/1600/1*-CY5bU4OqiJRox7G00sftw.gif'
|
||||||
|
|
||||||
|
const Image = createImageProgress(FastImage)
|
||||||
|
|
||||||
class PreloadExample extends Component {
|
class PreloadExample extends Component {
|
||||||
state = {
|
state = {
|
||||||
show: false,
|
show: false,
|
||||||
url: IMAGE_URL,
|
url: IMAGE_URL,
|
||||||
}
|
}
|
||||||
|
|
||||||
bustAndPreload = () => {
|
bustCache = () => {
|
||||||
const key = uuid()
|
const key = uuid()
|
||||||
const bust = `?bust=${key}`
|
const bust = `?bust=${key}`
|
||||||
// Preload images. This can be called anywhere.
|
// Preload images. This can be called anywhere.
|
||||||
const url = IMAGE_URL + bust
|
const url = IMAGE_URL + bust
|
||||||
FastImage.preload([{ uri: url }])
|
this.setState({
|
||||||
this.setState({ url, show: false })
|
url,
|
||||||
|
show: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
preload = () => {
|
||||||
|
FastImage.preload([{ uri: this.state.url }])
|
||||||
}
|
}
|
||||||
|
|
||||||
showImage = () => {
|
showImage = () => {
|
||||||
@ -34,18 +43,25 @@ class PreloadExample extends Component {
|
|||||||
<View>
|
<View>
|
||||||
<Section>
|
<Section>
|
||||||
<FeatureText text="• Preloading." />
|
<FeatureText text="• Preloading." />
|
||||||
|
<FeatureText text="• Progress indication using react-native-image-progress." />
|
||||||
</Section>
|
</Section>
|
||||||
<SectionFlex style={styles.section} onPress={this.props.onPressReload}>
|
<SectionFlex style={styles.section} onPress={this.props.onPressReload}>
|
||||||
{this.state.show ? (
|
{this.state.show ? (
|
||||||
<FastImage style={styles.image} source={{ uri: this.state.url }} />
|
<Image style={styles.image} source={{ uri: this.state.url }} />
|
||||||
) : (
|
) : (
|
||||||
<View style={styles.image} />
|
<View style={styles.image} />
|
||||||
)}
|
)}
|
||||||
<Button
|
<View style={{ flexDirection: 'row', marginHorizontal: 10 }}>
|
||||||
text="Bust cache and preload."
|
<View style={{ flex: 1 }}>
|
||||||
onPress={this.bustAndPreload}
|
<Button text="Bust" onPress={this.bustCache} />
|
||||||
/>
|
</View>
|
||||||
<Button text="Render image." onPress={this.showImage} />
|
<View style={{ flex: 1 }}>
|
||||||
|
<Button text="Preload" onPress={this.preload} />
|
||||||
|
</View>
|
||||||
|
<View style={{ flex: 1 }}>
|
||||||
|
<Button text="Render" onPress={this.showImage} />
|
||||||
|
</View>
|
||||||
|
</View>
|
||||||
</SectionFlex>
|
</SectionFlex>
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
|
@ -9,7 +9,8 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"react": "16.0.0",
|
"react": "16.0.0",
|
||||||
"react-native": "0.50.4",
|
"react-native": "0.50.4",
|
||||||
"react-native-fast-image": "file:../react-native-fast-image-2.0.0.tgz",
|
"react-native-fast-image": "file:../react-native-fast-image-2.2.5.tgz",
|
||||||
|
"react-native-image-progress": "^1.1.0",
|
||||||
"react-native-vector-icons": "^4.4.2",
|
"react-native-vector-icons": "^4.4.2",
|
||||||
"react-navigation": "^1.0.0-beta.21",
|
"react-navigation": "^1.0.0-beta.21",
|
||||||
"react-timeout": "^1.0.1",
|
"react-timeout": "^1.0.1",
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
@implementation FFFastImageView {
|
@implementation FFFastImageView {
|
||||||
BOOL hasSentOnLoadStart;
|
BOOL hasSentOnLoadStart;
|
||||||
BOOL isComplete;
|
BOOL hasCompleted;
|
||||||
BOOL hasError;
|
BOOL hasErrored;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setResizeMode:(RCTResizeMode)resizeMode
|
- (void)setResizeMode:(RCTResizeMode)resizeMode
|
||||||
@ -16,18 +16,25 @@
|
|||||||
|
|
||||||
- (void)setOnFastImageLoadEnd:(RCTBubblingEventBlock)onFastImageLoadEnd {
|
- (void)setOnFastImageLoadEnd:(RCTBubblingEventBlock)onFastImageLoadEnd {
|
||||||
_onFastImageLoadEnd = onFastImageLoadEnd;
|
_onFastImageLoadEnd = onFastImageLoadEnd;
|
||||||
if (isComplete) {
|
if (hasCompleted) {
|
||||||
_onFastImageLoadEnd(@{});
|
_onFastImageLoadEnd(@{});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setOnFastImageLoad:(RCTBubblingEventBlock)onFastImageLoad {
|
- (void)setOnFastImageLoad:(RCTBubblingEventBlock)onFastImageLoad {
|
||||||
_onFastImageLoad = onFastImageLoad;
|
_onFastImageLoad = onFastImageLoad;
|
||||||
if (isComplete && hasError == NO) {
|
if (hasCompleted) {
|
||||||
_onFastImageLoad(@{});
|
_onFastImageLoad(@{});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)setOnFastImageError:(RCTDirectEventBlock)onFastImageError {
|
||||||
|
_onFastImageError = onFastImageError;
|
||||||
|
if (hasErrored) {
|
||||||
|
_onFastImageError(@{});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (void)setOnFastImageLoadStart:(RCTBubblingEventBlock)onFastImageLoadStart {
|
- (void)setOnFastImageLoadStart:(RCTBubblingEventBlock)onFastImageLoadStart {
|
||||||
if (_source && !hasSentOnLoadStart) {
|
if (_source && !hasSentOnLoadStart) {
|
||||||
_onFastImageLoadStart = onFastImageLoadStart;
|
_onFastImageLoadStart = onFastImageLoadStart;
|
||||||
@ -41,16 +48,13 @@
|
|||||||
|
|
||||||
- (void)setSource:(FFFastImageSource *)source {
|
- (void)setSource:(FFFastImageSource *)source {
|
||||||
if (_source != source) {
|
if (_source != source) {
|
||||||
isComplete = NO;
|
|
||||||
hasError = NO;
|
|
||||||
|
|
||||||
_source = source;
|
_source = source;
|
||||||
|
|
||||||
// Set headers.
|
// Set headers.
|
||||||
[_source.headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString* header, BOOL *stop) {
|
[_source.headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString* header, BOOL *stop) {
|
||||||
[[SDWebImageDownloader sharedDownloader] setValue:header forHTTPHeaderField:key];
|
[[SDWebImageDownloader sharedDownloader] setValue:header forHTTPHeaderField:key];
|
||||||
}];
|
}];
|
||||||
|
|
||||||
// Set priority.
|
// Set priority.
|
||||||
SDWebImageOptions options = 0;
|
SDWebImageOptions options = 0;
|
||||||
options |= SDWebImageRetryFailed;
|
options |= SDWebImageRetryFailed;
|
||||||
@ -65,33 +69,33 @@
|
|||||||
options |= SDWebImageHighPriority;
|
options |= SDWebImageHighPriority;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_onFastImageLoadStart) {
|
if (_onFastImageLoadStart) {
|
||||||
_onFastImageLoadStart(@{});
|
_onFastImageLoadStart(@{});
|
||||||
hasSentOnLoadStart = YES;
|
hasSentOnLoadStart = YES;
|
||||||
} {
|
} {
|
||||||
hasSentOnLoadStart = NO;
|
hasSentOnLoadStart = NO;
|
||||||
}
|
}
|
||||||
|
hasCompleted = NO;
|
||||||
|
hasErrored = NO;
|
||||||
|
|
||||||
// Load the new source.
|
// Load the new source.
|
||||||
[self sd_setImageWithURL:_source.uri
|
[self sd_setImageWithURL:_source.uri
|
||||||
placeholderImage:nil
|
placeholderImage:nil
|
||||||
options:options
|
options:options
|
||||||
progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
|
progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
|
||||||
double progress = MIN(1, MAX(0, (double) receivedSize / (double) expectedSize));
|
|
||||||
if (_onFastImageProgress) {
|
if (_onFastImageProgress) {
|
||||||
_onFastImageProgress(@{
|
_onFastImageProgress(@{
|
||||||
@"loaded": @(receivedSize),
|
@"loaded": @(receivedSize),
|
||||||
@"total": @(expectedSize)
|
@"total": @(expectedSize)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} completed:^(UIImage * _Nullable image,
|
} completed:^(UIImage * _Nullable image,
|
||||||
NSError * _Nullable error,
|
NSError * _Nullable error,
|
||||||
SDImageCacheType cacheType,
|
SDImageCacheType cacheType,
|
||||||
NSURL * _Nullable imageURL) {
|
NSURL * _Nullable imageURL) {
|
||||||
isComplete = YES;
|
|
||||||
if (error) {
|
if (error) {
|
||||||
hasError = YES;
|
hasErrored = YES;
|
||||||
if (_onFastImageError) {
|
if (_onFastImageError) {
|
||||||
_onFastImageError(@{});
|
_onFastImageError(@{});
|
||||||
if (_onFastImageLoadEnd) {
|
if (_onFastImageLoadEnd) {
|
||||||
@ -99,6 +103,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
hasCompleted = YES;
|
||||||
if (_onFastImageLoad) {
|
if (_onFastImageLoad) {
|
||||||
_onFastImageLoad(@{});
|
_onFastImageLoad(@{});
|
||||||
if (_onFastImageLoadEnd) {
|
if (_onFastImageLoadEnd) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user