-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Glazzes/dev
Refactor gallery example and other small changes
- Loading branch information
Showing
11 changed files
with
636 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import { useWindowDimensions } from 'react-native'; | ||
import { ResizeMode, Video, type AVPlaybackStatus } from 'expo-av'; | ||
import type { Asset } from 'expo-media-library'; | ||
|
||
import { calculateItemSize } from './utils/utils'; | ||
import { | ||
listenToPauseVideoEvent, | ||
listenToPlayVideoEvent, | ||
listenToSeekVideoEvent, | ||
listenToStopVideoEvent, | ||
} from './utils/emitter'; | ||
import { type SharedValue } from 'react-native-reanimated'; | ||
|
||
type GalleryVideoProps = { | ||
asset: Asset; | ||
index: number; | ||
isLooping: boolean; | ||
isSeeking: SharedValue<boolean>; | ||
progress: SharedValue<number>; | ||
}; | ||
|
||
const GalleryVideo: React.FC<GalleryVideoProps> = ({ | ||
asset, | ||
index, | ||
isLooping, | ||
isSeeking, | ||
progress, | ||
}) => { | ||
const videoRef = useRef<Video>(null); | ||
const { width, height } = useWindowDimensions(); | ||
|
||
const phoneAspectRatio = width / height; | ||
const size = calculateItemSize( | ||
{ width: asset.width, height: asset.height }, | ||
{ width, height }, | ||
phoneAspectRatio | ||
); | ||
|
||
const playback = (status: AVPlaybackStatus) => { | ||
if (status.isLoaded && !isSeeking.value) { | ||
progress.value = status.positionMillis / (asset.duration * 1000); | ||
} | ||
}; | ||
|
||
const play = (activeIndex: number) => { | ||
if (activeIndex !== index) return; | ||
videoRef.current?.playAsync(); | ||
}; | ||
|
||
const pause = (activeIndex: number) => { | ||
if (activeIndex !== index) return; | ||
videoRef.current?.pauseAsync(); | ||
}; | ||
|
||
const stop = () => { | ||
videoRef.current?.stopAsync().finally(() => (isSeeking.value = false)); | ||
}; | ||
|
||
const seek = async (options: { positionMillis: number; index: number }) => { | ||
const { positionMillis, index: activeIndex } = options; | ||
if (index !== activeIndex) return; | ||
|
||
await videoRef.current?.setPositionAsync(positionMillis); | ||
progress.value = positionMillis / (asset.duration * 1000); | ||
isSeeking.value = false; | ||
}; | ||
|
||
useEffect(() => { | ||
const playSub = listenToPlayVideoEvent(play); | ||
const pauseSub = listenToPauseVideoEvent(pause); | ||
const stopSub = listenToStopVideoEvent(stop); | ||
const seekSub = listenToSeekVideoEvent(seek); | ||
|
||
return () => { | ||
playSub.remove(); | ||
pauseSub.remove(); | ||
stopSub.remove(); | ||
seekSub.remove(); | ||
}; | ||
}); | ||
|
||
return ( | ||
<Video | ||
ref={videoRef} | ||
source={{ uri: asset.uri }} | ||
style={{ ...size }} | ||
resizeMode={ResizeMode.COVER} | ||
shouldPlay={false} | ||
isLooping={isLooping} | ||
progressUpdateIntervalMillis={50} | ||
onPlaybackStatusUpdate={playback} | ||
/> | ||
); | ||
}; | ||
|
||
export default GalleryVideo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Taken from https://github.com/wcandillon/react-native-redash/blob/master/src/ReText.tsx | ||
import React from 'react'; | ||
import type { TextProps as RNTextProps } from 'react-native'; | ||
import { StyleSheet, TextInput } from 'react-native'; | ||
import Animated, { | ||
useAnimatedProps, | ||
type SharedValue, | ||
} from 'react-native-reanimated'; | ||
|
||
const styles = StyleSheet.create({ | ||
baseStyle: { | ||
color: 'black', | ||
}, | ||
}); | ||
|
||
type ReTextProps = { | ||
text: SharedValue<string>; | ||
style?: RNTextProps['style']; | ||
}; | ||
|
||
const AnimatedTextInput = Animated.createAnimatedComponent(TextInput); | ||
Animated.addWhitelistedNativeProps({ text: true }); | ||
|
||
const ReText: React.FC<ReTextProps> = ({ text, style }) => { | ||
const animatedProps = useAnimatedProps(() => { | ||
return { | ||
text: text.value, | ||
// Here we use any because the text prop is not available in the type | ||
} as any; | ||
}, [text]); | ||
|
||
return ( | ||
<AnimatedTextInput | ||
animatedProps={animatedProps} | ||
underlineColorAndroid="transparent" | ||
editable={false} | ||
value={text.value} | ||
style={[styles.baseStyle, style || undefined]} | ||
/> | ||
); | ||
}; | ||
|
||
export default ReText; |
Oops, something went wrong.