Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios): allow combine options to fb stories share #1115

Conversation

akinncar
Copy link
Contributor

@akinncar akinncar commented Oct 17, 2021

Overview

Allow option combinations for Facebook Stories on iOS.

Changes

  • Remove duplicated code inside functions and move logic to shareSingle function.
  • Allow option combinations, so the method option is not used anymore for Facebook Stories and can be considered as deprecated.

Test Plan

Sample Code
import React, {useCallback, useEffect, useRef, useState} from 'react';
import {AppState, StyleSheet, View, Button, Switch, Text} from 'react-native';

import RNFetchBlob from 'rn-fetch-blob';
import Share from 'react-native-share';

const App = () => {
const appState = useRef(AppState.currentState);
const [isLoading, setIsLoading] = useState(false);
const [includeImage, setIncludeImage] = useState(false);
const [includeVideo, setIncludeVideo] = useState(false);
const [includeSticker, setIncludeSticker] = useState(false);
const [includeBackgroundColor, setIncludeBackgroundColor] = useState(false);
const [cachePaths, setCachePaths] = useState([]);

const share = async () => {
  try {
    setIsLoading(true);
    const [cacheImage, cacheVideo, cacheSticker] = await Promise.all([
      RNFetchBlob.config({
        fileCache: true,
        appendExt: 'jpg',
      }).fetch('GET', 'https://via.placeholder.com/150.jpg/0000FF/808080'),
      RNFetchBlob.config({
        fileCache: true,
        appendExt: 'mp4',
      }).fetch('GET', 'https://www.w3schools.com/tags/movie.mp4'),
      RNFetchBlob.config({
        fileCache: true,
        appendExt: 'jpg',
      }).fetch('GET', 'https://via.placeholder.com/640x480.jpg'),
    ]);

    setCachePaths([
      cacheImage.path(),
      cacheVideo.path(),
      cacheSticker.path(),
    ]);

    const shareOptions = {
      backgroundImage: includeImage ? `file://${cacheImage.path()}` : null,
      backgroundVideo: includeVideo ? `file://${cacheVideo.path()}` : null,
      stickerImage: includeSticker ? `file://${cacheSticker.path()}` : null,
      backgroundTopColor: includeBackgroundColor ? '#cecec0' : null,
      backgroundBottomColor: includeBackgroundColor ? '#f0cccc' : null,
      attributionURL: 'https://github.com/akinncar',
      social: Share.Social.FACEBOOK_STORIES,
      appId: '355611832466432',
    };
    await Share.shareSingle(shareOptions);
  } catch (error) {
    console.error(error);
  } finally {
    setIsLoading(false);
  }
};

const removeTempFiles = useCallback(
  async (nextAppState) => {
    if (
      appState.current.match(/inactive|background/) &&
      nextAppState === 'active'
    ) {
      await Promise.all(
        cachePaths.map((path) => RNFetchBlob.fs.unlink(path)),
      );
      setCachePaths([]);
    }

    appState.current = nextAppState;
  },
  [cachePaths],
);

useEffect(() => {
  AppState.addEventListener('change', removeTempFiles);

  return () => {
    AppState.removeEventListener('change', removeTempFiles);
  };
}, [removeTempFiles]);

return (
  <View style={styles.container}>
    <View style={styles.switchContainer}>
      <Text style={styles.text}>With Image</Text>
      <Switch onValueChange={setIncludeImage} value={includeImage} />
    </View>
    <View style={styles.switchContainer}>
      <Text style={styles.text}>With Video</Text>
      <Switch onValueChange={setIncludeVideo} value={includeVideo} />
    </View>
    <View style={styles.switchContainer}>
      <Text style={styles.text}>With Sticker</Text>
      <Switch onValueChange={setIncludeSticker} value={includeSticker} />
    </View>
    <View style={styles.switchContainer}>
      <Text style={styles.text}>With Custom Background</Text>
      <Switch
        onValueChange={setIncludeBackgroundColor}
        value={includeBackgroundColor}
      />
    </View>
    <View style={styles.buttonContainer}>
      <Button disabled={isLoading} title="Share" onPress={share} />
    </View>
  </View>
);
};

const styles = StyleSheet.create({
container: {
  flex: 1,
  alignItems: 'center',
  justifyContent: 'center',
  backgroundColor: '#000',
},
text: {
  color: '#fff',
},
switchContainer: {
  flexDirection: 'row',
  justifyContent: 'space-between',
  marginBottom: 12,
  width: '70%',
},
buttonContainer: {
  width: '70%',
},
});

export default App;

Sample video

WhatsApp.Video.2021-10-17.at.12.50.42.AM.mp4

Related

Closes #1104

@akinncar akinncar changed the title feat: allow combine options to fb stories share feat: allow combine options to fb stories share (iOS) Oct 17, 2021
@akinncar akinncar changed the title feat: allow combine options to fb stories share (iOS) feat(ios): allow combine options to fb stories share Oct 17, 2021
example/App.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty amazing, I just had a question about the app id value

Copy link
Collaborator

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me, for what that's worth :-)

Copy link
Collaborator

@MateusAndrade MateusAndrade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow! This looks amazing, thanks a lot @akinncar! 🔥 🔥 🔥

@MateusAndrade MateusAndrade merged commit c21d80a into react-native-share:main Oct 18, 2021
MateusAndrade pushed a commit that referenced this pull request Oct 18, 2021
# [7.2.0](v7.1.2...v7.2.0) (2021-10-18)

### Features

* **ios:** allow combine options to fb stories share ([#1115](#1115)) ([c21d80a](c21d80a))
@MateusAndrade
Copy link
Collaborator

🎉 This PR is included in version 7.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@akinncar akinncar deleted the feat/fb-stories-combine-options-ios branch October 18, 2021 00:22
mobiledev7 added a commit to mobiledev7/react-native-share that referenced this pull request Dec 12, 2022
# [7.2.0](react-native-share/react-native-share@v7.1.2...v7.2.0) (2021-10-18)

### Features

* **ios:** allow combine options to fb stories share ([#1115](react-native-share/react-native-share#1115)) ([c21d80a](react-native-share/react-native-share@c21d80a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[iOS] Adding a backgroundVideo to Facebook stories
3 participants