Android sharing working

This commit is contained in:
Zhiyuan Zheng 2022-05-05 23:03:00 +02:00
parent 66301b177c
commit 4503fb991b
3 changed files with 132 additions and 40 deletions

View File

@ -31,12 +31,30 @@
<data android:scheme="tooot"/> <data android:scheme="tooot"/>
<data android:scheme="com.xmflsct.app.tooot"/> <data android:scheme="com.xmflsct.app.tooot"/>
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter> <intent-filter>
<action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" /> <data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" /> <data android:mimeType="image/*" />
<!-- Any other mime types you want to support --> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="video/*"/>
</intent-filter> </intent-filter>
</activity> </activity>
</application> </application>

View File

@ -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 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 --- a/node_modules/@types/react-native-share-menu/index.d.ts
+++ b/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 // Minimum TypeScript Version: 3.7
export interface ShareData { -export interface ShareData {
- mimeType: string; - mimeType: string;
- data: string | string[]; - data: string | string[];
- extraData?: object | undefined; - extraData?: object | undefined;
-}
+export type ShareData = {
+ data: {mimeType: string; data: string}[]; + data: {mimeType: string; data: string}[];
} +} | {mimeType: string; data: string | string[]}
export type ShareCallback = (share?: ShareData) => void; export type ShareCallback = (share?: ShareData) => void;
@@ -28,7 +26,7 @@ interface ShareMenuReactView { @@ -28,7 +26,7 @@ interface ShareMenuReactView {
dismissExtension(error?: string): void; dismissExtension(error?: string): void;
openApp(): void; openApp(): void;

View File

@ -161,61 +161,131 @@ const Screens: React.FC<Props> = ({ localCorrupt }) => {
// Share Extension // Share Extension
const handleShare = useCallback( 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) { if (instanceActive < 0) {
return return
} }
if (!item || !item.data || !Array.isArray(item.data) || !item.data) { if (!item || !item.data) {
return return
} }
let text: string | undefined = undefined let text: string | undefined = undefined
let images: { type: string; uri: string }[] = [] let images: { type: string; uri: string }[] = []
let video: { type: string; uri: string } | undefined = undefined let video: { type: string; uri: string } | undefined = undefined
item.data.forEach((d, i) => {
const typesImage = ['png', 'jpg', 'jpeg', 'gif'] switch (Platform.OS) {
const typesVideo = ['mp4', 'm4v', 'mov', 'webm'] case 'ios':
const { mimeType, data } = d if (!Array.isArray(item.data) || !item.data) {
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])
return return
} }
images.push({ type: mimeType.split('/')[1], uri: data })
} else if (mimeType.startsWith('video/')) { item.data.forEach(d => {
if (!typesVideo.includes(mimeType.split('/')[1])) { if (typeof d === 'string') return
console.warn('Video type not supported:', mimeType.split('/')[1]) 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 return
} }
video = { type: mimeType.split('/')[1], uri: data } let tempData: string[]
} else { if (!Array.isArray(item.data)) {
if (typesImage.includes(data.split('.').pop() || '')) { tempData = [item.data]
images.push({ type: data.split('.').pop()!, uri: data }) } else {
return tempData = item.data
} }
if (typesVideo.includes(data.split('.').pop() || '')) { tempData.forEach(d => {
video = { type: data.split('.').pop()!, uri: data } const typesImage = ['png', 'jpg', 'jpeg', 'gif']
return const typesVideo = ['mp4', 'm4v', 'mov', 'webm', 'mpeg']
} if (item.mimeType!.startsWith('image/')) {
text = !text ? data : text.concat(text, `\n${data}`) if (!typesImage.includes(item.mimeType.split('/')[1])) {
} console.warn(
}) 'Image type not supported:',
navigationRef.navigate('Screen-Compose', { item.mimeType.split('/')[1]
type: 'share', )
text, return
images, }
video 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] [instanceActive]
) )
useEffect(() => { useEffect(() => {
ShareMenu.getInitialShare(item => handleShare(item)) ShareMenu.getInitialShare(handleShare)
}, []) }, [])
useEffect(() => { useEffect(() => {
const listener = ShareMenu.addNewShareListener(item => handleShare(item)) const listener = ShareMenu.addNewShareListener(handleShare)
return () => { return () => {
listener.remove() listener.remove()
} }