From 04fda0f1429d26586d308a619f91d4504bdd3521 Mon Sep 17 00:00:00 2001 From: Ansh Date: Fri, 29 Sep 2023 10:49:59 +0700 Subject: [PATCH] Fix 1405 handle android back button in composer (#1446) * handle android back button in composer * improve backHandler error handling * simplify composer onClose functionality --- src/lib/routes/back-handler.ts | 17 ++++++++++++++--- src/view/com/composer/Composer.tsx | 24 +++++++++++++++++++----- src/view/shell/Composer.tsx | 3 --- src/view/shell/Composer.web.tsx | 3 --- src/view/shell/index.tsx | 6 ++++-- src/view/shell/index.web.tsx | 1 - 6 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/lib/routes/back-handler.ts b/src/lib/routes/back-handler.ts index c4067c53..aae2f2c2 100644 --- a/src/lib/routes/back-handler.ts +++ b/src/lib/routes/back-handler.ts @@ -1,8 +1,19 @@ +import {isAndroid} from 'platform/detection' import {BackHandler} from 'react-native' import {RootStoreModel} from 'state/index' export function init(store: RootStoreModel) { - BackHandler.addEventListener('hardwareBackPress', () => { - return store.shell.closeAnyActiveElement() - }) + // only register back handler on android, otherwise it throws an error + if (isAndroid) { + const backHandler = BackHandler.addEventListener( + 'hardwareBackPress', + () => { + return store.shell.closeAnyActiveElement() + }, + ) + return () => { + backHandler.remove() + } + } + return () => {} } diff --git a/src/view/com/composer/Composer.tsx b/src/view/com/composer/Composer.tsx index 8629c4fc..f7b65727 100644 --- a/src/view/com/composer/Composer.tsx +++ b/src/view/com/composer/Composer.tsx @@ -2,6 +2,7 @@ import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react' import {observer} from 'mobx-react-lite' import { ActivityIndicator, + BackHandler, Keyboard, KeyboardAvoidingView, Platform, @@ -49,14 +50,10 @@ import {SelectLangBtn} from './select-language/SelectLangBtn' import {EmojiPickerButton} from './text-input/web/EmojiPicker.web' import {insertMentionAt} from 'lib/strings/mention-manip' -type Props = ComposerOpts & { - onClose: () => void -} - +type Props = ComposerOpts export const ComposePost = observer(function ComposePost({ replyTo, onPost, - onClose, quote: initQuote, mention: initMention, }: Props) { @@ -90,6 +87,9 @@ export const ComposePost = observer(function ComposePost({ const [labels, setLabels] = useState([]) const [suggestedLinks, setSuggestedLinks] = useState>(new Set()) const gallery = useMemo(() => new GalleryModel(store), [store]) + const onClose = useCallback(() => { + store.shell.closeComposer() + }, [store]) const autocompleteView = useMemo( () => new UserAutocompleteModel(store), @@ -129,6 +129,20 @@ export const ComposePost = observer(function ComposePost({ onClose() } }, [store, onClose, graphemeLength, gallery]) + // android back button + useEffect(() => { + const backHandler = BackHandler.addEventListener( + 'hardwareBackPress', + () => { + onPressCancel() + return true + }, + ) + + return () => { + backHandler.remove() + } + }, [onPressCancel]) // initial setup useEffect(() => { diff --git a/src/view/shell/Composer.tsx b/src/view/shell/Composer.tsx index d7c6a80b..219a594e 100644 --- a/src/view/shell/Composer.tsx +++ b/src/view/shell/Composer.tsx @@ -11,7 +11,6 @@ export const Composer = observer(function ComposerImpl({ winHeight, replyTo, onPost, - onClose, quote, mention, }: { @@ -19,7 +18,6 @@ export const Composer = observer(function ComposerImpl({ winHeight: number replyTo?: ComposerOpts['replyTo'] onPost?: ComposerOpts['onPost'] - onClose: () => void quote?: ComposerOpts['quote'] mention?: ComposerOpts['mention'] }) { @@ -64,7 +62,6 @@ export const Composer = observer(function ComposerImpl({ diff --git a/src/view/shell/Composer.web.tsx b/src/view/shell/Composer.web.tsx index f4b2d9a4..c3ec37e5 100644 --- a/src/view/shell/Composer.web.tsx +++ b/src/view/shell/Composer.web.tsx @@ -13,7 +13,6 @@ export const Composer = observer(function ComposerImpl({ replyTo, quote, onPost, - onClose, mention, }: { active: boolean @@ -21,7 +20,6 @@ export const Composer = observer(function ComposerImpl({ replyTo?: ComposerOpts['replyTo'] quote: ComposerOpts['quote'] onPost?: ComposerOpts['onPost'] - onClose: () => void mention?: ComposerOpts['mention'] }) { const pal = usePalette('default') @@ -47,7 +45,6 @@ export const Composer = observer(function ComposerImpl({ replyTo={replyTo} quote={quote} onPost={onPost} - onClose={onClose} mention={mention} /> diff --git a/src/view/shell/index.tsx b/src/view/shell/index.tsx index e313450f..3119715e 100644 --- a/src/view/shell/index.tsx +++ b/src/view/shell/index.tsx @@ -44,7 +44,10 @@ const ShellInner = observer(function ShellInnerImpl() { ) const canGoBack = useNavigationState(state => !isStateAtTabRoot(state)) React.useEffect(() => { - backHandler.init(store) + const listener = backHandler.init(store) + return () => { + listener() + } }, [store]) return ( @@ -68,7 +71,6 @@ const ShellInner = observer(function ShellInnerImpl() { store.shell.closeComposer()} winHeight={winDim.height} replyTo={store.shell.composerOpts?.replyTo} onPost={store.shell.composerOpts?.onPost} diff --git a/src/view/shell/index.web.tsx b/src/view/shell/index.web.tsx index 67f98884..3f2fed69 100644 --- a/src/view/shell/index.web.tsx +++ b/src/view/shell/index.web.tsx @@ -48,7 +48,6 @@ const ShellInner = observer(function ShellInnerImpl() { )} store.shell.closeComposer()} winHeight={0} replyTo={store.shell.composerOpts?.replyTo} quote={store.shell.composerOpts?.quote}