diff options
| author | Ivan Chen <ivan@tagg.id> | 2021-06-25 16:40:35 -0400 |
|---|---|---|
| committer | Ivan Chen <ivan@tagg.id> | 2021-06-25 16:40:35 -0400 |
| commit | 4bab63b7b0c24043749e78cbb4d639e8a4047bad (patch) | |
| tree | a484681eeb461692d050559e5c240db9cccf274b /src/screens | |
| parent | 8198e4349dd589be2fbc9fb42aefaa8c5915bef5 (diff) | |
Add logic to pause video when not visible
Diffstat (limited to 'src/screens')
| -rw-r--r-- | src/screens/profile/IndividualMoment.tsx | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/screens/profile/IndividualMoment.tsx b/src/screens/profile/IndividualMoment.tsx index ca31ad5b..7944c336 100644 --- a/src/screens/profile/IndividualMoment.tsx +++ b/src/screens/profile/IndividualMoment.tsx @@ -1,7 +1,7 @@ import {RouteProp} from '@react-navigation/native'; import {StackNavigationProp} from '@react-navigation/stack'; import React, {useEffect, useRef, useState} from 'react'; -import {FlatList, Keyboard} from 'react-native'; +import {FlatList, Keyboard, ViewToken} from 'react-native'; import {useSelector} from 'react-redux'; import {MomentPost, TabsGradient} from '../../components'; import {AVATAR_DIM} from '../../constants'; @@ -17,6 +17,7 @@ import {isIPhoneX} from '../../utils'; type MomentContextType = { keyboardVisible: boolean; scrollTo: (index: number) => void; + currentVisibleMomentId: string | undefined; }; export const MomentContext = React.createContext({} as MomentContextType); @@ -48,6 +49,18 @@ const IndividualMoment: React.FC<IndividualMomentProps> = ({route}) => { ); const initialIndex = momentData.findIndex((m) => m.moment_id === moment_id); const [keyboardVisible, setKeyboardVisible] = useState(false); + const [currentVisibleMomentId, setCurrentVisibleMomentId] = useState< + string | undefined + >(); + // https://stackoverflow.com/a/57502343 + const viewabilityConfigCallback = useRef( + (info: {viewableItems: ViewToken[]; changed: ViewToken[]}) => { + const index = info.viewableItems[0].index; + if (index !== null) { + setCurrentVisibleMomentId(momentData[index].moment_id); + } + }, + ); useEffect(() => { const showKeyboard = () => setKeyboardVisible(true); @@ -74,6 +87,7 @@ const IndividualMoment: React.FC<IndividualMomentProps> = ({route}) => { value={{ keyboardVisible, scrollTo, + currentVisibleMomentId, }}> <FlatList ref={scrollRef} @@ -89,6 +103,7 @@ const IndividualMoment: React.FC<IndividualMomentProps> = ({route}) => { keyExtractor={(item, _) => item.moment_id} showsVerticalScrollIndicator={false} initialScrollIndex={initialIndex} + onViewableItemsChanged={viewabilityConfigCallback.current} onScrollToIndexFailed={(info) => { setTimeout(() => { scrollRef.current?.scrollToIndex({ |
