From 22bcf3cb667a442da9791784e259765ea7ed273c Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Date: Tue, 20 Sep 2022 20:14:34 -0300 Subject: [PATCH] add crop image option --- app/views/ShareView/Thumbs.tsx | 34 ++++++++-------- app/views/ShareView/index.tsx | 73 ++++++++++++++++++++++++++++------ 2 files changed, 79 insertions(+), 28 deletions(-) diff --git a/app/views/ShareView/Thumbs.tsx b/app/views/ShareView/Thumbs.tsx index d9425bfed..d7c59f8d8 100644 --- a/app/views/ShareView/Thumbs.tsx +++ b/app/views/ShareView/Thumbs.tsx @@ -11,7 +11,7 @@ import { allowPreview } from './utils'; import { TSupportedThemes } from '../../theme'; import { IShareAttachment } from '../../definitions'; -export const THUMB_SIZE = 64; +const THUMB_SIZE = 64; const styles = StyleSheet.create({ list: { @@ -139,21 +139,23 @@ const Thumb = ({ item, theme, isShareExtension, onPress, onRemove }: IThumb) => const Thumbs = ({ attachments, theme, isShareExtension, onPress, onRemove }: IThumbs) => { if (attachments?.length > 1) { return ( - item.path} - renderItem={({ item }) => ( - onPress(item)} - onRemove={() => onRemove(item)} - /> - )} - style={[styles.list, { backgroundColor: themes[theme].messageboxBackground }]} - /> + + item.path} + renderItem={({ item }) => ( + onPress(item)} + onRemove={() => onRemove(item)} + /> + )} + style={[styles.list, { backgroundColor: themes[theme].messageboxBackground }]} + /> + ); } return null; diff --git a/app/views/ShareView/index.tsx b/app/views/ShareView/index.tsx index b9379d35e..6297f56d9 100644 --- a/app/views/ShareView/index.tsx +++ b/app/views/ShareView/index.tsx @@ -5,6 +5,7 @@ import { NativeModules, Text, View } from 'react-native'; import { connect } from 'react-redux'; import ShareExtension from 'rn-extensions-share'; import { Q } from '@nozbe/watermelondb'; +import ImagePicker, { Image } from 'react-native-image-crop-picker'; import { InsideStackParamList } from '../../stacks/types'; import { themes } from '../../lib/constants'; @@ -62,6 +63,7 @@ class ShareView extends Component { private files: any[]; private isShareExtension: boolean; private serverInfo: IServer; + private canEdit: boolean; constructor(props: IShareViewProps) { super(props); @@ -69,6 +71,7 @@ class ShareView extends Component { this.files = props.route.params?.attachments ?? []; this.isShareExtension = props.route.params?.isShareExtension; this.serverInfo = props.route.params?.serverInfo ?? {}; + this.canEdit = props.route.params?.canEdit; this.state = { selected: {} as IShareAttachment, @@ -94,6 +97,43 @@ class ShareView extends Component { console.countReset(`${this.constructor.name}.render calls`); }; + cropImage = () => { + const { attachments, selected } = this.state; + ImagePicker.openCropper({ + path: this.state.selected.path, + mediaType: 'photo', + writeTempFile: true, + includeExif: true + }) + .then((image: Image) => { + let editedAttachment: undefined | IShareAttachment; + const newAttachments = attachments.map(attachment => { + if (attachment.filename === selected.filename) { + const editedImage = { + ...attachment, + ...image, + uri: image.path, + filename: `${image.path.substring(image.path.lastIndexOf('/') + 1)}` + }; + editedAttachment = editedImage; + return editedImage; + } + return attachment; + }); + this.setState({ attachments: newAttachments, selected: editedAttachment! }); + }) + .catch(() => {}); + }; + + headerRight = () => { + const { theme } = this.props; + return ( + + + + ); + }; + setHeader = () => { const { room, thread, readOnly, attachments } = this.state; const { navigation, theme } = this.props; @@ -109,7 +149,9 @@ class ShareView extends Component { options.headerLeft = () => ; } - if (!attachments.length && !readOnly) { + if (this.canEdit && this.state.selected.mime === 'image/jpeg') { + options.headerRight = this.headerRight; + } else if (!attachments.length && !readOnly) { options.headerRight = () => ( @@ -256,6 +298,8 @@ class ShareView extends Component { selectFile = (item: IShareAttachment) => { const { attachments, selected } = this.state; + const { navigation } = this.props; + if (attachments.length > 0) { const text = this.messagebox.current?.text; const newAttachments = attachments.map(att => { @@ -264,7 +308,14 @@ class ShareView extends Component { } return att; }); - return this.setState({ attachments: newAttachments, selected: item }); + + return this.setState({ attachments: newAttachments, selected: item }, () => { + if (item.mime === 'image/jpeg') { + navigation.setOptions({ headerRight: this.headerRight }); + } else { + navigation.setOptions({ headerRight: undefined }); + } + }); } }; @@ -293,7 +344,6 @@ class ShareView extends Component { renderContent = () => { const { attachments, selected, room, text } = this.state; const { theme, navigation } = this.props; - if (attachments.length) { return ( @@ -318,15 +368,14 @@ class ShareView extends Component { isFocused={navigation.isFocused} iOSScrollBehavior={NativeModules.KeyboardTrackingViewManager?.KeyboardTrackingScrollBehaviorNone} isActionsEnabled={false} - > - - + /> + ); }