From 23de3d754ea96e5276f5ed32d6a035106a9f68d3 Mon Sep 17 00:00:00 2001 From: Hailey Date: Tue, 11 Jun 2024 22:25:43 -0700 Subject: [PATCH] handle saving viewshot --- src/components/StarterPack/QrCode.tsx | 121 ++++++++------------ src/components/StarterPack/QrCodeDialog.tsx | 21 ++-- 2 files changed, 55 insertions(+), 87 deletions(-) diff --git a/src/components/StarterPack/QrCode.tsx b/src/components/StarterPack/QrCode.tsx index 4ac7a5ca13..a69c1772c0 100644 --- a/src/components/StarterPack/QrCode.tsx +++ b/src/components/StarterPack/QrCode.tsx @@ -12,11 +12,14 @@ import {useTheme} from '#/alf' import {atoms as a} from '#/alf' import {Text} from '#/components/Typography' -export function QrCode({ - starterPack, -}: { +interface Props { starterPack: AppBskyGraphDefs.StarterPackView -}) { +} + +export const QrCode = React.forwardRef(function QrCode( + {starterPack}, + ref, +) { const t = useTheme() const {record} = starterPack @@ -30,50 +33,18 @@ export function QrCode({ } return ( - + - - - @@ -84,7 +55,7 @@ export function QrCode({ style={[a.font_bold, a.text_center, {color: 'white', fontSize: 18}]}> Join the conversation - + @@ -107,45 +78,43 @@ export function QrCode({ ) -} +}) export function QrCodeInner({url}: {url: string}) { const t = useTheme() return ( - - - + ) } diff --git a/src/components/StarterPack/QrCodeDialog.tsx b/src/components/StarterPack/QrCodeDialog.tsx index e6e13e366a..0f8f501be8 100644 --- a/src/components/StarterPack/QrCodeDialog.tsx +++ b/src/components/StarterPack/QrCodeDialog.tsx @@ -1,5 +1,6 @@ import React from 'react' import {View} from 'react-native' +import ViewShot from 'react-native-view-shot' import * as FS from 'expo-file-system' import {AppBskyGraphDefs} from '@atproto/api' import {msg, Trans} from '@lingui/macro' @@ -24,23 +25,21 @@ export function QrCodeDialog({ }) { const {_} = useLingui() - // No types for this - // See https://github.com/tokkozhin/react-native-qrcode-styled/blob/main/example/src/examples/DownloadQR.tsx for usage - const qrCodeRef = React.useRef() + const ref = React.useRef(null) const onSavePress = () => { - qrCodeRef.current?.toDataURL(async (base64Code: string) => { + ref.current?.capture?.().then(async (uri: string) => { const filename = `${FS.cacheDirectory}/${nanoid(12)}.png` - await FS.writeAsStringAsync(filename, base64Code, { - encoding: FS.EncodingType.Base64, - }) + await FS.copyAsync({from: uri, to: filename}) await saveImageToMediaLibrary({uri: filename}) - control.close(() => { - Toast.show(_(msg`QR code saved to your camera roll!`)) - }) + await FS.deleteAsync(filename) + Toast.show(_(msg`QR code saved to your camera roll!`)) + + control.close() }) } + return ( @@ -50,7 +49,7 @@ export function QrCodeDialog({ Share this starter pack with friends! - +