Skip to content

Fix - Hybrid - Android - After downloading receipt and tapping on receipt, receipt disappears #59540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/AttachmentModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ function AttachmentModal({

if (typeof sourceURL === 'string') {
const fileName = type === CONST.ATTACHMENT_TYPE.SEARCH ? getFileName(`${sourceURL}`) : file?.name;
fileDownload(sourceURL, fileName ?? '');
fileDownload(sourceURL, fileName ?? '', undefined, undefined, undefined, undefined, undefined, !draftTransactionID);
}

// At ios, if the keyboard is open while opening the attachment, then after downloading
// the attachment keyboard will show up. So, to fix it we need to dismiss the keyboard.
Keyboard.dismiss();
}, [isAuthTokenRequiredState, sourceState, file, type]);
}, [isAuthTokenRequiredState, sourceState, file, type, draftTransactionID]);

/**
* Execute the onConfirm callback and close the modal.
Expand Down
28 changes: 14 additions & 14 deletions src/libs/fileDownload/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {FetchBlobResponse} from 'react-native-blob-util';
import RNFetchBlob from 'react-native-blob-util';
import RNFS from 'react-native-fs';
import CONST from '@src/CONST';
import * as FileUtils from './FileUtils';
import {appendTimeToFileName, getFileName, showGeneralErrorAlert, showPermissionErrorAlert, showSuccessAlert} from './FileUtils';
import type {FileDownload} from './types';

/**
Expand Down Expand Up @@ -35,14 +35,14 @@ function hasAndroidPermission(): Promise<boolean> {
/**
* Handling the download
*/
function handleDownload(url: string, fileName?: string, successMessage?: string): Promise<void> {
function handleDownload(url: string, fileName?: string, successMessage?: string, shouldUnlink = true): Promise<void> {
return new Promise((resolve) => {
const dirs = RNFetchBlob.fs.dirs;

// Android files will download to Download directory
const path = dirs.DownloadDir;
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- Disabling this line for safeness as nullish coalescing works only if the value is undefined or null, and since fileName can be an empty string we want to default to `FileUtils.getFileName(url)`
const attachmentName = FileUtils.appendTimeToFileName(fileName || FileUtils.getFileName(url));
const attachmentName = appendTimeToFileName(fileName || getFileName(url));

const isLocalFile = url.startsWith('file://');

Expand Down Expand Up @@ -84,13 +84,13 @@ function handleDownload(url: string, fileName?: string, successMessage?: string)
);
})
.then(() => {
if (attachmentPath) {
if (attachmentPath && shouldUnlink) {
RNFetchBlob.fs.unlink(attachmentPath);
}
FileUtils.showSuccessAlert(successMessage);
showSuccessAlert(successMessage);
})
.catch(() => {
FileUtils.showGeneralErrorAlert();
showGeneralErrorAlert();
})
.finally(() => resolve());
});
Expand All @@ -114,14 +114,14 @@ const postDownloadFile = (url: string, fileName?: string, formData?: FormData, o
return response.text();
})
.then((fileData) => {
const finalFileName = FileUtils.appendTimeToFileName(fileName ?? 'Expensify');
const finalFileName = appendTimeToFileName(fileName ?? 'Expensify');
const downloadPath = `${RNFS.DownloadDirectoryPath}/${finalFileName}`;
return RNFS.writeFile(downloadPath, fileData, 'utf8').then(() => downloadPath);
})
.then((downloadPath) =>
RNFetchBlob.MediaCollection.copyToMediaStore(
{
name: FileUtils.getFileName(downloadPath),
name: getFileName(downloadPath),
parentFolder: 'Expensify',
mimeType: null,
},
Expand All @@ -131,11 +131,11 @@ const postDownloadFile = (url: string, fileName?: string, formData?: FormData, o
)
.then((downloadPath) => {
RNFetchBlob.fs.unlink(downloadPath);
FileUtils.showSuccessAlert();
showSuccessAlert();
})
.catch(() => {
if (!onDownloadFailed) {
FileUtils.showGeneralErrorAlert();
showGeneralErrorAlert();
}
onDownloadFailed?.();
});
Expand All @@ -144,20 +144,20 @@ const postDownloadFile = (url: string, fileName?: string, formData?: FormData, o
/**
* Checks permission and downloads the file for Android
*/
const fileDownload: FileDownload = (url, fileName, successMessage, _, formData, requestType, onDownloadFailed) =>
const fileDownload: FileDownload = (url, fileName, successMessage, _, formData, requestType, onDownloadFailed, shouldUnlink) =>
new Promise((resolve) => {
hasAndroidPermission()
.then((hasPermission) => {
if (hasPermission) {
if (requestType === CONST.NETWORK.METHOD.POST) {
return postDownloadFile(url, fileName, formData, onDownloadFailed);
}
return handleDownload(url, fileName, successMessage);
return handleDownload(url, fileName, successMessage, shouldUnlink);
}
FileUtils.showPermissionErrorAlert();
showPermissionErrorAlert();
})
.catch(() => {
FileUtils.showPermissionErrorAlert();
showPermissionErrorAlert();
})
.finally(() => resolve());
});
Expand Down
1 change: 1 addition & 0 deletions src/libs/fileDownload/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type FileDownload = (
formData?: FormData,
requestType?: RequestType,
onDownloadFailed?: () => void,
shouldUnlink?: boolean,
) => Promise<void>;
type ImageResolution = {width: number; height: number};
type GetImageResolution = (url: File | Asset) => Promise<ImageResolution>;
Expand Down