|
| 1 | +/** |
| 2 | +* Copyright (c) 2015-present, Facebook, Inc. All rights reserved. |
| 3 | +* |
| 4 | +* You are hereby granted a non-exclusive, worldwide, royalty-free license to use, |
| 5 | +* copy, modify, and distribute this software in source code or binary form for use |
| 6 | +* in connection with the web services and APIs provided by Facebook. |
| 7 | +* |
| 8 | +* As with any software that integrates with the Facebook platform, your use of |
| 9 | +* this software is subject to the Facebook Developer Principles and Policies |
| 10 | +* [http://developers.facebook.com/policy/]. This copyright notice shall be |
| 11 | +* included in all copies or substantial portions of the software. |
| 12 | +* |
| 13 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 15 | +* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 16 | +* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 17 | +* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 18 | +* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 | +*/ |
| 20 | + |
| 21 | +'use strict'; |
| 22 | + |
| 23 | +var React = require('react-native'); |
| 24 | +var { |
| 25 | + StyleSheet, |
| 26 | + Image, |
| 27 | + Text, |
| 28 | + View, |
| 29 | + ScrollView, |
| 30 | + PixelRatio, |
| 31 | + TouchableHighlight, |
| 32 | +} = React; |
| 33 | + |
| 34 | +var FBSDKCore = require('react-native-fbsdkcore'); |
| 35 | +var { |
| 36 | + FBSDKGraphRequest, |
| 37 | +} = FBSDKCore; |
| 38 | + |
| 39 | +var FBSDKShare = require('react-native-fbsdkshare'); |
| 40 | +var { |
| 41 | + FBSDKShareDialog, |
| 42 | + FBSDKShareLinkContent, |
| 43 | +} = FBSDKShare; |
| 44 | + |
| 45 | +var ImageWidth = 150; |
| 46 | + |
| 47 | +class Feed extends React.Component { |
| 48 | + constructor() { |
| 49 | + super(); |
| 50 | + |
| 51 | + // Make a graph request to get photos from the New Horizons Facebook page. |
| 52 | + var feedRequest = new FBSDKGraphRequest( |
| 53 | + this._handleRequest.bind(this), |
| 54 | + '/495164637280739/photos', |
| 55 | + { |
| 56 | + type: { string: 'uploaded' }, |
| 57 | + fields: { string: 'images' } |
| 58 | + } |
| 59 | + ); |
| 60 | + feedRequest.start(); |
| 61 | + |
| 62 | + this.state = { |
| 63 | + photos: [], |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + render() { |
| 68 | + return ( |
| 69 | + <ScrollView style={this.props.style}> |
| 70 | + {this.state.photos} |
| 71 | + </ScrollView> |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + _renderPhoto(photo) { |
| 76 | + var height = ImageWidth * (photo.height / photo.width); |
| 77 | + return ( |
| 78 | + <View style={styles.imageBox}> |
| 79 | + <Image source={{uri: photo.source}} style={{ |
| 80 | + width: ImageWidth * PixelRatio.get(), |
| 81 | + height: height * PixelRatio.get(), |
| 82 | + }} |
| 83 | + /> |
| 84 | + <TouchableHighlight |
| 85 | + style={[styles.shareButton, { width: ImageWidth * PixelRatio.get(), height: 25 * PixelRatio.get()}]} |
| 86 | + onPress={() => this._sharePhoto.bind(this)(photo)}> |
| 87 | + <Text style={styles.shareText}>share</Text> |
| 88 | + </TouchableHighlight> |
| 89 | + </View> |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Shares a photo to Facebook using the native share dialog. |
| 95 | + */ |
| 96 | + _sharePhoto(photo) { |
| 97 | + // Build up a shareable link to the photo. |
| 98 | + var linkContent = new FBSDKShareLinkContent(photo.source, 'A picture from New Horizons.', 'New Horizons', photo.source); |
| 99 | + // Share the link using the native share dialog. |
| 100 | + FBSDKShareDialog.show(linkContent, (error, result) => { |
| 101 | + if (!error) { |
| 102 | + if (result.isCancelled) { |
| 103 | + alert('So sad, you canceled. :('); |
| 104 | + } else { |
| 105 | + alert('You shared Pluto!'); |
| 106 | + } |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Handles the response from the graph request for New Horizons pictures. |
| 113 | + */ |
| 114 | + _handleRequest(error, result) { |
| 115 | + if (!error) { |
| 116 | + var photos = result.data; |
| 117 | + var renderedPhotos = []; |
| 118 | + for (var i = 0, il = photos.length; i < il; i++) { |
| 119 | + var photo = photos[i]; |
| 120 | + if (photo.images && photo.images.length > 0) { |
| 121 | + renderedPhotos.push(this._renderPhoto(photo.images[0])); |
| 122 | + } |
| 123 | + } |
| 124 | + this.setState({ photos: renderedPhotos }); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +var styles = StyleSheet.create(require('./styles.js')); |
| 130 | + |
| 131 | +module.exports = Feed; |
0 commit comments