diff options
author | Ivan Chen <ivan@tagg.id> | 2021-06-11 15:30:56 -0400 |
---|---|---|
committer | Ivan Chen <ivan@tagg.id> | 2021-06-11 15:30:56 -0400 |
commit | eb672872d85f203085c96005758314d5dba359f2 (patch) | |
tree | 51507fdd6645045679928b83d9ed681695c7d2f0 /src/screens | |
parent | 6ad0a54704523d7cb69b6789323ba26c4d53f63e (diff) |
Simplify scroll logic, Cleanup code
Diffstat (limited to 'src/screens')
-rw-r--r-- | src/screens/profile/IndividualMoment.tsx | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index a91f1913..ddf4c478 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -17,7 +17,8 @@ import {normalize, StatusBarHeight} from '../../utils'; type MomentContextType = { keyboardVisible: boolean; - setScrollToTargetIndex: (index: number) => void; + currentScrollToIndex: number; + scrollTo: (index: number) => void; }; export const MomentContext = React.createContext({} as MomentContextType); @@ -51,37 +52,39 @@ const IndividualMoment: React.FC<IndividualMomentProps> = ({ (m) => m.moment_category === moment_category, ); const initialIndex = momentData.findIndex((m) => m.moment_id === moment_id); - const [scrollToTargetIndex, setScrollToTargetIndex] = + const [currentScrollToIndex, setCurrentScrollToIndex] = useState<number>(initialIndex); - const [keyboardVisible, setKeyboardVisible] = React.useState(false); + const [keyboardVisible, setKeyboardVisible] = useState(false); useEffect(() => { const showKeyboard = () => setKeyboardVisible(true); - Keyboard.addListener('keyboardWillShow', showKeyboard); - return () => Keyboard.removeListener('keyboardWillShow', showKeyboard); - }, []); - - useEffect(() => { const hideKeyboard = () => setKeyboardVisible(false); + Keyboard.addListener('keyboardWillShow', showKeyboard); Keyboard.addListener('keyboardWillHide', hideKeyboard); - return () => Keyboard.removeListener('keyboardWillHide', hideKeyboard); + return () => { + Keyboard.removeListener('keyboardWillShow', showKeyboard); + Keyboard.removeListener('keyboardWillHide', hideKeyboard); + }; }, []); - useEffect(() => { - if (keyboardVisible) { + const scrollTo = (index: number) => { + setCurrentScrollToIndex(index); + setTimeout(() => { + console.log('scrolling to', index); scrollRef.current?.scrollToIndex({ - index: scrollToTargetIndex, + index: index, // viewOffset: -(AVATAR_DIM + normalize(120)), viewOffset: -(AVATAR_DIM + normalize(90)), }); - } - }, [scrollToTargetIndex, keyboardVisible]); + }, 100); + }; return ( <MomentContext.Provider value={{ keyboardVisible, - setScrollToTargetIndex, + currentScrollToIndex, + scrollTo, }}> <BlurView blurType="light" |