save and copy qr code

This commit is contained in:
Hailey
2024-06-13 13:29:59 -07:00
parent 5eb8b6ac60
commit 9655947a1b
3 changed files with 115 additions and 43 deletions
+21 -17
View File
@@ -31,7 +31,7 @@ export const QrCode = React.forwardRef<ViewShot, Props>(function QrCode(
<ViewShot ref={ref}> <ViewShot ref={ref}>
<LinearGradientBackground <LinearGradientBackground
style={[ style={[
{width: 300, height: 440}, {width: 300, minHeight: 390},
a.align_center, a.align_center,
a.px_sm, a.px_sm,
a.py_xl, a.py_xl,
@@ -45,29 +45,33 @@ export const QrCode = React.forwardRef<ViewShot, Props>(function QrCode(
{record.name} {record.name}
</Text> </Text>
</View> </View>
<Text <View style={[a.gap_xl, a.align_center]}>
style={[a.font_bold, a.text_center, {color: 'white', fontSize: 18}]}>
<Trans>Join the conversation</Trans>
</Text>
<View style={[a.rounded_sm, a.overflow_hidden]}>
<QrCodeInner url={makeStarterPackLink(starterPack)} />
</View>
<View style={[a.flex_row, a.align_center, {gap: 5}]}>
<Text <Text
style={[ style={[
a.font_bold, a.font_bold,
a.text_center, a.text_center,
{color: 'white', fontSize: 18}, {color: 'white', fontSize: 18},
]}> ]}>
<Trans>on</Trans> <Trans>Join the conversation</Trans>
</Text> </Text>
<Logo width={26} fill="white" /> <View style={[a.rounded_sm, a.overflow_hidden]}>
<Logotype <QrCodeInner url={makeStarterPackLink(starterPack)} />
width={66} </View>
fill="white"
style={{marginTop: 6, marginLeft: 2}} <View style={[a.flex_row, a.align_center, {gap: 5}]}>
/> <Text
style={[
a.font_bold,
a.text_center,
{color: 'white', fontSize: 18},
]}>
<Trans>on</Trans>
</Text>
<Logo width={26} fill="white" />
<View style={[{marginTop: 7, marginLeft: 3}]}>
<Logotype width={68} fill="white" />
</View>
</View>
</View> </View>
</LinearGradientBackground> </LinearGradientBackground>
</ViewShot> </ViewShot>
+93 -25
View File
@@ -1,14 +1,16 @@
import React from 'react' import React from 'react'
import {View} from 'react-native' import {View} from 'react-native'
import ViewShot from 'react-native-view-shot' import ViewShot from 'react-native-view-shot'
import {setImageAsync} from 'expo-clipboard'
import * as FS from 'expo-file-system' import * as FS from 'expo-file-system'
import {requestMediaLibraryPermissionsAsync} from 'expo-image-picker' import {requestMediaLibraryPermissionsAsync} from 'expo-image-picker'
import {AppBskyGraphDefs} from '@atproto/api' import {AppBskyGraphDefs, AppBskyGraphStarterpack} from '@atproto/api'
import {msg, Trans} from '@lingui/macro' import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
import {nanoid} from 'nanoid/non-secure' import {nanoid} from 'nanoid/non-secure'
import {saveImageToMediaLibrary} from 'lib/media/manip' import {saveImageToMediaLibrary} from 'lib/media/manip'
import {isNative} from 'platform/detection'
import * as Toast from '#/view/com/util/Toast' import * as Toast from '#/view/com/util/Toast'
import {atoms as a} from '#/alf' import {atoms as a} from '#/alf'
import {Button, ButtonText} from '#/components/Button' import {Button, ButtonText} from '#/components/Button'
@@ -28,25 +30,79 @@ export function QrCodeDialog({
const ref = React.useRef<ViewShot>(null) const ref = React.useRef<ViewShot>(null)
const onSavePress = () => { const getCanvas = (base64: string): Promise<HTMLCanvasElement> => {
ref.current?.capture?.().then(async (uri: string) => { return new Promise(resolve => {
const res = await requestMediaLibraryPermissionsAsync() const image = new Image()
image.onload = () => {
const canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
if (!res) { const ctx = canvas.getContext('2d')
Toast.show( ctx?.drawImage(image, 0, 0)
_(msg`You must grant access to your photo library to save a QR code`), resolve(canvas)
}
image.src = base64
})
}
const onSavePress = async () => {
ref.current?.capture?.().then(async (uri: string) => {
if (isNative) {
const res = await requestMediaLibraryPermissionsAsync()
if (!res) {
Toast.show(
_(
msg`You must grant access to your photo library to save a QR code`,
),
)
return
}
const filename = `${FS.cacheDirectory}/${nanoid(12)}.png`
await FS.copyAsync({from: uri, to: filename})
await saveImageToMediaLibrary({uri: filename})
await FS.deleteAsync(filename)
control.close()
} else {
if (!AppBskyGraphStarterpack.isRecord(starterPack.record)) {
return
}
const canvas = await getCanvas(uri)
const imgHref = canvas
.toDataURL('image/png')
.replace('image/png', 'image/octet-stream')
const link = document.createElement('a')
link.setAttribute(
'download',
`${starterPack.record.name.replaceAll(' ', '_')}_Share_Card.png`,
) )
return link.setAttribute('href', imgHref)
link.click()
} }
const filename = `${FS.cacheDirectory}/${nanoid(12)}.png`
await FS.copyAsync({from: uri, to: filename})
await saveImageToMediaLibrary({uri: filename})
await FS.deleteAsync(filename)
Toast.show(_(msg`QR code saved to your camera roll!`)) Toast.show(_(msg`QR code saved to your camera roll!`))
})
}
control.close() const onCopyPress = async () => {
ref.current?.capture?.().then(async (uri: string) => {
if (isNative) {
await setImageAsync(uri)
} else {
const canvas = await getCanvas(uri)
canvas.toBlob((blob: Blob) => {
const item = new ClipboardItem({'image/png': blob})
navigator.clipboard.write([item])
})
}
Toast.show(_(msg`QR code copied to your clipboard!`))
}) })
} }
@@ -55,21 +111,33 @@ export function QrCodeDialog({
<Dialog.Handle /> <Dialog.Handle />
<Dialog.ScrollableInner <Dialog.ScrollableInner
label={_(msg`Create a QR code for a starter pack`)}> label={_(msg`Create a QR code for a starter pack`)}>
<View style={[a.flex_1, a.align_center, a.gap_3xl]}> <View style={[a.flex_1, a.align_center, a.gap_5xl]}>
<Text style={[a.font_bold, a.text_xl, a.text_center]}> <Text style={[a.font_bold, a.text_xl, a.text_center]}>
Share this starter pack with friends! Share this starter pack with friends!
</Text> </Text>
<QrCode starterPack={starterPack} ref={ref} /> <QrCode starterPack={starterPack} ref={ref} />
<Button <View style={[a.flex_row, a.gap_md]}>
label={_(msg`Save QR code`)} <Button
variant="solid" label={_(msg`Save QR code`)}
color="primary" variant="solid"
size="medium" color="primary"
onPress={onSavePress}> size="medium"
<ButtonText> onPress={onSavePress}>
<Trans>Save QR code</Trans> <ButtonText>
</ButtonText> <Trans>Save</Trans>
</Button> </ButtonText>
</Button>
<Button
label={_(msg`Copy QR code`)}
variant="solid"
color="primary"
size="medium"
onPress={onCopyPress}>
<ButtonText>
<Trans>Copy</Trans>
</ButtonText>
</Button>
</View>
</View> </View>
</Dialog.ScrollableInner> </Dialog.ScrollableInner>
</Dialog.Outer> </Dialog.Outer>
+1 -1
View File
@@ -641,7 +641,7 @@ function Footer({
)} )}
</Text> </Text>
)} )}
{state.currentStep === 'Profiles' && items.length < 8 ? ( {state.currentStep === 'Profiles' && items.length < 8 && !__DEV__ ? (
<Text <Text
style={[ style={[
a.font_bold, a.font_bold,