import * as Sharing from 'expo-sharing';
import * as FileSystem from 'expo-file-system';

// <https://www.planetgeek.ch/wp-content/uploads/2013/06/Clean-Code-V2.1.pdf>
const downloadFile = async () => {
  const fileUri = FileSystem.documentDirectory + 'r.pdf'; // Set the file name and directory
  const downloadResumable = FileSystem.createDownloadResumable(
    '<https://www.planetgeek.ch/wp-content/uploads/2013/06/Clean-Code-V2.1.pdf>', // URL of the file to download
    fileUri, // Location to save the downloaded file
    {}, // Optional headers to include in the request
    (downloadProgress) => {
      const progress = downloadProgress.totalBytesWritten / downloadProgress.totalBytesExpectedToWrite;
      console.log(`Download progress: ${progress * 100}%`);
    }
  );

  try {
    const { uri } = await downloadResumable.downloadAsync(); // Start the download
    await Sharing.shareAsync(uri); //Allow user to select location to save file
    console.log(`Downloaded file stored at: ${uri}`);

  } catch (error) {
    console.error(error);
  }
};

https://forums.expo.dev/t/how-to-save-the-file-to-devices-folder-like-download/2398/52