From 34be0ad6f524d0d768500171204c7e70b9ae4d38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ferreiro?= Date: Mon, 5 Dec 2022 15:25:43 +0000 Subject: [PATCH] upload profile photos (save them locally) --- src/view/com/util/UserAvatar.tsx | 62 +++++++++++++++++++++++++++----- src/view/com/util/UserBanner.tsx | 58 ++++++++++++++++++++++++++---- 2 files changed, 105 insertions(+), 15 deletions(-) diff --git a/src/view/com/util/UserAvatar.tsx b/src/view/com/util/UserAvatar.tsx index f4dc982be3..eeef51b2ef 100644 --- a/src/view/com/util/UserAvatar.tsx +++ b/src/view/com/util/UserAvatar.tsx @@ -1,7 +1,8 @@ -import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' -import React, {useCallback} from 'react' -import {Alert, StyleSheet, View, TouchableOpacity} from 'react-native' +import React, {useCallback, useState} from 'react' +import {StyleSheet, View, TouchableOpacity, Alert, Image} from 'react-native' import Svg, {Circle, Text, Defs, LinearGradient, Stop} from 'react-native-svg' +import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' +import {openCamera, openCropper, openPicker} from 'react-native-image-crop-picker' import {getGradient} from '../../lib/asset-gen' import {colors} from '../../lib/styles' @@ -16,9 +17,49 @@ export function UserAvatar({ handle: string displayName: string | undefined }) { + const [localAvatarPicture, setLocalAvatarPicture] = useState( + null, + ) const initials = getInitials(displayName || handle) const gradient = getGradient(handle) + const handleEditAvatar = useCallback(() => { + Alert.alert('', 'Select upload method', [ + { + text: 'Take a new photo', + onPress: () => { + openCamera({ + mediaType: 'photo', + cropping: true, + width: 80, + height: 80, + cropperCircleOverlay: true, + }).then(item => { + setLocalAvatarPicture(item.path) + }) + }, + }, + { + text: 'Select from gallery', + onPress: () => { + openPicker({ + mediaType: 'photo', + }).then(async item => { + await openCropper({ + mediaType: 'photo', + path: item.path, + width: 80, + height: 80, + cropperCircleOverlay: true, + }).then(croppedItem => { + setLocalAvatarPicture(croppedItem.path) + }) + }) + }, + }, + ]) + }, []) + const renderSvg = (size: number, initials: string) => ( @@ -40,13 +81,13 @@ export function UserAvatar({ ) - const handleEditAvatar = useCallback(() => { - Alert.alert('TB Implemented') - }, []) - return isMe ? ( - {renderSvg(size, initials)} + {localAvatarPicture != null ? ( + + ) : ( + renderSvg(size, initials) + )} ( + null, + ) + const gradient = getGradient(handle) + const handleEditBanner = useCallback(() => { + Alert.alert('', 'Select upload method', [ + { + text: 'Take a new photo', + onPress: () => { + openCamera({ + mediaType: 'photo', + cropping: true, + width: 1000, + height: 120, + }).then(item => { + setLocalBannerPicture(item.path) + }) + }, + }, + { + text: 'Select from gallery', + onPress: () => { + openPicker({ + mediaType: 'photo', + }).then(async item => { + await openCropper({ + mediaType: 'photo', + path: item.path, + width: 1000, + height: 120, + }).then(croppedItem => { + setLocalBannerPicture(croppedItem.path) + }) + }) + }, + }, + ]) + }, []) + const renderSvg = () => ( @@ -31,13 +71,13 @@ export function UserBanner({ ) - const handleEditBanner = useCallback(() => { - Alert.alert('TB Implemented') - }, []) - return isMe ? ( - {renderSvg()} + {localBannerPicture != null ? ( + + ) : ( + renderSvg() + )}