From 3a33bdaa27b0d68fc2ee692b18d7f527e0f342f5 Mon Sep 17 00:00:00 2001 From: Dylan Vann Date: Mon, 29 Jan 2018 01:42:29 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=98=20Add=20border=20radius=20support?= =?UTF-8?q?=20to=20android=20and=20refactor=20and=20update=20examples.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastImage.js | 55 ++++-- example/App.js | 4 +- .../BorderRadiusAndChildrenExample.js | 49 +++++ example/fastImage/Button.js | 22 +++ example/fastImage/FastImageExample.js | 168 ------------------ example/fastImage/FastImageExamples.js | 76 ++++++++ example/fastImage/FeatureText.js | 10 ++ example/fastImage/GifExample.js | 33 ++++ example/fastImage/PreloadExample.js | 68 +++++++ example/fastImage/PriorityExample.js | 59 ++++++ example/fastImage/ProgressExample.js | 49 +++++ example/fastImage/Section.js | 13 ++ example/fastImage/SectionFlex.js | 20 +++ example/fastImage/withCacheBust.js | 28 +++ 14 files changed, 474 insertions(+), 180 deletions(-) create mode 100644 example/fastImage/BorderRadiusAndChildrenExample.js create mode 100644 example/fastImage/Button.js delete mode 100644 example/fastImage/FastImageExample.js create mode 100644 example/fastImage/FastImageExamples.js create mode 100644 example/fastImage/FeatureText.js create mode 100644 example/fastImage/GifExample.js create mode 100644 example/fastImage/PreloadExample.js create mode 100644 example/fastImage/PriorityExample.js create mode 100644 example/fastImage/ProgressExample.js create mode 100644 example/fastImage/Section.js create mode 100644 example/fastImage/SectionFlex.js create mode 100644 example/fastImage/withCacheBust.js diff --git a/FastImage.js b/FastImage.js index 1e2c5cb30..cc8cf587a 100644 --- a/FastImage.js +++ b/FastImage.js @@ -1,10 +1,12 @@ import React, { Component } from 'react' import PropTypes from 'prop-types' import { + View, Image, NativeModules, requireNativeComponent, ViewPropTypes, + StyleSheet, } from 'react-native' const resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource') @@ -24,6 +26,9 @@ class FastImage extends Component { onLoad, onError, onLoadEnd, + style, + children, + borderRadius, ...props } = this.props @@ -44,21 +49,51 @@ class FastImage extends Component { } const resolvedSource = resolveAssetSource(source) + + if (!children && !borderRadius) { + return ( + (this._root = e)} + {...props} + style={style} + source={resolvedSource} + onFastImageLoadStart={onLoadStart} + onFastImageProgress={onProgress} + onFastImageLoad={onLoad} + onFastImageError={onError} + onFastImageLoadEnd={onLoadEnd} + /> + ) + } + return ( - (this._root = e)} - {...props} - source={resolvedSource} - onFastImageLoadStart={onLoadStart} - onFastImageProgress={onProgress} - onFastImageLoad={onLoad} - onFastImageError={onError} - onFastImageLoadEnd={onLoadEnd} - /> + + + (this._root = e)} + {...props} + style={StyleSheet.absoluteFill} + source={resolvedSource} + onFastImageLoadStart={onLoadStart} + onFastImageProgress={onProgress} + onFastImageLoad={onLoad} + onFastImageError={onError} + onFastImageLoadEnd={onLoadEnd} + /> + + {children} + ) } } +const styles = StyleSheet.create({ + imageContainer: { + ...StyleSheet.absoluteFillObject, + overflow: 'hidden', + }, +}) + FastImage.resizeMode = { contain: 'contain', cover: 'cover', diff --git a/example/App.js b/example/App.js index ce3a8d5ed..532e0e735 100644 --- a/example/App.js +++ b/example/App.js @@ -1,13 +1,13 @@ import React from 'react' import { TabNavigator, TabBarBottom } from 'react-navigation' -import FastImageExample from './fastImage/FastImageExample' +import FastImageExamples from './fastImage/FastImageExamples' import FastImageGrid from './fastImage/FastImageGrid' import DefaultImageGrid from './fastImage/DefaultImageGrid' const App = TabNavigator( { fastImageExample: { - screen: FastImageExample, + screen: FastImageExamples, }, image: { screen: DefaultImageGrid, diff --git a/example/fastImage/BorderRadiusAndChildrenExample.js b/example/fastImage/BorderRadiusAndChildrenExample.js new file mode 100644 index 000000000..364287c71 --- /dev/null +++ b/example/fastImage/BorderRadiusAndChildrenExample.js @@ -0,0 +1,49 @@ +import React from 'react' +import { StyleSheet, View } from 'react-native' +import withCacheBust from './withCacheBust' +import SectionFlex from './SectionFlex' +import FastImage from 'react-native-fast-image' +import Section from './Section' +import FeatureText from './FeatureText' + +const IMAGE_URL = 'https://media.giphy.com/media/GEsoqZDGVoisw/giphy.gif' +const PLUS_IMAGE_URL = + 'https://cdn3.iconfinder.com/data/icons/block/32/add-512.png' + +const BorderRadiusAndChildrenExample = ({ onPressReload, bust }) => ( + +
+ +
+ + + + + +
+) + +const styles = StyleSheet.create({ + image: { + height: 100, + backgroundColor: '#ddd', + margin: 20, + width: 100, + flex: 0, + }, + plus: { + width: 30, + height: 30, + position: 'absolute', + bottom: 0, + right: 0, + }, +}) + +export default withCacheBust(BorderRadiusAndChildrenExample) diff --git a/example/fastImage/Button.js b/example/fastImage/Button.js new file mode 100644 index 000000000..e63459bcb --- /dev/null +++ b/example/fastImage/Button.js @@ -0,0 +1,22 @@ +import React from 'react' +import { Text, TouchableOpacity, StyleSheet } from 'react-native' + +const Button = ({ text, onPress }) => ( + + {text} + +) + +const styles = StyleSheet.create({ + button: { + backgroundColor: 'black', + color: 'white', + margin: 5, + padding: 5, + paddingLeft: 10, + paddingRight: 10, + borderRadius: 2, + }, +}) + +export default Button diff --git a/example/fastImage/FastImageExample.js b/example/fastImage/FastImageExample.js deleted file mode 100644 index 638bb2d91..000000000 --- a/example/fastImage/FastImageExample.js +++ /dev/null @@ -1,168 +0,0 @@ -import React, { Component } from 'react' -import { - Button, - PixelRatio, - ScrollView, - StatusBar, - StyleSheet, - Text, - View, -} from 'react-native' -import Icon from 'react-native-vector-icons/Ionicons' -import FastImage from 'react-native-fast-image' -import uuid from 'uuid/v4' - -const getImageUrl = (id, width, height) => - `https://source.unsplash.com/${id}/${width}x${height}` - -const IMAGE_SIZE = 150 -const IMAGE_SIZE_PX = PixelRatio.getPixelSizeForLayoutSize(IMAGE_SIZE) - -// The server is used to test that sending headers is working correctly. -const USE_SERVER = false -const TOKEN = 'someToken' - -const getImages = () => { - if (USE_SERVER) { - const baseUrl = '192.168.2.11' - return [ - `http://${baseUrl}:8080/pictures/ahmed-saffu-235616.jpg`, - `http://${baseUrl}:8080/pictures/alex-bertha-236361.jpg`, - `http://${baseUrl}:8080/pictures/jaromir-kavan-233699.jpg`, - ] - } - return [ - getImageUrl('x58soEovG_M', IMAGE_SIZE_PX, IMAGE_SIZE_PX), - getImageUrl('yPI7myL5eWY', IMAGE_SIZE_PX, IMAGE_SIZE_PX), - 'https://cdn-images-1.medium.com/max/1600/1*-CY5bU4OqiJRox7G00sftw.gif', - ] -} - -const getTestProgressCallbacks = label => ({ - onLoadStart: () => console.log(`${label} - onLoadStart`), - onProgress: e => - console.log( - `${label} - onProgress - ${e.nativeEvent.loaded / e.nativeEvent.total}`, - ), - onLoad: () => console.log(`${label} - onLoad`), - onError: () => console.log(`${label} - onError`), - onLoadEnd: () => console.log(`${label} - onLoadEnd`), -}) - -const images = getImages() - -// Preload images. This can be called anywhere. -FastImage.preload([ - { - uri: 'https://facebook.github.io/react/img/logo_og.png', - headers: { Authorization: 'someAuthToken' }, - }, - { - uri: 'https://facebook.github.io/react/img/logo_og.png', - headers: { Authorization: 'someAuthToken' }, - }, -]) - -class FastImageExample extends Component { - state = { bust: '?bust' } - - onPressedReload = () => { - // Force complete re-render and bust image cache. - const key = uuid() - const bust = `?bust=${key}` - this.setState({ bust }) - } - - render() { - return ( - - - - - FastImage - • priority (low, normal, high) - • authentication (token) -