mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix 1405 handle android back button in composer (#1446)

* handle android back button in composer

* improve backHandler error handling

* simplify composer onClose functionality

authored by

Ansh and committed by
GitHub
04fda0f1 1f60e1a7

+37 -17
+14 -3
src/lib/routes/back-handler.ts
··· 1 + import {isAndroid} from 'platform/detection' 1 2 import {BackHandler} from 'react-native' 2 3 import {RootStoreModel} from 'state/index' 3 4 4 5 export function init(store: RootStoreModel) { 5 - BackHandler.addEventListener('hardwareBackPress', () => { 6 - return store.shell.closeAnyActiveElement() 7 - }) 6 + // only register back handler on android, otherwise it throws an error 7 + if (isAndroid) { 8 + const backHandler = BackHandler.addEventListener( 9 + 'hardwareBackPress', 10 + () => { 11 + return store.shell.closeAnyActiveElement() 12 + }, 13 + ) 14 + return () => { 15 + backHandler.remove() 16 + } 17 + } 18 + return () => {} 8 19 }
+19 -5
src/view/com/composer/Composer.tsx
··· 2 2 import {observer} from 'mobx-react-lite' 3 3 import { 4 4 ActivityIndicator, 5 + BackHandler, 5 6 Keyboard, 6 7 KeyboardAvoidingView, 7 8 Platform, ··· 49 50 import {EmojiPickerButton} from './text-input/web/EmojiPicker.web' 50 51 import {insertMentionAt} from 'lib/strings/mention-manip' 51 52 52 - type Props = ComposerOpts & { 53 - onClose: () => void 54 - } 55 - 53 + type Props = ComposerOpts 56 54 export const ComposePost = observer(function ComposePost({ 57 55 replyTo, 58 56 onPost, 59 - onClose, 60 57 quote: initQuote, 61 58 mention: initMention, 62 59 }: Props) { ··· 90 87 const [labels, setLabels] = useState<string[]>([]) 91 88 const [suggestedLinks, setSuggestedLinks] = useState<Set<string>>(new Set()) 92 89 const gallery = useMemo(() => new GalleryModel(store), [store]) 90 + const onClose = useCallback(() => { 91 + store.shell.closeComposer() 92 + }, [store]) 93 93 94 94 const autocompleteView = useMemo<UserAutocompleteModel>( 95 95 () => new UserAutocompleteModel(store), ··· 129 129 onClose() 130 130 } 131 131 }, [store, onClose, graphemeLength, gallery]) 132 + // android back button 133 + useEffect(() => { 134 + const backHandler = BackHandler.addEventListener( 135 + 'hardwareBackPress', 136 + () => { 137 + onPressCancel() 138 + return true 139 + }, 140 + ) 141 + 142 + return () => { 143 + backHandler.remove() 144 + } 145 + }, [onPressCancel]) 132 146 133 147 // initial setup 134 148 useEffect(() => {
-3
src/view/shell/Composer.tsx
··· 11 11 winHeight, 12 12 replyTo, 13 13 onPost, 14 - onClose, 15 14 quote, 16 15 mention, 17 16 }: { ··· 19 18 winHeight: number 20 19 replyTo?: ComposerOpts['replyTo'] 21 20 onPost?: ComposerOpts['onPost'] 22 - onClose: () => void 23 21 quote?: ComposerOpts['quote'] 24 22 mention?: ComposerOpts['mention'] 25 23 }) { ··· 64 62 <ComposePost 65 63 replyTo={replyTo} 66 64 onPost={onPost} 67 - onClose={onClose} 68 65 quote={quote} 69 66 mention={mention} 70 67 />
-3
src/view/shell/Composer.web.tsx
··· 13 13 replyTo, 14 14 quote, 15 15 onPost, 16 - onClose, 17 16 mention, 18 17 }: { 19 18 active: boolean ··· 21 20 replyTo?: ComposerOpts['replyTo'] 22 21 quote: ComposerOpts['quote'] 23 22 onPost?: ComposerOpts['onPost'] 24 - onClose: () => void 25 23 mention?: ComposerOpts['mention'] 26 24 }) { 27 25 const pal = usePalette('default') ··· 47 45 replyTo={replyTo} 48 46 quote={quote} 49 47 onPost={onPost} 50 - onClose={onClose} 51 48 mention={mention} 52 49 /> 53 50 </View>
+4 -2
src/view/shell/index.tsx
··· 44 44 ) 45 45 const canGoBack = useNavigationState(state => !isStateAtTabRoot(state)) 46 46 React.useEffect(() => { 47 - backHandler.init(store) 47 + const listener = backHandler.init(store) 48 + return () => { 49 + listener() 50 + } 48 51 }, [store]) 49 52 50 53 return ( ··· 68 71 </View> 69 72 <Composer 70 73 active={store.shell.isComposerActive} 71 - onClose={() => store.shell.closeComposer()} 72 74 winHeight={winDim.height} 73 75 replyTo={store.shell.composerOpts?.replyTo} 74 76 onPost={store.shell.composerOpts?.onPost}
-1
src/view/shell/index.web.tsx
··· 48 48 )} 49 49 <Composer 50 50 active={store.shell.isComposerActive} 51 - onClose={() => store.shell.closeComposer()} 52 51 winHeight={0} 53 52 replyTo={store.shell.composerOpts?.replyTo} 54 53 quote={store.shell.composerOpts?.quote}