Skip to content

Commit

Permalink
Merge pull request #34116 from abzokhattab/add-custom-success-message…
Browse files Browse the repository at this point in the history
…-for-download-qr

Add a custom success message for QR download
  • Loading branch information
marcaaron authored Jan 16, 2024
2 parents 9d40a26 + 3cf66f4 commit eb47b90
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 14 deletions.
7 changes: 5 additions & 2 deletions src/components/QRShare/QRShareWithDownload/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ import React, {forwardRef, useImperativeHandle, useRef} from 'react';
import ViewShot from 'react-native-view-shot';
import getQrCodeFileName from '@components/QRShare/getQrCodeDownloadFileName';
import type {QRShareProps} from '@components/QRShare/types';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import fileDownload from '@libs/fileDownload';
import QRShare from '..';
import type QRShareWithDownloadHandle from './types';

function QRShareWithDownload(props: QRShareProps, ref: ForwardedRef<QRShareWithDownloadHandle>) {
const {isOffline} = useNetwork();
const {translate} = useLocalize();

const qrCodeScreenshotRef = useRef<ViewShot>(null);

useImperativeHandle(
ref,
() => ({
download: () => qrCodeScreenshotRef.current?.capture?.().then((uri) => fileDownload(uri, getQrCodeFileName(props.title))),
download: () => qrCodeScreenshotRef.current?.capture?.().then((uri) => fileDownload(uri, getQrCodeFileName(props.title), translate('fileDownload.success.qrMessage'))),
}),
[props.title],
[props.title, translate],
);

return (
Expand Down
1 change: 1 addition & 0 deletions src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,7 @@ export default {
success: {
title: 'Downloaded!',
message: 'Attachment successfully downloaded!',
qrMessage: 'Check your photos or downloads folder for a copy of your QR code. Protip: Add it to a presentation for your audience to scan and connect with you directly.',
},
generalError: {
title: 'Attachment Error',
Expand Down
2 changes: 2 additions & 0 deletions src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1829,6 +1829,8 @@ export default {
success: {
title: '!Descargado!',
message: 'Archivo descargado correctamente',
qrMessage:
'Busca la copia de tu código QR en la carpeta de fotos o descargas. Consejo: Añádelo a una presentación para que el público pueda escanearlo y conectar contigo directamente.',
},
generalError: {
title: 'Error en la descarga',
Expand Down
5 changes: 3 additions & 2 deletions src/libs/fileDownload/FileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import type {ReadFileAsync, SplitExtensionFromFileName} from './types';

/**
* Show alert on successful attachment download
* @param successMessage
*/
function showSuccessAlert() {
function showSuccessAlert(successMessage?: string) {
Alert.alert(
Localize.translateLocal('fileDownload.success.title'),
Localize.translateLocal('fileDownload.success.message'),
successMessage ?? Localize.translateLocal('fileDownload.success.message'),
[
{
text: Localize.translateLocal('common.ok'),
Expand Down
8 changes: 4 additions & 4 deletions src/libs/fileDownload/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function hasAndroidPermission(): Promise<boolean> {
/**
* Handling the download
*/
function handleDownload(url: string, fileName: string): Promise<void> {
function handleDownload(url: string, fileName: string, successMessage?: string): Promise<void> {
return new Promise((resolve) => {
const dirs = RNFetchBlob.fs.dirs;

Expand Down Expand Up @@ -84,7 +84,7 @@ function handleDownload(url: string, fileName: string): Promise<void> {
if (attachmentPath) {
RNFetchBlob.fs.unlink(attachmentPath);
}
FileUtils.showSuccessAlert();
FileUtils.showSuccessAlert(successMessage);
})
.catch(() => {
FileUtils.showGeneralErrorAlert();
Expand All @@ -96,12 +96,12 @@ function handleDownload(url: string, fileName: string): Promise<void> {
/**
* Checks permission and downloads the file for Android
*/
const fileDownload: FileDownload = (url, fileName) =>
const fileDownload: FileDownload = (url, fileName, successMessage) =>
new Promise((resolve) => {
hasAndroidPermission()
.then((hasPermission) => {
if (hasPermission) {
return handleDownload(url, fileName);
return handleDownload(url, fileName, successMessage);
}
FileUtils.showPermissionErrorAlert();
})
Expand Down
4 changes: 2 additions & 2 deletions src/libs/fileDownload/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function downloadVideo(fileUrl: string, fileName: string): Promise<string> {
/**
* Download the file based on type(image, video, other file types)for iOS
*/
const fileDownload: FileDownload = (fileUrl, fileName) =>
const fileDownload: FileDownload = (fileUrl, fileName, successMessage) =>
new Promise((resolve) => {
let fileDownloadPromise;
const fileType = FileUtils.getFileType(fileUrl);
Expand All @@ -93,7 +93,7 @@ const fileDownload: FileDownload = (fileUrl, fileName) =>
return;
}

FileUtils.showSuccessAlert();
FileUtils.showSuccessAlert(successMessage);
})
.catch((err) => {
// iOS shows permission popup only once. Subsequent request will only throw an error.
Expand Down
2 changes: 1 addition & 1 deletion src/libs/fileDownload/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Asset} from 'react-native-image-picker';

type FileDownload = (url: string, fileName: string) => Promise<void>;
type FileDownload = (url: string, fileName: string, successMessage?: string) => Promise<void>;

type ImageResolution = {width: number; height: number};
type GetImageResolution = (url: File | Asset) => Promise<ImageResolution>;
Expand Down
4 changes: 2 additions & 2 deletions src/libs/localFileDownload/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type LocalFileDownload from './types';
* and textContent, so we're able to copy it to the Android public download dir.
* After the file is copied, it is removed from the internal dir.
*/
const localFileDownload: LocalFileDownload = (fileName, textContent) => {
const localFileDownload: LocalFileDownload = (fileName, textContent, successMessage) => {
const newFileName = FileUtils.appendTimeToFileName(fileName);
const dir = RNFetchBlob.fs.dirs.DocumentDir;
const path = `${dir}/${newFileName}.txt`;
Expand All @@ -23,7 +23,7 @@ const localFileDownload: LocalFileDownload = (fileName, textContent) => {
path,
)
.then(() => {
FileUtils.showSuccessAlert();
FileUtils.showSuccessAlert(successMessage);
})
.catch(() => {
FileUtils.showGeneralErrorAlert();
Expand Down
2 changes: 1 addition & 1 deletion src/libs/localFileDownload/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type LocalFileDownload = (fileName: string, textContent: string) => void;
type LocalFileDownload = (fileName: string, textContent: string, successMessage?: string) => void;

export default LocalFileDownload;

0 comments on commit eb47b90

Please sign in to comment.