From 4503fb991b3a47e772e2864f391ec2bd0db99eb0 Mon Sep 17 00:00:00 2001 From: Zhiyuan Zheng Date: Thu, 5 May 2022 23:03:00 +0200 Subject: [PATCH] Android sharing working --- android/app/src/main/AndroidManifest.xml | 20 ++- ...@types+react-native-share-menu+5.0.2.patch | 12 +- src/Screens.tsx | 140 +++++++++++++----- 3 files changed, 132 insertions(+), 40 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 7a814b0f..46884301 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -31,12 +31,30 @@ + + + + + + + + + + + + + + - + + + + + diff --git a/patches/@types+react-native-share-menu+5.0.2.patch b/patches/@types+react-native-share-menu+5.0.2.patch index 997211e6..9d30b147 100644 --- a/patches/@types+react-native-share-menu+5.0.2.patch +++ b/patches/@types+react-native-share-menu+5.0.2.patch @@ -1,18 +1,22 @@ diff --git a/node_modules/@types/react-native-share-menu/index.d.ts b/node_modules/@types/react-native-share-menu/index.d.ts -index f52822c..b1d3bdd 100755 +index f52822c..ee98565 100755 --- a/node_modules/@types/react-native-share-menu/index.d.ts +++ b/node_modules/@types/react-native-share-menu/index.d.ts -@@ -6,9 +6,7 @@ +@@ -5,11 +5,9 @@ + // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // Minimum TypeScript Version: 3.7 - export interface ShareData { +-export interface ShareData { - mimeType: string; - data: string | string[]; - extraData?: object | undefined; +-} ++export type ShareData = { + data: {mimeType: string; data: string}[]; - } ++} | {mimeType: string; data: string | string[]} export type ShareCallback = (share?: ShareData) => void; + @@ -28,7 +26,7 @@ interface ShareMenuReactView { dismissExtension(error?: string): void; openApp(): void; diff --git a/src/Screens.tsx b/src/Screens.tsx index 9065ee5d..8420c867 100644 --- a/src/Screens.tsx +++ b/src/Screens.tsx @@ -161,61 +161,131 @@ const Screens: React.FC = ({ localCorrupt }) => { // Share Extension const handleShare = useCallback( - (item?: { data?: { mimeType: string; data: string }[] }) => { + ( + item?: + | { + data: { mimeType: string; data: string }[] + mimeType: undefined + } + | { data: string | string[]; mimeType: string } + ) => { if (instanceActive < 0) { return } - if (!item || !item.data || !Array.isArray(item.data) || !item.data) { + if (!item || !item.data) { return } let text: string | undefined = undefined let images: { type: string; uri: string }[] = [] let video: { type: string; uri: string } | undefined = undefined - item.data.forEach((d, i) => { - const typesImage = ['png', 'jpg', 'jpeg', 'gif'] - const typesVideo = ['mp4', 'm4v', 'mov', 'webm'] - const { mimeType, data } = d - console.log('mimeType', mimeType) - console.log('data', data) - if (mimeType.startsWith('image/')) { - if (!typesImage.includes(mimeType.split('/')[1])) { - console.warn('Image type not supported:', mimeType.split('/')[1]) + + switch (Platform.OS) { + case 'ios': + if (!Array.isArray(item.data) || !item.data) { return } - images.push({ type: mimeType.split('/')[1], uri: data }) - } else if (mimeType.startsWith('video/')) { - if (!typesVideo.includes(mimeType.split('/')[1])) { - console.warn('Video type not supported:', mimeType.split('/')[1]) + + item.data.forEach(d => { + if (typeof d === 'string') return + const typesImage = ['png', 'jpg', 'jpeg', 'gif'] + const typesVideo = ['mp4', 'm4v', 'mov', 'webm'] + const { mimeType, data } = d + if (mimeType.startsWith('image/')) { + if (!typesImage.includes(mimeType.split('/')[1])) { + console.warn( + 'Image type not supported:', + mimeType.split('/')[1] + ) + return + } + images.push({ type: mimeType.split('/')[1], uri: data }) + } else if (mimeType.startsWith('video/')) { + if (!typesVideo.includes(mimeType.split('/')[1])) { + console.warn( + 'Video type not supported:', + mimeType.split('/')[1] + ) + return + } + video = { type: mimeType.split('/')[1], uri: data } + } else { + if (typesImage.includes(data.split('.').pop() || '')) { + images.push({ type: data.split('.').pop()!, uri: data }) + return + } + if (typesVideo.includes(data.split('.').pop() || '')) { + video = { type: data.split('.').pop()!, uri: data } + return + } + text = !text ? data : text.concat(text, `\n${data}`) + } + }) + break + case 'android': + if (!item.mimeType) { return } - video = { type: mimeType.split('/')[1], uri: data } - } else { - if (typesImage.includes(data.split('.').pop() || '')) { - images.push({ type: data.split('.').pop()!, uri: data }) - return + let tempData: string[] + if (!Array.isArray(item.data)) { + tempData = [item.data] + } else { + tempData = item.data } - if (typesVideo.includes(data.split('.').pop() || '')) { - video = { type: data.split('.').pop()!, uri: data } - return - } - text = !text ? data : text.concat(text, `\n${data}`) - } - }) - navigationRef.navigate('Screen-Compose', { - type: 'share', - text, - images, - video - }) + tempData.forEach(d => { + const typesImage = ['png', 'jpg', 'jpeg', 'gif'] + const typesVideo = ['mp4', 'm4v', 'mov', 'webm', 'mpeg'] + if (item.mimeType!.startsWith('image/')) { + if (!typesImage.includes(item.mimeType.split('/')[1])) { + console.warn( + 'Image type not supported:', + item.mimeType.split('/')[1] + ) + return + } + images.push({ type: item.mimeType.split('/')[1], uri: d }) + } else if (item.mimeType.startsWith('video/')) { + if (!typesVideo.includes(item.mimeType.split('/')[1])) { + console.warn( + 'Video type not supported:', + item.mimeType.split('/')[1] + ) + return + } + video = { type: item.mimeType.split('/')[1], uri: d } + } else { + if (typesImage.includes(d.split('.').pop() || '')) { + images.push({ type: d.split('.').pop()!, uri: d }) + return + } + if (typesVideo.includes(d.split('.').pop() || '')) { + video = { type: d.split('.').pop()!, uri: d } + return + } + text = !text ? d : text.concat(text, `\n${d}`) + } + }) + break + } + + if (!text && (!images || !images.length) && !video) { + return + } else { + navigationRef.navigate('Screen-Compose', { + type: 'share', + text, + images, + video + }) + } }, [instanceActive] ) useEffect(() => { - ShareMenu.getInitialShare(item => handleShare(item)) + ShareMenu.getInitialShare(handleShare) }, []) useEffect(() => { - const listener = ShareMenu.addNewShareListener(item => handleShare(item)) + const listener = ShareMenu.addNewShareListener(handleShare) return () => { listener.remove() }