Skip to content

Commit

Permalink
Merge pull request #46944 from nkdengineer/fix/46882
Browse files Browse the repository at this point in the history
Display loading while image is resizing
  • Loading branch information
luacmartins authored Aug 12, 2024
2 parents 87bdc24 + 3206131 commit 146a8d3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Shutter from '@assets/images/shutter.svg';
import type {FileObject} from '@components/AttachmentModal';
import AttachmentPicker from '@components/AttachmentPicker';
import Button from '@components/Button';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import ImageSVG from '@components/ImageSVG';
Expand Down Expand Up @@ -67,6 +68,7 @@ function IOURequestStepScan({
const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.reportID ?? -1}`);
const [cameraPermissionStatus, setCameraPermissionStatus] = useState<string | null>(null);
const [didCapturePhoto, setDidCapturePhoto] = useState(false);
const [isLoadingReceipt, setIsLoadingReceipt] = useState(false);

const [pdfFile, setPdfFile] = useState<null | FileObject>(null);

Expand Down Expand Up @@ -398,7 +400,13 @@ function IOURequestStepScan({
return;
}

// With the image size > 24MB, we use manipulateAsync to resize the image.
// It takes a long time so we should display a loading indicator while the resize image progresses.
if (Str.isImage(originalFile.name ?? '') && (originalFile?.size ?? 0) > CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE) {
setIsLoadingReceipt(true);
}
FileUtils.resizeImageIfNeeded(originalFile).then((file) => {
setIsLoadingReceipt(false);
// Store the receipt on the transaction object in Onyx
// On Android devices, fetching blob for a file with name containing spaces fails to retrieve the type of file.
// So, let us also save the file type in receipt for later use during blob fetch
Expand Down Expand Up @@ -470,6 +478,7 @@ function IOURequestStepScan({
shouldShowWrapper={!!backTo}
testID={IOURequestStepScan.displayName}
>
{isLoadingReceipt && <FullScreenLoadingIndicator />}
{pdfFile && (
<PDFThumbnail
style={styles.invisiblePDF}
Expand Down
55 changes: 33 additions & 22 deletions src/pages/iou/request/step/IOURequestStepScan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Button from '@components/Button';
import ConfirmModal from '@components/ConfirmModal';
import CopyTextToClipboard from '@components/CopyTextToClipboard';
import {DragAndDropContext} from '@components/DragAndDrop/Provider';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import PDFThumbnail from '@components/PDFThumbnail';
Expand Down Expand Up @@ -78,6 +79,7 @@ function IOURequestStepScan({

const getScreenshotTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report?.reportID ?? -1}`);
const [isLoadingReceipt, setIsLoadingReceipt] = useState(false);

const [videoConstraints, setVideoConstraints] = useState<MediaTrackConstraints>();
const tabIndex = 1;
Expand Down Expand Up @@ -431,7 +433,13 @@ function IOURequestStepScan({
return;
}

// With the image size > 24MB, we use manipulateAsync to resize the image.
// It takes a long time so we should display a loading indicator while the resize image progresses.
if (Str.isImage(originalFile.name ?? '') && (originalFile?.size ?? 0) > CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE) {
setIsLoadingReceipt(true);
}
FileUtils.resizeImageIfNeeded(originalFile).then((file) => {
setIsLoadingReceipt(false);
// Store the receipt on the transaction object in Onyx
const source = URL.createObjectURL(file as Blob);
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
Expand Down Expand Up @@ -692,28 +700,31 @@ function IOURequestStepScan({
testID={IOURequestStepScan.displayName}
>
{(isDraggingOverWrapper) => (
<View style={[styles.flex1, !Browser.isMobile() && styles.uploadReceiptView(isSmallScreenWidth)]}>
{!(isDraggingOver ?? isDraggingOverWrapper) && (Browser.isMobile() ? mobileCameraView() : desktopUploadView())}
<ReceiptDropUI
onDrop={(e) => {
const file = e?.dataTransfer?.files[0];
if (file) {
file.uri = URL.createObjectURL(file);
setReceiptAndNavigate(file);
}
}}
receiptImageTopPosition={receiptImageTopPosition}
/>
<ConfirmModal
title={attachmentInvalidReasonTitle ? translate(attachmentInvalidReasonTitle) : ''}
onConfirm={hideRecieptModal}
onCancel={hideRecieptModal}
isVisible={isAttachmentInvalid}
prompt={attachmentInvalidReason ? translate(attachmentInvalidReason) : ''}
confirmText={translate('common.close')}
shouldShowCancelButton={false}
/>
</View>
<>
{isLoadingReceipt && <FullScreenLoadingIndicator />}
<View style={[styles.flex1, !Browser.isMobile() && styles.uploadReceiptView(isSmallScreenWidth)]}>
{!(isDraggingOver ?? isDraggingOverWrapper) && (Browser.isMobile() ? mobileCameraView() : desktopUploadView())}
<ReceiptDropUI
onDrop={(e) => {
const file = e?.dataTransfer?.files[0];
if (file) {
file.uri = URL.createObjectURL(file);
setReceiptAndNavigate(file);
}
}}
receiptImageTopPosition={receiptImageTopPosition}
/>
<ConfirmModal
title={attachmentInvalidReasonTitle ? translate(attachmentInvalidReasonTitle) : ''}
onConfirm={hideRecieptModal}
onCancel={hideRecieptModal}
isVisible={isAttachmentInvalid}
prompt={attachmentInvalidReason ? translate(attachmentInvalidReason) : ''}
confirmText={translate('common.close')}
shouldShowCancelButton={false}
/>
</View>
</>
)}
</StepScreenDragAndDropWrapper>
);
Expand Down

0 comments on commit 146a8d3

Please sign in to comment.