Massive/write.ts
Brandon Presley 434f29652f Just use a toast for download notifications
The minor convenience of having that notification
doesn't justify me adding the library.
If I find another reason for me to have notifications
then i'll do it.
2022-11-22 22:18:15 +13:00

21 lines
753 B
TypeScript

import {PermissionsAndroid, Platform} from 'react-native'
import {Dirs, FileSystem} from 'react-native-file-access'
import {toast} from './toast'
export const write = async (name: string, data: string) => {
const filePath = `${Dirs.DocumentDir}/${name}`
const permission = async () => {
if (Platform.OS !== 'android') return true
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
)
return granted === PermissionsAndroid.RESULTS.GRANTED
}
const granted = await permission()
if (!granted) return
await FileSystem.writeFile(filePath, data)
if (Platform.OS === 'android')
await FileSystem.cpExternal(filePath, name, 'downloads')
toast(`Downloaded ${name}`)
}