Skip to content

Commit 9807b0e

Browse files
committed
fix: jumpy progress animations
1 parent 7ed389e commit 9807b0e

File tree

1 file changed

+35
-14
lines changed
  • src/screens/Music/overlays/NowPlaying

1 file changed

+35
-14
lines changed

src/screens/Music/overlays/NowPlaying/index.tsx

+35-14
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ function NowPlaying() {
111111
const { index, track } = useCurrentTrack();
112112
const { buffered, position } = useProgress();
113113
const defaultStyles = useDefaultStyles();
114-
const previousIndex = usePrevious(index);
114+
const previousBuffered = usePrevious(buffered);
115+
const previousPosition = usePrevious(position);
116+
115117
const navigation = useNavigation<MusicNavigationProp>();
116118

117119
const bufferAnimation = useRef(new Animated.Value(0));
@@ -122,21 +124,40 @@ function NowPlaying() {
122124
}, [navigation]);
123125

124126
useEffect(() => {
125-
const hasChangedTrack = previousIndex !== index || track?.duration === 0;
126127
const duration = (track?.duration || 0) / 10_000_000;
127128

128-
Animated.timing(bufferAnimation.current, {
129-
toValue: calculateProgressTranslation(buffered, duration, NOW_PLAYING_POPOVER_WIDTH),
130-
duration: hasChangedTrack ? 0 : 500,
131-
useNativeDriver: true,
132-
easing: Easing.ease,
133-
}).start();
134-
Animated.timing(progressAnimation.current, {
135-
toValue: calculateProgressTranslation(position, duration, NOW_PLAYING_POPOVER_WIDTH),
136-
duration: hasChangedTrack ? 0 : 500,
137-
useNativeDriver: true,
138-
}).start();
139-
}, [buffered, track?.duration, position, index, previousIndex]);
129+
// GUARD: Don't update when the duration is 0, cause it will put the
130+
// bars in a weird space.
131+
if (duration === 0) {
132+
return;
133+
}
134+
135+
// First calculate the new value for the buffer animation. Then, check
136+
// whether the buffered state is smaller than the previous one, in which
137+
// case we'll just set the value without animation
138+
const bufferValue = calculateProgressTranslation(buffered, duration, NOW_PLAYING_POPOVER_WIDTH);
139+
if (buffered < (previousBuffered || 0)) {
140+
bufferAnimation.current.setValue(bufferValue);
141+
} else {
142+
Animated.timing(bufferAnimation.current, {
143+
toValue: bufferValue,
144+
duration: 500,
145+
useNativeDriver: true,
146+
}).start();
147+
}
148+
149+
// Then, do the same for the progress animation
150+
const progressValule = calculateProgressTranslation(position, duration, NOW_PLAYING_POPOVER_WIDTH);
151+
if (position < (previousPosition || 0)) {
152+
progressAnimation.current.setValue(progressValule);
153+
} else {
154+
Animated.timing(progressAnimation.current, {
155+
toValue: progressValule,
156+
duration: 500,
157+
useNativeDriver: true,
158+
}).start();
159+
}
160+
}, [buffered, track?.duration, position, index, previousBuffered, previousPosition]);
140161

141162
if (!track) {
142163
return null;

0 commit comments

Comments
 (0)