From a245dec56a2602b150c4d9569c3d15b041b277e6 Mon Sep 17 00:00:00 2001 From: Nouman Tahir Date: Sun, 25 Apr 2021 01:35:19 +0500 Subject: [PATCH] refined external sharing flow --- .../markdownEditor/view/markdownEditorView.js | 14 ++- src/redux/store/store.js | 2 +- .../container/applicationContainer.js | 4 +- .../editor/container/editorContainer.js | 108 ++++++------------ src/screens/editor/screen/editorScreen.js | 2 + 5 files changed, 49 insertions(+), 81 deletions(-) diff --git a/src/components/markdownEditor/view/markdownEditorView.js b/src/components/markdownEditor/view/markdownEditorView.js index a2cb3f834..f47414a14 100644 --- a/src/components/markdownEditor/view/markdownEditorView.js +++ b/src/components/markdownEditor/view/markdownEditorView.js @@ -68,6 +68,7 @@ const MarkdownEditorView = ({ getCommunity, currentAccount, autoFocusText, + sharedSnippetText, }) => { const [text, setText] = useState(draftBody || ''); const [selection, setSelection] = useState({ start: 0, end: 0 }); @@ -97,6 +98,12 @@ const MarkdownEditorView = ({ } }, [draftBody]); + useEffect(() => { + if (sharedSnippetText) { + _handleOnSnippetReceived(sharedSnippetText); + } + }, [sharedSnippetText]); + useEffect(() => { if (editable === null) { // workaround for android context menu issue @@ -208,7 +215,7 @@ const MarkdownEditorView = ({ ); - const _handleOnSnippetSelect = (snippetText) => { + const _handleOnSnippetReceived = (snippetText) => { applySnippet({ text, selection, @@ -398,7 +405,10 @@ const MarkdownEditorView = ({ animationType="slide" style={styles.modalStyle} > - + { + () => { navigate({ routeName: ROUTES.SCREENS.EDITOR, - params: { upload: files }, + params: { hasSharedIntent: true }, }); // files returns as JSON Array example //[{ filePath: null, text: null, weblink: null, mimeType: null, contentUri: null, fileName: null, extension: null }] diff --git a/src/screens/editor/container/editorContainer.js b/src/screens/editor/container/editorContainer.js index 0cfc5a974..c6df85806 100644 --- a/src/screens/editor/container/editorContainer.js +++ b/src/screens/editor/container/editorContainer.js @@ -8,6 +8,7 @@ import AsyncStorage from '@react-native-community/async-storage'; // Services and Actions import { Buffer } from 'buffer'; +import ReceiveSharingIntent from 'react-native-receive-sharing-intent'; import { uploadImage, addDraft, @@ -69,6 +70,7 @@ class EditorContainer extends Component { community: [], rewardType: 'default', beneficiaries: [], + sharedSnippetText: null, }; } @@ -81,9 +83,11 @@ class EditorContainer extends Component { let isEdit; let post; let _draft; + let hasSharedIntent = false; if (navigation.state && navigation.state.params) { const navigationParams = navigation.state.params; + hasSharedIntent = navigationParams.hasSharedIntent; if (navigationParams.draft) { _draft = navigationParams.draft; @@ -98,30 +102,6 @@ class EditorContainer extends Component { community: navigationParams.community, }); } - if (navigationParams.upload) { - const { upload } = navigationParams; - - upload.forEach((el) => { - if (el.filePath && el.fileName) { - // this.setState({ isUploading: true }); - const _media = { - path: el.filePath, - mime: el.mimeType, - filename: el.fileName || `img_${Math.random()}.jpg`, - }; - - this._uploadImage(_media, { shouldInsert: true }); - } else if (el.text) { - this.setState({ - draftPost: { - title: '', - body: el.text, - tags: [], - }, - }); - } - }); - } if (navigationParams.post) { ({ post } = navigationParams); @@ -154,11 +134,36 @@ class EditorContainer extends Component { } } - if (!isEdit && !_draft) { + if (!isEdit && !_draft && !hasSharedIntent) { this._fetchDraftsForComparison(isReply); } else { this._requestKeyboardFocus(); } + + ReceiveSharingIntent.getReceivedFiles( + (files) => { + files.forEach((el) => { + if (el.filePath && el.fileName) { + const _media = { + path: el.filePath, + mime: el.mimeType, + filename: el.fileName || `img_${Math.random()}.jpg`, + }; + + this._uploadImage(_media, { shouldInsert: true }); + } else if (el.text) { + this.setState({ + sharedSnippetText: el.text, + }); + } + }); + // To clear Intents + ReceiveSharingIntent.clearReceivedFiles(); + }, + (error) => { + console.log('error :>> ', error); + }, + ); } componentWillUnmount() { @@ -433,57 +438,6 @@ class EditorContainer extends Component { isUploading: false, }); } - - // uploadImage(media, currentAccount.name, sign).then((res) => { - // if (res.data && res.data.url) { - // res.data.hash = res.data.url.split('/').pop(); - // this.setState({ - // uploadedImage: res.data, - // isUploading: false, - // }); - // } - // }) - // .catch((error) => { - // console.log(error, error.message); - // if (error.toString().includes('code 413')) { - // Alert.alert( - // intl.formatMessage({ - // id: 'alert.fail', - // }), - // intl.formatMessage({ - // id: 'alert.payloadTooLarge', - // }), - // ); - // } else if (error.toString().includes('code 429')) { - // Alert.alert( - // intl.formatMessage({ - // id: 'alert.fail', - // }), - // intl.formatMessage({ - // id: 'alert.quotaExceeded', - // }), - // ); - // } else if (error.toString().includes('code 400')) { - // Alert.alert( - // intl.formatMessage({ - // id: 'alert.fail', - // }), - // intl.formatMessage({ - // id: 'alert.invalidImage', - // }), - // ); - // } else { - // Alert.alert( - // intl.formatMessage({ - // id: 'alert.fail', - // }), - // error.message || error.toString(), - // ); - // } - // this.setState({ - // isUploading: false, - // }); - // }); }; _handleMediaOnSelectFailure = (error) => { @@ -1100,6 +1054,7 @@ class EditorContainer extends Component { uploadedImage, community, isDraft, + sharedSnippetText, } = this.state; const tags = navigation.state.params && navigation.state.params.tags; @@ -1133,6 +1088,7 @@ class EditorContainer extends Component { community={community} currentAccount={currentAccount} isDraft={isDraft} + sharedSnippetText={sharedSnippetText} /> ); } diff --git a/src/screens/editor/screen/editorScreen.js b/src/screens/editor/screen/editorScreen.js index 915a315ae..327fddd3d 100644 --- a/src/screens/editor/screen/editorScreen.js +++ b/src/screens/editor/screen/editorScreen.js @@ -320,6 +320,7 @@ class EditorScreen extends Component { handleBeneficiaries, currentAccount, autoFocusText, + sharedSnippetText, } = this.props; const rightButtonText = intl.formatMessage({ id: isEdit ? 'basic_header.update' : isReply ? 'basic_header.reply' : 'basic_header.publish', @@ -396,6 +397,7 @@ class EditorScreen extends Component { onTitleChanged={this._handleChangeTitle} getCommunity={this._getCommunity} autoFocusText={autoFocusText} + sharedSnippetText={sharedSnippetText} />