mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at rm-precision 59 lines 1.8 kB view raw
1import {useCallback} from 'react' 2import * as MediaLibrary from 'expo-media-library' 3import {t} from '@lingui/macro' 4 5import {isNative} from '#/platform/detection' 6import * as Toast from '#/view/com/util/Toast' 7import {saveImageToMediaLibrary} from './manip' 8 9/** 10 * Same as `saveImageToMediaLibrary`, but also handles permissions and toasts 11 */ 12export function useSaveImageToMediaLibrary() { 13 const [permissionResponse, requestPermission, getPermission] = 14 MediaLibrary.usePermissions({ 15 granularPermissions: ['photo'], 16 }) 17 return useCallback( 18 async (uri: string) => { 19 if (!isNative) { 20 throw new Error('useSaveImageToMediaLibrary is native only') 21 } 22 23 async function save() { 24 try { 25 await saveImageToMediaLibrary({uri}) 26 Toast.show(t`Image saved`) 27 } catch (e: any) { 28 Toast.show(t`Failed to save image: ${String(e)}`, 'xmark') 29 } 30 } 31 32 const permission = permissionResponse ?? (await getPermission()) 33 34 if (permission.granted) { 35 await save() 36 } else { 37 if (permission.canAskAgain) { 38 // request again once 39 const askAgain = await requestPermission() 40 if (askAgain.granted) { 41 await save() 42 } else { 43 // since we've been explicitly denied, show a toast. 44 Toast.show( 45 t`Images cannot be saved unless permission is granted to access your photo library.`, 46 'xmark', 47 ) 48 } 49 } else { 50 Toast.show( 51 t`Permission to access your photo library was denied. Please enable it in your system settings.`, 52 'xmark', 53 ) 54 } 55 } 56 }, 57 [permissionResponse, requestPermission, getPermission], 58 ) 59}