Guest User

expo-share

a guest
Jan 31st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2. import {
  3.   View,
  4.   StyleSheet,
  5.   TouchableOpacity,
  6.   Text,
  7.   Image,
  8.   Share
  9. } from "react-native";
  10. import { Constants } from "expo";
  11. import { Constants as AppConstants } from "./common/Index";
  12. import {
  13.   StringHelper,
  14.   CustomizationHelper,
  15.   AnalyticsHelper
  16. } from "../helpers/Index";
  17. import { LogService } from "../services/Index";
  18.  
  19. const secondbrainAppsConfig = require("../../amplify/backend/function/sbapigetallitems/src/config");
  20.  
  21. class ShareButton extends React.PureComponent {
  22.   constructor(props) {
  23.     super(props);
  24.   }
  25.  
  26.   /*--------------------------------------------------
  27.     Render UI
  28.   ----------------------------------------------------*/
  29.   render() {
  30.     return (
  31.       <TouchableOpacity
  32.         onPress={this._onShare}
  33.         style={styles.container}
  34.         activeOpacity={0.6}
  35.       >
  36.         <View style={styles.subcontainer}>
  37.           <Image
  38.             source={CustomizationHelper.getIconForApp(
  39.               this.props.appKey,
  40.               CustomizationHelper.customizationElements.share
  41.             )}
  42.             style={styles.icon}
  43.             resizeMode="contain"
  44.           />
  45.           <Text style={styles.text}>Share</Text>
  46.         </View>
  47.       </TouchableOpacity>
  48.     );
  49.   }
  50.  
  51.   /*--------------------------------------------------
  52.     Helpers & Handlers
  53.   ----------------------------------------------------*/
  54.   _onShare = async () => {
  55.     try {
  56.       let item = this.props.currentItem.fields;
  57.       let title = "An excerpt I liked";
  58.       let url = item.image === undefined ? "" : item.image[0].url;
  59.       let message = this._getMessageForSharing(this.props.appKey, item);
  60.  
  61.       const result = await Share.share(
  62.         {
  63.           message: message,
  64.           title: title,
  65.           url: url
  66.         },
  67.         {
  68.           tintColor: Constants.manifest.tintColor,
  69.           excludedActivityTypes: [
  70.             "com.apple.UIKit.activity.Print",
  71.             "com.apple.UIKit.activity.AssignToContact",
  72.             "com.apple.UIKit.activity.AddToReadingList",
  73.             "com.apple.UIKit.activity.AirDrop",
  74.             "com.apple.UIKit.activity.OpenInIBooks",
  75.             "com.apple.UIKit.activity.MarkupAsPDF",
  76.             "com.apple.reminders.RemindersEditorExtension", //Reminders
  77.             "com.apple.mobilenotes.SharingExtension", // Notes
  78.             "com.apple.mobileslideshow.StreamShareService" // iCloud Photo Sharing - This also does nothing :{
  79.           ]
  80.         }
  81.       );
  82.  
  83.       AnalyticsHelper.trackEvent(
  84.         this.props.appKey,
  85.         AnalyticsHelper.eventEnum().share
  86.       );
  87.  
  88.       if (result.action === Share.sharedAction) {
  89.         if (result.activityType) {
  90.           // Shared with activity type of result.activityType
  91.         } else {
  92.           // Shared with no activity type
  93.         }
  94.       } else if (result.action === Share.dismissedAction) {
  95.         // Dismissed
  96.       }
  97.     } catch (error) {
  98.       LogService.log(error.message);
  99.     }
  100.   };
  101.  
  102.   _getAppStoreURI(appKey) {
  103.     const appConfig = secondbrainAppsConfig.find(app => {
  104.       return app.key === appKey ? true : false;
  105.     });
  106.  
  107.     return appConfig.appUri;
  108.   }
  109.  
  110.   _getMessageForSharing(appKey, item) {
  111.     let author = StringHelper.convertToCamelCase(item.author);
  112.     let body = StringHelper.convertToSentenceCase(item.extract);
  113.     let newLine = "\n" + "\n";
  114.     let endLine = "\n";
  115.  
  116.     let appURI = this._getAppStoreURI(this.props.appKey);
  117.     let hookCopy =
  118.       "Liked this? Download the app and get inspired with powerful, daily nudges: ";
  119.     let hook = appURI !== "" ? "-" + endLine + hookCopy + endLine + appURI : "";
  120.  
  121.     let message;
  122.     if (item.extract === undefined || item.extract === "-") {
  123.       message = ""; // Since the content is an image
  124.     } else {
  125.       message = author + ":" + newLine + body + newLine + hook;
  126.     }
  127.  
  128.     return message;
  129.   }
  130. }
  131.  
  132. /*---------------------------------------------------
  133.     Styles
  134. ----------------------------------------------------*/
  135. const styles = StyleSheet.create({
  136.   container: {
  137.     height: 50
  138.   },
  139.   subcontainer: {
  140.     flexDirection: "row"
  141.   },
  142.   icon: {
  143.     width: 13,
  144.     height: 13,
  145.     marginRight: 3,
  146.     top: 3,
  147.     left: -2
  148.   },
  149.   text: {
  150.     fontFamily: "overpass-light",
  151.     fontSize: 15,
  152.     color: AppConstants.baseColors.white
  153.   }
  154. });
  155.  
  156. /*---------------------------------------------------
  157.     Exports
  158. ----------------------------------------------------*/
  159. export { ShareButton };
Add Comment
Please, Sign In to add comment