fix: jumpy progress animations

This commit is contained in:
Lei Nelissen
2022-11-13 12:09:48 +01:00
parent 7ed389ead6
commit 9807b0e920

View File

@@ -111,7 +111,9 @@ function NowPlaying() {
const { index, track } = useCurrentTrack(); const { index, track } = useCurrentTrack();
const { buffered, position } = useProgress(); const { buffered, position } = useProgress();
const defaultStyles = useDefaultStyles(); const defaultStyles = useDefaultStyles();
const previousIndex = usePrevious(index); const previousBuffered = usePrevious(buffered);
const previousPosition = usePrevious(position);
const navigation = useNavigation<MusicNavigationProp>(); const navigation = useNavigation<MusicNavigationProp>();
const bufferAnimation = useRef(new Animated.Value(0)); const bufferAnimation = useRef(new Animated.Value(0));
@@ -122,21 +124,40 @@ function NowPlaying() {
}, [navigation]); }, [navigation]);
useEffect(() => { useEffect(() => {
const hasChangedTrack = previousIndex !== index || track?.duration === 0;
const duration = (track?.duration || 0) / 10_000_000; const duration = (track?.duration || 0) / 10_000_000;
Animated.timing(bufferAnimation.current, { // GUARD: Don't update when the duration is 0, cause it will put the
toValue: calculateProgressTranslation(buffered, duration, NOW_PLAYING_POPOVER_WIDTH), // bars in a weird space.
duration: hasChangedTrack ? 0 : 500, if (duration === 0) {
useNativeDriver: true, return;
easing: Easing.ease, }
}).start();
Animated.timing(progressAnimation.current, { // First calculate the new value for the buffer animation. Then, check
toValue: calculateProgressTranslation(position, duration, NOW_PLAYING_POPOVER_WIDTH), // whether the buffered state is smaller than the previous one, in which
duration: hasChangedTrack ? 0 : 500, // case we'll just set the value without animation
useNativeDriver: true, const bufferValue = calculateProgressTranslation(buffered, duration, NOW_PLAYING_POPOVER_WIDTH);
}).start(); if (buffered < (previousBuffered || 0)) {
}, [buffered, track?.duration, position, index, previousIndex]); bufferAnimation.current.setValue(bufferValue);
} else {
Animated.timing(bufferAnimation.current, {
toValue: bufferValue,
duration: 500,
useNativeDriver: true,
}).start();
}
// Then, do the same for the progress animation
const progressValule = calculateProgressTranslation(position, duration, NOW_PLAYING_POPOVER_WIDTH);
if (position < (previousPosition || 0)) {
progressAnimation.current.setValue(progressValule);
} else {
Animated.timing(progressAnimation.current, {
toValue: progressValule,
duration: 500,
useNativeDriver: true,
}).start();
}
}, [buffered, track?.duration, position, index, previousBuffered, previousPosition]);
if (!track) { if (!track) {
return null; return null;