Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Attachment Picker briefly displayed after taking a picture #2656

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 30 additions & 25 deletions src/components/AttachmentPicker/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ class AttachmentPicker extends Component {

this.state = {
isVisible: false,
result: null,
onPicked: () => {},
};

Expand All @@ -157,29 +156,20 @@ class AttachmentPicker extends Component {
},
];

this.setResult = this.setResult.bind(this);
this.close = this.close.bind(this);
this.completeAttachmentSelection = this.completeAttachmentSelection.bind(this);
this.pickAttachment = this.pickAttachment.bind(this);
}

/**
* Store the selected attachment mapped to an appropriate file interface
* Handles the image/document picker result and
* sends the selected attachment to the caller (parent component)
*
* @param {ImagePickerResponse|DocumentPickerResponse} attachment
*/
setResult(attachment) {
pickAttachment(attachment) {
if (attachment && !attachment.didCancel && !attachment.error) {
const result = getDataForUpload(attachment);
this.setState({result});
}
}
kidroca marked this conversation as resolved.
Show resolved Hide resolved

/**
* Triggers the `onPicked` callback with the selected attachment
*/
completeAttachmentSelection() {
if (this.state.result) {
this.state.onPicked(this.state.result);
this.state.onPicked(result);
kidroca marked this conversation as resolved.
Show resolved Hide resolved
}
}
kidroca marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -191,7 +181,6 @@ class AttachmentPicker extends Component {
open(onPicked) {
this.setState({
isVisible: true,
result: null,
onPicked,
});
}
Expand All @@ -203,6 +192,25 @@ class AttachmentPicker extends Component {
this.setState({isVisible: false});
}

/**
* Setup native attachment selection to start after this popover closes
*
* @param {{pickAttachment: function}} item - an item from this.menuItemData
*/
selectItem(item) {
/* setTimeout delays execution to the frame after the modal closes
* without this on iOS closing the modal closes the gallery/camera as well */
this.onModalHide = () => setTimeout(
() => item.pickAttachment()
.then(this.pickAttachment)
.catch(console.error)
.finally(() => delete this.onModalHide),
10,
);

this.close();
}
kidroca marked this conversation as resolved.
Show resolved Hide resolved

/**
* Call the `children` renderProp with the interface defined in propTypes
*
Expand All @@ -221,19 +229,16 @@ class AttachmentPicker extends Component {
onClose={this.close}
isVisible={this.state.isVisible}
anchorPosition={styles.createMenuPosition}
onModalHide={this.completeAttachmentSelection}
onModalHide={this.onModalHide}
joelbettner marked this conversation as resolved.
Show resolved Hide resolved
>
<View style={this.props.isSmallScreenWidth ? {} : styles.createMenuContainer}>
{
this.menuItemData.map(({icon, text, pickAttachment}) => (
this.menuItemData.map(item => (
<MenuItem
key={text}
icon={icon}
title={text}
onPress={() => pickAttachment()
.then(this.setResult)
.then(this.close)
.catch(console.error)}
key={item.text}
icon={item.icon}
title={item.text}
onPress={() => this.selectItem(item)}
kidroca marked this conversation as resolved.
Show resolved Hide resolved
/>
))
}
Expand Down