2022-08-25 01:01:01 +00:00
|
|
|
import {useContext} from 'react';
|
|
|
|
import {PermissionsAndroid} from 'react-native';
|
2022-07-19 04:24:16 +00:00
|
|
|
import {Dirs, FileSystem} from 'react-native-file-access';
|
2022-08-25 01:01:01 +00:00
|
|
|
import {SnackbarContext} from './App';
|
2022-07-19 04:24:16 +00:00
|
|
|
|
2022-08-25 01:01:01 +00:00
|
|
|
export const useWrite = () => {
|
|
|
|
const {toast} = useContext(SnackbarContext);
|
|
|
|
|
|
|
|
return {
|
|
|
|
write: async (name: string, data: string) => {
|
|
|
|
const filePath = `${Dirs.DocumentDir}/${name}`;
|
|
|
|
const permission = async () => {
|
|
|
|
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 (!FileSystem.exists(filePath)) return;
|
|
|
|
await FileSystem.cpExternal(filePath, name, 'downloads');
|
|
|
|
toast(`Saved "${name}" in your downloads folder.`, 6000);
|
|
|
|
},
|
2022-07-19 04:24:16 +00:00
|
|
|
};
|
|
|
|
};
|