fix: jumpy progress animations
This commit is contained in:
@@ -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;
|
||||||
|
|
||||||
|
// GUARD: Don't update when the duration is 0, cause it will put the
|
||||||
|
// bars in a weird space.
|
||||||
|
if (duration === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// First calculate the new value for the buffer animation. Then, check
|
||||||
|
// whether the buffered state is smaller than the previous one, in which
|
||||||
|
// case we'll just set the value without animation
|
||||||
|
const bufferValue = calculateProgressTranslation(buffered, duration, NOW_PLAYING_POPOVER_WIDTH);
|
||||||
|
if (buffered < (previousBuffered || 0)) {
|
||||||
|
bufferAnimation.current.setValue(bufferValue);
|
||||||
|
} else {
|
||||||
Animated.timing(bufferAnimation.current, {
|
Animated.timing(bufferAnimation.current, {
|
||||||
toValue: calculateProgressTranslation(buffered, duration, NOW_PLAYING_POPOVER_WIDTH),
|
toValue: bufferValue,
|
||||||
duration: hasChangedTrack ? 0 : 500,
|
duration: 500,
|
||||||
useNativeDriver: true,
|
useNativeDriver: true,
|
||||||
easing: Easing.ease,
|
|
||||||
}).start();
|
}).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, {
|
Animated.timing(progressAnimation.current, {
|
||||||
toValue: calculateProgressTranslation(position, duration, NOW_PLAYING_POPOVER_WIDTH),
|
toValue: progressValule,
|
||||||
duration: hasChangedTrack ? 0 : 500,
|
duration: 500,
|
||||||
useNativeDriver: true,
|
useNativeDriver: true,
|
||||||
}).start();
|
}).start();
|
||||||
}, [buffered, track?.duration, position, index, previousIndex]);
|
}
|
||||||
|
}, [buffered, track?.duration, position, index, previousBuffered, previousPosition]);
|
||||||
|
|
||||||
if (!track) {
|
if (!track) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user